slcom/slangc/model/TemplateModel.sauce

63 lines
1.8 KiB
Plaintext

package slangc.model;
import slang.data.List;
import slangc.parser.Branch;
import slangc.parser.Node;
import slangc.parser.NodeType;
public class TemplateModel {
TemplateModel original = null;
TypeModel typeDefinition;
private List<TemplateArgumentModel> arguments = new List<TemplateArgumentModel>();
public TemplateModel(TypeModel templatedNode, Node templateArguments) {
this.typeDefinition = templatedNode;
Branch b = (Branch)((Branch) templateArguments).getSubnode(1);
for (int i = 0; i < b.countSubnodes(); i++) {
//System.out.println("GOT A " + b.getSubnode(i).getNodeType());
if (b.getSubnode(i).getNodeType() != NodeType.COMMA) {
arguments.append(new TemplateArgumentModel(this, i, b.getSubnode(i)));
}
}
}
private TemplateModel(TypeModel newOwner, TemplateModel original, TemplateArguments targs) {
this.original = original;
this.typeDefinition = newOwner;
for (int i = 0; i < targs.countArguments(); i++) {
arguments.append(original.arguments.get(i).specialised(this, targs.getArgument(i)));
}
}
public TemplateModel getOriginal() {
return original;
}
public TemplateModel specialised(TypeModel newOwner, TemplateArguments targs) {
if (targs.countArguments() != arguments.count()) {
throw new Error("Internal error/TODO: Template instantiated with wrong number of sizeExpression");
}
return new TemplateModel(newOwner, this, targs);
}
public TypeModel getTypeDefinition() {
return typeDefinition;
}
public int countArguments() {
return arguments.count();
}
public TemplateArgumentModel getArgument(int i) {
return arguments.get(i);
}
public int expand() {
int total = 0;
for (int i = 0; i < countArguments(); i++) {
total += getArgument(i).expand();
}
return total;
}
}