Skip to content

Commit

Permalink
More specialization (who knew! )
Browse files Browse the repository at this point in the history
  • Loading branch information
Amos Wenger committed May 30, 2012
1 parent f643795 commit d7e95e0
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
1 change: 0 additions & 1 deletion source/rock/frontend/AstBuilder.ooc
Expand Up @@ -180,7 +180,6 @@ AstBuilder: class {
}

onSpecialize: unmangled(nq_onSpecialize) func (type: Type) {
"Type to be specialized: %s" printfln(type toString())
module typesToSpecialize add(type)
}

Expand Down
32 changes: 31 additions & 1 deletion source/rock/middle/ClassDecl.ooc
@@ -1,4 +1,4 @@
import structs/ArrayList
import structs/[ArrayList, HashMap]
import ../io/TabbedWriter

import ../frontend/Token
Expand All @@ -13,6 +13,9 @@ ClassDecl: class extends TypeDecl {
LOAD_FUNC_NAME := static const "__load__"
DEFAULTS_FUNC_NAME := static const "__defaults__"

specializations := HashMap<Type, ClassDecl> new()
typeArgMappings := HashMap<String, Type> new()

isAbstract := false
isFinal := false

Expand Down Expand Up @@ -45,6 +48,33 @@ ClassDecl: class extends TypeDecl {
false
}

clone: func -> This {
copy := This new(name, superType, isMeta, token)
typeArgs each(|ta| copy addTypeArg(ta clone()))
// TODO: missing things here probably.
copy
}

specialize: func (tts: Type) {
ta := tts getTypeArgs()

if (ta size != typeArgs size) {
Exception new("Wrong specialization (typeargs don't match)") throw()
}

copy := clone()
specializations put(tts, copy)

for (i in 0..typeArgs size) {
lhs := typeArgs get(i)
rhs := ta get(i)
"-- Mappings --" println()
"%s => %s" printfln(lhs getName(), rhs toString())
// Oh, this is unsafe..
copy typeArgMappings put(lhs getName(), rhs getRef() as TypeDecl getType())
}
}

resolve: func (trail: Trail, res: Resolver) -> Response {

if(shouldCheckNoArgConstructor) {
Expand Down
23 changes: 22 additions & 1 deletion source/rock/middle/Module.ooc
Expand Up @@ -2,7 +2,7 @@ import io/File, text/EscapeSequence
import structs/[HashMap, ArrayList, List, OrderedMultiMap]
import ../frontend/[Token, BuildParams, PathList, AstBuilder]
import Node, FunctionDecl, Visitor, Import, Include, Use, UseDef, TypeDecl,
FunctionCall, Type, Declaration, VariableAccess, OperatorDecl,
ClassDecl, FunctionCall, Type, Declaration, VariableAccess, OperatorDecl,
Scope, NamespaceDecl, BaseType, FuncType, Addon
import tinker/[Response, Resolver, Trail, Errors]

Expand Down Expand Up @@ -385,6 +385,27 @@ Module: class extends Node {

trail push(this)

{
for (tts in typesToSpecialize) if (tts getRef() == null) {
response := tts resolve(trail, res)
if(!response ok()) {
finalResponse = response
} else {
match (tts getRef()) {
case cd: ClassDecl =>
"Type to specialize refers to %s" printfln(tts getRef() toString())
cd specialize(tts)
case =>
Exception new("Trying to specialize a non-object type! %s") throw()
}
}
}

if (!finalResponse ok()) {
return finalResponse;
}
}

{
response := body resolve(trail, res)
if(!response ok()) {
Expand Down

0 comments on commit d7e95e0

Please sign in to comment.