Commit 3fbc9aee authored by Sven Gestegård Robertz's avatar Sven Gestegård Robertz
Browse files

ASTbuilder: add checks to only append typedefs from the same source

parent 71869dc0
...@@ -158,13 +158,17 @@ public class ASTbuilder implements TypeDefParser.ParsedSymbolVisitor { ...@@ -158,13 +158,17 @@ public class ASTbuilder implements TypeDefParser.ParsedSymbolVisitor {
/** Create a labcomm AST for the ParsedTypeDef d, including /** Create a labcomm AST for the ParsedTypeDef d, including
* all declarations from p. * all declarations from p.
* *
* This copies the declarations in p, and creates a new AST.
* *
* If d is a sampleDecl, include the typedefs it depends on. * If p is not null, and the source of d matches the
* TypeDefParser this ASTbuilder was created for,
* creates a new AST containing the declarations in p
* and d and (if d is a sampleDecl) its dependencies.
*
* Otherwise, makeProgram(d)
*/ */
public Program makeProgram(TypeDefParser.ParsedTypeDef d, public Program makeProgram(TypeDefParser.ParsedTypeDef d,
Program p) { Program p) {
if(p != null) { if(p != null && d.checkSource(tdp)) {
return makeProgram(d, p.getDecls().fullCopy()); return makeProgram(d, p.getDecls().fullCopy());
} else { } else {
return makeProgram(d); return makeProgram(d);
......
...@@ -2,8 +2,9 @@ package se.lth.control.labcomm; ...@@ -2,8 +2,9 @@ package se.lth.control.labcomm;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.DataInputStream; import java.io.DataInputStream;
...@@ -17,6 +18,10 @@ import se.lth.control.labcomm.TypeBinding; ...@@ -17,6 +18,10 @@ import se.lth.control.labcomm.TypeBinding;
public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler { public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
public interface TypeDefListener {
void onTypeDef(ParsedTypeDef d);
}
static class SelfBinding extends TypeDef { static class SelfBinding extends TypeDef {
private int sampleIndex; private int sampleIndex;
...@@ -48,10 +53,25 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler { ...@@ -48,10 +53,25 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
} }
} }
public interface TypeDefListener { static class EmptyIterator<T> implements java.util.Iterator {
void onTypeDef(ParsedTypeDef d); @Override
public boolean hasNext() {
return false;
}
@Override
public T next() {
throw new NoSuchElementException();
}
public void remove() {
throw new UnsupportedOperationException();
}
} }
static EmptyIterator<ParsedTypeDef> emptyIterator =
new EmptyIterator<>();
private HashMap<Integer,TypeDef> typeDefs; private HashMap<Integer,TypeDef> typeDefs;
private HashMap<Integer,Integer> typeBindings; private HashMap<Integer,Integer> typeBindings;
private HashSet<TypeDefListener> listeners; private HashSet<TypeDefListener> listeners;
...@@ -403,10 +423,22 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler { ...@@ -403,10 +423,22 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
private int idx; private int idx;
private String name; private String name;
private ParsedType type; private ParsedType type;
private final TypeDefParser source;
ParsedTypeDef(TypeDefParser source, int idx, String name){
this.source = source;
this.idx = idx;
this.name = name;
}
ParsedTypeDef(TypeDefParser source,
int idx, String name, ParsedType type) {
this(source, idx, name);
this.type = type;
}
ParsedTypeDef(int idx, String name){ ParsedTypeDef(int idx, String name){
this.idx = idx; this(null, idx, name);
this.name = name;
} }
ParsedTypeDef(int idx, String name, ParsedType type) { ParsedTypeDef(int idx, String name, ParsedType type) {
...@@ -414,6 +446,16 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler { ...@@ -414,6 +446,16 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
this.type = type; this.type = type;
} }
/** @return true if source is same as
*/
public boolean checkSource(TypeDefParser o) {
boolean result = source == o;
System.err.println("ParsedTypeDef.checkSource: source = "+source);
System.err.println("ParsedTypeDef.checkSource: o "+o);
System.err.println("ParsedTypeDef.checkSource: "+result);
return result;
}
/** To be overridden in ParsedSampleDef /** To be overridden in ParsedSampleDef
*/ */
public boolean isSampleDef() { public boolean isSampleDef() {
...@@ -421,7 +463,7 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler { ...@@ -421,7 +463,7 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
} }
Iterator<ParsedTypeDef> getDepIterator() { Iterator<ParsedTypeDef> getDepIterator() {
throw new Error("ParseTypeDef has no dependencies"); return emptyIterator;
} }
void setType(ParsedType type) { void setType(ParsedType type) {
...@@ -465,8 +507,9 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler { ...@@ -465,8 +507,9 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
public class ParsedSampleDef extends ParsedTypeDef{ public class ParsedSampleDef extends ParsedTypeDef{
private HashSet<ParsedTypeDef> deps; private HashSet<ParsedTypeDef> deps;
ParsedSampleDef(ParsedTypeDef td) { ParsedSampleDef(ParsedTypeDef td) {
super(td.getIndex(), td.getName(), td.getType()); super(td.source, td.getIndex(), td.getName(), td.getType());
this.deps = new HashSet<ParsedTypeDef>(); this.deps = new HashSet<ParsedTypeDef>();
} }
...@@ -496,7 +539,7 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler { ...@@ -496,7 +539,7 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
private LinkedList<TypeDef> typeStack; private LinkedList<TypeDef> typeStack;
ParsedTypeDef newTypeDef() { ParsedTypeDef newTypeDef() {
currentParsed =new ParsedTypeDef(getCurrentIndex(), getCurrentName()); currentParsed =new ParsedTypeDef(TypeDefParser.this, getCurrentIndex(), getCurrentName());
return currentParsed; return currentParsed;
} }
......
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