use of com.sun.tools.javac.comp.AttrContext 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.comp.AttrContext in project ceylon-compiler by ceylon.
the class DeclarationImpl method getPosition.
/**
* {@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);
}
use of com.sun.tools.javac.comp.AttrContext in project bazel by bazelbuild.
the class Resolver method findField.
/**
* Finds the field with name {@code name} in a given type.
*
* <p>
* The method adheres to all the rules of Java's scoping (while also
* considering the imports) for name resolution.
*
* @param name
* The name of the field.
* @param type
* The type of the receiver (i.e., the type in which to look for
* the field).
* @param path
* The tree path to the local scope.
* @return The element for the field.
*/
public VariableElement findField(String name, TypeMirror type, TreePath path) {
Log.DiagnosticHandler discardDiagnosticHandler = new Log.DiscardDiagnosticHandler(log);
try {
JavacScope scope = (JavacScope) trees.getScope(path);
Env<AttrContext> env = scope.getEnv();
Element res = wrapInvocation(FIND_IDENT_IN_TYPE, env, type, names.fromString(name), VAR);
if (res.getKind() == ElementKind.FIELD) {
return (VariableElement) res;
} else {
// Most likely didn't find the field and the Element is a SymbolNotFoundError
return null;
}
} finally {
log.popDiagnosticHandler(discardDiagnosticHandler);
}
}
use of com.sun.tools.javac.comp.AttrContext in project lombok by rzwitserloot.
the class JavacResolution method memberEnterAndAttribute.
private void memberEnterAndAttribute(JCTree copy, Env<AttrContext> env, Context context) {
MemberEnter memberEnter = MemberEnter.instance(context);
Env<AttrContext> oldEnv = getEnvOfMemberEnter(memberEnter);
setEnvOfMemberEnter(memberEnter, env);
try {
copy.accept(memberEnter);
} catch (Exception ignore) {
// intentionally ignored; usually even if this step fails, val will work (but not for val in method local inner classes and anonymous inner classes).
AssertionLogger.assertLog("member enter failed.", ignore);
} finally {
setEnvOfMemberEnter(memberEnter, oldEnv);
}
attrib(copy, env);
}
use of com.sun.tools.javac.comp.AttrContext in project ceylon-compiler by ceylon.
the class JavacTrees method getAttrContext.
private Env<AttrContext> getAttrContext(TreePath path) {
if (// implicit null-check
!(path.getLeaf() instanceof JCTree))
throw new IllegalArgumentException();
// then the classes will already have been entered.
if (javacTaskImpl != null) {
try {
javacTaskImpl.enter(null);
} catch (IOException e) {
throw new Error("unexpected error while entering symbols: " + e);
}
}
JCCompilationUnit unit = (JCCompilationUnit) path.getCompilationUnit();
Copier copier = new Copier(treeMaker.forToplevel(unit));
Env<AttrContext> env = null;
JCMethodDecl method = null;
JCVariableDecl field = null;
List<Tree> l = List.nil();
TreePath p = path;
while (p != null) {
l = l.prepend(p.getLeaf());
p = p.getParentPath();
}
for (; l.nonEmpty(); l = l.tail) {
Tree tree = l.head;
switch(tree.getKind()) {
case COMPILATION_UNIT:
// System.err.println("COMP: " + ((JCCompilationUnit)tree).sourcefile);
env = enter.getTopLevelEnv((JCCompilationUnit) tree);
break;
case ANNOTATION_TYPE:
case CLASS:
case ENUM:
case INTERFACE:
// System.err.println("CLASS: " + ((JCClassDecl)tree).sym.getSimpleName());
env = enter.getClassEnv(((JCClassDecl) tree).sym);
break;
case METHOD:
// System.err.println("METHOD: " + ((JCMethodDecl)tree).sym.getSimpleName());
method = (JCMethodDecl) tree;
break;
case VARIABLE:
// System.err.println("FIELD: " + ((JCVariableDecl)tree).sym.getSimpleName());
field = (JCVariableDecl) tree;
break;
case BLOCK:
{
// System.err.println("BLOCK: ");
if (method != null)
env = memberEnter.getMethodEnv(method, env);
JCTree body = copier.copy((JCTree) tree, (JCTree) path.getLeaf());
env = attribStatToTree(body, env, copier.leafCopy);
return env;
}
default:
// System.err.println("DEFAULT: " + tree.getKind());
if (field != null && field.getInitializer() == tree) {
env = memberEnter.getInitEnv(field, env);
JCExpression expr = copier.copy((JCExpression) tree, (JCTree) path.getLeaf());
env = attribExprToTree(expr, env, copier.leafCopy);
return env;
}
}
}
return field != null ? memberEnter.getInitEnv(field, env) : env;
}
Aggregations