Mercurial > projects > ddmd
annotate dmd/CtorDeclaration.d @ 135:af1bebfd96a4 dmd2037
dmd 2.038
author | Eldar Insafutdinov <e.insafutdinov@gmail.com> |
---|---|
date | Mon, 13 Sep 2010 22:19:42 +0100 |
parents | 60bb0fe4563e |
children | e3afd1303184 |
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 { |
32 super(loc, endloc, Id.ctor, STC.STCundefined, null); | |
33 | |
34 this.arguments = arguments; | |
35 this.varargs = varargs; | |
36 //printf("CtorDeclaration(loc = %s) %s\n", loc.toChars(), toChars()); | |
37 } | |
38 | |
72 | 39 override Dsymbol syntaxCopy(Dsymbol) |
0 | 40 { |
49 | 41 CtorDeclaration f = new CtorDeclaration(loc, endloc, null, varargs); |
42 | |
43 f.outId = outId; | |
44 f.frequire = frequire ? frequire.syntaxCopy() : null; | |
45 f.fensure = fensure ? fensure.syntaxCopy() : null; | |
46 f.fbody = fbody ? fbody.syntaxCopy() : null; | |
47 assert(!fthrows); // deprecated | |
48 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
49 f.arguments = Parameter.arraySyntaxCopy(arguments); |
49 | 50 return f; |
0 | 51 } |
52 | |
72 | 53 override void semantic(Scope sc) |
0 | 54 { |
55 AggregateDeclaration ad; | |
56 Type tret; | |
57 | |
58 //printf("CtorDeclaration.semantic() %s\n", toChars()); | |
59 if (type) | |
60 return; | |
61 | |
62 sc = sc.push(); | |
63 sc.stc &= ~STCstatic; // not a static constructor | |
64 | |
65 parent = sc.parent; | |
66 Dsymbol parent = toParent2(); | |
67 ad = parent.isAggregateDeclaration(); | |
68 if (!ad || parent.isUnionDeclaration()) | |
69 { | |
70 error("constructors are only for class or struct definitions"); | |
71 tret = Type.tvoid; | |
72 } | |
73 else | |
74 { | |
75 tret = ad.handle; | |
76 assert(tret); | |
77 } | |
78 type = new TypeFunction(arguments, tret, varargs, LINKd); | |
79 | |
80 version (STRUCTTHISREF) { | |
81 if (ad && ad.isStructDeclaration()) | |
82 (cast(TypeFunction)type).isref = true; | |
83 } | |
84 if (!originalType) | |
85 originalType = type; | |
86 | |
87 sc.flags |= SCOPE.SCOPEctor; | |
88 type = type.semantic(loc, sc); | |
89 sc.flags &= ~SCOPE.SCOPEctor; | |
90 | |
91 // Append: | |
92 // return this; | |
93 // to the function body | |
94 if (fbody) | |
95 { | |
96 Expression e = new ThisExp(loc); | |
97 Statement s = new ReturnStatement(loc, e); | |
98 fbody = new CompoundStatement(loc, fbody, s); | |
99 } | |
100 | |
101 FuncDeclaration.semantic(sc); | |
102 | |
103 sc.pop(); | |
104 | |
105 // 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
|
106 if (ad && varargs == 0 && Parameter.dim(arguments) == 0) |
0 | 107 { if (ad.isStructDeclaration()) |
108 error("default constructor not allowed for structs"); | |
109 else | |
110 ad.defaultCtor = this; | |
111 } | |
112 } | |
113 | |
72 | 114 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) |
0 | 115 { |
116 assert(false); | |
117 } | |
118 | |
72 | 119 override string kind() |
0 | 120 { |
121 return "constructor"; | |
122 } | |
123 | |
72 | 124 override string toChars() |
0 | 125 { |
126 return "this"; | |
127 } | |
128 | |
72 | 129 override bool isVirtual() |
0 | 130 { |
131 return false; | |
132 } | |
133 | |
72 | 134 override bool addPreInvariant() |
0 | 135 { |
136 return false; | |
137 } | |
138 | |
72 | 139 override bool addPostInvariant() |
0 | 140 { |
141 return (isThis() && vthis && global.params.useInvariants); | |
142 } | |
143 | |
72 | 144 override void toDocBuffer(OutBuffer buf) |
0 | 145 { |
146 assert(false); | |
147 } | |
148 | |
72 | 149 override CtorDeclaration isCtorDeclaration() { return this; } |
150 } |