langtools/src/share/classes/com/sun/tools/javac/api/JavacTrees.java
author jjg
Tue, 09 Sep 2008 10:28:21 -0700
changeset 1257 873b053bf757
parent 10 06bc494ca11e
child 1264 076a3cde30d5
permissions -rw-r--r--
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType Reviewed-by: mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
 * have any questions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javac.api;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import java.util.Map;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import javax.annotation.processing.ProcessingEnvironment;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import javax.lang.model.element.AnnotationMirror;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import javax.lang.model.element.AnnotationValue;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import javax.lang.model.element.Element;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import javax.lang.model.element.ExecutableElement;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import javax.lang.model.element.TypeElement;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import javax.lang.model.type.DeclaredType;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import javax.lang.model.type.TypeMirror;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import javax.tools.JavaCompiler;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import javax.tools.JavaFileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
import com.sun.source.tree.CompilationUnitTree;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
import com.sun.source.tree.Scope;
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
import com.sun.source.tree.Tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
import com.sun.source.util.SourcePositions;
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
import com.sun.source.util.TreePath;
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
import com.sun.source.util.Trees;
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
import com.sun.tools.javac.code.Symbol.ClassSymbol;
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
import com.sun.tools.javac.code.Symbol.TypeSymbol;
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
import com.sun.tools.javac.code.Symbol;
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
import com.sun.tools.javac.comp.Attr;
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
import com.sun.tools.javac.comp.AttrContext;
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
import com.sun.tools.javac.comp.Enter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
import com.sun.tools.javac.comp.Env;
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
import com.sun.tools.javac.comp.MemberEnter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
import com.sun.tools.javac.comp.Resolve;
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
import com.sun.tools.javac.model.JavacElements;
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
import com.sun.tools.javac.processing.JavacProcessingEnvironment;
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
import com.sun.tools.javac.tree.JCTree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
import com.sun.tools.javac.tree.JCTree;
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
import com.sun.tools.javac.tree.TreeCopier;
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
import com.sun.tools.javac.tree.TreeInfo;
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
import com.sun.tools.javac.tree.TreeMaker;
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
import com.sun.tools.javac.util.Context;
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
import com.sun.tools.javac.util.List;
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
import com.sun.tools.javac.util.Log;
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
import com.sun.tools.javac.util.Pair;
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
 * Provides an implementation of Trees.
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
 * <p><b>This is NOT part of any API supported by Sun Microsystems.
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
 * If you write code that depends on this, you do so at your own
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
 * risk.  This code and its internal interfaces are subject to change
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
 * or deletion without notice.</b></p>
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
 * @author Peter von der Ah&eacute;
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
public class JavacTrees extends Trees {
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
    private final Resolve resolve;
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    private final Enter enter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
    private final Log log;
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
    private final MemberEnter memberEnter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
    private final Attr attr;
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    private final TreeMaker treeMaker;
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
    private final JavacElements elements;
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
    private final JavacTaskImpl javacTaskImpl;
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
    public static JavacTrees instance(JavaCompiler.CompilationTask task) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
        if (!(task instanceof JavacTaskImpl))
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
            throw new IllegalArgumentException();
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
        return instance(((JavacTaskImpl)task).getContext());
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
    public static JavacTrees instance(ProcessingEnvironment env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        if (!(env instanceof JavacProcessingEnvironment))
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
            throw new IllegalArgumentException();
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
        return instance(((JavacProcessingEnvironment)env).getContext());
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
    public static JavacTrees instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
        JavacTrees instance = context.get(JavacTrees.class);
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
        if (instance == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
            instance = new JavacTrees(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
    private JavacTrees(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
        context.put(JavacTrees.class, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
        attr = Attr.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
        enter = Enter.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
        elements = JavacElements.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
        log = Log.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
        resolve = Resolve.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
        treeMaker = TreeMaker.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
        memberEnter = MemberEnter.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
        javacTaskImpl = context.get(JavacTaskImpl.class);
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
    public SourcePositions getSourcePositions() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
        return new SourcePositions() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
                public long getStartPosition(CompilationUnitTree file, Tree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
                    return TreeInfo.getStartPos((JCTree) tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
                public long getEndPosition(CompilationUnitTree file, Tree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
                    Map<JCTree,Integer> endPositions = ((JCCompilationUnit) file).endPositions;
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
                    return TreeInfo.getEndPos((JCTree) tree, endPositions);
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
            };
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
    public JCClassDecl getTree(TypeElement element) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
        return (JCClassDecl) getTree((Element) element);
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
    public JCMethodDecl getTree(ExecutableElement method) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
        return (JCMethodDecl) getTree((Element) method);
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
    public JCTree getTree(Element element) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
        Symbol symbol = (Symbol) element;
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
        TypeSymbol enclosing = symbol.enclClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
        Env<AttrContext> env = enter.getEnv(enclosing);
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
        if (env == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
        JCClassDecl classNode = env.enclClass;
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
        if (classNode != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
            if (TreeInfo.symbolFor(classNode) == element)
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
                return classNode;
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
            for (JCTree node : classNode.getMembers())
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
                if (TreeInfo.symbolFor(node) == element)
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
                    return node;
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
    public JCTree getTree(Element e, AnnotationMirror a) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
        return getTree(e, a, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
    public JCTree getTree(Element e, AnnotationMirror a, AnnotationValue v) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
        Pair<JCTree, JCCompilationUnit> treeTopLevel = elements.getTreeAndTopLevel(e, a, v);
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
        if (treeTopLevel == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
        return treeTopLevel.fst;
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
    public TreePath getPath(CompilationUnitTree unit, Tree node) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
        return TreePath.getPath(unit, node);
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
    public TreePath getPath(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
        return getPath(e, null, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
    public TreePath getPath(Element e, AnnotationMirror a) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
        return getPath(e, a, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
    public TreePath getPath(Element e, AnnotationMirror a, AnnotationValue v) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
        final Pair<JCTree, JCCompilationUnit> treeTopLevel = elements.getTreeAndTopLevel(e, a, v);
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
        if (treeTopLevel == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
        return TreePath.getPath(treeTopLevel.snd, treeTopLevel.fst);
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
    public Element getElement(TreePath path) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
        Tree t = path.getLeaf();
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
        return TreeInfo.symbolFor((JCTree) t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
    public TypeMirror getTypeMirror(TreePath path) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
        Tree t = path.getLeaf();
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
        return ((JCTree)t).type;
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
    public JavacScope getScope(TreePath path) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
        return new JavacScope(getAttrContext(path));
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
    public boolean isAccessible(Scope scope, TypeElement type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
        if (scope instanceof JavacScope && type instanceof ClassSymbol) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
            Env<AttrContext> env = ((JavacScope) scope).env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
            return resolve.isAccessible(env, (ClassSymbol)type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
        } else
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
    public boolean isAccessible(Scope scope, Element member, DeclaredType type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
        if (scope instanceof JavacScope
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
                && member instanceof Symbol
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
                && type instanceof com.sun.tools.javac.code.Type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
            Env<AttrContext> env = ((JavacScope) scope).env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
            return resolve.isAccessible(env, (com.sun.tools.javac.code.Type)type, (Symbol)member);
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
        } else
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
    private Env<AttrContext> getAttrContext(TreePath path) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
        if (!(path.getLeaf() instanceof JCTree))  // implicit null-check
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
            throw new IllegalArgumentException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
        // if we're being invoked via from a JSR199 client, we need to make sure
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
        // all the classes have been entered; if we're being invoked from JSR269,
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
        // then the classes will already have been entered.
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
        if (javacTaskImpl != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
                javacTaskImpl.enter(null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
            } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
                throw new Error("unexpected error while entering symbols: " + e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
        JCCompilationUnit unit = (JCCompilationUnit) path.getCompilationUnit();
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
        Copier copier = new Copier(treeMaker.forToplevel(unit));
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
        Env<AttrContext> env = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
        JCMethodDecl method = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
        JCVariableDecl field = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
        List<Tree> l = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
        TreePath p = path;
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
        while (p != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
            l = l.prepend(p.getLeaf());
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
            p = p.getParentPath();
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
        for ( ; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
            Tree tree = l.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
            switch (tree.getKind()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
                case COMPILATION_UNIT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
//                    System.err.println("COMP: " + ((JCCompilationUnit)tree).sourcefile);
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
                    env = enter.getTopLevelEnv((JCCompilationUnit)tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
                case CLASS:
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
//                    System.err.println("CLASS: " + ((JCClassDecl)tree).sym.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
                    env = enter.getClassEnv(((JCClassDecl)tree).sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
                case METHOD:
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
//                    System.err.println("METHOD: " + ((JCMethodDecl)tree).sym.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
                    method = (JCMethodDecl)tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
                case VARIABLE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
//                    System.err.println("FIELD: " + ((JCVariableDecl)tree).sym.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
                    field = (JCVariableDecl)tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
                case BLOCK: {
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
//                    System.err.println("BLOCK: ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
                    if (method != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
                        env = memberEnter.getMethodEnv(method, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
                    JCTree body = copier.copy((JCTree)tree, (JCTree) path.getLeaf());
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
                    env = attribStatToTree(body, env, copier.leafCopy);
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
                    return env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
                default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
//                    System.err.println("DEFAULT: " + tree.getKind());
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
                    if (field != null && field.getInitializer() == tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
                        env = memberEnter.getInitEnv(field, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
                        JCExpression expr = copier.copy((JCExpression)tree, (JCTree) path.getLeaf());
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
                        env = attribExprToTree(expr, env, copier.leafCopy);
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
                        return env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
        return field != null ? memberEnter.getInitEnv(field, env) : env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
    private Env<AttrContext> attribStatToTree(JCTree stat, Env<AttrContext>env, JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
        JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
            return attr.attribStatToTree(stat, env, tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
            log.useSource(prev);
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
    private Env<AttrContext> attribExprToTree(JCExpression expr, Env<AttrContext>env, JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
        JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
            return attr.attribExprToTree(expr, env, tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
            log.useSource(prev);
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
     * Makes a copy of a tree, noting the value resulting from copying a particular leaf.
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
     **/
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
    static class Copier extends TreeCopier<JCTree> {
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
        JCTree leafCopy = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
        Copier(TreeMaker M) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
            super(M);
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
        public <T extends JCTree> T copy(T t, JCTree leaf) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
            T t2 = super.copy(t, leaf);
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
            if (t == leaf)
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
                leafCopy = t2;
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
            return t2;
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
    }
1257
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   325
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   326
    /**
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   327
     * Gets the original type from the ErrorType object.
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   328
     * @param errorType The errorType for which we want to get the original type.
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   329
     * @returns TypeMirror corresponding to the original type, replaced by the ErrorType.
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   330
     *          noType (type.tag == NONE) is returned if there is no original type.
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   331
     */
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   332
    public TypeMirror getOriginalType(javax.lang.model.type.ErrorType errorType) {
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   333
        if (errorType instanceof com.sun.tools.javac.code.Type.ErrorType) {
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   334
            return ((com.sun.tools.javac.code.Type.ErrorType)errorType).getOriginalType();
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   335
        }
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   336
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   337
        return com.sun.tools.javac.code.Type.noType;
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   338
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
}