diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/CodeGen/ConstructorGen.cc | 10 | ||||
| -rw-r--r-- | lib/CodeGen/ValueTypeGen.cc | 6 |
2 files changed, 13 insertions, 3 deletions
diff --git a/lib/CodeGen/ConstructorGen.cc b/lib/CodeGen/ConstructorGen.cc index 8ca0258..286435e 100644 --- a/lib/CodeGen/ConstructorGen.cc +++ b/lib/CodeGen/ConstructorGen.cc @@ -114,8 +114,16 @@ bool CodeGen::emitConstruct(const std::string &TypeName, Value *Addr, return true; } +// Guide 10.2 creation pattern: a plain DECLARE only runs NEW when the class has +// a parameterless one; otherwise the object stays zeroed (fields constructed) +// and a later `x <- NEW C(...)` binds it. bool CodeGen::emitDefaultConstruct(const std::string &TypeName, Value *Addr, int Line) { - return emitConstruct(TypeName, Addr, {}, Line); + const TypeInfo *Info = resolveType(TypeName); + if (!Info) return false; + for (const FuncSig *Sig : FuncGen->getOverloads(TypeName + ".NEW")) + if (Sig->Params.empty()) return emitConstruct(TypeName, Addr, {}, Line); + SaveAndRestore<int> Location(DiagnosticLine, Line); + return !Info->asRecord() || emitFieldDefaults(Info, Addr, Line); } // Default-constructs, in declaration order, each field of the record at Addr diff --git a/lib/CodeGen/ValueTypeGen.cc b/lib/CodeGen/ValueTypeGen.cc index e73b189..af32d74 100644 --- a/lib/CodeGen/ValueTypeGen.cc +++ b/lib/CodeGen/ValueTypeGen.cc @@ -262,6 +262,8 @@ void CodeGen::emitDeclareStmt(DeclareStmtAST *Stmt) { reportError("Constructor arguments require a class type"); return; } - if (emitConstruct(Info->Name, Alloca, Stmt->getArgs(), Stmt->getLine())) - activateStackObject(Lifetime); + bool Constructed = Stmt->hasParens() + ? emitConstruct(Info->Name, Alloca, Stmt->getArgs(), Stmt->getLine()) + : emitDefaultConstruct(Info->Name, Alloca, Stmt->getLine()); + if (Constructed) activateStackObject(Lifetime); } |
