forked from steshaw/blue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AST.cs
executable file
·4001 lines (3296 loc) · 122 KB
/
AST.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: AST.cs
// The abstract syntax tree
//
// All AST nodes derive from class Node for utility purposes (dumping,
// source file mapping, etc).
//-----------------------------------------------------------------------------
#region Comment on AST node hierarchy
//-----------------------------------------------------------------------------
// Nodes fall into 3 categories:
// 1) Decl - declare something
// 2) Statement - statements in program
// 3) Exp - part of an expression
//
// Each node may reference its corresponding derived class from SymEntry
// The AST is pure and does not contain any resolved information besides
// the symbol table entry. All resolved data is stored on the symbol entry.
// The parser creates the AST with strings for the identifiers.
// Then the semantic checking populates the SymbolTable and does a lookup
// on the string name and sets the SymEntry fields.
//
// The node organization is:
// + Node - root class
// + ProgramDecl - declare a program
// + NamespaceDecl - declare a namespace
// + ClassDecl - declare a class
// + MemberDecl - member within a class
// + MethodDecl
// + FieldDecl
// + VarDecl
// + LocalVarDecl - declare a local variable
// + ParamVarDecl - declare a parameter
// + PropertyDecl
//
// + Statement
// + ReturnStatement
// + AssignStatement
// + IfStatement
// + LoopStatement
// + WhileStatement
// + DoStatement
//
// + Exp - nodes that form expressions
// + LiteralExp
// + IntExp
// + BoolExp
// + StringExp
// + CharExp
// + DoubleExp
// + BinaryExp
// + UnaryExp
// + ObjExp
// + SimpleObjExp - apply single identifier to ObjExp
// + DotObjExp - single identifier
// + MethodCallExp - evaluates to a method call
// + NewObjExp - create new objects
// + CastObjExp - cast an expression into an object
//
// + TypeSig - store a type
//-----------------------------------------------------------------------------
#endregion
using System;
using System.Collections;
using System.Diagnostics;
using System.Xml;
using SymbolEngine;
using Blue.Public;
//-----------------------------------------------------------------------------
/// <summary>
/// <c>Identifier</c> is a helper class to associate a string with a <c>FileRange</c>.
/// All AST references to a string-data should refer to an Identifier.
/// </summary>
//-----------------------------------------------------------------------------
public class Identifier
{
public Identifier(string stText)
: this(stText, null)
{ }
public Identifier(string stText, FileRange location)
{
m_stText = stText;
m_filerange = location;
}
protected string m_stText;
protected FileRange m_filerange;
public string Text
{
get { return m_stText; }
}
public FileRange Location
{
get { return m_filerange; }
}
}
//-----------------------------------------------------------------------------
// The Abstract Syntax Tree
//-----------------------------------------------------------------------------
namespace AST
{
#region Node, the base class for all AST nodes
//-----------------------------------------------------------------------------
// Base AST node serves for utility purposes
//-----------------------------------------------------------------------------
/// <summary>
/// <c>Node</c> is the base class for all nodes in the Abstract Syntax tree.
/// </summary>
/// <remarks>
/// <list type="bullet">
/// <item>It is associated with a <c>FileRange</c> object to guarantee that all nodes
/// can provide line number information. </item>
/// <item>It also provides some standard utility functions to serialize a subtree to
/// XML or strings.</item>
/// </list>
/// Nodes are split into the following major categories:
/// Declarations, Members, Statements, Expressions
/// </remarks>
public abstract class Node
{
#region Checks
// Utility to Dump to XML file
public static void DumpTree(Node root, XmlWriter o)
{
o.WriteStartDocument();
o.WriteStartElement("AST");
root.Dump(o);
o.WriteEndElement(); // AST
o.WriteEndDocument();
o.Close();
}
// Dump as xml. XmlWriter must be opended & document must be started
// Will not close document.
public abstract void Dump(XmlWriter o);
// Debugging function only:
// Debugging check to be done _after_ symbol resolution
// That way, we have a symbol table that we can verify everything against.
// Assert everything that we can possibly think of so that CodeGen
// has a well-resolved tree.
// If any of the asserts do fire, that means symbolic resolution is
// making a mistake and should be fixed.
public abstract void DebugCheck(ISemanticResolver s);
// These flags are used during the DebugCheck() to allow features
// that haven't been implemented to pass.
// remove these flags (set to false) as more stuff gets resolved
public readonly static bool m_DbgAllowNoCLRType = false;
// Debugging facility. Dump() spits out XML which can be really tough to read,
// and it's not clear what it should include.
// So we have a facility to spit it out as a string.
static string GetAsSourceString(Node n)
{
//System.Text.StringBuilder sb = new System.Text.StringBuilder();
//n.ToSource(sb);
//return sb.ToString();
return "empty";
}
public static void DumpSourceToStream(Node n, System.IO.TextWriter t)
{
Console.WriteLine("*** Begin Source dump: [");
System.CodeDom.Compiler.IndentedTextWriter i = new
System.CodeDom.Compiler.IndentedTextWriter(t, " ");
n.ToSource(i);
Console.WriteLine("] *** End source dump");
i.Close();
i = null;
}
// Dump the string to the console.
public static void DumpSource(Node n)
{
DumpSourceToStream(n, Console.Out);
}
// This should look like source but inject a few extra characters
// to make it clear how it was resolved.
public virtual void ToSource(System.CodeDom.Compiler.IndentedTextWriter sb)
{
//sb.Append("['" + this.GetType().ToString() + "' not implemented]");
sb.Write("['{0}' not implemented]", this.GetType().ToString());
}
#endregion
#region Linenumber info
// Line information.
protected FileRange m_filerange;
public FileRange Location
{
get { return m_filerange; }
}
// Parser has to be able to set the line number information.
public void SetLocation(FileRange l)
{
m_filerange = l;
}
#endregion Linenumber info
// Shortcut helper functions.
static public void PrintError(SymbolError.SymbolErrorException e)
{
Blue.Driver.StdErrorLog.PrintError(e);
}
static public void ThrowError(SymbolError.SymbolErrorException e)
{
Blue.Driver.StdErrorLog.ThrowError(e);
}
}
#endregion
#region Node for a Compilation-Unit
//-----------------------------------------------------------------------------
// Top level program
//-----------------------------------------------------------------------------
/// <summary>
/// The root AST node for an entire multi-file program.
/// </summary>
/// <remarks>
/// A ProgramDecl node contains all global namespace decls.
/// </remarks>
public class ProgramDecl : Node
{
#region Construction
public ProgramDecl(NamespaceDecl [] nGlobal)
{
Debug.Assert(nGlobal != null);
m_nGlobal = nGlobal;
}
#endregion
#region Checks
public override void Dump(XmlWriter o)
{
o.WriteStartElement("Program");
foreach(NamespaceDecl n in m_nGlobal)
n.Dump(o);
o.WriteEndElement();
}
// Debugging check
public override void DebugCheck(ISemanticResolver s)
{
foreach(NamespaceDecl n in m_nGlobal)
n.DebugCheck(s);
}
#endregion
#region Properties & Data
// Source file has a single implicity global namespace
NamespaceDecl [] m_nGlobal;
public NamespaceDecl [] GlobalNamespaces
{
get { return m_nGlobal; }
}
// List of classes, created by flattening all classes in
// all the namespaces.
TypeDeclBase[] m_arClasses;
public TypeDeclBase[] Classes
{
get {
return m_arClasses;
}
}
#endregion
#region Other Functions
// Get a flat array of classes from the namespaces
// The array is topologically sorted such that:
// if (i < j) then T[i] does not depend on T[j]
protected void CreateClassListFromNamespaces()
{
Debug.Assert(m_arClasses == null);
ArrayList a = new ArrayList();
foreach(NamespaceDecl n in m_nGlobal)
n.ReportClasses(a);
m_arClasses = new TypeDeclBase[a.Count];
for(int i = 0; i < a.Count; i++)
m_arClasses[i] = (TypeDeclBase) a[i];
}
#endregion
#region Resolution
public void ResolveNamespace(ISemanticResolver s, Scope scopeGlobal)
{
// First must do namespaces so that type stubs even
// have a context.
foreach(NamespaceDecl n in m_nGlobal)
n.ResolveNamespace("", s, scopeGlobal);
// Next, resolve type stubs so that using alias at least
// has stub types to refer to
ResolveTypeStubs(s);
// Now resolve Using decls. Have to do this before we
// try and use any types (since using decls will affect resolution)
foreach(NamespaceDecl n in m_nGlobal)
n.ResolveUsingDecls(s);
}
public void NotifyResolutionDone()
{
// Can't create the single class list until after
// all symbols have been resolved
CreateClassListFromNamespaces();
}
// Add stubs for all user types.
public void ResolveTypeStubs(ISemanticResolver s)
{
foreach(NamespaceDecl n in m_nGlobal)
n.ResolveTypesAsBlueStub(s);
}
// Resolve the types
public void ResolveTypes(
ISemanticResolver s,
ICLRtypeProvider provider)
{
// Then go through and resolve them to CLR types.
foreach(NamespaceDecl n in m_nGlobal)
n.ResolveTypesAsCLR(s, provider);
}
// Resolve the member declarations within a class.
// Since these can refer to other classes, we must have
// resolved all types before resolving any members
public void ResolveMemberDecls(
ISemanticResolver s,
ICLRtypeProvider provider
)
{
foreach(NamespaceDecl n in m_nGlobal)
n.ResolveMemberDecls(s, provider);
}
// Resolve the bodies of methods
public void ResolveBodies(ISemanticResolver s)
{
foreach(NamespaceDecl n in m_nGlobal)
n.ResolveBodies(s);
}
#endregion
} // end class Program
#endregion
#region AST Nodes that go in a Namespace Decl
//-----------------------------------------------------------------------------
// Using directive
//-----------------------------------------------------------------------------
public class UsingDirective : Node
{
#region Construction
// For alias
public UsingDirective(string stAlias, Exp eNamespace)
{
Debug.Assert(stAlias != null);
Debug.Assert(eNamespace != null);
m_stAlias = stAlias;
m_eNamespace = eNamespace;
}
// For search path
public UsingDirective(Exp eNamespace)
{
Debug.Assert(eNamespace != null);
m_stAlias = null;
m_eNamespace = eNamespace;
}
#endregion
#region Properties & Data
// We have 2 types of using directives: Alias & Search.
public bool IsAliasType
{
get { return m_stAlias != null; }
}
string m_stAlias;
Exp m_eNamespace;
#endregion
#region Resolution
// Lookup the symbol in this Using Alias/Directive.
// Return symbol if found.
// Return null if not found.
public SymEntry LookupSymbol(string stText)
{
SymEntry sym = null;
if (IsAliasType)
{
// For an alias, if the string name matches, then we already have the symbol
// to return. No search needed.
if (stText == m_stAlias)
{
if (m_eNamespace is NamespaceExp)
{
return ((NamespaceExp) m_eNamespace).Symbol;
}
else if (m_eNamespace is TypeExp)
{
return ((TypeExp) m_eNamespace).Symbol;
}
Debug.Assert(false);
}
}
else
{
// Lookup in associated namespace
Scope scope = ((NamespaceExp) m_eNamespace).Symbol.ChildScope;
sym = scope.LookupSymbolInThisScopeOnly(stText);
// We specifically do not look in nested namespaces
// See section 9.3.2 of the C# spec for details
if (sym is NamespaceEntry)
sym = null;
}
return sym;
}
public void Resolve(ISemanticResolver s, Scope scopeNamespace)
{
// Have to see what our namespace / class resolves to.
//m_eNamespace.ResolveExpAsRight(s);
Exp.ResolveExpAsRight(ref m_eNamespace, s);
if (IsAliasType)
{
// Alias could be to either a class or namespace
//Debug.Assert(m_eNamespace.SymbolMode != ObjExp.Mode.cExpEntry);
Debug.Assert(m_eNamespace is TypeExp || m_eNamespace is NamespaceExp);
}
else
{
// Directives can only give us namespaces
//Debug.Assert(m_eNamespace.SymbolMode == ObjExp.Mode.cNamespaceEntry);
Debug.Assert(m_eNamespace is NamespaceExp);
}
}
#endregion
#region Checks
public override void Dump(XmlWriter o)
{
o.WriteStartElement("Using");
if (m_stAlias != null)
o.WriteAttributeString("alias", m_stAlias);
m_eNamespace.Dump(o);
o.WriteEndElement();
}
// Debugging check
public override void DebugCheck(ISemanticResolver s)
{
}
#endregion
}
//-----------------------------------------------------------------------------
// Declare a user namespace
// Note that a single namespace can be declared across multiple NamespaceDecls.
// Each source file is considered to be in the Global Namespace.
// Each namespace block can have its own set of using directives. (So even
// if 2 blocks refer to the same namespace, they can have different using
// directives)
//-----------------------------------------------------------------------------
public class NamespaceDecl : Node, SymbolEngine.ILookupController
{
#region Construction
// Any of the array parameters may be null
public NamespaceDecl(
Identifier idName, // must be non-null
UsingDirective [] arUsingDirectives,
NamespaceDecl[] arNestedNamespaces,
ClassDecl[] arClasses,
TypeDeclBase[] arTypes
)
{
m_idName = idName;
m_arUsingDirectives = (arUsingDirectives == null) ? new UsingDirective [0] : arUsingDirectives;
m_arNestedNamespaces = (arNestedNamespaces == null) ? new NamespaceDecl[0] : arNestedNamespaces;
//m_arClasses = (arClasses == null) ? new ClassDecl[0] : arClasses;
Debug.Assert(arClasses == null);
m_arTypes = (arTypes == null) ? new TypeDeclBase[0] : arTypes;
}
#endregion
#region Other Functions
// Recursively Create a flat list of classes in a program
public void ReportClasses(ArrayList alClasses)
{
// At each stop, we make sure all of our prerequisite types are added
// before us. So it doesn't matter what order we add in here.
foreach(TypeDeclBase e in m_arTypes)
e.ReportClass(alClasses);
// Recursively add all classes from nested namespaces
foreach(NamespaceDecl n in NestedNamespaces)
n.ReportClasses(alClasses);
}
#endregion
#region Properties & Data
Identifier m_idName;
UsingDirective [] m_arUsingDirectives;
NamespaceDecl[] m_arNestedNamespaces;
//ClassDecl[] m_arClasses;
//EnumDecl[] m_arEnums;
// @todo -merge this with classes
TypeDeclBase [] m_arTypes;
UserNamespaceEntry m_symbol;
public string Name
{
get { return m_idName.Text; }
}
public NamespaceDecl[] NestedNamespaces
{
get { return m_arNestedNamespaces; }
}
Scope m_context;
#endregion
#region Resolution
#region Impl ILookupController
// @dogfood - make these explicit interface methods
// Return null if not found.
public SymEntry SmartLookup(string stIdentifier, Scope scope)
{
// Do a normal lookup of things defined directly in this namespace.
SymEntry sym = scope.LookupSymbolInThisScopeOnly(stIdentifier);
if (sym != null)
return sym;
// Look in the using directives associated with this namespace node.
sym = LookupSymbolInUsingDirectives(stIdentifier);
return sym;
}
// Get a node responsible for this scope.
// For imported types, this will be null
public AST.Node OwnerNode {
get { return this; }
}
// Get a symbol responsible for this scope.
// This may be null. (If this is null, then OwnerNode should not be null).
public SymEntry OwnerSymbol {
get { return this.m_symbol; }
}
public void DumpScope(Scope scope)
{
if (this.m_arUsingDirectives.Length > 0)
{
Console.Write("[has using...]");
}
return;
}
#endregion
#if true
// Check the using directives in this namespace block.
// Give precedence to an alias over a directive.
// Return null if not found.
// Throw error if ambigious (ie, a symbol can be found refered from multiple
// using directives)
// If we haven't resolved all of our own using directives yet, then return null
// (this is to prevent using directives from affecting each other)
public SymEntry LookupSymbolInUsingDirectives(string stName)
{
if (!m_fResolvedUsing)
return null;
// @todo - resolve ambiguity
SymEntry sym = null;
foreach(UsingDirective u in this.m_arUsingDirectives)
{
sym = u.LookupSymbol(stName);
if (sym != null)
return sym;
}
return null;
}
#endif
// Flag to note if our using decls have been resolved yet
bool m_fResolvedUsing = false;
// Before we can process the classes, we need to add all the namespaces
public void ResolveNamespace(
string stParentNamespace,
ISemanticResolver s,
Scope scopeParent)
{
Debug.Assert(m_symbol == null);
// We can have one namespaces spread across multiple blocks (NamespaceDecl).
// All blocks share the same scope (something defined in any block is visible
// to all blocks), but each block can have its own lexical parent & set
// of using clause.
#if true
m_symbol = (UserNamespaceEntry) scopeParent.LookupSymbolInThisScopeOnly(Name);
if (m_symbol == null)
{
// Create new namespace
string stFullName;
if (stParentNamespace == "")
stFullName = this.Name;
else
stFullName = stParentNamespace + "." + this.Name;
m_symbol = new UserNamespaceEntry(this, stFullName);
scopeParent.AddSymbol(m_symbol);
}
// The symbol has the scope with all the data. But each namespace decl creates
// a proxy scope that links to the symbol's data (Because all blocks share that)
// but has a tie to its own set of using clauses & lexical parent.
m_context = m_symbol.ChildScope.CreateSharedScope(this, scopeParent);
foreach(NamespaceDecl n in NestedNamespaces)
n.ResolveNamespace(m_symbol.FullName, s, m_context);
#else
// Since we can have multiple disjoint namespace decls refer
// to the same namespace, we have to check and see if this
// symbol is already created.
m_symbol = (UserNamespaceEntry) s.GetCurrentScope().LookupSymbolInThisScopeOnly(Name);
if (m_symbol == null)
{
string stFullName;
if (stParentNamespace == "")
stFullName = this.Name;
else
stFullName = stParentNamespace + "." + this.Name;
m_symbol = new UserNamespaceEntry(this, stFullName);
s.GetCurrentScope().AddSymbol(m_symbol);
}
EnterNamespace(s);
foreach(NamespaceDecl n in NestedNamespaces)
n.ResolveNamespace(m_symbol.FullName, s, scopeParent);
ExitNamespace(s);
#endif
}
/*
void EnterNamespace(ISemanticResolver s)
{
m_symbol.SetCurrentNode(this);
s.PushScope(m_symbol.ChildScope);
}
void ExitNamespace(ISemanticResolver s)
{
m_symbol.SetCurrentNode(null);
s.PopScope(m_symbol.ChildScope);
}
*/
// Resolve the using declarations
public void ResolveUsingDecls(ISemanticResolver s)
{
// All using declarations are resolved at global scope,
// so don't enter / exit namespaces here
//EnterNamespace(s);
// Resolve all the using clauses in this namespace block
Scope prev = s.SetCurrentContext(this.m_context);
foreach(UsingDirective u in this.m_arUsingDirectives)
u.Resolve(s, m_context);
s.RestoreContext(prev);
// Recursively add all classes from nested namespaces
foreach(NamespaceDecl n in NestedNamespaces)
n.ResolveUsingDecls(s);
//ExitNamespace(s);
m_fResolvedUsing = true;
}
public void ResolveTypesAsBlueStub(
ISemanticResolver s)
{
//EnterNamespace(s);
// add all classes in this namespace
{
foreach(TypeDeclBase e in m_arTypes)
e.ResolveTypesAsBlueStub(s, this.m_symbol.FullName, m_context);
}
// Recursively add all classes from nested namespaces
foreach(NamespaceDecl n in NestedNamespaces)
n.ResolveTypesAsBlueStub(s);
//ExitNamespace(s);
}
public void ResolveTypesAsCLR(
ISemanticResolver s,
ICLRtypeProvider provider
)
{
// We shouldn't have to update the scope stack since
// we should only be looking at resolved symbols
Scope prev = s.SetCurrentContext(m_context);
// add all classes in this namespace
{
foreach(TypeDeclBase e in m_arTypes)
e.ResolveTypesAsCLR(s, provider);
}
s.RestoreContext(prev);
// Recursively add all classes from nested namespaces
foreach(NamespaceDecl n in NestedNamespaces)
n.ResolveTypesAsCLR(s, provider);
}
public void ResolveMemberDecls(
ISemanticResolver s,
ICLRtypeProvider provider
)
{
// Do for all types in this namespace
foreach(TypeDeclBase e in m_arTypes)
e.ResolveMemberDecls(s, provider);
// Recursively do all classes from nested namespaces
foreach(NamespaceDecl n in NestedNamespaces)
n.ResolveMemberDecls(s, provider);
}
// Resolve the bodies of methods
public void ResolveBodies(ISemanticResolver s)
{
foreach(TypeDeclBase e in m_arTypes)
e.ResolveBodies(s);
// Recursively add all classes from nested namespaces
foreach(NamespaceDecl n in NestedNamespaces)
n.ResolveBodies(s);
}
#endregion
#region Checks
// Dump the AST node contents to an xml file
public override void Dump(XmlWriter o)
{
o.WriteStartElement("Namespace");
o.WriteAttributeString("name", Name);
o.WriteStartElement("UsingDirectives");
foreach(UsingDirective n in m_arUsingDirectives)
n.Dump(o);
o.WriteEndElement();
o.WriteStartElement("NestedNamespaces");
foreach(NamespaceDecl n in m_arNestedNamespaces)
n.Dump(o);
o.WriteEndElement();
/*
o.WriteStartElement("Classes");
foreach(ClassDecl n in m_arClasses)
n.Dump(o);
o.WriteEndElement();
*/
o.WriteStartElement("Types");
foreach(TypeDeclBase e in m_arTypes)
e.Dump(o);
o.WriteEndElement();
o.WriteEndElement();
}
// Debugging check
public override void DebugCheck(ISemanticResolver s)
{
//EnterNamespace(s);
Debug.Assert(m_arUsingDirectives != null);
Debug.Assert(m_arNestedNamespaces != null);
//Debug.Assert(m_arClasses != null);
/*
foreach(ClassDecl c in m_arClasses)
{
c.DebugCheck(s);
}
*/
foreach(NamespaceDecl n in this.m_arNestedNamespaces)
{
n.DebugCheck(s);
}
foreach(TypeDeclBase e in m_arTypes)
e.DebugCheck(s);
foreach(UsingDirective u in this.m_arUsingDirectives)
{
u.DebugCheck(s);
}
//ExitNamespace(s);
}
#endregion
}
#region Base class for all type declarations
//-----------------------------------------------------------------------------
// Base for type declarations
//-----------------------------------------------------------------------------
public abstract class TypeDeclBase : Node
{
#region Resolution
// Resolution is the act of changing strings (in a parse tree) into symbols.
// Symbols are associated with the appropriate object in the System.Reflection]
// namespace.
public abstract void ResolveTypesAsBlueStub(
ISemanticResolver s,
string stNamespace, // namespace that a type goes in. Includes nested classes.
Scope scopeParent // our Lexical parent's scope that we should add ourselves too.
);
public abstract void ResolveTypesAsCLR(
ISemanticResolver s,
ICLRtypeProvider provider
);
public abstract void ResolveMemberDecls(
ISemanticResolver s,
ICLRtypeProvider provider
);
public abstract void ResolveBodies(ISemanticResolver s);
public abstract void ReportClass(ArrayList alClasses);
#endregion
#region Generate
// Generate the body for this type
public abstract void GenerateType(Blue.CodeGen.EmitCodeGen gen);
// Get the CLR type that this node represents. Useful for codegen.
public abstract System.Type CLRType
{
get;
}
#endregion
} // end TypeDeclBase
#endregion // Type Decl base
#region Delegate type declaration
//-----------------------------------------------------------------------------
// Delegates
//-----------------------------------------------------------------------------
public class DelegateDecl : TypeDeclBase
{
#region Construction
public DelegateDecl(
Identifier idName,
TypeSig tRetType,
ParamVarDecl[] arParams,
Modifiers mods
)
{
Debug.Assert(idName != null);
Debug.Assert(tRetType != null);
Debug.Assert(arParams != null);
m_idName = idName;
m_tRetType = tRetType;
m_arParams = arParams;
m_mods = mods;
// Implied sealed
m_mods.SetSealed();
}
#endregion Construction
#region Properties & Data
Identifier m_idName;
TypeSig m_tRetType;
ParamVarDecl[] m_arParams;
Modifiers m_mods;
ClassDecl m_nodeProxy;
//TypeEntry m_symbol;
#endregion
#region Resolution
// Helper function
// Given a symbol for a delegate, get the params for it
static public System.Type [] GetParams(TypeEntry t)
{
// We can do this by looking up the invoke function
MethodHeaderEntry header = t.LookupMethodHeader("Invoke");
MethodExpEntry m = header.GetFirstMethod();
Debug.Assert(header.GetNextMethod(m) == null, "Delegate should only have 1 Invoke() method");
// Get parameters off this method
System.Type [] al = m.ParamTypes(false);
return al;
}
// Is this CLR type a delegate?
public static bool IsDelegate(System.Type t)
{
// Determine by base type..
return
(t != null) &&
(t.BaseType == typeof(System.MulticastDelegate));
}
// Delegates are really just blessed Types.
void CreateProxyType(ISemanticResolver s)
{
Debug.Assert(m_nodeProxy == null, "only create proxy once");
// The delegate R F(A) (where R is a return type, and A is a parameter list)
// Can be converted into the type:
// sealed class F : System.MulticastDelegate {
// F(object, native int) { }