Skip to content

Commit

Permalink
Remove all method.type.noError() check in Namer
Browse files Browse the repository at this point in the history
  • Loading branch information
equation314 committed Nov 1, 2019
1 parent b91f976 commit 127b9a7
Showing 1 changed file with 20 additions and 23 deletions.
43 changes: 20 additions & 23 deletions src/main/java/decaf/frontend/typecheck/Namer.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ public void visitClassDef(Tree.ClassDef clazz, ScopeStack ctx) {

@Override
public void visitVarDef(Tree.VarDef varDef, ScopeStack ctx) {
varDef.typeLit.accept(this, ctx);

var earlier = ctx.findConflict(varDef.name);
if (earlier.isPresent()) {
if (earlier.get().isVarSymbol() && earlier.get().domain() != ctx.currentScope()) {
Expand All @@ -198,7 +200,6 @@ public void visitVarDef(Tree.VarDef varDef, ScopeStack ctx) {
return;
}

varDef.typeLit.accept(this, ctx);
if (varDef.typeLit.type.eq(BuiltInType.VOID)) {
issue(new BadVarTypeError(varDef.pos, varDef.name));
return;
Expand All @@ -221,7 +222,7 @@ public void visitMethodDef(Tree.MethodDef method, ScopeStack ctx) {
// Only non-static methods can be overriden, but the type signature must be equivalent.
var formal = new FormalScope();
typeMethod(method, ctx, formal);
if (method.type.noError() && method.type.subtypeOf(suspect.type)) { // override success
if (method.type.subtypeOf(suspect.type)) { // override success
var symbol = new MethodSymbol(method.name, method.type, formal, method.pos, method.modifiers,
ctx.currentClass());
ctx.declare(symbol);
Expand All @@ -243,30 +244,26 @@ public void visitMethodDef(Tree.MethodDef method, ScopeStack ctx) {

var formal = new FormalScope();
typeMethod(method, ctx, formal);
if (method.type.noError()) {
var symbol = new MethodSymbol(method.name, method.type, formal, method.pos, method.modifiers,
ctx.currentClass());
ctx.declare(symbol);
method.symbol = symbol;
ctx.open(formal);
method.body.accept(this, ctx);
ctx.close();
}
var symbol = new MethodSymbol(method.name, method.type, formal, method.pos, method.modifiers,
ctx.currentClass());
ctx.declare(symbol);
method.symbol = symbol;
ctx.open(formal);
method.body.accept(this, ctx);
ctx.close();
}

private void typeMethod(Tree.MethodDef method, ScopeStack ctx, FormalScope formal) {
method.returnType.accept(this, ctx);
if (method.returnType.type.noError()) {
ctx.open(formal);
if (!method.isStatic()) ctx.declare(VarSymbol.thisVar(ctx.currentClass().type, method.id.pos));
var argTypes = new ArrayList<Type>();
for (var param : method.params) {
param.accept(this, ctx);
argTypes.add(param.typeLit.type);
}
method.type = new FunType(method.returnType.type, argTypes);
ctx.close();
ctx.open(formal);
if (!method.isStatic()) ctx.declare(VarSymbol.thisVar(ctx.currentClass().type, method.id.pos));
var argTypes = new ArrayList<Type>();
for (var param : method.params) {
param.accept(this, ctx);
argTypes.add(param.typeLit.type);
}
method.type = new FunType(method.returnType.type, argTypes);
ctx.close();
}

@Override
Expand All @@ -281,14 +278,14 @@ public void visitBlock(Tree.Block block, ScopeStack ctx) {

@Override
public void visitLocalVarDef(Tree.LocalVarDef def, ScopeStack ctx) {
def.typeLit.accept(this, ctx);

var earlier = ctx.findConflict(def.name);
if (earlier.isPresent()) {
issue(new DeclConflictError(def.pos, def.name, earlier.get().pos));
def.typeLit.type = BuiltInType.ERROR;
return;
}

def.typeLit.accept(this, ctx);
if (def.typeLit.type.eq(BuiltInType.VOID)) {
issue(new BadVarTypeError(def.pos, def.name));
return;
Expand Down

0 comments on commit 127b9a7

Please sign in to comment.