comparison dmd/CtorDeclaration.d @ 0:10317f0c89a5

Initial commit
author korDen
date Sat, 24 Oct 2009 08:42:06 +0400
parents
children 0aa7d1437ada
comparison
equal deleted inserted replaced
-1:000000000000 0:10317f0c89a5
1 module dmd.CtorDeclaration;
2
3 import dmd.FuncDeclaration;
4 import dmd.ArrayTypes;
5 import dmd.Loc;
6 import dmd.Dsymbol;
7 import dmd.Scope;
8 import dmd.OutBuffer;
9 import dmd.HdrGenState;
10 import dmd.STC;
11 import dmd.AggregateDeclaration;
12 import dmd.TypeFunction;
13 import dmd.Type;
14 import dmd.Global;
15 import dmd.LINK;
16 import dmd.Expression;
17 import dmd.ThisExp;
18 import dmd.Statement;
19 import dmd.ReturnStatement;
20 import dmd.CompoundStatement;
21 import dmd.Argument;
22 import dmd.Id;
23
24 class CtorDeclaration : FuncDeclaration
25 {
26 Arguments arguments;
27 int varargs;
28
29 this(Loc loc, Loc endloc, Arguments arguments, int varargs)
30 {
31 super(loc, endloc, Id.ctor, STC.STCundefined, null);
32
33 this.arguments = arguments;
34 this.varargs = varargs;
35 //printf("CtorDeclaration(loc = %s) %s\n", loc.toChars(), toChars());
36 }
37
38 Dsymbol syntaxCopy(Dsymbol)
39 {
40 assert(false);
41 }
42
43 void semantic(Scope sc)
44 {
45 AggregateDeclaration ad;
46 Type tret;
47
48 //printf("CtorDeclaration.semantic() %s\n", toChars());
49 if (type)
50 return;
51
52 sc = sc.push();
53 sc.stc &= ~STCstatic; // not a static constructor
54
55 parent = sc.parent;
56 Dsymbol parent = toParent2();
57 ad = parent.isAggregateDeclaration();
58 if (!ad || parent.isUnionDeclaration())
59 {
60 error("constructors are only for class or struct definitions");
61 tret = Type.tvoid;
62 }
63 else
64 {
65 tret = ad.handle;
66 assert(tret);
67 }
68 type = new TypeFunction(arguments, tret, varargs, LINKd);
69
70 version (STRUCTTHISREF) {
71 if (ad && ad.isStructDeclaration())
72 (cast(TypeFunction)type).isref = true;
73 }
74 if (!originalType)
75 originalType = type;
76
77 sc.flags |= SCOPE.SCOPEctor;
78 type = type.semantic(loc, sc);
79 sc.flags &= ~SCOPE.SCOPEctor;
80
81 // Append:
82 // return this;
83 // to the function body
84 if (fbody)
85 {
86 Expression e = new ThisExp(loc);
87 Statement s = new ReturnStatement(loc, e);
88 fbody = new CompoundStatement(loc, fbody, s);
89 }
90
91 FuncDeclaration.semantic(sc);
92
93 sc.pop();
94
95 // See if it's the default constructor
96 if (ad && varargs == 0 && Argument.dim(arguments) == 0)
97 { if (ad.isStructDeclaration())
98 error("default constructor not allowed for structs");
99 else
100 ad.defaultCtor = this;
101 }
102 }
103
104 void toCBuffer(OutBuffer buf, HdrGenState* hgs)
105 {
106 assert(false);
107 }
108
109 string kind()
110 {
111 return "constructor";
112 }
113
114 string toChars()
115 {
116 return "this";
117 }
118
119 bool isVirtual()
120 {
121 return false;
122 }
123
124 bool addPreInvariant()
125 {
126 return false;
127 }
128
129 bool addPostInvariant()
130 {
131 return (isThis() && vthis && global.params.useInvariants);
132 }
133
134 void toDocBuffer(OutBuffer buf)
135 {
136 assert(false);
137 }
138
139 CtorDeclaration isCtorDeclaration() { return this; }
140 }