Mercurial > projects > dang
annotate sema/ScopeCheck.d @ 92:771ac63898e2 new_gen
A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
author | Anders Johnsen <skabet@gmail.com> |
---|---|
date | Mon, 05 May 2008 18:44:20 +0200 |
parents | sema/Declarations.d@eb5b2c719a39 |
children | 857f0d530789 |
rev | line source |
---|---|
92
771ac63898e2
A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents:
88
diff
changeset
|
1 module sema.ScopeCheck; |
13
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
2 |
28
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
24
diff
changeset
|
3 import sema.Visitor, |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
24
diff
changeset
|
4 sema.DType; |
13
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
5 |
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
6 import tango.io.Stdout; |
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
7 |
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
8 import misc.Error; |
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
9 |
92
771ac63898e2
A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents:
88
diff
changeset
|
10 class ScopeCheck : Visitor!(void) |
13
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
11 { |
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
12 int[char[]] types; |
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
13 |
29
41d23f2762c3
Merge, and updated Error class
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
14 private Error error(uint line, char[] msg) |
41d23f2762c3
Merge, and updated Error class
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
15 { |
41d23f2762c3
Merge, and updated Error class
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
16 return new Error(msg); |
41d23f2762c3
Merge, and updated Error class
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
17 } |
41d23f2762c3
Merge, and updated Error class
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
18 |
13
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
19 override void visitIdentifier(Identifier i) |
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
20 { |
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
21 auto symbol = i.env.find(i); |
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
22 |
28
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
24
diff
changeset
|
23 if(symbol is null) |
29
41d23f2762c3
Merge, and updated Error class
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
24 throw error(__LINE__, "Undefined identifier: '%0'") |
88
eb5b2c719a39
Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
25 .arg(i.get); |
eb5b2c719a39
Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
26 //.loc(i.token.location); |
13
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
27 } |
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
28 |
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
29 override void visitVarDecl(VarDecl d) |
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
30 { |
63
9f8131676242
Now Decl's have a DType type(), and should use varType and returnType to get the old type id
Anders Halager <halager@gmail.com>
parents:
55
diff
changeset
|
31 if(!d.env.findType(d.varType)) |
29
41d23f2762c3
Merge, and updated Error class
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
32 throw error(__LINE__, "Undefined type: '%0'") |
88
eb5b2c719a39
Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
33 .arg(d.varType.get); |
eb5b2c719a39
Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
34 //.loc(d.varType.token.location); |
13
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
35 |
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
36 visitExp(d.identifier); |
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
37 if (d.init) |
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
38 visitExp(d.init); |
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
39 } |
68
381975d76baf
A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
Anders Johnsen <skabet@gmail.com>
parents:
63
diff
changeset
|
40 |
28
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
24
diff
changeset
|
41 override void visitFuncDecl(FuncDecl f) |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
24
diff
changeset
|
42 { |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
24
diff
changeset
|
43 visitExp(f.identifier); |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
24
diff
changeset
|
44 |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
24
diff
changeset
|
45 foreach (stmt; f.statements) |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
24
diff
changeset
|
46 visitStmt(stmt); |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
24
diff
changeset
|
47 } |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
24
diff
changeset
|
48 |
68
381975d76baf
A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
Anders Johnsen <skabet@gmail.com>
parents:
63
diff
changeset
|
49 override void visitCastExp(CastExp exp) |
381975d76baf
A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
Anders Johnsen <skabet@gmail.com>
parents:
63
diff
changeset
|
50 { |
381975d76baf
A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
Anders Johnsen <skabet@gmail.com>
parents:
63
diff
changeset
|
51 visitExp(exp.exp); |
381975d76baf
A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
Anders Johnsen <skabet@gmail.com>
parents:
63
diff
changeset
|
52 } |
381975d76baf
A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
Anders Johnsen <skabet@gmail.com>
parents:
63
diff
changeset
|
53 |
55
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
39
diff
changeset
|
54 override void visitMemberReference(MemberReference m) |
28
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
24
diff
changeset
|
55 { |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
24
diff
changeset
|
56 switch(m.target.expType) |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
24
diff
changeset
|
57 { |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
24
diff
changeset
|
58 case ExpType.Identifier: |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
24
diff
changeset
|
59 auto target = cast(Identifier)m.target; |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
24
diff
changeset
|
60 auto child = m.child; |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
24
diff
changeset
|
61 auto st = cast(DStruct)(target.env.find(target).type); |
39
1a7a308f75b2
Added some struct tests, and implemented a wrong struct assignment
Anders Halager <halager@gmail.com>
parents:
31
diff
changeset
|
62 if((child.get in st.members) is null) |
1a7a308f75b2
Added some struct tests, and implemented a wrong struct assignment
Anders Halager <halager@gmail.com>
parents:
31
diff
changeset
|
63 throw error(__LINE__, "%0 %1 has no member %2") |
1a7a308f75b2
Added some struct tests, and implemented a wrong struct assignment
Anders Halager <halager@gmail.com>
parents:
31
diff
changeset
|
64 .arg(st.name) |
30
3147a52d1247
Ooops.. should have compiled before commit.. now works again
Anders Halager <halager@gmail.com>
parents:
29
diff
changeset
|
65 .arg(target.get) |
88
eb5b2c719a39
Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
66 .arg(child.get); |
eb5b2c719a39
Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
67 //.tok(child.token); |
39
1a7a308f75b2
Added some struct tests, and implemented a wrong struct assignment
Anders Halager <halager@gmail.com>
parents:
31
diff
changeset
|
68 break; |
55
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
39
diff
changeset
|
69 case ExpType.MemberReference: |
39
1a7a308f75b2
Added some struct tests, and implemented a wrong struct assignment
Anders Halager <halager@gmail.com>
parents:
31
diff
changeset
|
70 break; |
28
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
24
diff
changeset
|
71 } |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
24
diff
changeset
|
72 } |
13
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
73 |
63
9f8131676242
Now Decl's have a DType type(), and should use varType and returnType to get the old type id
Anders Halager <halager@gmail.com>
parents:
55
diff
changeset
|
74 private bool isType(char[] s) |
13
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
75 { |
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
76 return (s in types? true : false); |
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
77 } |
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
78 } |
22 | 79 |