1 package polyglot.ext.jl.ast; 2 3 import polyglot.ast.*; 4 5 import polyglot.types.*; 6 import polyglot.util.*; 7 import polyglot.visit.*; 8 import polyglot.main.Options; 9 10 16 public class Import_c extends Node_c implements Import 17 { 18 protected Kind kind; 19 protected String name; 20 21 public Import_c(Position pos, Kind kind, String name) { 22 super(pos); 23 this.name = name; 24 this.kind = kind; 25 } 26 27 28 public String name() { 29 return this.name; 30 } 31 32 33 public Import name(String name) { 34 Import_c n = (Import_c) copy(); 35 n.name = name; 36 return n; 37 } 38 39 40 public Kind kind() { 41 return this.kind; 42 } 43 44 45 public Import kind(Kind kind) { 46 Import_c n = (Import_c) copy(); 47 n.kind = kind; 48 return n; 49 } 50 51 52 public Node buildTypes(TypeBuilder tb) throws SemanticException { 53 ImportTable it = tb.importTable(); 54 55 if (kind == CLASS) { 56 it.addClassImport(name); 57 } 58 else if (kind == PACKAGE) { 59 it.addPackageImport(name); 60 } 61 62 return this; 63 } 64 65 66 public Node typeCheck(TypeChecker tc) throws SemanticException { 67 if (kind == PACKAGE && tc.typeSystem().packageExists(name)) { 68 return this; 69 } 70 71 73 String pkgName = StringUtil.getFirstComponent(name); 75 76 if (! tc.typeSystem().packageExists(pkgName)) { 77 throw new SemanticException("Package \"" + pkgName + 78 "\" not found.", position()); 79 } 80 81 Named nt = tc.typeSystem().forName(name); 83 84 if (nt instanceof Type) { 86 Type t = (Type) nt; 87 if (t.isClass()) { 88 tc.typeSystem().classAccessibleFromPackage(t.toClass(), 89 tc.context().package_()); 90 } 91 } 92 93 return this; 94 } 95 96 public String toString() { 97 return "import " + name + (kind == PACKAGE ? ".*" : ""); 98 } 99 100 101 public void prettyPrint(CodeWriter w, PrettyPrinter tr) { 102 if (! Options.global.fully_qualified_names) { 103 w.write("import "); 104 w.write(name); 105 106 if (kind == PACKAGE) { 107 w.write(".*"); 108 } 109 110 w.write(";"); 111 w.newline(0); 112 } 113 } 114 } 115 | Popular Tags |