forked from steshaw/blue
/
ObjExpAST.cs
executable file
·2290 lines (1888 loc) · 66.5 KB
/
ObjExpAST.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//-----------------------------------------------------------------------------
// File: ObjExpAST.cs
//
// Description:
// all Exp nodes that are used to generate an expression evaluating to an
// object
//
// See AST.cs for details
//
// Exp-> ObjExp -> <any derived class below>
//
// SimpleObjExp -> id // a single identifier
// DotObjExp -> ObjExp '.' id // Field Access
// MethodCallExp -> id '(' param_list ')' // MethodCall
// MethodCallExp -> ObjExp '.' id '(' param_list ')' // MethodCall
// ArrayAccessExp -> ObjExp '[' int_exp ']' // array access
// CastObjExp -> '(' Type ')' Exp // Type Cast
// NewObjExp -> 'new' type '(' param_list ')' // new - ctor
//-----------------------------------------------------------------------------
using System;
using System.Diagnostics;
using System.Xml;
using SymbolEngine;
using Blue.Public;
using CodeGen = Blue.CodeGen;
namespace AST
{
#region Resolved Exp
//-----------------------------------------------------------------------------
// Expression that represents a namespace
//-----------------------------------------------------------------------------
public class NamespaceExp : Exp
{
public NamespaceExp(
SymbolEngine.NamespaceEntry symbol
)
{
m_symbol = symbol;
}
#region Properties & Data
NamespaceEntry m_symbol;
public NamespaceEntry Symbol
{
get { return m_symbol; }
}
#endregion
#region Checks
public override void DebugCheck(ISemanticResolver s)
{
Debug.Assert(m_symbol != null);
}
public override void Dump(XmlWriter o)
{
}
public override void ToSource(System.CodeDom.Compiler.IndentedTextWriter sb)
{
sb.Write("<N>'{0}'", m_symbol.FullName);
}
#endregion
protected override Type CalcCLRTypeHelper(ISemanticResolver s)
{
return null;
}
protected override Exp ResolveExpAsRight(ISemanticResolver s)
{
return this;
}
}
//-----------------------------------------------------------------------------
// Type, this is also temporary
//-----------------------------------------------------------------------------
class TypeExp : Exp
{
public TypeExp(TypeEntry symbol)
{
m_symbol = symbol;
}
TypeEntry m_symbol;
public TypeEntry Symbol
{
get { return m_symbol; }
}
#region Checks
public override void DebugCheck(ISemanticResolver s)
{
Debug.Assert(m_symbol != null);
}
public override void Dump(XmlWriter o)
{
}
public override void ToSource(System.CodeDom.Compiler.IndentedTextWriter sb)
{
sb.Write("({0})", m_symbol.FullName);
}
#endregion
#region Resolution
protected override Type CalcCLRTypeHelper(ISemanticResolver s)
{
return m_symbol.CLRType;
}
protected override Exp ResolveExpAsRight(ISemanticResolver s)
{
// We can only do the CLR types after we do them for the symbols.
// But we may refer to a type before we have resolved it to a CLR symbol.
// (such as when we specify a base class).
if (m_symbol.CLRType != null)
CalcCLRType(s);
return this;
}
#endregion
}
//-----------------------------------------------------------------------------
// Local var
//-----------------------------------------------------------------------------
public class LocalExp : Exp
{
public LocalExp(
SymbolEngine.LocalVarExpEntry symbol
)
{
m_symbol = symbol;
}
LocalVarExpEntry m_symbol;
public LocalVarExpEntry Symbol
{
get { return m_symbol; }
}
#region Checks
public override void DebugCheck(ISemanticResolver s)
{
Debug.Assert(m_symbol != null);
}
public override void Dump(XmlWriter o)
{
}
public override void ToSource(System.CodeDom.Compiler.IndentedTextWriter sb)
{
sb.Write("<L>{0}", m_symbol.Name);
}
#endregion
#region Resolution
protected override Type CalcCLRTypeHelper(ISemanticResolver s)
{
return m_symbol.CLRType;
}
protected override Exp ResolveExpAsLeft(ISemanticResolver s)
{
CalcCLRType(s);
return this;
}
protected override Exp ResolveExpAsRight(ISemanticResolver s)
{
CalcCLRType(s);
return this;
}
#endregion
#region Generate
public override void GenerateAsLeftPre(CodeGen.EmitCodeGen gen)
{
gen.GenerateAsLeftPre(this);
}
public override void GenerateAsRight(CodeGen.EmitCodeGen gen)
{
gen.Generate(this);
}
public override void GenerateAsLeftPost(CodeGen.EmitCodeGen gen)
{
gen.GenerateAsLeftPost(this);
}
public override void GenerateAddrOf(CodeGen.EmitCodeGen gen)
{
gen.GenerateAddrOf(this);
}
#endregion
}
//-----------------------------------------------------------------------------
// Expression refering to a parameter
//-----------------------------------------------------------------------------
public class ParamExp : Exp
{
public ParamExp(
SymbolEngine.ParamVarExpEntry symbol
)
{
m_symbol = symbol;
}
ParamVarExpEntry m_symbol;
public ParamVarExpEntry Symbol
{
get { return m_symbol; }
}
#region Checks
public override void DebugCheck(ISemanticResolver s)
{
Debug.Assert(m_symbol != null);
}
public override void Dump(XmlWriter o)
{
}
public override void ToSource(System.CodeDom.Compiler.IndentedTextWriter sb)
{
sb.Write("<P>{0}", m_symbol.Name);
}
#endregion
#region Resolution
protected override Type CalcCLRTypeHelper(ISemanticResolver s)
{
return m_symbol.CLRType;
}
protected override Exp ResolveExpAsLeft(ISemanticResolver s)
{
CalcCLRType(s);
return this;
}
protected override Exp ResolveExpAsRight(ISemanticResolver s)
{
CalcCLRType(s);
return this;
}
#endregion
#region Generate
public override void GenerateAsLeftPre(CodeGen.EmitCodeGen gen)
{
gen.GenerateAsLeftPre(this);
}
public override void GenerateAsRight(CodeGen.EmitCodeGen gen)
{
gen.Generate(this);
}
public override void GenerateAsLeftPost(CodeGen.EmitCodeGen gen)
{
gen.GenerateAsLeftPost(this);
}
public override void GenerateAddrOf(CodeGen.EmitCodeGen gen)
{
gen.GenerateAddrOf(this);
}
#endregion
}
//-----------------------------------------------------------------------------
// For Properties
//-----------------------------------------------------------------------------
public class PropertyExp : Exp
{
public PropertyExp(
PropertyExpEntry symbol,
Exp expInstance // null for statics
)
{
Debug.Assert(symbol != null);
m_symbol = symbol;
m_expInstance = expInstance;
}
#region Properties & Data
SymbolEngine.PropertyExpEntry m_symbol;
public PropertyExpEntry Symbol
{
get { return m_symbol; }
}
Exp m_expInstance;
public Exp InstanceExp
{
get { return m_expInstance; }
}
public bool IsStatic
{
get { return m_expInstance == null; }
}
#endregion
#region Checks
public override void DebugCheck(ISemanticResolver s)
{
Debug.Assert(m_symbol != null);
Debug.Assert(m_symbol.IsStatic == IsStatic);
}
public override void Dump(XmlWriter o)
{
}
public override void ToSource(System.CodeDom.Compiler.IndentedTextWriter sb)
{
if (m_expInstance == null)
//sb.Write("[static]");
sb.Write(m_symbol.SymbolClass.FullName);
else
m_expInstance.ToSource(sb);
sb.Write(".<prop>{0}", m_symbol.Name);
}
#endregion
#region Resolution
protected override Type CalcCLRTypeHelper(ISemanticResolver s)
{
return m_symbol.CLRType;
}
protected override Exp ResolveExpAsLeft(ISemanticResolver s)
{
// Whoever is resolving against us will have to change us to a
// Set method (because only they have access to the RHS).
CalcCLRType(s);
return this;
}
protected override Exp ResolveExpAsRight(ISemanticResolver s)
{
//CalcCLRType(s);
//return this;
// Transform into a get
Exp eResolved = new MethodCallExp(
this.InstanceExp,
Symbol.SymbolGet,
new ArgExp[0],
s
);
Exp.ResolveExpAsRight(ref eResolved, s);
return eResolved;
}
#endregion
#region Generate
// Properties don't generate code.
// They should be transformed into MethodCalls by that point
#endregion
}
//-----------------------------------------------------------------------------
// For MethodPointers (used for delegates)
//-----------------------------------------------------------------------------
public class MethodPtrExp : Exp
{
public MethodPtrExp(
MethodExpEntry symbol
)
{
Debug.Assert(symbol != null);
m_symbol = symbol;
}
#region Properties & Data
MethodExpEntry m_symbol;
public MethodExpEntry Symbol
{
get { return m_symbol; }
}
#endregion
#region Checks
public override void DebugCheck(ISemanticResolver s)
{
Debug.Assert(m_symbol != null);
}
public override void Dump(XmlWriter o)
{
o.WriteStartElement("MethodPtrExp");
string stName = TypeEntry.GetDecoratedParams(m_symbol);
o.WriteAttributeString("name", stName);
o.WriteEndElement();
}
public override void ToSource(System.CodeDom.Compiler.IndentedTextWriter sb)
{
sb.Write("<Mptr>'{0}'", m_symbol.PrettyDecoratedName);
}
#endregion
#region Resolution
protected override Type CalcCLRTypeHelper(ISemanticResolver s)
{
// This is a pointer, and pointers are ints.
// Also, we pass this to the delegate ctor, which expects an int
return Type.GetType("System.IntPtr");
//return typeof(int);
}
protected override Exp ResolveExpAsRight(ISemanticResolver s)
{
CalcCLRType(s);
return this;
}
#endregion
public override void GenerateAsRight(CodeGen.EmitCodeGen gen)
{
gen.Generate(this);
}
}
//-----------------------------------------------------------------------------
// For events
// This is only a temporary node (like properties) and should be transformed
// into method calls.
//-----------------------------------------------------------------------------
public class EventExp : Exp
{
public EventExp(
EventExpEntry symbol,
Exp expInstance // null for statics
)
{
Debug.Assert(symbol != null);
m_symbol = symbol;
m_expInstance = expInstance;
}
EventExpEntry m_symbol;
public EventExpEntry Symbol
{
get { return m_symbol; }
}
Exp m_expInstance;
public Exp InstanceExp
{
get { return m_expInstance; }
}
#region Checks
public override void DebugCheck(ISemanticResolver s)
{
Debug.Assert(false, "EventExp node should be replaced by end of resolution");
Debug.Assert(m_symbol != null);
}
public override void Dump(XmlWriter o)
{
}
public override void ToSource(System.CodeDom.Compiler.IndentedTextWriter sb)
{
sb.Write("<E>{0}", m_symbol.Name);
}
#endregion
#region Resolution
protected override Type CalcCLRTypeHelper(ISemanticResolver s)
{
return m_symbol.EventType.CLRType;
}
// Ok to resolve as a LS thing (really only in += and -=).
// This node will get replaced with a method call anyways..
protected override Exp ResolveExpAsLeft(ISemanticResolver s)
{
CalcCLRType(s);
return this;
}
// See section 10.7 of the C# spec.
// Outside of our class, we can only use an event as the LS in += and -=
// However, within our class, we can use an event on the RS, like a delegate.
// This is a little goofy.
// We can actually never use an event on the RS. When try to, we're actually
// using a compiler generated delegate (that has the exact same name) instead.
// However, we can't put both an event & a delegate in the same scope (because
// the names would conflict). So the delegate has a different name than the event.
// When the user tries to access an event as a RHS, we switch it to the delegate here.
protected override Exp ResolveExpAsRight(ISemanticResolver s)
{
FieldExpEntry f = m_symbol.Field;
if (f == null)
{
// Events aren't allowed as RHS expressions.
ThrowError(SymbolError.NoEventOnRHS(this));
}
Exp e = new FieldExp(f, this.InstanceExp);
Exp.ResolveExpAsRight(ref e, s);
return e;
}
#endregion
}
//-----------------------------------------------------------------------------
// For fields
//-----------------------------------------------------------------------------
public class FieldExp : Exp
{
public FieldExp(
FieldExpEntry symbol,
Exp expInstance // null for statics
)
{
Debug.Assert(symbol != null);
m_symbol = symbol;
m_expInstance = expInstance;
}
#region Properties & Data
SymbolEngine.FieldExpEntry m_symbol;
public FieldExpEntry Symbol
{
get { return m_symbol; }
}
Exp m_expInstance;
public Exp InstanceExp
{
get { return m_expInstance; }
}
public bool IsStatic
{
get { return m_expInstance == null; }
}
#endregion
#region Checks
public override void DebugCheck(ISemanticResolver s)
{
Debug.Assert(m_symbol != null);
Debug.Assert(m_symbol.IsStatic == IsStatic);
}
public override void Dump(XmlWriter o)
{
}
public override void ToSource(System.CodeDom.Compiler.IndentedTextWriter sb)
{
if (m_expInstance == null)
//sb.Write("[static]");
sb.Write(m_symbol.SymbolClass.FullName);
else
m_expInstance.ToSource(sb);
sb.Write(".<field>{0}", m_symbol.Name);
}
#endregion
#region Resolution
protected override Type CalcCLRTypeHelper(ISemanticResolver s)
{
return m_symbol.CLRType;
}
protected override Exp ResolveExpAsLeft(ISemanticResolver s)
{
CalcCLRType(s);
return this;
}
protected override Exp ResolveExpAsRight(ISemanticResolver s)
{
CalcCLRType(s);
return this;
}
#endregion
#region Generate
public override void GenerateAsLeftPre(CodeGen.EmitCodeGen gen)
{
gen.GenerateAsLeftPre(this);
}
public override void GenerateAsRight(CodeGen.EmitCodeGen gen)
{
gen.Generate(this);
}
public override void GenerateAsLeftPost(CodeGen.EmitCodeGen gen)
{
gen.GenerateAsLeftPost(this);
}
public override void GenerateAddrOf(CodeGen.EmitCodeGen gen)
{
gen.GenerateAddrOf(this);
}
#endregion
}
#endregion
//-----------------------------------------------------------------------------
// ObjExp -> id
// Resolve to another, more-specific, Node
//-----------------------------------------------------------------------------
public class SimpleObjExp : Exp
{
public SimpleObjExp(Identifier id)
{
m_filerange = id.Location;
m_strId = id;
}
public SimpleObjExp(string st)
{
m_filerange = null;
m_strId = new Identifier(st, m_filerange);
}
#region Properties & Data
Identifier m_strId;
public Identifier Name
{
get { return m_strId; }
}
#endregion
#region Checks
public override void DebugCheck(ISemanticResolver s)
{
Debug.Assert(false, "Temporary node still in Final pass");
}
// Dump as XML
public override void Dump(XmlWriter o)
{
o.WriteStartElement("SimpleObjExp");
o.WriteAttributeString("id", m_strId.Text);
o.WriteEndElement();
}
public override string ToString()
{
return this.m_strId.Text;
}
public override void ToSource(System.CodeDom.Compiler.IndentedTextWriter sb)
{
sb.Write("'{0}'", this.Name.Text);
}
#endregion
// m_type is only null if we're a namespace, in which case we have an ancestor
// DotObjExp which will have the type
// Resolve as a LHS
protected override Exp ResolveExpAsLeft(ISemanticResolver s)
{
Exp e = GetResolvedNode(s, false);
Debug.Assert(e != this);
Exp.ResolveExpAsLeft(ref e, s);
e.SetLocation(this.Location);
return e;
}
// Resolve as a RHS
protected override Exp ResolveExpAsRight(ISemanticResolver s)
{
Exp e = GetResolvedNode(s, true);
if (e != this)
Exp.ResolveExpAsRight(ref e, s);
e.SetLocation(this.Location);
return e;
}
// An ObjExp is just a temporary node. But that's the best a Context-Free parse can
// do. So now that we're building a symbol table, we can do a Context-Sensitive resolution
// and figure out what type of node this really is.
public Exp GetResolvedNode(ISemanticResolver s, bool fRight)
{
Exp eResolved = null;
// Lookup the symbol and determine what we are
//SymEntry sym = s.LookupSymbol(this.m_strId, true);
string stName = this.m_strId.Text;
SymEntry sym = s.LookupSymbolWithContext(this.m_strId, false); // allow methods
// Namespace
if (sym is NamespaceEntry)
{
eResolved = new NamespaceExp(sym as NamespaceEntry);
}
// Local Variable
else if (sym is LocalVarExpEntry)
{
eResolved = new LocalExp(sym as LocalVarExpEntry);
}
// Parameter
else if (sym is ParamVarExpEntry)
{
eResolved = new ParamExp(sym as ParamVarExpEntry);
}
// A type name
else if (sym is TypeEntry)
{
eResolved = new TypeExp(sym as TypeEntry);
}
// A field (w/ an implied 'this' pointer)
else if (sym is FieldExpEntry)
{
// When a single identifier resolves to a field, it can be either
// an instance field with an implied 'this' ref, or a static field of this class.
FieldExpEntry f = sym as FieldExpEntry;
Exp expInstance = null;
if (!f.IsStatic)
{
expInstance = new SimpleObjExp("this");
Exp.ResolveExpAsRight(ref expInstance, s);
}
eResolved = new FieldExp(f, expInstance);
}
// An event (w/ an implied 'this' ptr)
else if (sym is EventExpEntry)
{
EventExpEntry e = (EventExpEntry) sym;
Exp expInstance = null;
if (!e.Mods.IsStatic)
{
expInstance = new SimpleObjExp("this");
Exp.ResolveExpAsRight(ref expInstance, s);
}
eResolved = new EventExp(e, expInstance);
}
// A property (w/ an implied 'this' ptr).
// Properties will eventually be converted into method calls.
else if (sym is PropertyExpEntry)
{
PropertyExpEntry p = (PropertyExpEntry) sym;
Exp expInstance = null;
if (!p.IsStatic)
{
expInstance = new SimpleObjExp("this");
Exp.ResolveExpAsRight(ref expInstance, s);
}
eResolved = new PropertyExp(p, expInstance);
}
// Not recognized.
else {
if (stName == "this") // check a common error case...
Debug.Assert(false, "Can't access 'this'. Are we in a static?");
if (sym == null)
{
MethodHeaderEntry h = s.GetCurrentClass().LookupMethodHeader(this.m_strId.Text);
if (h != null)
{
return this;
}
ThrowError(SymbolError.UndefinedSymbol(m_strId));
//Debug.Assert(false, "Unknown name in SimpleObjExp:" + stName);
}
Debug.Assert(false, "Unknown symbol type:" + ((sym == null) ? "null" : sym.ToString()));
}
Debug.Assert(eResolved != null);
return eResolved;
}
}
//-----------------------------------------------------------------------------
// DotObjExp -> ObjExp '.' id
//-----------------------------------------------------------------------------
public class DotObjExp : Exp
{
#region Construction
public DotObjExp(Exp left, Identifier id)
{
m_left = left;
m_strId = id;
m_filerange = id.Location;
}
#endregion
#region Properties & Data
Exp m_left;
public Exp LeftExp
{
get { return m_left; }
}
readonly Identifier m_strId;
public Identifier Id
{
get { return m_strId; }
}
#endregion
#region Resolution
// Resolve as a LHS
protected override Exp ResolveExpAsLeft(ISemanticResolver s)
{
Exp.ResolveExpAsRight(ref m_left, s);
Exp e = GetResolvedNode(s);
Exp.ResolveExpAsLeft(ref e, s);
e.SetLocation(this.Location);
return e;
}
// Resolve as a RHS
protected override Exp ResolveExpAsRight(ISemanticResolver s)
{
// E.i
// First resolve the exp on the left of the dot.
Exp.ResolveExpAsRight(ref m_left, s);
// Now we can figure out what we ought to be.
Exp e = GetResolvedNode(s);
// If we changed nodes, then resolve the new one.
// If we didn't change, then don't resolve again (else we have stack-overflow)
if (e != this)
Exp.ResolveExpAsRight(ref e, s);
e.SetLocation(this.Location);
return e;
}
// An ObjExp is just a temporary node. But that's the best a Context-Free parse can
// do. So now that we're building a symbol table, we can do a Context-Sensitive resolution
// and figure out what type of node this really is.
public Exp GetResolvedNode(ISemanticResolver s)
{
string stText = this.m_strId.Text;
// Left must already be resolved, then we resolve right in the context of left
Debug.Assert(m_left != null);
Exp eResolved = null;
// @todo, what if left is a NullExp?
if (m_left is NamespaceExp)
{
// We're either a nested namespace or a class
NamespaceEntry n = (m_left as NamespaceExp).Symbol;
SymEntry sym = n.ChildScope.LookupSymbol(stText);
if (sym is NamespaceEntry)
{
eResolved = new NamespaceExp(sym as NamespaceEntry);
}
else if (sym is TypeEntry)
{
eResolved = new TypeExp(sym as TypeEntry);
} else {
//ThrowError_UndefinedSymbolInNamespace(s, n, m_strId);
ThrowError(SymbolError.UndefinedSymbolInNamespace(n, m_strId));
}
}
// Check for statics
else if (m_left is TypeExp)
{
TypeEntry t = ((TypeExp) m_left).Symbol;
t.EnsureResolved(s);
SymEntry sym = t.MemberScope.LookupSymbol(stText);
if (sym is FieldExpEntry)
{
Debug.Assert(((FieldExpEntry) sym).IsStatic);
eResolved = new FieldExp(sym as FieldExpEntry, null); // static
}
else if (sym is PropertyExpEntry)
{
eResolved = new PropertyExp(sym as PropertyExpEntry, null);
}
else if (sym is EventExpEntry)
{
eResolved = new EventExp(sym as EventExpEntry, null);
}
// Allow nested types
else if (sym is TypeEntry)
{
eResolved = new TypeExp(sym as TypeEntry);
}
else
{
// Must be a method. The node transform occurs higher up though.
Debug.Assert((sym = t.LookupMethodHeader(stText)) != null);
eResolved = this;
}
if (eResolved == null) {
//ThrowError_UndefinedSymbolInType(s, t, m_strId);
ThrowError(SymbolError.UndefinedSymbolInType(t, m_strId));
}
}
// m_left is a variable, and we're doing an instance member dereference
else {
TypeEntry t = null;
t = s.ResolveCLRTypeToBlueType(this.m_left.CLRType);
t.EnsureResolved(s);
Scope scope = t.MemberScope;
// @todo - broken for an interface. IA : IB, scope for IA doesn't link to IB.
SymEntry sym = scope.LookupSymbol(stText);
if (sym is FieldExpEntry)
{
eResolved = new FieldExp(sym as FieldExpEntry, this.m_left);
}
else if (sym is PropertyExpEntry)
{
eResolved = new PropertyExp(sym as PropertyExpEntry, this.m_left);
}
else if (sym is EventExpEntry)
{
eResolved = new EventExp(sym as EventExpEntry, this.m_left);
}
else
{
// Must be a method. The node transform occurs higher up though.
sym = t.LookupMethodHeader(stText);
if (sym != null)
eResolved = this;
}
if (eResolved == null)
{
ThrowError(SymbolError.UndefinedSymbolInType(t, m_strId));
}
}
Debug.Assert(eResolved != null);
return eResolved;
}
#endregion