Mercurial > projects > dang
annotate sema/ScopeCheck.d @ 168:7982eb63c0eb
Some changes to get function overloading to work. Also class inherit works now - to some extend. needs vtables and all the complex stuff of it.
author | Anders Johnsen <skabet@gmail.com> |
---|---|
date | Thu, 24 Jul 2008 12:06:48 +0200 |
parents | 57b0b4464a0b |
children | 01c2c49775ef |
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, |
130 | 4 sema.Symbol, |
136 | 5 sema.Scope, |
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
|
6 sema.DType; |
13
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
7 |
136 | 8 import basic.Message, |
9 basic.Attribute; | |
13
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
10 |
114
3a0cd42de9cc
Removed misc/Error.d and is now using the error system all way through.
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
11 import tango.io.Stdout; |
13
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
12 |
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
|
13 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
|
14 { |
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
15 |
114
3a0cd42de9cc
Removed misc/Error.d and is now using the error system all way through.
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
16 this(MessageHandler messages) |
29
41d23f2762c3
Merge, and updated Error class
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
17 { |
114
3a0cd42de9cc
Removed misc/Error.d and is now using the error system all way through.
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
18 this.messages = messages; |
29
41d23f2762c3
Merge, and updated Error class
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
19 } |
41d23f2762c3
Merge, and updated Error class
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
20 |
13
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
21 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
|
22 { |
145
a14ac9e5c858
Changes Scope to use char[]'s insted of Identifiers for lookup.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
23 auto symbol = i.env.find(i.get); |
13
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
24 |
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
|
25 if(symbol is null) |
114
3a0cd42de9cc
Removed misc/Error.d and is now using the error system all way through.
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
26 messages.report(UndefinedIdentifier, i.loc) |
88
eb5b2c719a39
Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
27 .arg(i.get); |
13
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 |
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
30 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
|
31 { |
145
a14ac9e5c858
Changes Scope to use char[]'s insted of Identifiers for lookup.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
32 if(!d.env.findType(d.varType.get)) |
114
3a0cd42de9cc
Removed misc/Error.d and is now using the error system all way through.
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
33 messages.report(UndefinedType, d.varType.loc) |
88
eb5b2c719a39
Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
34 .arg(d.varType.get); |
13
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
35 |
136 | 36 auto env = d.env; |
37 if (d.env.enclosing) | |
145
a14ac9e5c858
Changes Scope to use char[]'s insted of Identifiers for lookup.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
38 if (d.env.enclosing.find(d.identifier.get) !is null) |
136 | 39 if (d.env.parentFunction !is null) |
40 while( d.env.parentFunction.env !is env) | |
41 { | |
168
7982eb63c0eb
Some changes to get function overloading to work. Also class inherit works now - to some extend. needs vtables and all the complex stuff of it.
Anders Johnsen <skabet@gmail.com>
parents:
158
diff
changeset
|
42 if (d.env.enclosing.find(d.identifier.get)[0].env == env) |
136 | 43 messages.report(CannotRedeclare, d.identifier.loc) |
44 .arg(d.identifier.get); | |
45 env = env.enclosing; | |
46 } | |
47 | |
13
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
48 visitExp(d.identifier); |
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
49 if (d.init) |
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
50 visitExp(d.init); |
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
51 } |
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
|
52 |
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
|
53 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
|
54 { |
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 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
|
56 |
136 | 57 inFunction = true; |
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
|
58 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
|
59 visitStmt(stmt); |
136 | 60 inFunction = false; |
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
|
61 } |
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
|
62 |
99
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
92
diff
changeset
|
63 override void visitImportDecl(ImportDecl) { } |
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
92
diff
changeset
|
64 |
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
|
65 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
|
66 { |
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
|
67 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
|
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
|
69 |
55
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
39
diff
changeset
|
70 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
|
71 { |
130 | 72 internalVisitMemberRef(m); |
73 } | |
74 | |
75 private Symbol internalVisitMemberRef(MemberReference m) | |
76 { | |
168
7982eb63c0eb
Some changes to get function overloading to work. Also class inherit works now - to some extend. needs vtables and all the complex stuff of it.
Anders Johnsen <skabet@gmail.com>
parents:
158
diff
changeset
|
77 Symbol visitRef(MemberReference m, Identifier target, Symbol st) |
7982eb63c0eb
Some changes to get function overloading to work. Also class inherit works now - to some extend. needs vtables and all the complex stuff of it.
Anders Johnsen <skabet@gmail.com>
parents:
158
diff
changeset
|
78 { |
7982eb63c0eb
Some changes to get function overloading to work. Also class inherit works now - to some extend. needs vtables and all the complex stuff of it.
Anders Johnsen <skabet@gmail.com>
parents:
158
diff
changeset
|
79 auto child = m.child; |
7982eb63c0eb
Some changes to get function overloading to work. Also class inherit works now - to some extend. needs vtables and all the complex stuff of it.
Anders Johnsen <skabet@gmail.com>
parents:
158
diff
changeset
|
80 auto res = st.findMembers(child.get); |
7982eb63c0eb
Some changes to get function overloading to work. Also class inherit works now - to some extend. needs vtables and all the complex stuff of it.
Anders Johnsen <skabet@gmail.com>
parents:
158
diff
changeset
|
81 |
7982eb63c0eb
Some changes to get function overloading to work. Also class inherit works now - to some extend. needs vtables and all the complex stuff of it.
Anders Johnsen <skabet@gmail.com>
parents:
158
diff
changeset
|
82 if(!res.length) |
7982eb63c0eb
Some changes to get function overloading to work. Also class inherit works now - to some extend. needs vtables and all the complex stuff of it.
Anders Johnsen <skabet@gmail.com>
parents:
158
diff
changeset
|
83 messages.report(MissingMember, m.loc) |
7982eb63c0eb
Some changes to get function overloading to work. Also class inherit works now - to some extend. needs vtables and all the complex stuff of it.
Anders Johnsen <skabet@gmail.com>
parents:
158
diff
changeset
|
84 .arg(st.type.name) |
7982eb63c0eb
Some changes to get function overloading to work. Also class inherit works now - to some extend. needs vtables and all the complex stuff of it.
Anders Johnsen <skabet@gmail.com>
parents:
158
diff
changeset
|
85 .arg(target.get) |
7982eb63c0eb
Some changes to get function overloading to work. Also class inherit works now - to some extend. needs vtables and all the complex stuff of it.
Anders Johnsen <skabet@gmail.com>
parents:
158
diff
changeset
|
86 .arg(child.get); |
7982eb63c0eb
Some changes to get function overloading to work. Also class inherit works now - to some extend. needs vtables and all the complex stuff of it.
Anders Johnsen <skabet@gmail.com>
parents:
158
diff
changeset
|
87 else |
7982eb63c0eb
Some changes to get function overloading to work. Also class inherit works now - to some extend. needs vtables and all the complex stuff of it.
Anders Johnsen <skabet@gmail.com>
parents:
158
diff
changeset
|
88 internalCheckProtection(res[0], child); |
7982eb63c0eb
Some changes to get function overloading to work. Also class inherit works now - to some extend. needs vtables and all the complex stuff of it.
Anders Johnsen <skabet@gmail.com>
parents:
158
diff
changeset
|
89 |
7982eb63c0eb
Some changes to get function overloading to work. Also class inherit works now - to some extend. needs vtables and all the complex stuff of it.
Anders Johnsen <skabet@gmail.com>
parents:
158
diff
changeset
|
90 return res.length ? res[0] : null; |
7982eb63c0eb
Some changes to get function overloading to work. Also class inherit works now - to some extend. needs vtables and all the complex stuff of it.
Anders Johnsen <skabet@gmail.com>
parents:
158
diff
changeset
|
91 } |
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
|
92 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
|
93 { |
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
|
94 case ExpType.Identifier: |
168
7982eb63c0eb
Some changes to get function overloading to work. Also class inherit works now - to some extend. needs vtables and all the complex stuff of it.
Anders Johnsen <skabet@gmail.com>
parents:
158
diff
changeset
|
95 return visitRef(m, cast(Identifier)m.target, |
7982eb63c0eb
Some changes to get function overloading to work. Also class inherit works now - to some extend. needs vtables and all the complex stuff of it.
Anders Johnsen <skabet@gmail.com>
parents:
158
diff
changeset
|
96 (cast(Identifier)m.target).getSymbol); |
55
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
39
diff
changeset
|
97 case ExpType.MemberReference: |
130 | 98 Symbol s = internalVisitMemberRef(cast(MemberReference)m.target); |
99 if(!s) | |
100 return null; | |
168
7982eb63c0eb
Some changes to get function overloading to work. Also class inherit works now - to some extend. needs vtables and all the complex stuff of it.
Anders Johnsen <skabet@gmail.com>
parents:
158
diff
changeset
|
101 return visitRef(m, cast(Identifier)m.target, s); |
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
|
102 } |
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
|
103 } |
13
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
104 |
136 | 105 override void visitExp(Exp exp) |
106 { | |
107 if (exp.expType == ExpType.Identifier && inFunction | |
145
a14ac9e5c858
Changes Scope to use char[]'s insted of Identifiers for lookup.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
108 && exp.env.find((cast(Identifier)exp).get) !is null) |
136 | 109 { |
145
a14ac9e5c858
Changes Scope to use char[]'s insted of Identifiers for lookup.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
110 if (exp.env.findType((cast(Identifier)exp).get) is null) |
136 | 111 internalCheckProtection( |
168
7982eb63c0eb
Some changes to get function overloading to work. Also class inherit works now - to some extend. needs vtables and all the complex stuff of it.
Anders Johnsen <skabet@gmail.com>
parents:
158
diff
changeset
|
112 exp.env.find((cast(Identifier)exp).get)[0].sym, |
136 | 113 cast(Identifier)exp); |
114 } | |
115 | |
116 super.visitExp(exp); | |
117 } | |
118 | |
119 private void internalCheckProtection(Symbol sym, Identifier iden) | |
120 { | |
121 if (isChildOf(sym.decl.env, iden.env)) | |
122 return; | |
123 | |
124 switch(sym.decl.att.getProtection) | |
125 { | |
126 case Protection.Private: | |
127 /* if (iden.env.inModule == sym.decl.getIdentifier.env.inModule | |
128 && sym.decl.getIdentifier.env.enclosing == iden.env.inModule) | |
129 {} | |
130 else*/ | |
131 messages.report(CannotAccessPrivate, iden.loc); | |
132 return; | |
133 default: | |
134 return; | |
135 } | |
136 } | |
137 | |
138 private bool isChildOf(Scope parent, Scope child) | |
139 { | |
140 if (child is parent) | |
141 return true; | |
142 | |
143 if (child.enclosing !is null) | |
144 return isChildOf(parent, child.enclosing); | |
145 | |
146 return false; | |
147 } | |
148 | |
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
|
149 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
|
150 { |
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
151 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
|
152 } |
114
3a0cd42de9cc
Removed misc/Error.d and is now using the error system all way through.
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
153 |
3a0cd42de9cc
Removed misc/Error.d and is now using the error system all way through.
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
154 int[char[]] types; |
3a0cd42de9cc
Removed misc/Error.d and is now using the error system all way through.
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
155 MessageHandler messages; |
136 | 156 bool inFunction; |
13
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
diff
changeset
|
157 } |
22 | 158 |