diff dmd/StaticAssert.d @ 0:10317f0c89a5

Initial commit
author korDen
date Sat, 24 Oct 2009 08:42:06 +0400
parents
children 427f8aa74d28 1628b221808d
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dmd/StaticAssert.d	Sat Oct 24 08:42:06 2009 +0400
@@ -0,0 +1,103 @@
+module dmd.StaticAssert;
+
+import dmd.Dsymbol;
+import dmd.Expression;
+import dmd.OutBuffer;
+import dmd.HdrGenState;
+import dmd.ScopeDsymbol;
+import dmd.Loc;
+import dmd.Scope;
+import dmd.Id;
+import dmd.WANT;
+import dmd.Global;
+import dmd.Util;
+
+class StaticAssert : Dsymbol
+{
+    Expression exp;
+    Expression msg;
+
+    this(Loc loc, Expression exp, Expression msg)
+	{
+		super(Id.empty);
+		
+		this.loc = loc;
+		this.exp = exp;
+		this.msg = msg;
+	}
+
+    Dsymbol syntaxCopy(Dsymbol s)
+	{
+		assert(false);
+	}
+	
+    bool addMember(Scope sc, ScopeDsymbol sd, int memnum)
+	{
+		return false;		// we didn't add anything
+	}
+	
+    void semantic(Scope sc)
+	{
+	}
+	
+    void semantic2(Scope sc)
+	{
+		Expression e;
+
+		//printf("StaticAssert::semantic2() %s\n", toChars());
+		e = exp.semantic(sc);
+		e = e.optimize(WANT.WANTvalue | WANT.WANTinterpret);
+		if (e.isBool(false))
+		{
+			if (msg)
+			{   
+				HdrGenState hgs;
+				scope OutBuffer buf = new OutBuffer();
+
+				msg = msg.semantic(sc);
+				msg = msg.optimize(WANT.WANTvalue | WANT.WANTinterpret);
+				hgs.console = 1;
+				msg.toCBuffer(buf, &hgs);
+				error("%s", buf.toChars());
+			}
+			else
+				error("(%s) is false", exp.toChars());
+
+			if(sc.tinst)
+				sc.tinst.printInstantiationTrace();
+
+			if (!global.gag) {
+				fatal();
+			}
+		}
+		else if (!e.isBool(true))
+		{
+			error("(%s) is not evaluatable at compile time", exp.toChars());
+		}
+	}
+	
+    void inlineScan()
+	{
+		assert(false);
+	}
+	
+    bool oneMember(Dsymbol* ps)
+	{
+		assert(false);
+	}
+	
+    void toObjFile(int multiobj)
+	{
+		assert(false);
+	}
+	
+    string kind()
+	{
+		return "static assert";
+	}
+	
+    void toCBuffer(OutBuffer buf, HdrGenState* hgs)
+	{
+		assert(false);
+	}
+}
\ No newline at end of file