Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package IC.SemanticChecks;
- import IC.AST.*;
- import IC.SymbolTable.*;
- import IC.TypeTable.TableMethodType;
- import IC.TypeTable.TableType;
- import IC.TypeTable.TypeTable;
- public class ScopeRules {
- private static int whileCounter = 0;
- public static TypeTable typeTable;
- public static void checkScopeRules(ASTNode root) {
- for (ICClass c : ((Program) root).getClasses()) {
- checkScopeRulesClass(c);
- }
- }
- private static void checkScopeRulesClass(ICClass Class) {
- for (Method m : Class.getMethods()) {
- checkScopeRulesMethod(m);
- }
- for(Field f: Class.getFields()){
- if(f.getType() instanceof UserType){
- if(SymbolTable.getSymbolTableByName(f.getType().getName())==null)
- new SemanticException("field "+f.getName()+" type: "+f.getType().getName() +" is not defined", f.getLine());
- }
- }
- }
- private static void checkScopeRulesMethod(Method m) {
- for (Statement s : m.getStatements()) {
- checkScopeRulesStatement(s);
- if(m.getType() instanceof UserType){
- if(SymbolTable.getSymbolTableByName(m.getType().getName())==null)
- new SemanticException("return type: "+m.getType().getName() +" is not defined", m.getLine());
- }
- for(Formal f: m.getFormals()){
- if(f.getType() instanceof UserType){
- if(SymbolTable.getSymbolTableByName(f.getType().getName())==null)
- new SemanticException("foraml "+f.getName()+" type: "+f.getType().getName() +" is not defined", f.getLine());
- }
- }
- }
- }
- private static void checkScopeRulesStatement(Statement s) {
- if (s instanceof Assignment) {
- checkScopeRulesStatement((Assignment) s);
- } else if (s instanceof If) {
- checkScopeRulesStatement((If) s);
- } else if (s instanceof CallStatement) {
- checkScopeRulesStatement((CallStatement) s);
- } else if (s instanceof StatementsBlock) {
- checkScopeRulesStatement((StatementsBlock) s);
- } else if (s instanceof While) {
- checkScopeRulesStatement((While) s);
- } else if (s instanceof Break) {
- checkScopeRulesStatement((Break) s);
- } else if (s instanceof Continue) {
- checkScopeRulesStatement((Continue) s);
- } else if (s instanceof LocalVariable) {
- checkScopeRulesStatement((LocalVariable) s);
- }else if (s instanceof Return) {
- checkScopeRulesStatement((Return) s);
- }
- }
- private static void checkScopeRulesStatement(LocalVariable s){
- if(s.hasInitValue())
- checkScopeRulesExpression(s.getInitValue());
- }
- private static void checkScopeRulesStatement(Assignment s) {
- checkScopeRulesExpression(s.getAssignment());
- checkScopeRulesExpression(s.getVariable());
- }
- private static void checkScopeRulesStatement(If s) {
- checkScopeRulesExpression(s.getCondition());
- checkScopeRulesStatement(s.getOperation());
- if (s.hasElse())
- checkScopeRulesStatement(s.getElseOperation());
- }
- private static void checkScopeRulesStatement(Break s) {
- if (whileCounter == 0)
- new SemanticException("Break is not in While",s.getLine());
- }
- private static void checkScopeRulesStatement(Continue s) {
- if (whileCounter == 0)
- new SemanticException("Continue is not in While",s.getLine());
- }
- private static void checkScopeRulesStatement(CallStatement s) {
- checkScopeRulesExpression(s.getCall());
- }
- private static void checkScopeRulesStatement(StatementsBlock sb) {
- for (Statement s : sb.getStatements()) {
- checkScopeRulesStatement(s);
- }
- }
- private static void checkScopeRulesStatement(While s) {
- whileCounter++;
- checkScopeRulesExpression(s.getCondition());
- checkScopeRulesStatement(s.getOperation());
- whileCounter--;
- }
- private static void checkScopeRulesStatement(Return ret) {
- checkScopeRulesExpression(ret.getValue());
- }
- private static void checkScopeRulesExpression(Expression e) {
- if (e instanceof BinaryOp) {
- checkScopeRulesExpression((BinaryOp) e);
- } else if (e instanceof Call) {
- checkScopeRulesExpression((Call) e);
- } else if (e instanceof ExpressionBlock) {
- checkScopeRulesExpression((ExpressionBlock) e);
- } else if (e instanceof Length) {
- checkScopeRulesExpression((Length) e);
- } else if (e instanceof Literal) {
- checkScopeRulesExpression((Literal) e);
- } else if (e instanceof Location) {
- checkScopeRulesExpression((Location) e);
- } else if (e instanceof New) {
- checkScopeRulesExpression((New) e);
- } else if (e instanceof This) {
- checkScopeRulesExpression((This) e);
- } else if (e instanceof UnaryOp) {
- checkScopeRulesExpression((UnaryOp) e);
- }
- }
- private static void checkScopeRulesExpression(BinaryOp bo) {
- checkScopeRulesExpression(bo.getFirstOperand());
- checkScopeRulesExpression(bo.getSecondOperand());
- }
- private static void checkScopeRulesExpression(Call c) {
- SymbolTable scope=c.getSymbolTable();
- for (Expression e : c.getArguments())
- checkScopeRulesExpression(e);
- if (c instanceof VirtualCall) {
- boolean found=false;
- if (((VirtualCall) c).getLocation() == null) {
- SymbolTable parent = scope;
- while(parent != null && !found){
- while (!parent.getId().equals("Class")) {
- parent = parent.getParentSymbolTable();
- if (parent == null)
- new SemanticException("method "+c.getName()+" not found",c.getLine());
- }
- if (!parent.getEntries().containsKey(c.getName())){
- parent = parent.getParentSymbolTable();
- }
- else if(!Kind.getValue(parent.getEntries().get(c.getName()).getKind()).equals("Virtual method")){
- new SemanticException("virtual method "+c.getName()+" not found",c.getLine());
- }
- else{
- String m = parent.getEntries().get(c.getName()).getRealType();
- TableType mTable = typeTable.getValue(m);
- if(((TableMethodType) mTable).getParams().size()!=c.getArguments().size())
- new SemanticException("method "+c.getName()+" not found (number of args is not matching)",c.getLine()); //method don't exist
- parent=scope;
- while (!parent.getId().equals("Method")) {
- parent = parent.getParentSymbolTable();
- }
- if(Kind.getValue((parent.getParentSymbolTable().getEntries().get(parent.getName()).getKind())).equals("Static method"))
- new SemanticException("method "+c.getName()+" (is a virtual function) can not be accssed from static method",c.getLine());
- found=true;
- }
- }
- if(!found)
- new SemanticException("Function 555 "+c.getName()+" not found",c.getLine());
- } else {
- checkScopeRulesExpression(((VirtualCall) c).getLocation());
- TypeChecking typeCheck=new TypeChecking(typeTable);
- ((VirtualCall) c).getLocation().accept(typeCheck);
- SymbolTable classScope=SymbolTable.getSymbolTableByName(((VirtualCall) c).getLocation().getExprTrueType().getName());
- if(classScope==null)
- new SemanticException("Class "+((VirtualCall) c).getLocation().getExprTrueType().getName()+" not found , for call "+c.getName(),c.getLine());// class don't exist
- while(classScope!=null && !found){
- if(!classScope.getEntries().containsKey(c.getName()))
- classScope= classScope.getParentSymbolTable();
- else if(!Kind.getValue(classScope.getEntries().get(c.getName()).getKind()).equals("Virtual method"))
- new SemanticException("virtual method "+c.getName()+" not found",c.getLine()); //don't Virtual method
- else{
- String m = classScope.getEntries().get(c.getName()).getRealType();
- TableType mTable = typeTable.getValue(m);
- if(((TableMethodType) mTable).getParams().size()!=c.getArguments().size())
- new SemanticException("method "+c.getName()+" not found (number of args is not matching)",c.getLine()); //method don't exist
- found=true;
- }
- }
- if(!found)
- new SemanticException("Function "+c.getName()+" not found",c.getLine()); //don't exist
- }
- } else if (c instanceof StaticCall){
- SymbolTable classOfCall=SymbolTable.getSymbolTableByName(((StaticCall) c).getClassName());
- if(classOfCall==null){
- new SemanticException("Class "+((VirtualCall) c).getLocation().getExprTrueType().getName()+" not found , for call "+c.getName(),c.getLine());//class don't exist
- }
- if(!classOfCall.getEntries().containsKey("*"+c.getName()))
- new SemanticException("method "+c.getName()+" not found",c.getLine());//method don't found
- else if (!Kind.getValue(classOfCall.getEntries().get("*"+c.getName()).getKind()).equals("Static method"))
- new SemanticException("method "+c.getName()+" not found",c.getLine());//don't static method
- else{
- String m = classOfCall.getEntries().get("*"+c.getName()).getRealType();
- TableType mTable = typeTable.getValue(m);
- if(((TableMethodType) mTable).getParams().size()!=c.getArguments().size())
- new SemanticException("method "+c.getName()+" not found (number of args is not matching)",c.getLine()); //method don't exist
- }
- }
- }
- private static void checkScopeRulesExpression(ExpressionBlock eb) {
- checkScopeRulesExpression(eb.getExpression());
- }
- private static void checkScopeRulesExpression(Length l) {
- checkScopeRulesExpression(l.getArray());
- }
- // done nothing , only contain Type
- private static void checkScopeRulesExpression(Literal l) {
- }
- private static void checkScopeRulesExpression(Location l) {
- SymbolTable scope =l.getSymbolTable();
- if( l instanceof ArrayLocation){
- checkScopeRulesExpression(((ArrayLocation) l).getArray());
- checkScopeRulesExpression(((ArrayLocation) l).getIndex());
- }else if( l instanceof VariableLocation){
- boolean found=false;
- if(((VariableLocation) l).getLocation()==null){
- //// var in this method scope only
- while(scope!=null && !found){
- if(!scope.getEntries().containsKey(((VariableLocation) l).getName()))
- scope=scope.getParentSymbolTable();
- else if(!Kind.getValue(scope.getEntries().get(((VariableLocation) l).getName()).getKind()).equals("Local variable")
- && !Kind.getValue(scope.getEntries().get(((VariableLocation) l).getName()).getKind()).equals("Parameter")
- && !Kind.getValue(scope.getEntries().get(((VariableLocation) l).getName()).getKind()).equals("Field")){
- new SemanticException("Variable "+((VariableLocation) l).getName()+" not found ",l.getLine());// exist but not a var
- } else {
- if(scope.getId().equals("Class")){
- SymbolTable scope1=l.getSymbolTable();
- while(!scope1.getId().equals("Method")){
- scope1=scope1.getParentSymbolTable();
- }
- if(scope1.isStatic())
- new SemanticException("field "+((VariableLocation) l).getName()+" is accsessed from static method",l.getLine());// try to accses field from static method
- found=true;
- }else{
- if(Kind.getValue(scope.getEntries().get(((VariableLocation) l).getName()).getKind()).equals("Parameter"))
- found=true;
- else if(scope.getEntries().get(((VariableLocation) l).getName()).getType().getLine()>l.getLine())
- scope=scope.getParentSymbolTable();
- else
- found=true;
- }
- }
- }
- if(!found)
- new SemanticException("unresolved identifier "+((VariableLocation) l).getName(),l.getLine());// don't exist var
- } else{
- //// private var of location need to know location type
- checkScopeRulesExpression(((VariableLocation) l).getLocation());
- TypeChecking typeCheck=new TypeChecking(typeTable);
- ((VariableLocation) l).getLocation().accept(typeCheck);
- SymbolTable classScope=SymbolTable.getSymbolTableByName(((VariableLocation) l).getLocation().getExprTrueType().getName());
- if(classScope==null)
- new SemanticException("Class "+((VariableLocation) l).getLocation().getExprTrueType().getName()+" not found , for call "+((VariableLocation) l).getName(),l.getLine());// class don't exist
- while(classScope!=null && !found){
- if(!classScope.getEntries().containsKey(((VariableLocation) l).getName()))
- classScope= classScope.getParentSymbolTable();
- else if(!Kind.getValue(classScope.getEntries().get((((VariableLocation) l).getName())).getKind()).equals("Field"))
- new SemanticException("unresolved identifier "+((VariableLocation) l).getName()+" in class +"+((VariableLocation) l).getLocation().getExprTrueType().getName(),l.getLine()); //don't a Field
- else
- found=true;
- }
- if(!found)
- new SemanticException("unresolved identifier "+((VariableLocation) l).getName()+" in class +"+((VariableLocation) l).getLocation().getExprTrueType().getName(),l.getLine()); //don't exist
- }
- }
- }
- private static void checkScopeRulesExpression(New n) {
- if(n instanceof NewArray){
- checkScopeRulesExpression(((NewArray) n).getSize());
- if(((NewArray) n).getType() instanceof UserType){// not primitive type
- // check if class name exist
- if(SymbolTable.getSymbolTableByName(((NewArray) n).getType().getName())==null)
- new SemanticException("Class "+((NewArray) n).getType().getName()+"is not defined",n.getLine());//class don't exist, try to create a new not exist class
- }
- }else if( n instanceof NewClass){
- //check if class name exist
- if(SymbolTable.getSymbolTableByName(((NewClass) n).getName())==null)
- new SemanticException("Class "+((NewClass) n).getName()+"is not defined",n.getLine());//class don't exist, try to create a new not exist class
- }
- }
- private static void checkScopeRulesExpression(This t) {
- SymbolTable scope=t.getSymbolTable();
- while(scope!=null){
- if(scope.getId().equals("Method"))break;
- scope=scope.getParentSymbolTable();
- }
- if(scope==null)
- new SemanticException("\"this\" cannot be used in static method ",t.getLine());//this in non virtual method
- if(scope.isStatic())
- new SemanticException("\"this\" cannot be used in static method ",t.getLine());//this in non virtual method
- }
- private static void checkScopeRulesExpression(UnaryOp uo) {
- checkScopeRulesExpression(uo.getOperand());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement