/** * 该类用于表示声明的类 */ package minijava.symboltable; import minijava.minijava2piglet.PigletBinding; import minijava.minijava2piglet.PigletTemp; import minijava.typecheck.PrintError; public class MClass extends MLocalVarType { public MClasses all_classes; // 所有类的列表,在建立符号表时完成 public boolean isDeclared = false; // 是否已声明,用于检查符号表 public String extend_class_name = null; // 所继承的类名 public MClass extend_class = null; // 所继承的类,在符号表建立完成后才能求得 public int extend_tag = 0; // 检测循环继承时用,0表示未检测 public MMethodList methods; public MMethodList all_methods; // 该类所有方法,包括其父类,考虑方法覆盖 public MClass(String v_name, MClasses all, int m_line, int m_column) { super(m_line, m_column); name = v_name; all_classes = all; methods = new MMethodList(); all_methods = null; } public String insertVar(MVariable var) { String var_name = var.getName(); if (vars.findVar(var_name)!=-1) { return "Variable double declaration " + "\"" + var_name + "\""; } vars.insertVar(var); return null; } public String insertMethod(MMethod method) { String method_name = method.getName(); if (vars.findVar(method_name)!=-1) { return "Method name \'" + method_name + "\' is the same of some variable."; } if (methods.findMethod(method_name)!=-1) { return "Method double declaration " + "\"" + method_name + "\"."; } methods.addMethod(method); method.method_class = this; return null; } public void buildMethodRef() { if (all_methods!=null) { return; } if (extend_class==null) { all_methods = methods; return; } // now build the all_methods list if (extend_class.all_methods==null) { extend_class.buildMethodRef(); } all_methods = new MMethodList(); // first copy the list from parent class for (int i=0; i=0; i--) { --nMethods; result += "HSTORE " + t_methods + " " + nMethods*4 + " " + c.getName() + "_" + c.methods.methods.elementAt(i).getName() + "\n"; } }*/ // initiate all variables to 0 for (int i=1; i<=nVars; i++) { result += "HSTORE " + t_vars + " " + i*4 + " 0\n"; } result += "HSTORE " + t_vars + " 0 " + t_methods + "\n"; result += "RETURN " + t_vars + "\nEND"; return result; } // 检测类型名是否有效 public boolean validType(String s_type) { if (s_type==MIdentifier.intType || s_type==MIdentifier.boolType || s_type==MIdentifier.arrType || s_type==MIdentifier.voidType || s_type==MIdentifier.sArrayType) { return true; } if (all_classes.findClassByName(s_type)!=null) { return true; } return false; } // 对类进行类型检查 public boolean classVarCheck() { boolean result = true; for (int i=0; i