comparison sema/AstAction.d @ 144:6e6355fb5f0f

- Parsing nested attributes. - Creating classes and interfaces in AST. - Updated AstPrinter to print attributes, classes and interfaces.
author Anders Johnsen <skabet@gmail.com>
date Mon, 21 Jul 2008 17:41:40 +0200
parents d76cc5cad4fc
children 393a1f47a6d2 6c5a3c0bb4fb
comparison
equal deleted inserted replaced
143:d76cc5cad4fc 144:6e6355fb5f0f
39 39
40 private Identifier identifierFromTok(Token t) 40 private Identifier identifierFromTok(Token t)
41 { 41 {
42 return new Identifier(t.location, sm.getText(t.asRange)); 42 return new Identifier(t.location, sm.getText(t.asRange));
43 } 43 }
44 44 override
45 override ModuleT actOnModule(ref Token _module, char[] name) 45 {
46 ModuleT actOnModule(ref Token _module, char[] name)
46 { 47 {
47 return new Module(name); 48 return new Module(name);
48 } 49 }
49 50
50 override ModuleT actOnImplicitModule(SLoc startLoc, char[] name) 51 ModuleT actOnImplicitModule(SLoc startLoc, char[] name)
51 { 52 {
52 return new Module(name); 53 return new Module(name);
53 } 54 }
54 55
55 override void actOnModuleDecl(ModuleT m, DeclT d) 56 void actOnModuleDecl(ModuleT m, DeclT d)
56 { 57 {
57 (cast(Module)m).addDecl(cast(Decl)d); 58 (cast(Module)m).addDecl(cast(Decl)d);
58 } 59 }
59 60
60 // -- Declarations -- 61 // -- Declarations --
61 override DeclT actOnImport(ref Token _, ref ModuleName target, Id* name) 62 DeclT actOnImport(ref Token _, ref ModuleName target, Id* name)
62 { 63 {
63 auto res = new ImportDecl; 64 auto res = new ImportDecl;
64 Identifier[] packages = new Identifier[target.packages.length]; 65 Identifier[] packages = new Identifier[target.packages.length];
65 foreach (i, v; target.packages) 66 foreach (i, v; target.packages)
66 packages[i] = identifierFromTok(v.tok); 67 packages[i] = identifierFromTok(v.tok);
70 if (name !is null) 71 if (name !is null)
71 res.aliasedName = identifierFromTok(name.tok); 72 res.aliasedName = identifierFromTok(name.tok);
72 return res; 73 return res;
73 } 74 }
74 75
75 override void addSelectiveImport(DeclT _import, ref Id target, Id* name) 76 void addSelectiveImport(DeclT _import, ref Id target, Id* name)
76 { 77 {
77 auto d = cast(ImportDecl)_import; 78 auto d = cast(ImportDecl)_import;
78 Identifier t = identifierFromTok(target.tok); 79 Identifier t = identifierFromTok(target.tok);
79 Identifier n = t; 80 Identifier n = t;
80 if (name !is null) 81 if (name !is null)
81 n = identifierFromTok(name.tok); 82 n = identifierFromTok(name.tok);
82 d.explicitSymbols ~= [t, n]; 83 d.explicitSymbols ~= [t, n];
83 } 84 }
84 85
85 override DeclT actOnDeclarator(ref Id type, ref Id id, ExprT init, Attribute att) 86 DeclT actOnDeclarator(ref Id type, ref Id id, ExprT init, Attribute att)
86 { 87 {
87 Decl d; 88 Decl d;
88 Exp exp = cast(Exp)init; 89 Exp exp = cast(Exp)init;
89 if(type.tok.type == Tok.Struct) 90 if (type.tok.type == Tok.Struct)
90 d = new StructDecl(identifierFromTok(id.tok)); 91 d = new StructDecl(identifierFromTok(id.tok));
92 else if (type.tok.type == Tok.Class)
93 d = new ClassDecl(identifierFromTok(id.tok));
94 else if (type.tok.type == Tok.Interface)
95 d = new InterfaceDecl(identifierFromTok(id.tok));
91 else 96 else
92 d = new VarDecl(handleType(type), identifierFromTok(id.tok), exp); 97 d = new VarDecl(handleType(type), identifierFromTok(id.tok), exp);
93 98
94 d.att = att; 99 d.att = att;
95 return d; 100 return d;
96 } 101 }
97 102
98 override void actOnStructMember(DeclT st_decl, DeclT m_decl) //ref Id type, ref Id name, ExprT init) 103 void actOnStructMember(DeclT st_decl, DeclT m_decl) //ref Id type, ref Id name, ExprT init)
99 { 104 {
100 StructDecl st = cast(StructDecl)st_decl; 105 StructDecl st = cast(StructDecl)st_decl;
101 st.addMember(cast(Decl)m_decl); 106 st.addMember(cast(Decl)m_decl);
102 } 107 }
103 108
104 override ExprT actOnMemberReference(ExprT lhs, SLoc op, Id member) 109 void actOnClassMember(DeclT cl_decl, DeclT m_decl)
110 {
111 ClassDecl cl = cast(ClassDecl)cl_decl;
112 cl.addMember(cast(Decl)m_decl);
113 }
114
115 void actOnClassBaseClass(DeclT cl_decl, ref Id name)
116 {
117 ClassDecl cl = cast(ClassDecl)cl_decl;
118 cl.addBaseClass(identifierFromTok(name.tok));
119 }
120
121 void actOnInterfaceMember(DeclT if_decl, DeclT m_decl)
122 {
123 InterfaceDecl inf = cast(InterfaceDecl)if_decl;
124 inf.addMember(cast(Decl)m_decl);
125 }
126
127 void actOnInterfaceBaseClass(DeclT if_decl, ref Id name)
128 {
129 InterfaceDecl inf = cast(InterfaceDecl)if_decl;
130 inf.addBaseClass(identifierFromTok(name.tok));
131 }
132
133 ExprT actOnMemberReference(ExprT lhs, SLoc op, Id member)
105 { 134 {
106 Exp exp = cast(Exp)lhs; 135 Exp exp = cast(Exp)lhs;
107 Identifier id = identifierFromTok(member.tok); 136 Identifier id = identifierFromTok(member.tok);
108 return new MemberReference(op, exp, id); 137 return new MemberReference(op, exp, id);
109 } 138 }
110 139
111 override DeclT actOnStartOfFunctionDef(ref Id type, ref Id name, Attribute att) 140 DeclT actOnStartOfFunctionDef(ref Id type, ref Id name, Attribute att)
112 { 141 {
113 auto res = new FuncDecl(identifierFromTok(type.tok), identifierFromTok(name.tok)); 142 auto res = new FuncDecl(identifierFromTok(type.tok), identifierFromTok(name.tok));
114 res.att = att; 143 res.att = att;
115 return res; 144 return res;
116 } 145 }
117 146
118 override void addFuncArg(DeclT func, Id type, Id name) 147 void addFuncArg(DeclT func, Id type, Id name)
119 { 148 {
120 FuncDecl fd = cast(FuncDecl)func; 149 FuncDecl fd = cast(FuncDecl)func;
121 if(name) 150 if(name)
122 fd.addParam(handleType(type), identifierFromTok(name.tok)); 151 fd.addParam(handleType(type), identifierFromTok(name.tok));
123 else 152 else
124 fd.addParam(identifierFromTok(type.tok)); 153 fd.addParam(identifierFromTok(type.tok));
125 } 154 }
126 155
127 override DeclT actOnEndOfFunction(DeclT func, StmtT stmts) 156 DeclT actOnEndOfFunction(DeclT func, StmtT stmts)
128 { 157 {
129 FuncDecl fd = cast(FuncDecl)func; 158 FuncDecl fd = cast(FuncDecl)func;
130 fd.setBody(cast(CompoundStatement)stmts); 159 fd.setBody(cast(CompoundStatement)stmts);
131 return fd; 160 return fd;
132 } 161 }
133 162
134 // -- Statements -- 163 // -- Statements --
135 override StmtT actOnCompoundStmt(ref Token l, ref Token r, StmtT[] stmts) 164 StmtT actOnCompoundStmt(ref Token l, ref Token r, StmtT[] stmts)
136 { 165 {
137 Stmt[] statements = cast(Stmt[])stmts; 166 Stmt[] statements = cast(Stmt[])stmts;
138 return new CompoundStatement(statements.dup); 167 return new CompoundStatement(statements.dup);
139 } 168 }
140 169
141 override StmtT actOnExprStmt(ExprT exp) 170 StmtT actOnExprStmt(ExprT exp)
142 { 171 {
143 return new ExpStmt(cast(Exp)exp); 172 return new ExpStmt(cast(Exp)exp);
144 } 173 }
145 174
146 override StmtT actOnReturnStmt(ref Token loc, ExprT exp) 175 StmtT actOnReturnStmt(ref Token loc, ExprT exp)
147 { 176 {
148 Exp e = cast(Exp)exp; 177 Exp e = cast(Exp)exp;
149 auto res = new ReturnStmt; 178 auto res = new ReturnStmt;
150 res.exp = e; 179 res.exp = e;
151 return res; 180 return res;
152 } 181 }
153 182
154 override StmtT actOnIfStmt(ref Token ifTok, ExprT cond, StmtT thenBody, 183 StmtT actOnIfStmt(ref Token ifTok, ExprT cond, StmtT thenBody,
155 ref Token elseTok, StmtT elseBody) 184 ref Token elseTok, StmtT elseBody)
156 { 185 {
157 Exp c = cast(Exp)cond; 186 Exp c = cast(Exp)cond;
158 Stmt t = cast(Stmt)thenBody; 187 Stmt t = cast(Stmt)thenBody;
159 Stmt e = cast(Stmt)elseBody; 188 Stmt e = cast(Stmt)elseBody;
160 return new IfStmt(c, t, e); 189 return new IfStmt(c, t, e);
161 } 190 }
162 191
163 override StmtT actOnWhileStmt(ref Token tok, ExprT cond, StmtT whileBody) 192 StmtT actOnWhileStmt(ref Token tok, ExprT cond, StmtT whileBody)
164 { 193 {
165 Exp c = cast(Exp)cond; 194 Exp c = cast(Exp)cond;
166 Stmt b = cast(Stmt)whileBody; 195 Stmt b = cast(Stmt)whileBody;
167 return new WhileStmt(c, b); 196 return new WhileStmt(c, b);
168 } 197 }
169 198
170 override StmtT actOnDeclStmt(DeclT decl) 199 StmtT actOnDeclStmt(DeclT decl)
171 { 200 {
172 Decl d = cast(Decl)decl; 201 Decl d = cast(Decl)decl;
173 return new DeclStmt(d); 202 return new DeclStmt(d);
174 } 203 }
175 204
176 override StmtT actOnStartOfSwitchStmt(ExprT exp) 205 StmtT actOnStartOfSwitchStmt(ExprT exp)
177 { 206 {
178 return new SwitchStmt(cast(Exp)exp); 207 return new SwitchStmt(cast(Exp)exp);
179 } 208 }
180 209
181 override void actOnCaseStmt(StmtT stmt, ExprT[] exps, StmtT[] stmts) 210 void actOnCaseStmt(StmtT stmt, ExprT[] exps, StmtT[] stmts)
182 { 211 {
183 auto sw = cast(SwitchStmt)stmt; 212 auto sw = cast(SwitchStmt)stmt;
184 sw.addCase(cast(Exp[])exps, cast(Stmt[])stmts); 213 sw.addCase(cast(Exp[])exps, cast(Stmt[])stmts);
185 } 214 }
186 215
187 override void actOnDefaultStmt(StmtT stmt, StmtT[] stmts) 216 void actOnDefaultStmt(StmtT stmt, StmtT[] stmts)
188 { 217 {
189 auto sw = cast(SwitchStmt)stmt; 218 auto sw = cast(SwitchStmt)stmt;
190 sw.setDefault(cast(Stmt[])stmts); 219 sw.setDefault(cast(Stmt[])stmts);
191 } 220 }
192 221
193 // -- Expressions -- 222 // -- Expressions --
194 override ExprT actOnNumericConstant(Token c) 223 ExprT actOnNumericConstant(Token c)
195 { 224 {
196 return new IntegerLit(c.location, sm.getText(c.asRange)); 225 return new IntegerLit(c.location, sm.getText(c.asRange));
197 } 226 }
198 227
199 override ExprT actOnStringExp(Token s) 228 ExprT actOnStringExp(Token s)
200 { 229 {
201 return new StringExp(s.location, sm.getText(s.asRange)); 230 return new StringExp(s.location, sm.getText(s.asRange));
202 } 231 }
203 232
204 override ExprT actOnIdentifierExp(Id id) 233 ExprT actOnIdentifierExp(Id id)
205 { 234 {
206 return identifierFromTok(id.tok); 235 return identifierFromTok(id.tok);
207 } 236 }
208 237
209 override ExprT actOnBinaryOp(SLoc op_loc, Operator op, ExprT l, ExprT r) 238 ExprT actOnBinaryOp(SLoc op_loc, Operator op, ExprT l, ExprT r)
210 { 239 {
211 Exp left = cast(Exp)l; 240 Exp left = cast(Exp)l;
212 Exp right = cast(Exp)r; 241 Exp right = cast(Exp)r;
213 switch(op) 242 switch(op)
214 { 243 {
223 BinaryExp.Operator bin_op = cast(BinaryExp.Operator)op; 252 BinaryExp.Operator bin_op = cast(BinaryExp.Operator)op;
224 return new BinaryExp(op_loc, bin_op, left, right); 253 return new BinaryExp(op_loc, bin_op, left, right);
225 } 254 }
226 } 255 }
227 256
228 override ExprT actOnUnaryOp(Token op, ExprT operand) 257 ExprT actOnUnaryOp(Token op, ExprT operand)
229 { 258 {
230 Exp target = cast(Exp)operand; 259 Exp target = cast(Exp)operand;
231 if (op.type == Tok.Minus) 260 if (op.type == Tok.Minus)
232 return new NegateExp(op.location, target); 261 return new NegateExp(op.location, target);
233 if (op.type == Tok.Star) 262 if (op.type == Tok.Star)
234 return new DerefExp(op.location, target); 263 return new DerefExp(op.location, target);
235 assert(0, "Only valid unary expressions are -x and *x"); 264 assert(0, "Only valid unary expressions are -x and *x");
236 } 265 }
237 266
238 override ExprT actOnCallExpr(ExprT fn, ref Token, ExprT[] args, ref Token) 267 ExprT actOnCallExpr(ExprT fn, ref Token, ExprT[] args, ref Token)
239 { 268 {
240 Exp f = cast(Exp)fn; 269 Exp f = cast(Exp)fn;
241 Exp[] arguments = cast(Exp[])args.dup; 270 Exp[] arguments = cast(Exp[])args.dup;
242 return new CallExp(f, arguments); 271 return new CallExp(f, arguments);
243 } 272 }
244 273
245 override ExprT actOnCastExpr(ref Token _cast, Id id, ExprT exp) 274 ExprT actOnCastExpr(ref Token _cast, Id id, ExprT exp)
246 { 275 {
247 Exp target = cast(Exp)exp; 276 Exp target = cast(Exp)exp;
248 Identifier target_type = identifierFromTok(id.tok); 277 Identifier target_type = identifierFromTok(id.tok);
249 return new CastExp(_cast.location, target_type, target); 278 return new CastExp(_cast.location, target_type, target);
250 } 279 }
251 280
252 override ExprT 281 ExprT
253 actOnIndexEpr(ExprT arr, ref Token lb, ExprT index, ref Token rb) 282 actOnIndexEpr(ExprT arr, ref Token lb, ExprT index, ref Token rb)
254 { 283 {
255 Exp target = cast(Exp)arr; 284 Exp target = cast(Exp)arr;
256 Exp idx = cast(Exp)index; 285 Exp idx = cast(Exp)index;
257 return new IndexExp(target, lb.location, idx, rb.location); 286 return new IndexExp(target, lb.location, idx, rb.location);
258 } 287 }
259 } 288 }
260 289 }
290