Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate metaclass objects and initialize their names in the modules they were defined in #1004

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions source/rock/backend/cnaughty/CGenerator.ooc
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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")
}
}

Expand Down
2 changes: 1 addition & 1 deletion source/rock/backend/cnaughty/CastWriter.ooc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
54 changes: 15 additions & 39 deletions source/rock/backend/cnaughty/ClassDeclWriter.ooc
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,12 @@ 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())
}

// 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())
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -200,7 +192,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()
Expand Down Expand Up @@ -327,44 +319,27 @@ 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();")
}

writeClassGettingFunction: static func (this: Skeleton, cDecl: ClassDecl) {
writeMetaClass: 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<FunctionDecl> 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())
}

/**
Expand All @@ -382,6 +357,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)
}
Expand Down
16 changes: 15 additions & 1 deletion source/rock/backend/cnaughty/ModuleWriter.ooc
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,31 @@ 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) {
if(cDecl getVersion()) VersionWriter writeStart(this, cDecl getVersion())
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())
}
}

Expand Down