From ee3c8c3f57e205b63043e21813fc7660466a7956 Mon Sep 17 00:00:00 2001 From: Alexandros Naskos Date: Thu, 30 Jun 2016 17:45:26 +0300 Subject: [PATCH 1/2] Attempt at generating metaclass objects and initializing their names in their Module's load --- source/rock/backend/cnaughty/CGenerator.ooc | 10 ++-- source/rock/backend/cnaughty/CastWriter.ooc | 2 +- .../rock/backend/cnaughty/ClassDeclWriter.ooc | 52 +++++++------------ source/rock/backend/cnaughty/ModuleWriter.ooc | 16 +++++- 4 files changed, 41 insertions(+), 39 deletions(-) diff --git a/source/rock/backend/cnaughty/CGenerator.ooc b/source/rock/backend/cnaughty/CGenerator.ooc index 19a74329..0b785773 100644 --- a/source/rock/backend/cnaughty/CGenerator.ooc +++ b/source/rock/backend/cnaughty/CGenerator.ooc @@ -96,9 +96,9 @@ CGenerator: class extends Skeleton { match ref { case tDecl: TypeDecl => if (typeAccess inner pointerLevel() > 0) { - current app("lang_types__Pointer_class()") + current app("&lang_types__Pointer_class") } else { - current app(tDecl underName()). app("_class()") + current app("(&"). app(tDecl underName()). app("_class)") } case vDecl: VariableDecl => // TODO: fix namespaced type accesses? @@ -140,7 +140,7 @@ CGenerator: class extends Skeleton { if(isFunc) { fDecl := op left as VariableAccess ref as FunctionDecl - current app(fDecl owner as TypeDecl getFullName()). app("_class()->"). app(fDecl name) + current app(fDecl owner as TypeDecl getFullName()). app("_class."). app(fDecl name) } else { current app(op left) } @@ -240,14 +240,14 @@ CGenerator: class extends Skeleton { while(tDecl instanceOf?(CoverDecl) && tDecl as CoverDecl isAddon()) { tDecl = tDecl as CoverDecl getBase() getNonMeta() } - current app(tDecl getFullName()). app("_class()") + current app("(&"). app(tDecl getFullName()). app("_class)") case fDecl: FunctionDecl => FunctionDeclWriter writeFullName(this, fDecl) case fType: FuncType => // Yes, we need to write function types too ;D - current app("lang_types__Closure_class()") + current app("&lang_types__Closure_class") } } diff --git a/source/rock/backend/cnaughty/CastWriter.ooc b/source/rock/backend/cnaughty/CastWriter.ooc index 304dd21d..cf2d2995 100644 --- a/source/rock/backend/cnaughty/CastWriter.ooc +++ b/source/rock/backend/cnaughty/CastWriter.ooc @@ -27,7 +27,7 @@ CastWriter: abstract class extends Skeleton { } current app("(struct _"). app(iDecl getFatType() getInstanceType()). app(") {"). - app(".impl = "). app(implementor underName()). app("__impl__"). app(iDecl getName()). app("_class(), .obj = (lang_types__Object*) "). + app(".impl = &"). app(implementor underName()). app("__impl__"). app(iDecl getName()). app("_class, .obj = (lang_types__Object*) "). app(cast inner). app('}') } else { diff --git a/source/rock/backend/cnaughty/ClassDeclWriter.ooc b/source/rock/backend/cnaughty/ClassDeclWriter.ooc index 3964b762..8acc9339 100644 --- a/source/rock/backend/cnaughty/ClassDeclWriter.ooc +++ b/source/rock/backend/cnaughty/ClassDeclWriter.ooc @@ -46,16 +46,16 @@ ClassDeclWriter: abstract class extends Skeleton { } else { current = fw if(cDecl getVersion()) VersionWriter writeStart(this, cDecl getVersion()) - writeClassGettingPrototype(this, cDecl) + //writeClassGettingPrototype(this, cDecl) if(cDecl getVersion()) VersionWriter writeEnd(this, cDecl getVersion()) current = cw if(cDecl getVersion()) VersionWriter writeStart(this, cDecl getVersion()) } - // don't write class-getting functions of extern covers - it hurts + // don't write meta classes of extern covers - it hurts if(cDecl getNonMeta() == null || !cDecl getNonMeta() instanceOf?(CoverDecl) || !(cDecl getNonMeta() as CoverDecl isExtern() || cDecl getNonMeta() as CoverDecl isAddon())) { - writeClassGettingFunction(this, cDecl) + writeMetaClass(this, cDecl) } if(cDecl getVersion()) VersionWriter writeEnd(this, cDecl getVersion()) @@ -142,7 +142,7 @@ ClassDeclWriter: abstract class extends Skeleton { /** Write the prototypes of member functions */ writeMemberFuncPrototypes: static func (this: Skeleton, cDecl: ClassDecl) { - writeClassGettingPrototype(this, cDecl) + //writeClassGettingPrototype(this, cDecl) for(fDecl: FunctionDecl in cDecl functions) { @@ -200,7 +200,7 @@ ClassDeclWriter: abstract class extends Skeleton { } for(vDecl in cDecl variables) { if(vDecl getExpr() == null) continue - current nl(). app(cDecl getNonMeta() underName()). app("_class()->"). app(vDecl getName()). app(" = "). app(vDecl getExpr()). app(';') + current nl(). app(cDecl getNonMeta() underName()). app("_class."). app(vDecl getName()). app(" = "). app(vDecl getExpr()). app(';') } current closeBlock() @@ -327,44 +327,31 @@ ClassDeclWriter: abstract class extends Skeleton { } } - writeClassGettingPrototype: static func (this: Skeleton, cDecl: ClassDecl) { - realDecl := getClassType(cDecl) - current nl(). app(realDecl underName()). app(" *"). app(cDecl getNonMeta() getFullName()). app("_class();") - } + writeMetaClass: static func (this: Skeleton, cDecl: ClassDecl) { + //current = hw - writeClassGettingFunction: static func (this: Skeleton, cDecl: ClassDecl) { + if(cDecl getVersion()) VersionWriter writeStart(this, cDecl getVersion()) realDecl := getClassType(cDecl) underName := realDecl underName() - current nl(). nl(). app(underName). app(" *"). app(cDecl getNonMeta() getFullName()). app("_class()"). openBlock(). nl() + // Write forward declaration + current = hw + if(cDecl getVersion()) VersionWriter writeStart(this, cDecl getVersion()) + current nl(). nl(). app(underName). app(" "). app(cDecl getNonMeta() getFullName()). app("_class;") + if(cDecl getVersion()) VersionWriter writeEnd(this, cDecl getVersion()) + current = cw - if (cDecl getNonMeta() getSuperRef()) { - current app("static _Bool __done__ = false;"). nl() - } - current app("static "). app(underName). app(" class = "). nl() + // Write the initialization + current nl(). nl(). app(underName). app(" "). app(cDecl getNonMeta() getFullName()). app("_class = ") writeClassStructInitializers(this, realDecl, cDecl, ArrayList new(), true) current app(';') - if (cDecl getNonMeta() getSuperRef()) { - current nl(). app(This CLASS_NAME). app(" *classPtr = ("). app(This CLASS_NAME). app(" *) &class;") - current nl(). app("if(!__done__)"). openBlock() - match (cDecl getNonMeta()) { - case cd: CoverDecl => - // covers don't have super classes, silly. - current nl(). app("classPtr->super = NULL;") - case => - current nl(). app("classPtr->super = ("). app(This CLASS_NAME). app("*) "). app(cDecl getNonMeta() getSuperRef() getFullName()). app("_class();") - } - current nl(). app("__done__ = true;"). - nl(). app("classPtr->name = ") - writeStringLiteral(realDecl getNonMeta() name) - current app(";"). - closeBlock() - } - current nl(). app("return &class;"). closeBlock() + if(cDecl getVersion()) VersionWriter writeEnd(this, cDecl getVersion()) + + //current = cw } /** @@ -382,6 +369,7 @@ ClassDeclWriter: abstract class extends Skeleton { current app(','). nl(). app(".size = ") realClass getNonMeta() writeSize(current, false) // instance = false + } else { writeClassStructInitializers(this, parentClass getSuperRef() as ClassDecl, realClass, done, false) } diff --git a/source/rock/backend/cnaughty/ModuleWriter.ooc b/source/rock/backend/cnaughty/ModuleWriter.ooc index 57a05ff5..b19c6c26 100644 --- a/source/rock/backend/cnaughty/ModuleWriter.ooc +++ b/source/rock/backend/cnaughty/ModuleWriter.ooc @@ -149,10 +149,13 @@ ModuleWriter: abstract class extends Skeleton { } } - // Then, class load calls + // Then, class load calls and class names. for (type in module types) { if(type instanceOf?(ClassDecl)) { cDecl := type as ClassDecl + + if(cDecl getVersion()) VersionWriter writeStart(this, cDecl getVersion()) + finalScore: Int loadFunc := cDecl getFunction(ClassDecl LOAD_FUNC_NAME, null, null, finalScore&) if(loadFunc) { @@ -160,6 +163,17 @@ ModuleWriter: abstract class extends Skeleton { current nl(). app(loadFunc getFullName()). app("();") if(cDecl getVersion()) VersionWriter writeEnd(this, cDecl getVersion()) } + + if(cDecl getNonMeta() && (!cDecl getNonMeta() instanceOf?(CoverDecl) || !(cDecl getNonMeta() as CoverDecl isExtern() || cDecl getNonMeta() as CoverDecl isAddon()))) { + if (cDecl getNonMeta() getSuperRef()) { + realDecl := ClassDeclWriter getClassType(cDecl) + current nl(). app("(("). app(ClassDeclWriter CLASS_NAME). app(" *) &"). app(cDecl getNonMeta() getFullName()). app("_class"). app(")->name = ") + writeStringLiteral(realDecl getNonMeta() name) + current app(";") + } + } + + if(cDecl getVersion()) VersionWriter writeEnd(this, cDecl getVersion()) } } From 98adbeafc692a1498e64a86520786c82ee9cb180 Mon Sep 17 00:00:00 2001 From: Alexandros Naskos Date: Thu, 30 Jun 2016 17:50:08 +0300 Subject: [PATCH 2/2] A little bit of cleanup --- source/rock/backend/cnaughty/ClassDeclWriter.ooc | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/source/rock/backend/cnaughty/ClassDeclWriter.ooc b/source/rock/backend/cnaughty/ClassDeclWriter.ooc index 8acc9339..b35987d1 100644 --- a/source/rock/backend/cnaughty/ClassDeclWriter.ooc +++ b/source/rock/backend/cnaughty/ClassDeclWriter.ooc @@ -44,12 +44,6 @@ ClassDeclWriter: abstract class extends Skeleton { writeInstanceVirtualFuncs(this, cDecl) writeStaticFuncs(this, cDecl) } else { - current = fw - if(cDecl getVersion()) VersionWriter writeStart(this, cDecl getVersion()) - //writeClassGettingPrototype(this, cDecl) - if(cDecl getVersion()) VersionWriter writeEnd(this, cDecl getVersion()) - - current = cw if(cDecl getVersion()) VersionWriter writeStart(this, cDecl getVersion()) } @@ -142,8 +136,6 @@ ClassDeclWriter: abstract class extends Skeleton { /** Write the prototypes of member functions */ writeMemberFuncPrototypes: static func (this: Skeleton, cDecl: ClassDecl) { - //writeClassGettingPrototype(this, cDecl) - for(fDecl: FunctionDecl in cDecl functions) { if(fDecl isExtern()) { @@ -328,8 +320,6 @@ ClassDeclWriter: abstract class extends Skeleton { } writeMetaClass: static func (this: Skeleton, cDecl: ClassDecl) { - //current = hw - if(cDecl getVersion()) VersionWriter writeStart(this, cDecl getVersion()) realDecl := getClassType(cDecl) @@ -350,8 +340,6 @@ ClassDeclWriter: abstract class extends Skeleton { current app(';') if(cDecl getVersion()) VersionWriter writeEnd(this, cDecl getVersion()) - - //current = cw } /**