Skip to content

Commit

Permalink
Merge pull request #82 from magic-lang/rock_1.0.22
Browse files Browse the repository at this point in the history
rock 1.0.22
  • Loading branch information
thomasfanell authored Nov 21, 2016
2 parents b2ec2d9 + 35051cc commit 19876b3
Show file tree
Hide file tree
Showing 36 changed files with 1,459 additions and 604 deletions.
13 changes: 11 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
## 1.0.21 (2016-07-??)
## 1.0.22 (2016-11-28)
Summary:
- Class load function are now only evaluated once
- rock now handles rvalue references when dealing with cover properties
- rock now allows you to extend generic classes by generic type, as opposed to by concrete types.
- Calling super() in a shadowed function now generates an error by default (use `--allow-super-when-shadowing` to disable this error)
- The C generator now outputs `_this` instead of `this`
- rock now auto-generates count() and values() functions for enums (use `--no-auto-generated-enum-functions` to disable)
- Improved the obfuscator
- Added an AST printer, see `rock --help` on how to use it.
- You can now call methods that take arguments on tuples
- rock now generates an unload function for each module and class (reserved for future use, do not use)

## 1.0.21 (2016-08-29)
- Obfuscator hotfix

## 1.0.20 (2016-04-21)

Expand Down
4 changes: 2 additions & 2 deletions source/rock/RockVersion.ooc
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ RockVersion: class {

getMajor: static func -> Int { 1 }
getMinor: static func -> Int { 0 }
getPatch: static func -> Int { 21 }
getPatch: static func -> Int { 22 }
getRevision: static func -> String { "head" }
getCodename: static func -> String { "Det. Freppowicz" }
getCodename: static func -> String { "Mount Frepperest" }

getName: static func -> String { "%d.%d.%d%s codename %s" format(
getMajor(), getMinor(), getPatch(), (getRevision() ? "-" + getRevision() : ""),
Expand Down
6 changes: 6 additions & 0 deletions source/rock/backend/cnaughty/AccessWriter.ooc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ AccessWriter: abstract class extends Skeleton {

writeVariableDeclAccess: static func (this: Skeleton, vDecl: VariableDecl, isMember: Bool,
expr: Expression, token: Token, writeReferenceAddrOf: Bool) {
//
// Quick hack to change the output from 'this' to '_this'.
// This saves us from having to mess around with this in the middle phase.
//
if (vDecl name == "this")
vDecl name = "_this"

if(isMember && !(vDecl isExtern() && vDecl isStatic())) {
casted := false
Expand Down
6 changes: 6 additions & 0 deletions source/rock/backend/cnaughty/CGenerator.ooc
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@ CGenerator: class extends Skeleton {

/** Write a variable declaration */
visitVariableDecl: func (vDecl: VariableDecl) {
//
// Quick hack to change the output from 'this' to '_this'.
// This saves us from having to mess around with this in the middle phase.
//
if (vDecl name == "this")
vDecl name = "_this"
if(vDecl isExtern() && !vDecl isProto()) {
return
}
Expand Down
45 changes: 28 additions & 17 deletions source/rock/backend/cnaughty/ClassDeclWriter.ooc
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,9 @@ ClassDeclWriter: abstract class extends Skeleton {
current app(' '). openBlock(). nl()

if(decl getName() == ClassDecl LOAD_FUNC_NAME) {

// Make sure the load function is only evaluated once.
current app("static _Bool __done__ = false;") . nl() . app("if(!__done__)") . openBlock() . nl() . app("__done__ = true;") . nl()
current app("if(!#{cDecl getLoadedStateVariableName()})") . openBlock() . nl()
current app("#{cDecl getLoadedStateVariableName()} = true;") . nl()

superRef := cDecl getSuperRef()
finalScore: Int
Expand All @@ -202,7 +202,21 @@ ClassDeclWriter: abstract class extends Skeleton {
if(vDecl getExpr() == null) continue
current nl(). app(cDecl getNonMeta() underName()). app("_class()->"). app(vDecl getName()). app(" = "). app(vDecl getExpr()). app(';')
}


current closeBlock()
} else if (decl getName() == ClassDecl UNLOAD_FUNC_NAME) {
current app("if(#{cDecl getLoadedStateVariableName()})") . openBlock() . nl()
current app("#{cDecl getLoadedStateVariableName()} = false;") . nl()
superRef := cDecl getSuperRef()
finalScore: Int
superLoad := superRef getFunction(ClassDecl UNLOAD_FUNC_NAME, null, null, finalScore&)
if(superLoad) {
FunctionDeclWriter writeFullName(this, superLoad)
current app("();")
}
/*for (vDecl in cDecl variables) {
if (vDecl getExpr() == null) continue
}*/
current closeBlock()
}

Expand All @@ -229,9 +243,9 @@ ClassDeclWriter: abstract class extends Skeleton {
current app("return ("). app(fDecl returnType). app(") ")
}
if(cDecl getNonMeta() instanceOf?(InterfaceDecl)) {
current app("this.impl->")
current app("_this.impl->")
} else {
current app("(("). app(baseClass underName()). app(" *)"). app("((types__Object *)this)->class)->")
current app("(("). app(baseClass underName()). app(" *)"). app("((types__Object *)_this)->class)->")
}
FunctionDeclWriter writeSuffixedName(this, fDecl)
FunctionDeclWriter writeFuncArgs(this, fDecl, ArgsWriteModes NAMES_ONLY, baseClass)
Expand Down Expand Up @@ -313,7 +327,7 @@ ClassDeclWriter: abstract class extends Skeleton {
if(!shouldAssign) continue

realOwner := cDecl getVariable(refTypeArg getName()) getOwner()
current nl(). app("(("). app(realOwner getInstanceType()). app(") this)->"). app(refTypeArg getName()). app(" = (void*) "). app(typeArg). app(';')
current nl(). app("(("). app(realOwner getInstanceType()). app(") _this)->"). app(refTypeArg getName()). app(" = (void*) "). app(typeArg). app(';')

j += 1
}
Expand All @@ -327,13 +341,13 @@ ClassDeclWriter: abstract class extends Skeleton {
if(superDefaults) {
current nl()
FunctionDeclWriter writeFullName(this, superDefaults)
current app("_impl(("). app(superDefaults owner getInstanceType()). app(") this);")
current app("_impl(("). app(superDefaults owner getInstanceType()). app(") _this);")
}
}

for(vDecl in meat variables) {
if(vDecl getExpr() == null) continue
current nl(). app("this->"). app(vDecl getName()). app(" = "). app(vDecl getExpr()). app(';')
current nl(). app("_this->"). app(vDecl getName()). app(" = "). app(vDecl getExpr()). app(';')
}
}

Expand All @@ -357,31 +371,28 @@ ClassDeclWriter: abstract class extends Skeleton {

current nl(). nl(). app(underName). app(" *"). app(cDecl getNonMeta() getFullName()). app("_class()"). openBlock(). nl()

if (cDecl getNonMeta() getSuperRef()) {
current app("static _Bool __done__ = false;"). nl()
}
current app("static "). app(underName). app(" class = "). nl()

writeClassStructInitializers(this, realDecl, cDecl, ArrayList<FunctionDecl> new(), true)

current app(';')
current nl(). app("static bool __done__ = false;")
current nl(). app("if(!__done__)"). openBlock()
current nl(). app("__done__ = true;")
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 = ")
current nl(). app("classPtr->name = ")
writeStringLiteral(realDecl getNonMeta() name)
current app(";").
closeBlock()
current app(";")
}

current closeBlock()
current nl(). app("return &class;"). closeBlock()
}

Expand Down
4 changes: 2 additions & 2 deletions source/rock/backend/cnaughty/FunctionDeclWriter.ooc
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ FunctionDeclWriter: abstract class extends Skeleton {
if(baseType != null && !isInterface) {
current app("("). app(baseType getNonMeta() getInstanceType()). app(")")
}
current app("this")
current app("_this")
if(isInterface) current app(".obj")
case ArgsWriteModes TYPES_ONLY =>
if(isInterface) {
Expand All @@ -129,7 +129,7 @@ FunctionDeclWriter: abstract class extends Skeleton {
current app(type)
}
case =>
type write(current, "this")
type write(current, "_this")
}
}

Expand Down
122 changes: 84 additions & 38 deletions source/rock/backend/cnaughty/ModuleWriter.ooc
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import structs/List
import structs/[List, ArrayList]
import ../../middle/[Module, Include, Import, TypeDecl, FunctionDecl,
CoverDecl, ClassDecl, EnumDecl, OperatorDecl, InterfaceDecl,
VariableDecl, Type, FuncType, Argument, StructLiteral]
import ../../frontend/BuildParams
import ../../frontend/drivers/[Driver, SourceFolder]
import CoverDeclWriter, ClassDeclWriter, EnumDeclWriter, VersionWriter, Skeleton

ModuleWriter: abstract class extends Skeleton {
Expand Down Expand Up @@ -81,15 +82,29 @@ ModuleWriter: abstract class extends Skeleton {
inc := imp getModule() getPath(".h")
current nl(). app("#include <"). app(inc). app(">")
}

current nl()


current nl(). app("static volatile bool "). app(module getLoadedStateVariableName()). app(" = false;")

for (type in module getTypes()) {
if (type isMeta && type instanceOf?(ClassDecl)) {
classDecl := type as ClassDecl
if (classDecl lookupFunction(classDecl LOAD_FUNC_NAME)) {
if(classDecl getVersion()) VersionWriter writeStart(this, classDecl getVersion())
current nl(). app("static volatile bool #{classDecl getLoadedStateVariableName()} = false;")
if(classDecl getVersion()) VersionWriter writeEnd(this, classDecl getVersion())
}
}
}

// write the .c part of all global variables
for(stmt in module body) {
if(stmt instanceOf?(VariableDecl) && !stmt as VariableDecl getType() instanceOf?(AnonymousStructType)) {
vd := stmt as VariableDecl
// TODO: add 'local'
if(vd isExtern() && !vd isProto()) continue

current = cw
current nl()
if(vd isStatic()) current app("static ")
Expand All @@ -107,14 +122,14 @@ ModuleWriter: abstract class extends Skeleton {
if(!tDecl isMeta) continue
tDecl accept(this)
}

// write the .h part of all global variables
for(stmt in module body) {
if(stmt instanceOf?(VariableDecl) && !stmt as VariableDecl getType() instanceOf?(AnonymousStructType)) {
vd := stmt as VariableDecl
// TODO: add 'local'
if(vd isExtern() && !vd isProto()) continue

if(!vd isStatic()) {
current = fw
current nl(). app("extern ")
Expand All @@ -125,20 +140,53 @@ ModuleWriter: abstract class extends Skeleton {
}

// write load function
This writeLoadFunction(this, module)
This writeUnloadFunction(this, module)

// write all addons
for(addon in module addons) {
addon accept(this)
}

// write all functions
for(fDecl in module functions) {
fDecl accept(this)
}

// write all operator overloads
for(oDecl in module operators) {
oDecl accept(this)
}

// header end
current = hw
current nl(). nl(). app("#endif // "). app(hName)

// forward-header end
current = fw
current nl(). nl(). app("#endif // "). app(hFwdName)

// Write a default main if none provided in source
if(module main && !module functions contains?("main")) {
writeDefaultMain(this)
}

}

writeLoadFunction: static func (this: Skeleton, module: Module) {
current = fw
current nl(). app("void "). app(module getLoadFuncName()). app("();")
current = cw
current nl(). app("void "). app(module getLoadFuncName()). app("() {"). tab()
current nl(). app("static "). app("bool __done__ = false;"). nl(). app("if (!__done__)"). app("{"). tab()
current nl(). app("__done__ = true;")
current nl(). app ("if(!#{module getLoadedStateVariableName()}) {") . tab()
current nl(). app ("#{module getLoadedStateVariableName()} = true;")
for (imp in module getAllImports()) {
current nl(). app(imp getModule() getLoadFuncName()). app("();")
}

writeVDecl := func (decl: VariableDecl) {
current nl() . app(decl getFullName()) . app(" = ") . app(decl getExpr()) . app(';')
}

// We write generated variable declarations first, as they may be required by class static initializers.
for (stmt in module body) {
match stmt {
Expand All @@ -148,7 +196,6 @@ ModuleWriter: abstract class extends Skeleton {
}
}
}

// Then, class load calls
for (type in module types) {
if(type instanceOf?(ClassDecl)) {
Expand All @@ -162,7 +209,6 @@ ModuleWriter: abstract class extends Skeleton {
}
}
}

// Finally, the rest of the variable declarations and statements
for (stmt in module body) {
if (stmt instanceOf?(VariableDecl) && !stmt as VariableDecl getType() instanceOf?(AnonymousStructType)) {
Expand All @@ -176,35 +222,35 @@ ModuleWriter: abstract class extends Skeleton {
}
current untab(). nl(). app("}")
current untab(). nl(). app("}"). nl()
}

// write all addons
for(addon in module addons) {
addon accept(this)
}

// write all functions
for(fDecl in module functions) {
fDecl accept(this)
}

// write all operator overloads
for(oDecl in module operators) {
oDecl accept(this)
}

// header end
current = hw
current nl(). nl(). app("#endif // "). app(hName)

// forward-header end
writeUnloadFunction: static func (this: Skeleton, module: Module) {
current = fw
current nl(). nl(). app("#endif // "). app(hFwdName)

// Write a default main if none provided in source
if(module main && !module functions contains?("main")) {
writeDefaultMain(this)
current nl(). app("void "). app(module getUnloadFuncName()). app("();")
current = cw
current nl(). app("void "). app(module getUnloadFuncName()). app("() {"). tab()
current nl(). app("if(#{module getLoadedStateVariableName()}) {"). tab()
current nl(). app("#{module getLoadedStateVariableName()} = false;")
for (imp in module getAllImports()) {
if (imp getModule() simpleName == "Memory") {
continue;
}
current nl(). app(imp getModule() getUnloadFuncName()). app("();")
}

for (type in module types) {
if(type instanceOf?(ClassDecl)) {
cDecl := type as ClassDecl
finalScore: Int
unloadFunc := cDecl getFunction(ClassDecl UNLOAD_FUNC_NAME, null, null, finalScore&)
if(unloadFunc) {
if(cDecl getVersion()) VersionWriter writeStart(this, cDecl getVersion())
current nl(). app(unloadFunc getFullName()). app("();")
if(cDecl getVersion()) VersionWriter writeEnd(this, cDecl getVersion())
}
}
}
current untab(). nl(). app("}")
current untab(). nl(). app("}"). nl()
}

/** Write default main function */
Expand Down Expand Up @@ -323,7 +369,7 @@ ModuleWriter: abstract class extends Skeleton {
match (inc mode) {
case IncludeMode MACRO =>
// muffin to do.
case =>
case =>
current app("<")
}

Expand All @@ -336,7 +382,7 @@ ModuleWriter: abstract class extends Skeleton {
match (inc mode) {
case IncludeMode MACRO =>
// muffin to do
case =>
case =>
current app(".h>")
}

Expand Down
Loading

0 comments on commit 19876b3

Please sign in to comment.