Mercurial > projects > ddmd
annotate dmd/CtorDeclaration.d @ 178:e3afd1303184
Many small bugs fixed
Made all classes derive from TObject to detect memory leaks (functionality is disabled for now)
Began work on overriding backend memory allocations (to avoid memory leaks)
author | korDen |
---|---|
date | Sun, 17 Oct 2010 07:42:00 +0400 |
parents | 60bb0fe4563e |
children | cd48cb899aee |
rev | line source |
---|---|
0 | 1 module dmd.CtorDeclaration; |
2 | |
114 | 3 import dmd.common; |
0 | 4 import dmd.FuncDeclaration; |
5 import dmd.ArrayTypes; | |
6 import dmd.Loc; | |
7 import dmd.Dsymbol; | |
8 import dmd.Scope; | |
9 import dmd.OutBuffer; | |
10 import dmd.HdrGenState; | |
11 import dmd.STC; | |
12 import dmd.AggregateDeclaration; | |
13 import dmd.TypeFunction; | |
14 import dmd.Type; | |
15 import dmd.Global; | |
16 import dmd.LINK; | |
17 import dmd.Expression; | |
18 import dmd.ThisExp; | |
19 import dmd.Statement; | |
20 import dmd.ReturnStatement; | |
21 import dmd.CompoundStatement; | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
22 import dmd.Parameter; |
0 | 23 import dmd.Id; |
24 | |
25 class CtorDeclaration : FuncDeclaration | |
26 { | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
27 Parameters arguments; |
0 | 28 int varargs; |
29 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
30 this(Loc loc, Loc endloc, Parameters arguments, int varargs) |
0 | 31 { |
178 | 32 register(); |
0 | 33 super(loc, endloc, Id.ctor, STC.STCundefined, null); |
34 | |
35 this.arguments = arguments; | |
36 this.varargs = varargs; | |
37 //printf("CtorDeclaration(loc = %s) %s\n", loc.toChars(), toChars()); | |
38 } | |
39 | |
72 | 40 override Dsymbol syntaxCopy(Dsymbol) |
0 | 41 { |
49 | 42 CtorDeclaration f = new CtorDeclaration(loc, endloc, null, varargs); |
43 | |
44 f.outId = outId; | |
45 f.frequire = frequire ? frequire.syntaxCopy() : null; | |
46 f.fensure = fensure ? fensure.syntaxCopy() : null; | |
47 f.fbody = fbody ? fbody.syntaxCopy() : null; | |
48 assert(!fthrows); // deprecated | |
49 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
50 f.arguments = Parameter.arraySyntaxCopy(arguments); |
49 | 51 return f; |
0 | 52 } |
53 | |
72 | 54 override void semantic(Scope sc) |
0 | 55 { |
56 AggregateDeclaration ad; | |
57 Type tret; | |
58 | |
59 //printf("CtorDeclaration.semantic() %s\n", toChars()); | |
60 if (type) | |
61 return; | |
62 | |
63 sc = sc.push(); | |
64 sc.stc &= ~STCstatic; // not a static constructor | |
65 | |
66 parent = sc.parent; | |
67 Dsymbol parent = toParent2(); | |
68 ad = parent.isAggregateDeclaration(); | |
69 if (!ad || parent.isUnionDeclaration()) | |
70 { | |
71 error("constructors are only for class or struct definitions"); | |
72 tret = Type.tvoid; | |
73 } | |
74 else | |
75 { | |
76 tret = ad.handle; | |
77 assert(tret); | |
78 } | |
79 type = new TypeFunction(arguments, tret, varargs, LINKd); | |
80 | |
81 version (STRUCTTHISREF) { | |
82 if (ad && ad.isStructDeclaration()) | |
83 (cast(TypeFunction)type).isref = true; | |
84 } | |
85 if (!originalType) | |
86 originalType = type; | |
87 | |
88 sc.flags |= SCOPE.SCOPEctor; | |
89 type = type.semantic(loc, sc); | |
90 sc.flags &= ~SCOPE.SCOPEctor; | |
91 | |
92 // Append: | |
93 // return this; | |
94 // to the function body | |
95 if (fbody) | |
96 { | |
97 Expression e = new ThisExp(loc); | |
98 Statement s = new ReturnStatement(loc, e); | |
99 fbody = new CompoundStatement(loc, fbody, s); | |
100 } | |
101 | |
102 FuncDeclaration.semantic(sc); | |
103 | |
104 sc.pop(); | |
105 | |
106 // See if it's the default constructor | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
107 if (ad && varargs == 0 && Parameter.dim(arguments) == 0) |
0 | 108 { if (ad.isStructDeclaration()) |
109 error("default constructor not allowed for structs"); | |
110 else | |
111 ad.defaultCtor = this; | |
112 } | |
113 } | |
114 | |
72 | 115 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) |
0 | 116 { |
117 assert(false); | |
118 } | |
119 | |
72 | 120 override string kind() |
0 | 121 { |
122 return "constructor"; | |
123 } | |
124 | |
72 | 125 override string toChars() |
0 | 126 { |
127 return "this"; | |
128 } | |
129 | |
72 | 130 override bool isVirtual() |
0 | 131 { |
132 return false; | |
133 } | |
134 | |
72 | 135 override bool addPreInvariant() |
0 | 136 { |
137 return false; | |
138 } | |
139 | |
72 | 140 override bool addPostInvariant() |
0 | 141 { |
142 return (isThis() && vthis && global.params.useInvariants); | |
143 } | |
144 | |
72 | 145 override void toDocBuffer(OutBuffer buf) |
0 | 146 { |
147 assert(false); | |
148 } | |
149 | |
72 | 150 override CtorDeclaration isCtorDeclaration() { return this; } |
151 } |