Mercurial > projects > ddmd
diff dmd/CtorDeclaration.d @ 0:10317f0c89a5
Initial commit
author | korDen |
---|---|
date | Sat, 24 Oct 2009 08:42:06 +0400 |
parents | |
children | 0aa7d1437ada |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dmd/CtorDeclaration.d Sat Oct 24 08:42:06 2009 +0400 @@ -0,0 +1,140 @@ +module dmd.CtorDeclaration; + +import dmd.FuncDeclaration; +import dmd.ArrayTypes; +import dmd.Loc; +import dmd.Dsymbol; +import dmd.Scope; +import dmd.OutBuffer; +import dmd.HdrGenState; +import dmd.STC; +import dmd.AggregateDeclaration; +import dmd.TypeFunction; +import dmd.Type; +import dmd.Global; +import dmd.LINK; +import dmd.Expression; +import dmd.ThisExp; +import dmd.Statement; +import dmd.ReturnStatement; +import dmd.CompoundStatement; +import dmd.Argument; +import dmd.Id; + +class CtorDeclaration : FuncDeclaration +{ + Arguments arguments; + int varargs; + + this(Loc loc, Loc endloc, Arguments arguments, int varargs) + { + super(loc, endloc, Id.ctor, STC.STCundefined, null); + + this.arguments = arguments; + this.varargs = varargs; + //printf("CtorDeclaration(loc = %s) %s\n", loc.toChars(), toChars()); + } + + Dsymbol syntaxCopy(Dsymbol) + { + assert(false); + } + + void semantic(Scope sc) + { + AggregateDeclaration ad; + Type tret; + + //printf("CtorDeclaration.semantic() %s\n", toChars()); + if (type) + return; + + sc = sc.push(); + sc.stc &= ~STCstatic; // not a static constructor + + parent = sc.parent; + Dsymbol parent = toParent2(); + ad = parent.isAggregateDeclaration(); + if (!ad || parent.isUnionDeclaration()) + { + error("constructors are only for class or struct definitions"); + tret = Type.tvoid; + } + else + { + tret = ad.handle; + assert(tret); + } + type = new TypeFunction(arguments, tret, varargs, LINKd); + +version (STRUCTTHISREF) { + if (ad && ad.isStructDeclaration()) + (cast(TypeFunction)type).isref = true; +} + if (!originalType) + originalType = type; + + sc.flags |= SCOPE.SCOPEctor; + type = type.semantic(loc, sc); + sc.flags &= ~SCOPE.SCOPEctor; + + // Append: + // return this; + // to the function body + if (fbody) + { + Expression e = new ThisExp(loc); + Statement s = new ReturnStatement(loc, e); + fbody = new CompoundStatement(loc, fbody, s); + } + + FuncDeclaration.semantic(sc); + + sc.pop(); + + // See if it's the default constructor + if (ad && varargs == 0 && Argument.dim(arguments) == 0) + { if (ad.isStructDeclaration()) + error("default constructor not allowed for structs"); + else + ad.defaultCtor = this; + } + } + + void toCBuffer(OutBuffer buf, HdrGenState* hgs) + { + assert(false); + } + + string kind() + { + return "constructor"; + } + + string toChars() + { + return "this"; + } + + bool isVirtual() + { + return false; + } + + bool addPreInvariant() + { + return false; + } + + bool addPostInvariant() + { + return (isThis() && vthis && global.params.useInvariants); + } + + void toDocBuffer(OutBuffer buf) + { + assert(false); + } + + CtorDeclaration isCtorDeclaration() { return this; } +} \ No newline at end of file