comparison ast/Exp.d @ 129:ed815b31479b

Added a Symbol
author Anders Halager <halager@gmail.com>
date Sat, 21 Jun 2008 20:41:18 +0200
parents c3b24e7e8cf8
children 2be29b296081
comparison
equal deleted inserted replaced
128:7264c61088c4 129:ed815b31479b
1 module ast.Exp; 1 module ast.Exp;
2 2
3 import tango.text.Util; 3 import tango.text.Util,
4 Integer = tango.text.convert.Integer;
4 import tango.io.Stdout; 5 import tango.io.Stdout;
5 6
6 import ast.Decl, 7 import ast.Decl,
7 ast.Stmt; 8 ast.Stmt;
8 9
9 import lexer.Token; 10 import lexer.Token;
10 11
11 import sema.Scope, 12 import sema.Scope,
13 sema.Symbol,
12 sema.DType; 14 sema.DType;
13 15
14 import basic.LiteralParsing; 16 import basic.LiteralParsing;
15 17
16 enum ExpType 18 enum ExpType
36 { 38 {
37 this.expType = expType; 39 this.expType = expType;
38 this.loc = loc; 40 this.loc = loc;
39 } 41 }
40 42
43 /**
44 Get the fully qualified name for the expression (if it can be resolved to
45 one) - otherwise null is returned
46 **/
47 char[] getFQN() { return null; }
48
49 /// The same as getFQN, except that the name is mangled
50 char[] getMangledFQN() { return null; }
51
52 /**
53 Try to get the symbol the expression represents.
54
55 Returns null for most expressions as they don't represent any symbol.
56 Identifiers and member references can have a sensible value.
57 **/
58 Symbol getSymbol() { return null; }
59
41 /// Get the type of the expression 60 /// Get the type of the expression
42 abstract DType type(); 61 abstract DType type();
43 62
44 /// Indicates which type the expression is - to avoid a lot of casts 63 /// Indicates which type the expression is - to avoid a lot of casts
45 ExpType expType; 64 ExpType expType;
46 65
47 /// The environment of the expression 66 /// The environment of the expression
48 Scope env; 67 Scope env;
68
69 Symbol symbol;
49 70
50 int stmtIndex; 71 int stmtIndex;
51 72
52 /** 73 /**
53 The "main" location of the expression. 74 The "main" location of the expression.
94 return res; 115 return res;
95 } 116 }
96 117
97 Exp simplify() 118 Exp simplify()
98 { 119 {
120 /*
99 if(auto t = type.asStruct) 121 if(auto t = type.asStruct)
100 { 122 {
101 DFunction func_t = cast(DFunction)exp.type(); 123 DFunction func_t = cast(DFunction)exp.type();
102 assert(func_t !is null, "Calling on something that isn't a function"); 124 assert(func_t !is null, "Calling on something that isn't a function");
103 if (cast(DStruct)func_t.returnType is null) 125 if (cast(DStruct)func_t.returnType is null)
132 f.statements = stmts; 154 f.statements = stmts;
133 callExp.sret = true; 155 callExp.sret = true;
134 156
135 return i; 157 return i;
136 } 158 }
159 */
137 return this; 160 return this;
138 } 161 }
139 } 162 }
140 163
141 class AssignExp : BinaryExp 164 class AssignExp : BinaryExp
301 return this; 324 return this;
302 } 325 }
303 326
304 override DType type() 327 override DType type()
305 { 328 {
306 return exp.type().asPointer.pointerOf; 329 return exp.type().asPointer().pointerOf;
307 } 330 }
308 331
309 override SourceRange sourceRange() 332 override SourceRange sourceRange()
310 { 333 {
311 return SourceRange(loc) + exp.sourceRange; 334 return SourceRange(loc) + exp.sourceRange;
368 super(ExpType.MemberReference, dot); 391 super(ExpType.MemberReference, dot);
369 this.target = target; 392 this.target = target;
370 this.child = child; 393 this.child = child;
371 } 394 }
372 395
396 override char[] getFQN()
397 {
398 return getSymbol().getFQN();
399 }
400
401 override char[] getMangledFQN()
402 {
403 return target.type.mangle() ~ child.getMangledFQN();
404 }
405
406 override Symbol getSymbol()
407 {
408 auto s = target.getSymbol();
409 if (s !is null)
410 return s.findMember(child.get);
411 return null;
412 }
413
373 Exp simplify() 414 Exp simplify()
374 { 415 {
375 target = target.simplify; 416 target = target.simplify;
376 return this; 417 return this;
377 } 418 }
533 protected this(ExpType t, SLoc loc) 574 protected this(ExpType t, SLoc loc)
534 { 575 {
535 super(t, loc); 576 super(t, loc);
536 } 577 }
537 578
579 override char[] getFQN()
580 {
581 return name;
582 }
583
584 override char[] getMangledFQN()
585 {
586 return Integer.toString(name.length) ~ name;
587 }
588
589 override Symbol getSymbol()
590 {
591 if (auto decl = env.find(this))
592 return decl.sym;
593 else
594 return null;
595 }
596
538 override DType type() 597 override DType type()
539 { 598 {
540 if (myType !is null) 599 if (myType !is null)
541 return myType; 600 return myType;
542 if(auto s = env.find(this)) 601 else if (auto sym = getSymbol)
543 if(s.type) 602 myType = sym.type;
544 myType = s.type;
545 else 603 else
546 myType = DType.Int; 604 myType = DType.Int;
605
547 return myType; 606 return myType;
548 } 607 }
549 608
550 this(char[] name) 609 this(char[] name)
551 { 610 {
554 } 613 }
555 614
556 char[] get() 615 char[] get()
557 { 616 {
558 return name; 617 return name;
559 }
560
561 char[] getMangled()
562 {
563 DType t = type;
564
565 if(name == "main")
566 return "main";
567
568 return "_D"~name~t.mangle;
569 } 618 }
570 619
571 hash_t toHash() 620 hash_t toHash()
572 { 621 {
573 return jhash(name); 622 return jhash(name);