# HG changeset patch # User Trass3r # Date 1283213912 -7200 # Node ID d78e4992d6ec469255ce052bf50ac34dddec6842 # Parent 3a0b150c9841aa11c3a33df27760e9978c682c68 implemented most of PragmaStatement diff -r 3a0b150c9841 -r d78e4992d6ec dmd/PragmaStatement.d --- a/dmd/PragmaStatement.d Mon Aug 30 23:00:34 2010 +0100 +++ b/dmd/PragmaStatement.d Tue Aug 31 02:18:32 2010 +0200 @@ -1,7 +1,11 @@ module dmd.PragmaStatement; +import dmd.expression.Util; import dmd.Statement; +import dmd.StringExp; +import dmd.Id; import dmd.Identifier; +import dmd.Dsymbol; import dmd.Expression; import dmd.Loc; import dmd.Identifier; @@ -10,44 +14,168 @@ import dmd.HdrGenState; import dmd.IRState; import dmd.BE; +import dmd.TOK; +import dmd.WANT; class PragmaStatement : Statement { - Identifier ident; - Expressions args; // array of Expression's - Statement body_; + Identifier ident; + Expressions args; // array of Expression's + Statement body_; - this(Loc loc, Identifier ident, Expressions args, Statement body_) + this(Loc loc, Identifier ident, Expressions args, Statement body_) { super(loc); + this.ident = ident; + this.args = args; + this.body_ = body_; } - override Statement syntaxCopy() + override Statement syntaxCopy() { - assert(false); + Statement b = null; + if (body_) + b = body_.syntaxCopy(); + PragmaStatement s = new PragmaStatement(loc, + ident, Expression.arraySyntaxCopy(args), b); + return s; + } - override Statement semantic(Scope sc) + override Statement semantic(Scope sc) { - assert(false); - } + // Should be merged with PragmaDeclaration + //writef("PragmaStatement.semantic() %s\n", toChars()); + //writef("body = %p\n", body_); + if (ident == Id.msg) + { + if (args) + { + foreach (Expression e; args) + { + e = e.semantic(sc); + e = e.optimize(WANTvalue | WANTinterpret); + if (e.op == TOK.TOKstring) + { + StringExp se = cast(StringExp)e; + writef("%.*s", se.len, cast(char*)se.string_); + } + else + writef(e.toChars()); + } + writef("\n"); + } + } + else if (ident == Id.lib) + { +static if (true) +{ + /* Should this be allowed? + */ + error("pragma(lib) not allowed as statement"); +} +else +{ + if (!args || args.dim != 1) + error("string expected for library name"); + else + { + Expression e = args[0]; - override bool usesEH() - { - assert(false); + e = e.semantic(sc); + e = e.optimize(WANTvalue | WANTinterpret); + args[0] = e; + if (e.op != TOKstring) + error("string expected for library name, not '%s'", e.toChars()); + else if (global.params.verbose) + { + StringExp se = cast(StringExp)e; + writef("library %.*s\n", se.len, se.string_); + } + } +} + } +//version(DMDV2) // TODO: +//{ + else if (ident == Id.startaddress) + { + if (!args || args.dim != 1) + error("function name expected for start address"); + else + { + Expression e = args[0]; + e = e.semantic(sc); + e = e.optimize(WANTvalue | WANTinterpret); + args[0] = e; + Dsymbol sa = getDsymbol(e); + if (!sa || !sa.isFuncDeclaration()) + error("function name expected for start address, not '%s'", e.toChars()); + if (body_) + { + body_ = body_.semantic(sc); + } + return this; + } + } +//} + else + error("unrecognized pragma(%s)", ident.toChars()); + + if (body_) + { + body_ = body_.semantic(sc); + } + return body_; } - override BE blockExit() + override bool usesEH() + { + return body_ && body_.usesEH(); + } + + override BE blockExit() { - assert(false); + BE result = BEfallthru; +static if (false) // currently, no code is generated for Pragma's, so it's just fallthru +{ + if (arrayExpressionCanThrow(args)) + result |= BEthrow; + if (body_) + result |= body_.blockExit(); +} + return result; } - override void toCBuffer(OutBuffer buf, HdrGenState* hgs) + override void toCBuffer(OutBuffer buf, HdrGenState* hgs) { - assert(false); + buf.writestring("pragma ("); + buf.writestring(ident.toChars()); + if (args && args.dim) + { + buf.writestring(", "); + argsToCBuffer(buf, args, hgs); + } + buf.writeByte(')'); + if (body_) + { + buf.writenl(); + buf.writeByte('{'); + buf.writenl(); + + body_.toCBuffer(buf, hgs); + + buf.writeByte('}'); + buf.writenl(); + } + else + { + buf.writeByte(';'); + buf.writenl(); + } + } - override void toIR(IRState* irs) + override void toIR(IRState* irs) { assert(false); }