From 6b94bba36cb74290ccce68b3704fb08292bd9f38 Mon Sep 17 00:00:00 2001 From: Thomas Leonard Date: Fri, 19 Sep 2008 11:54:28 +0100 Subject: [PATCH] Added Delight "normal" mode restrictions If we're in a .dlt file that's not in the "dlt" package, disallow global state: - No module variables - No static constructors or destructors (since they can't do anything anyway) - No static class or function variables - No extern() linkage --- dmd/parse.c | 35 +++++++++++++++++++++++++++++++++++ dmd/parse.h | 1 + 2 files changed, 36 insertions(+) diff --git a/dmd/parse.c b/dmd/parse.c index b1bf3ce..5d64fbe 100644 --- a/dmd/parse.c +++ b/dmd/parse.c @@ -67,6 +67,7 @@ Parser::Parser(Module *module, unsigned char *base, unsigned length, int doDocCo //printf("Parser::Parser()\n"); md = NULL; linkage = LINKd; + dltNormalMode = FALSE; endloc = 0; inBrackets = 0; startBlockTok = TOKlcurly; @@ -78,6 +79,7 @@ DltParser::DltParser(Module *module, unsigned char *base, unsigned length, int d { //printf("DltParser::DltParser(%s)\n", module->ident->string); startBlockTok = TOKcolon; + dltNormalMode = TRUE; // becomes false if we find a "module dlt.*" } Array *Parser::parseModule() @@ -101,6 +103,10 @@ Array *Parser::parseModule() Identifier *id; id = token.ident; + + if (dltSyntax && id == Id::dlt) + dltNormalMode = FALSE; + while (nextToken() == TOKdot) { if (!a) @@ -130,6 +136,20 @@ Array *Parser::parseModule() { error("unrecognized declaration '%s'", token.toChars()); goto Lerr; } + + if (dltNormalMode) + { + // Check for global variables + for (int i = 0; i < decldefs->dim; i++) + { + Dsymbol *d = (Dsymbol *) decldefs->data[i]; + if (d->isVarDeclaration()) { + error("no global variables (%s) allowed in Delight; " + "try const, or put it in a class", d->toChars()); + } + } + } + return decldefs; Lerr: @@ -246,9 +266,17 @@ Array *Parser::parseDeclDefs(int once) case TOKstatic: nextToken(); if (token.value == TOKthis) + { s = parseStaticCtor(); + if (dltNormalMode) + error("no static constructors in Delight"); + } else if (token.value == TOKtilde) + { s = parseStaticDtor(); + if (dltNormalMode) + error("no static destructors in Delight"); + } else if (token.value == TOKassert) s = parseStaticAssert(); else if (token.value == TOKif) @@ -268,6 +296,8 @@ Array *Parser::parseDeclDefs(int once) } else { stc = STCstatic; + if (dltNormalMode) + error("no static variables in Delight"); goto Lstc2; } break; @@ -358,6 +388,9 @@ Array *Parser::parseDeclDefs(int once) a = parseBlock(); s = new LinkDeclaration(linkage, a); linkage = linksave; + + if (dltNormalMode) + error("access to external symbols can be done only by dlt.* modules"); break; } case TOKprivate: prot = PROTprivate; goto Lprot; @@ -2642,6 +2675,8 @@ Statement *Parser::parseStatement(int flags) condition = parseStaticIfCondition(); goto Lcondition; } + if (dltNormalMode) + error("no static variables in Delight"); goto Ldeclaration; } diff --git a/dmd/parse.h b/dmd/parse.h index 46c99ca..0bbaab4 100644 --- a/dmd/parse.h +++ b/dmd/parse.h @@ -63,6 +63,7 @@ struct Parser : Lexer Loc endloc; // set to location of last right curly int inBrackets; // inside [] of array index or slice TOK startBlockTok; + bool dltNormalMode; // disallow various dangerous features Parser(Module *module, unsigned char *base, unsigned length, int doDocComment); -- 2.11.4.GIT