changeset 54:d84fec04d462 new_gen

Move all root var-decls so they are generated before anything else This allows forward references to globals Added a few tests, testing that scoping is proper and forward referencing of structs and globals
author Anders Halager <halager@gmail.com>
date Sat, 26 Apr 2008 19:13:10 +0200
parents da551f90e03f
children 79cb0afafabe
files gen/CodeGen.d tests/code/scope_1.d tests/code/scope_2.d tests/code/scope_3.d tests/code/struct_5.d
diffstat 5 files changed, 51 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/gen/CodeGen.d	Sat Apr 26 18:52:27 2008 +0200
+++ b/gen/CodeGen.d	Sat Apr 26 19:13:10 2008 +0200
@@ -2,7 +2,7 @@
 
 import tango.io.Stdout,
        Int = tango.text.convert.Integer;
-import tango.core.Array : find;
+import tango.core.Array : find, partition;
 
 import llvm.llvm;
 
@@ -89,6 +89,12 @@
         auto visitor = new VisitFuncDecls(registerFunc);
         visitor.visit(decls);
 
+        // Before beginning we move all top level var-decls to the start
+        // and then we generate the var-decls first
+        // partition is NOT required to be stable, but that should not create
+        // any problems.
+        partition(decls, (Decl d) { return d.declType == DeclType.VarDecl; });
+
         foreach (decl; decls)
             genRootDecl(decl);
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/code/scope_1.d	Sat Apr 26 19:13:10 2008 +0200
@@ -0,0 +1,7 @@
+//test fail
+int main()
+{
+    int x = y;
+    int y = 1;
+    return x;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/code/scope_2.d	Sat Apr 26 19:13:10 2008 +0200
@@ -0,0 +1,11 @@
+//test fail
+int main()
+{
+    int x = 10;
+    while (x > 0)
+    {
+        int y = 1;
+        x = x -y;
+    }
+    return y;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/code/scope_3.d	Sat Apr 26 19:13:10 2008 +0200
@@ -0,0 +1,9 @@
+int y = 0;
+
+int main()
+{
+    return x + y;
+}
+
+int x = 0;
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/code/struct_5.d	Sat Apr 26 19:13:10 2008 +0200
@@ -0,0 +1,17 @@
+// Test forward references in struct members
+struct B
+{
+    int b;
+    A a;
+}
+
+struct A
+{
+    int a;
+}
+
+void main()
+{
+    B b;
+    b.b = 2;
+}