diff --git a/dmd2/mars.h b/dmd2/mars.h index 78d5f46248..abb4d52942 100644 --- a/dmd2/mars.h +++ b/dmd2/mars.h @@ -178,6 +178,7 @@ struct Param bool enforcePropertySyntax; bool addMain; // LDC_FIXME: Implement. bool allInst; // LDC_FIXME: Implement. + unsigned nestedTmpl; // maximum nested template instantiations #else bool pic; // generate position-independent-code for shared libs bool cov; // generate code coverage data diff --git a/dmd2/template.c b/dmd2/template.c index f3095c28e0..3e73e4d11e 100644 --- a/dmd2/template.c +++ b/dmd2/template.c @@ -5235,7 +5235,11 @@ void TemplateInstance::tryExpandMembers(Scope *sc2) static int nest; // extracted to a function to allow windows SEH to work without destructors in the same function //printf("%d\n", nest); +#if IN_LLVM + if (++nest > global.params.nestedTmpl) +#else if (++nest > 500) +#endif { global.gag = 0; // ensure error message gets printed error("recursive expansion"); @@ -5705,7 +5709,11 @@ void TemplateInstance::semantic(Scope *sc, Expressions *fargs) while (ti && !ti->deferred && ti->tinst) { ti = ti->tinst; +#if IN_LLVM + if (++nest > global.params.nestedTmpl) +#else if (++nest > 500) +#endif { global.gag = 0; // ensure error message gets printed error("recursive expansion"); @@ -7530,7 +7538,11 @@ void TemplateMixin::semantic(Scope *sc) static int nest; //printf("%d\n", nest); +#if IN_LLVM + if (++nest > global.params.nestedTmpl) +#else if (++nest > 500) +#endif { global.gag = 0; // ensure error message gets printed error("recursive expansion"); diff --git a/driver/cl_options.cpp b/driver/cl_options.cpp index 5b900e5362..a3d93a9c82 100644 --- a/driver/cl_options.cpp +++ b/driver/cl_options.cpp @@ -385,6 +385,12 @@ cl::opt allinst("allinst", cl::desc("generate code for all template instantiations"), cl::location(global.params.allInst)); +cl::opt nestedTemplateDepth("template-depth", + cl::desc("(experimental) set maximum number of nested template instantiations"), + cl::location(global.params.nestedTmpl), + cl::init(500)); + + static cl::extrahelp footer("\n" "-d-debug can also be specified without options, in which case it enables all\n" "debug checks (i.e. (asserts, boundchecks, contracts and invariants) as well\n" diff --git a/driver/cl_options.h b/driver/cl_options.h index e906c63fd9..6186333293 100644 --- a/driver/cl_options.h +++ b/driver/cl_options.h @@ -70,6 +70,8 @@ namespace opts { extern cl::opt boundsChecks; extern bool nonSafeBoundsChecks; + extern cl::opt nestedTemplateDepth; + // Arguments to -d-debug extern std::vector debugArgs; // Arguments to -run