use of com.sun.tools.javac.tree.JCTree in project ceylon-compiler by ceylon.
the class JavacTrees method getElement.
public Element getElement(TreePath path) {
JCTree tree = (JCTree) path.getLeaf();
Symbol sym = TreeInfo.symbolFor(tree);
if (sym == null && TreeInfo.isDeclaration(tree)) {
for (TreePath p = path; p != null; p = p.getParentPath()) {
JCTree t = (JCTree) p.getLeaf();
if (t.getTag() == JCTree.CLASSDEF) {
JCClassDecl ct = (JCClassDecl) t;
if (ct.sym != null) {
if ((ct.sym.flags_field & Flags.UNATTRIBUTED) != 0) {
attr.attribClass(ct.pos(), ct.sym);
sym = TreeInfo.symbolFor(tree);
}
break;
}
}
}
}
return sym;
}
use of com.sun.tools.javac.tree.JCTree in project ceylon-compiler by ceylon.
the class JavacTrees method getTree.
public JCTree getTree(Element element) {
Symbol symbol = (Symbol) element;
TypeSymbol enclosing = symbol.enclClass();
Env<AttrContext> env = enter.getEnv(enclosing);
if (env == null)
return null;
JCClassDecl classNode = env.enclClass;
if (classNode != null) {
if (TreeInfo.symbolFor(classNode) == element)
return classNode;
for (JCTree node : classNode.getMembers()) if (TreeInfo.symbolFor(node) == element)
return node;
}
return null;
}
use of com.sun.tools.javac.tree.JCTree in project ceylon-compiler by ceylon.
the class JavaCompiler method parse.
/** Parse contents of input stream.
* @param filename The name of the file from which input stream comes.
* @param input The input stream to be parsed.
*/
protected JCCompilationUnit parse(JavaFileObject filename, CharSequence content) {
long msec = now();
JCCompilationUnit tree = make.TopLevel(List.<JCTree.JCAnnotation>nil(), null, List.<JCTree>nil());
if (content != null) {
if (verbose) {
log.printVerbose("parsing.started", filename);
}
if (taskListener != null) {
TaskEvent e = new TaskEvent(TaskEvent.Kind.PARSE, filename);
taskListener.started(e);
}
Parser parser = parserFactory.newParser(content, keepComments(), genEndPos, lineDebugInfo);
tree = parser.parseCompilationUnit();
if (verbose) {
log.printVerbose("parsing.done", Long.toString(elapsed(msec)));
}
}
tree.sourcefile = filename;
if (content != null && taskListener != null) {
TaskEvent e = new TaskEvent(TaskEvent.Kind.PARSE, tree);
taskListener.finished(e);
}
return tree;
}
use of com.sun.tools.javac.tree.JCTree in project ceylon-compiler by ceylon.
the class JavaCompiler method removeMethodBodies.
JCClassDecl removeMethodBodies(JCClassDecl cdef) {
final boolean isInterface = (cdef.mods.flags & Flags.INTERFACE) != 0;
class MethodBodyRemover extends TreeTranslator {
@Override
public void visitMethodDef(JCMethodDecl tree) {
tree.mods.flags &= ~Flags.SYNCHRONIZED;
for (JCVariableDecl vd : tree.params) vd.mods.flags &= ~Flags.FINAL;
tree.body = null;
super.visitMethodDef(tree);
}
@Override
public void visitVarDef(JCVariableDecl tree) {
if (tree.init != null && tree.init.type.constValue() == null)
tree.init = null;
super.visitVarDef(tree);
}
@Override
public void visitClassDef(JCClassDecl tree) {
ListBuffer<JCTree> newdefs = lb();
for (List<JCTree> it = tree.defs; it.tail != null; it = it.tail) {
JCTree t = it.head;
switch(t.getTag()) {
case JCTree.CLASSDEF:
if (isInterface || (((JCClassDecl) t).mods.flags & (Flags.PROTECTED | Flags.PUBLIC)) != 0 || (((JCClassDecl) t).mods.flags & (Flags.PRIVATE)) == 0 && ((JCClassDecl) t).sym.packge().getQualifiedName() == names.java_lang)
newdefs.append(t);
break;
case JCTree.METHODDEF:
if (isInterface || (((JCMethodDecl) t).mods.flags & (Flags.PROTECTED | Flags.PUBLIC)) != 0 || ((JCMethodDecl) t).sym.name == names.init || (((JCMethodDecl) t).mods.flags & (Flags.PRIVATE)) == 0 && ((JCMethodDecl) t).sym.packge().getQualifiedName() == names.java_lang)
newdefs.append(t);
break;
case JCTree.VARDEF:
if (isInterface || (((JCVariableDecl) t).mods.flags & (Flags.PROTECTED | Flags.PUBLIC)) != 0 || (((JCVariableDecl) t).mods.flags & (Flags.PRIVATE)) == 0 && ((JCVariableDecl) t).sym.packge().getQualifiedName() == names.java_lang)
newdefs.append(t);
break;
default:
break;
}
}
tree.defs = newdefs.toList();
super.visitClassDef(tree);
}
}
MethodBodyRemover r = new MethodBodyRemover();
return r.translate(cdef);
}
use of com.sun.tools.javac.tree.JCTree in project ceylon-compiler by ceylon.
the class Gen method normalizeDefs.
/* ************************************************************************
* Normalizing class-members.
*************************************************************************/
/** Distribute member initializer code into constructors and <clinit>
* method.
* @param defs The list of class member declarations.
* @param c The enclosing class.
*/
List<JCTree> normalizeDefs(List<JCTree> defs, ClassSymbol c) {
ListBuffer<JCStatement> initCode = new ListBuffer<JCStatement>();
ListBuffer<JCStatement> clinitCode = new ListBuffer<JCStatement>();
ListBuffer<JCTree> methodDefs = new ListBuffer<JCTree>();
// - methodDefs for method definitions
for (List<JCTree> l = defs; l.nonEmpty(); l = l.tail) {
JCTree def = l.head;
switch(def.getTag()) {
case JCTree.BLOCK:
JCBlock block = (JCBlock) def;
if ((block.flags & STATIC) != 0)
clinitCode.append(block);
else
initCode.append(block);
break;
case JCTree.METHODDEF:
methodDefs.append(def);
break;
case JCTree.VARDEF:
JCVariableDecl vdef = (JCVariableDecl) def;
VarSymbol sym = vdef.sym;
checkDimension(vdef.pos(), sym.type);
if (vdef.init != null) {
if ((sym.flags() & STATIC) == 0) {
// Always initialize instance variables.
JCStatement init = make.at(vdef.pos()).Assignment(sym, vdef.init);
initCode.append(init);
if (endPositions != null) {
Integer endPos = endPositions.remove(vdef);
if (endPos != null)
endPositions.put(init, endPos);
}
} else if (sym.getConstValue() == null) {
// Initialize class (static) variables only if
// they are not compile-time constants.
JCStatement init = make.at(vdef.pos).Assignment(sym, vdef.init);
clinitCode.append(init);
if (endPositions != null) {
Integer endPos = endPositions.remove(vdef);
if (endPos != null)
endPositions.put(init, endPos);
}
} else {
checkStringConstant(vdef.init.pos(), sym.getConstValue());
}
}
break;
default:
Assert.error();
}
}
// Insert any instance initializers into all constructors.
if (initCode.length() != 0) {
List<JCStatement> inits = initCode.toList();
for (JCTree t : methodDefs) {
normalizeMethod((JCMethodDecl) t, inits);
}
}
// that contains them as its body.
if (clinitCode.length() != 0) {
MethodSymbol clinit = new MethodSymbol(STATIC, names.clinit, new MethodType(List.<Type>nil(), syms.voidType, List.<Type>nil(), syms.methodClass), c);
c.members().enter(clinit);
List<JCStatement> clinitStats = clinitCode.toList();
JCBlock block = make.at(clinitStats.head.pos()).Block(0, clinitStats);
block.endpos = TreeInfo.endPos(clinitStats.last());
methodDefs.append(make.MethodDef(clinit, block));
}
// Return all method definitions.
return methodDefs.toList();
}
Aggregations