Commit e696498a authored by Noric Couderc's avatar Noric Couderc
Browse files

Merge branch 'master' of git.cs.lth.se:al7330du/attol

parents f95b6bb1 3d49106d
......@@ -36,10 +36,6 @@ aspect IRGenType {
return retval;
}
@Override public IRTypeRef TypeVarDecl.genIRTypeRef() {
return new IRTypeFormalRef(genIR());
}
public IRTypeRef Type.genIR() {
Decl d = getIdUse().lookup().getDecl();
if (d instanceof TypeVarDecl) {
......
......@@ -37,6 +37,15 @@ aspect BuiltinTypes {
return equals(program().ARRAY());
}
syn boolean Type.isArray() {
Decl decl = getIdUse().lookup().getDecl();
if (decl == null)
return false;
if (!decl.isTypeDecl())
return false;
return decl.asTypeDecl().isArray();
}
// FIXME: general lookupDecl() and more convenient interface for that?
syn TypeDecl Type.baseType() {
return (TypeDecl) getIdUse().lookup().getDecl();
......
......@@ -27,4 +27,13 @@ aspect SemanticCheck {
VarDecl contributes new SemanticError(sourceLocation(), "Qualified local variabes must be initialized.")
when (!hasInitializer()) && (!isFormalParameterDecl()) &&
hasDeclaredType() && getDeclaredType().hasNonNullQualifier() && getScope() == VariableScope.LOCAL to Program.semanticErrors();
Type contributes new SemanticError(sourceLocation(), "Actual type parameters are disallowed for types other than array[T].")
when !isArray() && getNumActual() != 0 to Program.semanticErrors();
Type contributes new SemanticError(sourceLocation(), "array[T] should must have a single type parameter.")
when isArray() && getNumActual() != 1 to Program.semanticErrors();
TypeDecl contributes new SemanticError(sourceLocation(), "User defined type cannot have formal type parameters.")
when !isArray() && getNumTypeFormal() != 0 to Program.semanticErrors();
}
......@@ -76,5 +76,14 @@ aspect TypeAnalysis {
return program().ANY();
}
eq MemberAccess.type() {
Expr base = getExpr();
IdDecl varId = base.type().memberLookup(getIdentifier());
if (varId.isUnknown())
return program().ANY();
Decl d = varId.getDecl();
if (d.isVarDecl())
return d.asVarDecl().variableType();
return program().ANY();
}
}
......@@ -50,6 +50,7 @@ module("teal-3") {
include "parser/decl.parser"
include "parser/decl-base.parser"
include "parser/decl-base-teal2-compat.parser"
excludeFrom "teal-0", "parser/decl.parser"
excludeFrom "teal-0", "parser/type.parser"
......
// Backwards compatibility with TEAL-2, by desugaring
// type name[T1, ...] (x1 : t1, ...) ;
// to
// class name[T1, ...] (x1, t1, ...) = { }
ClassDecl class_decl = TYPE ID type_formals fun_formals_opt SEMICOLON
{: return new ClassDecl(new IdDecl(ID), type_formals, new List(), new List(), fun_formals_opt); :}
;
// IN: 0
// EXCEPTION: lang.ir.QualifierException
// IN: 1
// OUT: 6
type Pair(fst : nonnull array[nonnull any], snd : nonnull array[any]);
fun set(p : Pair, i : int, v1 : any, v2 : any) = {
(p.fst : array[nonnull int])[i] := v1;
(p.snd : array[nonnull int])[i] := v2;
}
fun main(n : int) = {
var ps := new Pair(new array[nonnull any](10), new array[any](10));
set(ps, 0, 1, null);
if (n == 0) {
set(ps, 1, null, 0);
} else {
set(ps, 1, 5, 0);
}
return (ps : Pair).fst[0] + (ps : Pair).fst[1] + (ps : Pair).snd[1];
}
// IN: 0
// EXCEPTION: lang.ir.QualifierException
// IN: 5
// OUT: 5
type Pair[S, T](fst : S, snd : T);
fun null_producer(n : int) = if (n == 0) { return null; } else { return n; }
fun main(n : int) = {
var p : Pair := new Pair[nonnull int, int](0, 1);
p.snd := null; // this should be fine
p.fst := null_producer(n);
return p.fst;
}
\ No newline at end of file
// IN: 0
// EXCEPTION: lang.ir.QualifierException
// IN: 1
// OUT: 6
type Pair(fst : nonnull any, snd : any);
fun set(pairs : array[Pair], i : int, v1 : any, v2 : any) = {
pairs[i] := new Pair(v1, v2);
}
fun main(n : int) = {
var ps := new array[Pair](10);
set(ps, 0, 1, null);
if (n == 0) {
set(ps, 1, null, 0);
} else {
set(ps, 1, 5, 0);
}
return (ps[0] : Pair).fst + (ps[1] : Pair).fst + (ps[1] : Pair).snd;
}
type List[T](head : T, tail : List[T]);
fun f(x : List[int]) = { }
fun g(a : array[array[int]]) = { }
\ No newline at end of file
testfiles/semacheck/teal2/struct-member.in[1:1-1:39]: User defined type cannot have formal type parameters.
testfiles/semacheck/teal2/struct-member.in[1:29-1:37]: Actual type parameters are disallowed for types other than array[T].
testfiles/semacheck/teal2/struct-member.in[3:9-3:19]: Actual type parameters are disallowed for types other than array[T].
......@@ -11,18 +11,6 @@ aspect IRInterpreter {
}
public void setField(IRVar var, IRValue val) throws InterpreterException {
// Variables having a type parameter need to be checked in the
// context of the object (which has concrete value for the type parameter)
if (var.getIRTypeRef() instanceof IRTypeFormalRef) {
IRType actualType = getType().lookup(((IRTypeFormalRef) var.getIRTypeRef()).getIRTypeFormal());
for (IRQualifier q : actualType.getIRQualifiers()) {
if (!q.check(val)) {
throw new QualifierException(q, val, var);
}
}
}
// This is a variable that has a concrete type and its qualifier
// are checked by the storage.
store.set(var, val);
}
......@@ -31,7 +19,6 @@ aspect IRInterpreter {
}
}
public void IRNewInsn.eval(IRFunctionEvalCtx ctx) throws InterpreterException {
IRType type = getIRType();
if (type.getIRTypeCon().getNumIRTypeFormal() != type.getNumTypeActual())
......
......@@ -16,10 +16,6 @@ aspect IRPrint {
out.println("end class\n");
}
public void IRTypeFormalRef.print(PrintStream out) {
getIRTypeFormal().print(out);
}
public void IRLoadInsn.print(PrintStream out) {
getDst().print(out);
out.print(" <- load ");
......
IRTypeFormalRef : IRTypeRef ::= <IRTypeFormal:IRTypeFormal>;
......@@ -6,7 +6,6 @@ module("teal-2-ir") {
jastadd {
include "ast/object.ast"
include "ast/generic.ast"
include "ast/IRPrint.jrag"
include "ast/IRInterpreter.jrag"
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment