Advertisement
Guest User

Untitled

a guest
Dec 20th, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.80 KB | None | 0 0
  1. package IC.SemanticChecks;
  2.  
  3. import IC.AST.*;
  4. import IC.SymbolTable.*;
  5. import IC.TypeTable.TableMethodType;
  6. import IC.TypeTable.TableType;
  7. import IC.TypeTable.TypeTable;
  8.  
  9. public class ScopeRules {
  10. private static int whileCounter = 0;
  11. public static TypeTable typeTable;
  12.  
  13. public static void checkScopeRules(ASTNode root) {
  14. for (ICClass c : ((Program) root).getClasses()) {
  15. checkScopeRulesClass(c);
  16. }
  17. }
  18.  
  19. private static void checkScopeRulesClass(ICClass Class) {
  20. for (Method m : Class.getMethods()) {
  21. checkScopeRulesMethod(m);
  22. }
  23. for(Field f: Class.getFields()){
  24. if(f.getType() instanceof UserType){
  25. if(SymbolTable.getSymbolTableByName(f.getType().getName())==null)
  26. new SemanticException("field "+f.getName()+" type: "+f.getType().getName() +" is not defined", f.getLine());
  27. }
  28. }
  29.  
  30. }
  31.  
  32. private static void checkScopeRulesMethod(Method m) {
  33. for (Statement s : m.getStatements()) {
  34. checkScopeRulesStatement(s);
  35. if(m.getType() instanceof UserType){
  36. if(SymbolTable.getSymbolTableByName(m.getType().getName())==null)
  37. new SemanticException("return type: "+m.getType().getName() +" is not defined", m.getLine());
  38. }
  39. for(Formal f: m.getFormals()){
  40. if(f.getType() instanceof UserType){
  41. if(SymbolTable.getSymbolTableByName(f.getType().getName())==null)
  42. new SemanticException("foraml "+f.getName()+" type: "+f.getType().getName() +" is not defined", f.getLine());
  43. }
  44. }
  45. }
  46. }
  47.  
  48. private static void checkScopeRulesStatement(Statement s) {
  49. if (s instanceof Assignment) {
  50. checkScopeRulesStatement((Assignment) s);
  51. } else if (s instanceof If) {
  52. checkScopeRulesStatement((If) s);
  53. } else if (s instanceof CallStatement) {
  54. checkScopeRulesStatement((CallStatement) s);
  55. } else if (s instanceof StatementsBlock) {
  56. checkScopeRulesStatement((StatementsBlock) s);
  57. } else if (s instanceof While) {
  58. checkScopeRulesStatement((While) s);
  59. } else if (s instanceof Break) {
  60. checkScopeRulesStatement((Break) s);
  61. } else if (s instanceof Continue) {
  62. checkScopeRulesStatement((Continue) s);
  63. } else if (s instanceof LocalVariable) {
  64. checkScopeRulesStatement((LocalVariable) s);
  65. }else if (s instanceof Return) {
  66. checkScopeRulesStatement((Return) s);
  67. }
  68.  
  69. }
  70.  
  71. private static void checkScopeRulesStatement(LocalVariable s){
  72. if(s.hasInitValue())
  73. checkScopeRulesExpression(s.getInitValue());
  74. }
  75.  
  76. private static void checkScopeRulesStatement(Assignment s) {
  77. checkScopeRulesExpression(s.getAssignment());
  78. checkScopeRulesExpression(s.getVariable());
  79. }
  80.  
  81. private static void checkScopeRulesStatement(If s) {
  82. checkScopeRulesExpression(s.getCondition());
  83. checkScopeRulesStatement(s.getOperation());
  84. if (s.hasElse())
  85. checkScopeRulesStatement(s.getElseOperation());
  86. }
  87.  
  88. private static void checkScopeRulesStatement(Break s) {
  89. if (whileCounter == 0)
  90. new SemanticException("Break is not in While",s.getLine());
  91. }
  92.  
  93. private static void checkScopeRulesStatement(Continue s) {
  94. if (whileCounter == 0)
  95. new SemanticException("Continue is not in While",s.getLine());
  96. }
  97.  
  98. private static void checkScopeRulesStatement(CallStatement s) {
  99. checkScopeRulesExpression(s.getCall());
  100. }
  101.  
  102. private static void checkScopeRulesStatement(StatementsBlock sb) {
  103. for (Statement s : sb.getStatements()) {
  104. checkScopeRulesStatement(s);
  105. }
  106. }
  107.  
  108. private static void checkScopeRulesStatement(While s) {
  109. whileCounter++;
  110. checkScopeRulesExpression(s.getCondition());
  111. checkScopeRulesStatement(s.getOperation());
  112. whileCounter--;
  113. }
  114.  
  115.  
  116. private static void checkScopeRulesStatement(Return ret) {
  117. checkScopeRulesExpression(ret.getValue());
  118. }
  119.  
  120. private static void checkScopeRulesExpression(Expression e) {
  121. if (e instanceof BinaryOp) {
  122. checkScopeRulesExpression((BinaryOp) e);
  123. } else if (e instanceof Call) {
  124. checkScopeRulesExpression((Call) e);
  125. } else if (e instanceof ExpressionBlock) {
  126. checkScopeRulesExpression((ExpressionBlock) e);
  127. } else if (e instanceof Length) {
  128. checkScopeRulesExpression((Length) e);
  129. } else if (e instanceof Literal) {
  130. checkScopeRulesExpression((Literal) e);
  131. } else if (e instanceof Location) {
  132. checkScopeRulesExpression((Location) e);
  133. } else if (e instanceof New) {
  134. checkScopeRulesExpression((New) e);
  135. } else if (e instanceof This) {
  136. checkScopeRulesExpression((This) e);
  137. } else if (e instanceof UnaryOp) {
  138. checkScopeRulesExpression((UnaryOp) e);
  139. }
  140. }
  141.  
  142. private static void checkScopeRulesExpression(BinaryOp bo) {
  143. checkScopeRulesExpression(bo.getFirstOperand());
  144. checkScopeRulesExpression(bo.getSecondOperand());
  145. }
  146.  
  147. private static void checkScopeRulesExpression(Call c) {
  148. SymbolTable scope=c.getSymbolTable();
  149. for (Expression e : c.getArguments())
  150. checkScopeRulesExpression(e);
  151. if (c instanceof VirtualCall) {
  152. boolean found=false;
  153. if (((VirtualCall) c).getLocation() == null) {
  154. SymbolTable parent = scope;
  155. while(parent != null && !found){
  156. while (!parent.getId().equals("Class")) {
  157. parent = parent.getParentSymbolTable();
  158.  
  159. if (parent == null)
  160. new SemanticException("method "+c.getName()+" not found",c.getLine());
  161. }
  162. if (!parent.getEntries().containsKey(c.getName())){
  163. parent = parent.getParentSymbolTable();
  164. }
  165. else if(!Kind.getValue(parent.getEntries().get(c.getName()).getKind()).equals("Virtual method")){
  166. new SemanticException("virtual method "+c.getName()+" not found",c.getLine());
  167. }
  168. else{
  169. String m = parent.getEntries().get(c.getName()).getRealType();
  170. TableType mTable = typeTable.getValue(m);
  171. if(((TableMethodType) mTable).getParams().size()!=c.getArguments().size())
  172. new SemanticException("method "+c.getName()+" not found (number of args is not matching)",c.getLine()); //method don't exist
  173. parent=scope;
  174. while (!parent.getId().equals("Method")) {
  175. parent = parent.getParentSymbolTable();
  176. }
  177. if(Kind.getValue((parent.getParentSymbolTable().getEntries().get(parent.getName()).getKind())).equals("Static method"))
  178. new SemanticException("method "+c.getName()+" (is a virtual function) can not be accssed from static method",c.getLine());
  179. found=true;
  180. }
  181. }
  182. if(!found)
  183. new SemanticException("Function 555 "+c.getName()+" not found",c.getLine());
  184.  
  185. } else {
  186. checkScopeRulesExpression(((VirtualCall) c).getLocation());
  187. TypeChecking typeCheck=new TypeChecking(typeTable);
  188. ((VirtualCall) c).getLocation().accept(typeCheck);
  189. SymbolTable classScope=SymbolTable.getSymbolTableByName(((VirtualCall) c).getLocation().getExprTrueType().getName());
  190. if(classScope==null)
  191. new SemanticException("Class "+((VirtualCall) c).getLocation().getExprTrueType().getName()+" not found , for call "+c.getName(),c.getLine());// class don't exist
  192. while(classScope!=null && !found){
  193. if(!classScope.getEntries().containsKey(c.getName()))
  194. classScope= classScope.getParentSymbolTable();
  195. else if(!Kind.getValue(classScope.getEntries().get(c.getName()).getKind()).equals("Virtual method"))
  196. new SemanticException("virtual method "+c.getName()+" not found",c.getLine()); //don't Virtual method
  197. else{
  198. String m = classScope.getEntries().get(c.getName()).getRealType();
  199. TableType mTable = typeTable.getValue(m);
  200. if(((TableMethodType) mTable).getParams().size()!=c.getArguments().size())
  201. new SemanticException("method "+c.getName()+" not found (number of args is not matching)",c.getLine()); //method don't exist
  202. found=true;
  203. }
  204.  
  205. }
  206. if(!found)
  207. new SemanticException("Function "+c.getName()+" not found",c.getLine()); //don't exist
  208. }
  209.  
  210. } else if (c instanceof StaticCall){
  211. SymbolTable classOfCall=SymbolTable.getSymbolTableByName(((StaticCall) c).getClassName());
  212. if(classOfCall==null){
  213. new SemanticException("Class "+((VirtualCall) c).getLocation().getExprTrueType().getName()+" not found , for call "+c.getName(),c.getLine());//class don't exist
  214. }
  215. if(!classOfCall.getEntries().containsKey("*"+c.getName()))
  216. new SemanticException("method "+c.getName()+" not found",c.getLine());//method don't found
  217. else if (!Kind.getValue(classOfCall.getEntries().get("*"+c.getName()).getKind()).equals("Static method"))
  218. new SemanticException("method "+c.getName()+" not found",c.getLine());//don't static method
  219. else{
  220. String m = classOfCall.getEntries().get("*"+c.getName()).getRealType();
  221. TableType mTable = typeTable.getValue(m);
  222. if(((TableMethodType) mTable).getParams().size()!=c.getArguments().size())
  223. new SemanticException("method "+c.getName()+" not found (number of args is not matching)",c.getLine()); //method don't exist
  224. }
  225. }
  226.  
  227.  
  228. }
  229.  
  230. private static void checkScopeRulesExpression(ExpressionBlock eb) {
  231. checkScopeRulesExpression(eb.getExpression());
  232. }
  233.  
  234. private static void checkScopeRulesExpression(Length l) {
  235. checkScopeRulesExpression(l.getArray());
  236. }
  237.  
  238. // done nothing , only contain Type
  239. private static void checkScopeRulesExpression(Literal l) {
  240.  
  241. }
  242.  
  243. private static void checkScopeRulesExpression(Location l) {
  244. SymbolTable scope =l.getSymbolTable();
  245. if( l instanceof ArrayLocation){
  246. checkScopeRulesExpression(((ArrayLocation) l).getArray());
  247. checkScopeRulesExpression(((ArrayLocation) l).getIndex());
  248. }else if( l instanceof VariableLocation){
  249. boolean found=false;
  250. if(((VariableLocation) l).getLocation()==null){
  251. //// var in this method scope only
  252. while(scope!=null && !found){
  253.  
  254. if(!scope.getEntries().containsKey(((VariableLocation) l).getName()))
  255. scope=scope.getParentSymbolTable();
  256. else if(!Kind.getValue(scope.getEntries().get(((VariableLocation) l).getName()).getKind()).equals("Local variable")
  257. && !Kind.getValue(scope.getEntries().get(((VariableLocation) l).getName()).getKind()).equals("Parameter")
  258. && !Kind.getValue(scope.getEntries().get(((VariableLocation) l).getName()).getKind()).equals("Field")){
  259. new SemanticException("Variable "+((VariableLocation) l).getName()+" not found ",l.getLine());// exist but not a var
  260. } else {
  261.  
  262. if(scope.getId().equals("Class")){
  263. SymbolTable scope1=l.getSymbolTable();
  264. while(!scope1.getId().equals("Method")){
  265.  
  266. scope1=scope1.getParentSymbolTable();
  267. }
  268.  
  269. if(scope1.isStatic())
  270. new SemanticException("field "+((VariableLocation) l).getName()+" is accsessed from static method",l.getLine());// try to accses field from static method
  271.  
  272. found=true;
  273.  
  274. }else{
  275. if(Kind.getValue(scope.getEntries().get(((VariableLocation) l).getName()).getKind()).equals("Parameter"))
  276. found=true;
  277. else if(scope.getEntries().get(((VariableLocation) l).getName()).getType().getLine()>l.getLine())
  278. scope=scope.getParentSymbolTable();
  279. else
  280. found=true;
  281.  
  282. }
  283. }
  284.  
  285. }
  286. if(!found)
  287. new SemanticException("unresolved identifier "+((VariableLocation) l).getName(),l.getLine());// don't exist var
  288. } else{
  289. //// private var of location need to know location type
  290. checkScopeRulesExpression(((VariableLocation) l).getLocation());
  291. TypeChecking typeCheck=new TypeChecking(typeTable);
  292. ((VariableLocation) l).getLocation().accept(typeCheck);
  293. SymbolTable classScope=SymbolTable.getSymbolTableByName(((VariableLocation) l).getLocation().getExprTrueType().getName());
  294. if(classScope==null)
  295. new SemanticException("Class "+((VariableLocation) l).getLocation().getExprTrueType().getName()+" not found , for call "+((VariableLocation) l).getName(),l.getLine());// class don't exist
  296. while(classScope!=null && !found){
  297. if(!classScope.getEntries().containsKey(((VariableLocation) l).getName()))
  298. classScope= classScope.getParentSymbolTable();
  299. else if(!Kind.getValue(classScope.getEntries().get((((VariableLocation) l).getName())).getKind()).equals("Field"))
  300. new SemanticException("unresolved identifier "+((VariableLocation) l).getName()+" in class +"+((VariableLocation) l).getLocation().getExprTrueType().getName(),l.getLine()); //don't a Field
  301. else
  302. found=true;
  303. }
  304. if(!found)
  305. new SemanticException("unresolved identifier "+((VariableLocation) l).getName()+" in class +"+((VariableLocation) l).getLocation().getExprTrueType().getName(),l.getLine()); //don't exist
  306.  
  307. }
  308. }
  309.  
  310. }
  311.  
  312. private static void checkScopeRulesExpression(New n) {
  313. if(n instanceof NewArray){
  314. checkScopeRulesExpression(((NewArray) n).getSize());
  315. if(((NewArray) n).getType() instanceof UserType){// not primitive type
  316. // check if class name exist
  317. if(SymbolTable.getSymbolTableByName(((NewArray) n).getType().getName())==null)
  318. new SemanticException("Class "+((NewArray) n).getType().getName()+"is not defined",n.getLine());//class don't exist, try to create a new not exist class
  319. }
  320.  
  321. }else if( n instanceof NewClass){
  322. //check if class name exist
  323. if(SymbolTable.getSymbolTableByName(((NewClass) n).getName())==null)
  324. new SemanticException("Class "+((NewClass) n).getName()+"is not defined",n.getLine());//class don't exist, try to create a new not exist class
  325. }
  326. }
  327.  
  328. private static void checkScopeRulesExpression(This t) {
  329. SymbolTable scope=t.getSymbolTable();
  330. while(scope!=null){
  331. if(scope.getId().equals("Method"))break;
  332. scope=scope.getParentSymbolTable();
  333. }
  334. if(scope==null)
  335. new SemanticException("\"this\" cannot be used in static method ",t.getLine());//this in non virtual method
  336. if(scope.isStatic())
  337. new SemanticException("\"this\" cannot be used in static method ",t.getLine());//this in non virtual method
  338.  
  339. }
  340.  
  341. private static void checkScopeRulesExpression(UnaryOp uo) {
  342. checkScopeRulesExpression(uo.getOperand());
  343. }
  344.  
  345. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement