/** * Print unit consisting of package clause and import statements in toplevel, followed by class * definition. if class definition == null, print all definitions in toplevel. * * @param tree The toplevel tree * @param cdef The class definition, which is assumed to be part of the toplevel tree. */ public void printUnit(JCCompilationUnit tree, JCClassDecl cdef) throws IOException { docComments = tree.docComments; printDocComment(tree); if (tree.pid != null) { consumeComments(tree.pos); print("package "); printExpr(tree.pid); print(";"); println(); } boolean firstImport = true; for (List<JCTree> l = tree.defs; l.nonEmpty() && (cdef == null || getTag(l.head) == IMPORT); l = l.tail) { if (getTag(l.head) == IMPORT) { JCImport imp = (JCImport) l.head; Name name = TreeInfo.name(imp.qualid); if (name == name.table.fromChars(new char[] {'*'}, 0, 1) || cdef == null || isUsed(TreeInfo.symbol(imp.qualid), cdef)) { if (firstImport) { firstImport = false; println(); } printStat(imp); } } else { printStat(l.head); } } if (cdef != null) { printStat(cdef); println(); } }
protected void printTree(String label, JCTree tree) { if (tree == null) { printNull(label); } else { indent(); String ext; try { ext = tree.getKind().name(); } catch (Throwable t) { ext = "n/a"; } out.print(label + ": " + info(tree.getClass(), tree.getTag(), ext)); if (showPositions) { // We can always get start position, but to get end position // and/or line+offset, we would need a JCCompilationUnit out.print(" pos:" + tree.pos); } if (showTreeTypes && tree.type != null) out.print(" type:" + toString(tree.type)); Symbol sym; if (showTreeSymbols && (sym = TreeInfo.symbolFor(tree)) != null) out.print(" sym:" + toString(sym)); out.println(); indent(+1); if (showSrc) { indent(); out.println("src: " + Pretty.toSimpleString(tree, maxSrcLength)); } tree.accept(treeVisitor); indent(-1); } }
private void loadCompiledModules( List<JCCompilationUnit> trees, LinkedList<JCCompilationUnit> moduleTrees) { compilerDelegate.visitModules(phasedUnits); Modules modules = ceylonContext.getModules(); // now make sure the phase units have their modules and packages set correctly for (PhasedUnit pu : phasedUnits.getPhasedUnits()) { Package pkg = pu.getPackage(); loadModuleFromSource(pkg, modules, moduleTrees, trees); } // also make sure we have packages and modules set up for every Java file we compile for (JCCompilationUnit cu : trees) { // skip Ceylon CUs if (cu instanceof CeylonCompilationUnit) continue; String packageName = ""; if (cu.pid != null) packageName = TreeInfo.fullName(cu.pid).toString(); /* * Stef: see javadoc for findOrCreateModulelessPackage() for why this is here. */ Package pkg = modelLoader.findOrCreateModulelessPackage(packageName); loadModuleFromSource(pkg, modules, moduleTrees, trees); } for (PhasedUnit phasedUnit : phasedUnits.getPhasedUnits()) { for (Tree.ModuleDescriptor modDescr : phasedUnit.getCompilationUnit().getModuleDescriptors()) { String name = phasedUnit.getPackage().getNameAsString(); CeylonPhasedUnit cpu = (CeylonPhasedUnit) phasedUnit; CeylonFileObject cfo = (CeylonFileObject) cpu.getFileObject(); moduleNamesToFileObjects.put(name, cfo); } } }
@Override public Void visitAnnotation(AnnotationTree node, Void p) { Element anno = TreeInfo.symbol((JCTree) node.getAnnotationType()); if (anno.toString().equals(DefaultType.class.getName())) { checker.report(Result.failure("annotation.not.allowed.in.src", anno.toString()), node); } return super.visitAnnotation(node, p); }
/** {@inheritDoc} */ public String getDocComment() { // Our doc comment is contained in a map in our toplevel, // indexed by our tree. Find our enter environment, which gives // us our toplevel. It also gives us a tree that contains our // tree: walk it to find our tree. This is painful. Env<AttrContext> enterEnv = getEnterEnv(); if (enterEnv == null) return null; JCTree tree = TreeInfo.declarationFor(sym, enterEnv.tree); return enterEnv.toplevel.docComments.get(tree); }
/** {@inheritDoc} */ public SourcePosition getPosition() { // Find the toplevel. From there use a tree-walking utility // that finds the tree for our symbol, and with it the position. Env<AttrContext> enterEnv = getEnterEnv(); if (enterEnv == null) return null; JCTree.JCCompilationUnit toplevel = enterEnv.toplevel; JavaFileObject sourcefile = toplevel.sourcefile; if (sourcefile == null) return null; int pos = TreeInfo.positionFor(sym, toplevel); return new SourcePositionImpl(sourcefile, pos, toplevel.lineMap); }
public void visitBinary(JCBinary tree) { try { int ownprec = TreeInfo.opPrec(getTag(tree)); String opname = operatorName(getTag(tree)); open(prec, ownprec); printExpr(tree.lhs, ownprec); print(" " + opname + " "); printExpr(tree.rhs, ownprec + 1); close(prec, ownprec); } catch (IOException e) { throw new UncheckedIOException(e); } }
public void visitUnary(JCUnary tree) { try { int ownprec = TreeInfo.opPrec(getTag(tree)); String opname = operatorName(getTag(tree)); open(prec, ownprec); if (getTag(tree) <= Javac.getCtcInt(JCTree.class, "PREDEC")) { print(opname); printExpr(tree.arg, ownprec); } else { printExpr(tree.arg, ownprec); print(opname); } close(prec, ownprec); } catch (IOException e) { throw new UncheckedIOException(e); } }
public void visitTopLevel(JCCompilationUnit tree) { JavaFileObject prev = log.useSource(tree.sourcefile); boolean addEnv = false; boolean isPkgInfo = tree.sourcefile.isNameCompatible("package-info", JavaFileObject.Kind.SOURCE); if (tree.pid != null) { tree.packge = reader.enterPackage(TreeInfo.fullName(tree.pid)); if (tree.packageAnnotations.nonEmpty()) { if (isPkgInfo) { addEnv = true; } else { log.error(tree.packageAnnotations.head.pos(), "pkg.annotations.sb.in.package-info.java"); } } } else { tree.packge = syms.unnamedPackage; } tree.packge.complete(); // Find all classes in package. Env<AttrContext> env = topLevelEnv(tree); // Save environment of package-info.java file. if (isPkgInfo) { Env<AttrContext> env0 = typeEnvs.get(tree.packge); if (env0 == null) { typeEnvs.put(tree.packge, env); } else { JCCompilationUnit tree0 = env0.toplevel; if (!fileManager.isSameFile(tree.sourcefile, tree0.sourcefile)) { log.warning( tree.pid != null ? tree.pid.pos() : null, "pkg-info.already.seen", tree.packge); if (addEnv || (tree0.packageAnnotations.isEmpty() && tree.docComments != null && tree.docComments.get(tree) != null)) { typeEnvs.put(tree.packge, env); } } } } classEnter(tree.defs, env); if (addEnv) { todo.append(env); } log.useSource(prev); result = null; }
private void loadCompiledModules( List<JCCompilationUnit> trees, LinkedList<JCCompilationUnit> moduleTrees) { phasedUnits.visitModules(); Modules modules = ceylonContext.getModules(); // now make sure the phase units have their modules and packages set correctly for (PhasedUnit pu : phasedUnits.getPhasedUnits()) { Package pkg = pu.getPackage(); loadModuleFromSource(pkg, modules, moduleTrees); } // also make sure we have packages and modules set up for every Java file we compile for (JCCompilationUnit cu : trees) { // skip Ceylon CUs if (cu instanceof CeylonCompilationUnit) continue; String packageName = ""; if (cu.pid != null) packageName = TreeInfo.fullName(cu.pid).toString(); Package pkg = modelLoader.findOrCreatePackage(null, packageName); loadModuleFromSource(pkg, modules, moduleTrees); } }
/** Print a set of modifiers. */ public void printFlags(long flags) throws IOException { if ((flags & SYNTHETIC) != 0) print("/*synthetic*/ "); print(TreeInfo.flagNames(flags)); if ((flags & StandardFlags) != 0) print(" "); if ((flags & ANNOTATION) != 0) print("@"); }
Attribute enterAttributeValue(Type expected, JCExpression tree, Env<AttrContext> env) { // first, try completing the attribution value sym - if a completion // error is thrown, we should recover gracefully, and display an // ordinary resolution diagnostic. try { expected.tsym.complete(); } catch (CompletionFailure e) { log.error(tree.pos(), "cant.resolve", Kinds.kindName(e.sym), e.sym); return new Attribute.Error(expected); } if (expected.isPrimitive() || types.isSameType(expected, syms.stringType)) { Type result = attr.attribExpr(tree, env, expected); if (result.isErroneous()) return new Attribute.Error(expected); if (result.constValue() == null) { log.error(tree.pos(), "attribute.value.must.be.constant"); return new Attribute.Error(expected); } result = cfolder.coerce(result, expected); return new Attribute.Constant(expected, result.constValue()); } if (expected.tsym == syms.classType.tsym) { Type result = attr.attribExpr(tree, env, expected); if (result.isErroneous()) return new Attribute.Error(expected); if (TreeInfo.name(tree) != names._class) { log.error(tree.pos(), "annotation.value.must.be.class.literal"); return new Attribute.Error(expected); } return new Attribute.Class(types, (((JCFieldAccess) tree).selected).type); } if ((expected.tsym.flags() & Flags.ANNOTATION) != 0 || types.isSameType(expected, syms.annotationType)) { if (tree.getTag() != JCTree.ANNOTATION) { log.error(tree.pos(), "annotation.value.must.be.annotation"); expected = syms.errorType; } return enterAnnotation((JCAnnotation) tree, expected, env); } if (expected.tag == TypeTags.ARRAY) { // should really be isArray() if (tree.getTag() != JCTree.NEWARRAY) { tree = make.at(tree.pos).NewArray(null, List.<JCExpression>nil(), List.of(tree)); } JCNewArray na = (JCNewArray) tree; if (na.elemtype != null) { log.error(na.elemtype.pos(), "new.not.allowed.in.annotation"); return new Attribute.Error(expected); } ListBuffer<Attribute> buf = new ListBuffer<Attribute>(); for (List<JCExpression> l = na.elems; l.nonEmpty(); l = l.tail) { buf.append(enterAttributeValue(types.elemtype(expected), l.head, env)); } na.type = expected; return new Attribute.Array(expected, buf.toArray(new Attribute[buf.length()])); } if (expected.tag == TypeTags.CLASS && (expected.tsym.flags() & Flags.ENUM) != 0) { attr.attribExpr(tree, env, expected); Symbol sym = TreeInfo.symbol(tree); if (sym == null || TreeInfo.nonstaticSelect(tree) || sym.kind != Kinds.VAR || (sym.flags() & Flags.ENUM) == 0) { log.error(tree.pos(), "enum.annotation.must.be.enum.constant"); return new Attribute.Error(expected); } VarSymbol enumerator = (VarSymbol) sym; return new Attribute.Enum(expected, enumerator); } if (!expected.isErroneous()) log.error(tree.pos(), "annotation.value.not.allowable.type"); return new Attribute.Error(attr.attribExpr(tree, env, expected)); }