Wed, 20 May 2009 13:36:23 -0700
6827026: Change javac source and target default to 7
Reviewed-by: darcy, ohair
duke@1 | 1 | /* |
xdono@54 | 2 | * Copyright 2003-2008 Sun Microsystems, Inc. All Rights Reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@1 | 7 | * published by the Free Software Foundation. Sun designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
duke@1 | 9 | * by Sun in the LICENSE file that accompanied this code. |
duke@1 | 10 | * |
duke@1 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 15 | * accompanied this code). |
duke@1 | 16 | * |
duke@1 | 17 | * You should have received a copy of the GNU General Public License version |
duke@1 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 20 | * |
duke@1 | 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
duke@1 | 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
duke@1 | 23 | * have any questions. |
duke@1 | 24 | */ |
duke@1 | 25 | |
duke@1 | 26 | package com.sun.tools.javac.comp; |
duke@1 | 27 | |
duke@1 | 28 | import java.util.*; |
duke@1 | 29 | import java.util.Set; |
duke@1 | 30 | import javax.tools.JavaFileObject; |
duke@1 | 31 | |
duke@1 | 32 | import com.sun.tools.javac.code.*; |
duke@1 | 33 | import com.sun.tools.javac.jvm.*; |
duke@1 | 34 | import com.sun.tools.javac.tree.*; |
duke@1 | 35 | import com.sun.tools.javac.util.*; |
duke@1 | 36 | import com.sun.tools.javac.util.List; |
duke@1 | 37 | |
duke@1 | 38 | import com.sun.tools.javac.code.Type.*; |
duke@1 | 39 | import com.sun.tools.javac.code.Symbol.*; |
duke@1 | 40 | import com.sun.tools.javac.tree.JCTree.*; |
duke@1 | 41 | |
duke@1 | 42 | import static com.sun.tools.javac.code.Flags.*; |
duke@1 | 43 | import static com.sun.tools.javac.code.Kinds.*; |
duke@1 | 44 | import static com.sun.tools.javac.code.TypeTags.*; |
duke@1 | 45 | import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; |
duke@1 | 46 | |
duke@1 | 47 | /** This is the second phase of Enter, in which classes are completed |
duke@1 | 48 | * by entering their members into the class scope using |
duke@1 | 49 | * MemberEnter.complete(). See Enter for an overview. |
duke@1 | 50 | * |
duke@1 | 51 | * <p><b>This is NOT part of any API supported by Sun Microsystems. If |
duke@1 | 52 | * you write code that depends on this, you do so at your own risk. |
duke@1 | 53 | * This code and its internal interfaces are subject to change or |
duke@1 | 54 | * deletion without notice.</b> |
duke@1 | 55 | */ |
duke@1 | 56 | public class MemberEnter extends JCTree.Visitor implements Completer { |
duke@1 | 57 | protected static final Context.Key<MemberEnter> memberEnterKey = |
duke@1 | 58 | new Context.Key<MemberEnter>(); |
duke@1 | 59 | |
duke@1 | 60 | /** A switch to determine whether we check for package/class conflicts |
duke@1 | 61 | */ |
duke@1 | 62 | final static boolean checkClash = true; |
duke@1 | 63 | |
jjg@113 | 64 | private final Names names; |
duke@1 | 65 | private final Enter enter; |
duke@1 | 66 | private final Log log; |
duke@1 | 67 | private final Check chk; |
duke@1 | 68 | private final Attr attr; |
duke@1 | 69 | private final Symtab syms; |
duke@1 | 70 | private final TreeMaker make; |
duke@1 | 71 | private final ClassReader reader; |
duke@1 | 72 | private final Todo todo; |
duke@1 | 73 | private final Annotate annotate; |
duke@1 | 74 | private final Types types; |
mcimadamore@89 | 75 | private final JCDiagnostic.Factory diags; |
duke@1 | 76 | private final Target target; |
duke@1 | 77 | |
duke@1 | 78 | private final boolean skipAnnotations; |
duke@1 | 79 | |
duke@1 | 80 | public static MemberEnter instance(Context context) { |
duke@1 | 81 | MemberEnter instance = context.get(memberEnterKey); |
duke@1 | 82 | if (instance == null) |
duke@1 | 83 | instance = new MemberEnter(context); |
duke@1 | 84 | return instance; |
duke@1 | 85 | } |
duke@1 | 86 | |
duke@1 | 87 | protected MemberEnter(Context context) { |
duke@1 | 88 | context.put(memberEnterKey, this); |
jjg@113 | 89 | names = Names.instance(context); |
duke@1 | 90 | enter = Enter.instance(context); |
duke@1 | 91 | log = Log.instance(context); |
duke@1 | 92 | chk = Check.instance(context); |
duke@1 | 93 | attr = Attr.instance(context); |
duke@1 | 94 | syms = Symtab.instance(context); |
duke@1 | 95 | make = TreeMaker.instance(context); |
duke@1 | 96 | reader = ClassReader.instance(context); |
duke@1 | 97 | todo = Todo.instance(context); |
duke@1 | 98 | annotate = Annotate.instance(context); |
duke@1 | 99 | types = Types.instance(context); |
mcimadamore@89 | 100 | diags = JCDiagnostic.Factory.instance(context); |
duke@1 | 101 | target = Target.instance(context); |
duke@1 | 102 | skipAnnotations = |
duke@1 | 103 | Options.instance(context).get("skipAnnotations") != null; |
duke@1 | 104 | } |
duke@1 | 105 | |
duke@1 | 106 | /** A queue for classes whose members still need to be entered into the |
duke@1 | 107 | * symbol table. |
duke@1 | 108 | */ |
duke@1 | 109 | ListBuffer<Env<AttrContext>> halfcompleted = new ListBuffer<Env<AttrContext>>(); |
duke@1 | 110 | |
duke@1 | 111 | /** Set to true only when the first of a set of classes is |
duke@1 | 112 | * processed from the halfcompleted queue. |
duke@1 | 113 | */ |
duke@1 | 114 | boolean isFirst = true; |
duke@1 | 115 | |
duke@1 | 116 | /** A flag to disable completion from time to time during member |
duke@1 | 117 | * enter, as we only need to look up types. This avoids |
duke@1 | 118 | * unnecessarily deep recursion. |
duke@1 | 119 | */ |
duke@1 | 120 | boolean completionEnabled = true; |
duke@1 | 121 | |
duke@1 | 122 | /* ---------- Processing import clauses ---------------- |
duke@1 | 123 | */ |
duke@1 | 124 | |
duke@1 | 125 | /** Import all classes of a class or package on demand. |
duke@1 | 126 | * @param pos Position to be used for error reporting. |
duke@1 | 127 | * @param tsym The class or package the members of which are imported. |
duke@1 | 128 | * @param toScope The (import) scope in which imported classes |
duke@1 | 129 | * are entered. |
duke@1 | 130 | */ |
duke@1 | 131 | private void importAll(int pos, |
duke@1 | 132 | final TypeSymbol tsym, |
duke@1 | 133 | Env<AttrContext> env) { |
duke@1 | 134 | // Check that packages imported from exist (JLS ???). |
duke@1 | 135 | if (tsym.kind == PCK && tsym.members().elems == null && !tsym.exists()) { |
duke@1 | 136 | // If we can't find java.lang, exit immediately. |
duke@1 | 137 | if (((PackageSymbol)tsym).fullname.equals(names.java_lang)) { |
mcimadamore@89 | 138 | JCDiagnostic msg = diags.fragment("fatal.err.no.java.lang"); |
duke@1 | 139 | throw new FatalError(msg); |
duke@1 | 140 | } else { |
duke@1 | 141 | log.error(pos, "doesnt.exist", tsym); |
duke@1 | 142 | } |
duke@1 | 143 | } |
duke@1 | 144 | final Scope fromScope = tsym.members(); |
duke@1 | 145 | final Scope toScope = env.toplevel.starImportScope; |
duke@1 | 146 | for (Scope.Entry e = fromScope.elems; e != null; e = e.sibling) { |
duke@1 | 147 | if (e.sym.kind == TYP && !toScope.includes(e.sym)) |
duke@1 | 148 | toScope.enter(e.sym, fromScope); |
duke@1 | 149 | } |
duke@1 | 150 | } |
duke@1 | 151 | |
duke@1 | 152 | /** Import all static members of a class or package on demand. |
duke@1 | 153 | * @param pos Position to be used for error reporting. |
duke@1 | 154 | * @param tsym The class or package the members of which are imported. |
duke@1 | 155 | * @param toScope The (import) scope in which imported classes |
duke@1 | 156 | * are entered. |
duke@1 | 157 | */ |
duke@1 | 158 | private void importStaticAll(int pos, |
duke@1 | 159 | final TypeSymbol tsym, |
duke@1 | 160 | Env<AttrContext> env) { |
duke@1 | 161 | final JavaFileObject sourcefile = env.toplevel.sourcefile; |
duke@1 | 162 | final Scope toScope = env.toplevel.starImportScope; |
duke@1 | 163 | final PackageSymbol packge = env.toplevel.packge; |
duke@1 | 164 | final TypeSymbol origin = tsym; |
duke@1 | 165 | |
duke@1 | 166 | // enter imported types immediately |
duke@1 | 167 | new Object() { |
duke@1 | 168 | Set<Symbol> processed = new HashSet<Symbol>(); |
duke@1 | 169 | void importFrom(TypeSymbol tsym) { |
duke@1 | 170 | if (tsym == null || !processed.add(tsym)) |
duke@1 | 171 | return; |
duke@1 | 172 | |
duke@1 | 173 | // also import inherited names |
duke@1 | 174 | importFrom(types.supertype(tsym.type).tsym); |
duke@1 | 175 | for (Type t : types.interfaces(tsym.type)) |
duke@1 | 176 | importFrom(t.tsym); |
duke@1 | 177 | |
duke@1 | 178 | final Scope fromScope = tsym.members(); |
duke@1 | 179 | for (Scope.Entry e = fromScope.elems; e != null; e = e.sibling) { |
duke@1 | 180 | Symbol sym = e.sym; |
duke@1 | 181 | if (sym.kind == TYP && |
duke@1 | 182 | (sym.flags() & STATIC) != 0 && |
duke@1 | 183 | staticImportAccessible(sym, packge) && |
duke@1 | 184 | sym.isMemberOf(origin, types) && |
duke@1 | 185 | !toScope.includes(sym)) |
duke@1 | 186 | toScope.enter(sym, fromScope, origin.members()); |
duke@1 | 187 | } |
duke@1 | 188 | } |
duke@1 | 189 | }.importFrom(tsym); |
duke@1 | 190 | |
duke@1 | 191 | // enter non-types before annotations that might use them |
duke@1 | 192 | annotate.earlier(new Annotate.Annotator() { |
duke@1 | 193 | Set<Symbol> processed = new HashSet<Symbol>(); |
duke@1 | 194 | |
duke@1 | 195 | public String toString() { |
duke@1 | 196 | return "import static " + tsym + ".*" + " in " + sourcefile; |
duke@1 | 197 | } |
duke@1 | 198 | void importFrom(TypeSymbol tsym) { |
duke@1 | 199 | if (tsym == null || !processed.add(tsym)) |
duke@1 | 200 | return; |
duke@1 | 201 | |
duke@1 | 202 | // also import inherited names |
duke@1 | 203 | importFrom(types.supertype(tsym.type).tsym); |
duke@1 | 204 | for (Type t : types.interfaces(tsym.type)) |
duke@1 | 205 | importFrom(t.tsym); |
duke@1 | 206 | |
duke@1 | 207 | final Scope fromScope = tsym.members(); |
duke@1 | 208 | for (Scope.Entry e = fromScope.elems; e != null; e = e.sibling) { |
duke@1 | 209 | Symbol sym = e.sym; |
duke@1 | 210 | if (sym.isStatic() && sym.kind != TYP && |
duke@1 | 211 | staticImportAccessible(sym, packge) && |
duke@1 | 212 | !toScope.includes(sym) && |
duke@1 | 213 | sym.isMemberOf(origin, types)) { |
duke@1 | 214 | toScope.enter(sym, fromScope, origin.members()); |
duke@1 | 215 | } |
duke@1 | 216 | } |
duke@1 | 217 | } |
duke@1 | 218 | public void enterAnnotation() { |
duke@1 | 219 | importFrom(tsym); |
duke@1 | 220 | } |
duke@1 | 221 | }); |
duke@1 | 222 | } |
duke@1 | 223 | |
duke@1 | 224 | // is the sym accessible everywhere in packge? |
duke@1 | 225 | boolean staticImportAccessible(Symbol sym, PackageSymbol packge) { |
duke@1 | 226 | int flags = (int)(sym.flags() & AccessFlags); |
duke@1 | 227 | switch (flags) { |
duke@1 | 228 | default: |
duke@1 | 229 | case PUBLIC: |
duke@1 | 230 | return true; |
duke@1 | 231 | case PRIVATE: |
duke@1 | 232 | return false; |
duke@1 | 233 | case 0: |
duke@1 | 234 | case PROTECTED: |
duke@1 | 235 | return sym.packge() == packge; |
duke@1 | 236 | } |
duke@1 | 237 | } |
duke@1 | 238 | |
duke@1 | 239 | /** Import statics types of a given name. Non-types are handled in Attr. |
duke@1 | 240 | * @param pos Position to be used for error reporting. |
duke@1 | 241 | * @param tsym The class from which the name is imported. |
duke@1 | 242 | * @param name The (simple) name being imported. |
duke@1 | 243 | * @param env The environment containing the named import |
duke@1 | 244 | * scope to add to. |
duke@1 | 245 | */ |
duke@1 | 246 | private void importNamedStatic(final DiagnosticPosition pos, |
duke@1 | 247 | final TypeSymbol tsym, |
duke@1 | 248 | final Name name, |
duke@1 | 249 | final Env<AttrContext> env) { |
duke@1 | 250 | if (tsym.kind != TYP) { |
duke@1 | 251 | log.error(pos, "static.imp.only.classes.and.interfaces"); |
duke@1 | 252 | return; |
duke@1 | 253 | } |
duke@1 | 254 | |
duke@1 | 255 | final Scope toScope = env.toplevel.namedImportScope; |
duke@1 | 256 | final PackageSymbol packge = env.toplevel.packge; |
duke@1 | 257 | final TypeSymbol origin = tsym; |
duke@1 | 258 | |
duke@1 | 259 | // enter imported types immediately |
duke@1 | 260 | new Object() { |
duke@1 | 261 | Set<Symbol> processed = new HashSet<Symbol>(); |
duke@1 | 262 | void importFrom(TypeSymbol tsym) { |
duke@1 | 263 | if (tsym == null || !processed.add(tsym)) |
duke@1 | 264 | return; |
duke@1 | 265 | |
duke@1 | 266 | // also import inherited names |
duke@1 | 267 | importFrom(types.supertype(tsym.type).tsym); |
duke@1 | 268 | for (Type t : types.interfaces(tsym.type)) |
duke@1 | 269 | importFrom(t.tsym); |
duke@1 | 270 | |
duke@1 | 271 | for (Scope.Entry e = tsym.members().lookup(name); |
duke@1 | 272 | e.scope != null; |
duke@1 | 273 | e = e.next()) { |
duke@1 | 274 | Symbol sym = e.sym; |
duke@1 | 275 | if (sym.isStatic() && |
duke@1 | 276 | sym.kind == TYP && |
duke@1 | 277 | staticImportAccessible(sym, packge) && |
duke@1 | 278 | sym.isMemberOf(origin, types) && |
duke@1 | 279 | chk.checkUniqueStaticImport(pos, sym, toScope)) |
duke@1 | 280 | toScope.enter(sym, sym.owner.members(), origin.members()); |
duke@1 | 281 | } |
duke@1 | 282 | } |
duke@1 | 283 | }.importFrom(tsym); |
duke@1 | 284 | |
duke@1 | 285 | // enter non-types before annotations that might use them |
duke@1 | 286 | annotate.earlier(new Annotate.Annotator() { |
duke@1 | 287 | Set<Symbol> processed = new HashSet<Symbol>(); |
duke@1 | 288 | boolean found = false; |
duke@1 | 289 | |
duke@1 | 290 | public String toString() { |
duke@1 | 291 | return "import static " + tsym + "." + name; |
duke@1 | 292 | } |
duke@1 | 293 | void importFrom(TypeSymbol tsym) { |
duke@1 | 294 | if (tsym == null || !processed.add(tsym)) |
duke@1 | 295 | return; |
duke@1 | 296 | |
duke@1 | 297 | // also import inherited names |
duke@1 | 298 | importFrom(types.supertype(tsym.type).tsym); |
duke@1 | 299 | for (Type t : types.interfaces(tsym.type)) |
duke@1 | 300 | importFrom(t.tsym); |
duke@1 | 301 | |
duke@1 | 302 | for (Scope.Entry e = tsym.members().lookup(name); |
duke@1 | 303 | e.scope != null; |
duke@1 | 304 | e = e.next()) { |
duke@1 | 305 | Symbol sym = e.sym; |
duke@1 | 306 | if (sym.isStatic() && |
duke@1 | 307 | staticImportAccessible(sym, packge) && |
duke@1 | 308 | sym.isMemberOf(origin, types)) { |
duke@1 | 309 | found = true; |
duke@1 | 310 | if (sym.kind == MTH || |
duke@1 | 311 | sym.kind != TYP && chk.checkUniqueStaticImport(pos, sym, toScope)) |
duke@1 | 312 | toScope.enter(sym, sym.owner.members(), origin.members()); |
duke@1 | 313 | } |
duke@1 | 314 | } |
duke@1 | 315 | } |
duke@1 | 316 | public void enterAnnotation() { |
duke@1 | 317 | JavaFileObject prev = log.useSource(env.toplevel.sourcefile); |
duke@1 | 318 | try { |
duke@1 | 319 | importFrom(tsym); |
duke@1 | 320 | if (!found) { |
duke@1 | 321 | log.error(pos, "cant.resolve.location", |
mcimadamore@80 | 322 | KindName.STATIC, |
mcimadamore@80 | 323 | name, List.<Type>nil(), List.<Type>nil(), |
mcimadamore@89 | 324 | Kinds.typeKindName(tsym.type), |
duke@1 | 325 | tsym.type); |
duke@1 | 326 | } |
duke@1 | 327 | } finally { |
duke@1 | 328 | log.useSource(prev); |
duke@1 | 329 | } |
duke@1 | 330 | } |
duke@1 | 331 | }); |
duke@1 | 332 | } |
duke@1 | 333 | /** Import given class. |
duke@1 | 334 | * @param pos Position to be used for error reporting. |
duke@1 | 335 | * @param tsym The class to be imported. |
duke@1 | 336 | * @param env The environment containing the named import |
duke@1 | 337 | * scope to add to. |
duke@1 | 338 | */ |
duke@1 | 339 | private void importNamed(DiagnosticPosition pos, Symbol tsym, Env<AttrContext> env) { |
duke@1 | 340 | if (tsym.kind == TYP && |
duke@1 | 341 | chk.checkUniqueImport(pos, tsym, env.toplevel.namedImportScope)) |
duke@1 | 342 | env.toplevel.namedImportScope.enter(tsym, tsym.owner.members()); |
duke@1 | 343 | } |
duke@1 | 344 | |
duke@1 | 345 | /** Construct method type from method signature. |
duke@1 | 346 | * @param typarams The method's type parameters. |
duke@1 | 347 | * @param params The method's value parameters. |
duke@1 | 348 | * @param res The method's result type, |
duke@1 | 349 | * null if it is a constructor. |
duke@1 | 350 | * @param thrown The method's thrown exceptions. |
duke@1 | 351 | * @param env The method's (local) environment. |
duke@1 | 352 | */ |
duke@1 | 353 | Type signature(List<JCTypeParameter> typarams, |
duke@1 | 354 | List<JCVariableDecl> params, |
duke@1 | 355 | JCTree res, |
duke@1 | 356 | List<JCExpression> thrown, |
duke@1 | 357 | Env<AttrContext> env) { |
duke@1 | 358 | |
duke@1 | 359 | // Enter and attribute type parameters. |
duke@1 | 360 | List<Type> tvars = enter.classEnter(typarams, env); |
duke@1 | 361 | attr.attribTypeVariables(typarams, env); |
duke@1 | 362 | |
duke@1 | 363 | // Enter and attribute value parameters. |
duke@1 | 364 | ListBuffer<Type> argbuf = new ListBuffer<Type>(); |
duke@1 | 365 | for (List<JCVariableDecl> l = params; l.nonEmpty(); l = l.tail) { |
duke@1 | 366 | memberEnter(l.head, env); |
duke@1 | 367 | argbuf.append(l.head.vartype.type); |
duke@1 | 368 | } |
duke@1 | 369 | |
duke@1 | 370 | // Attribute result type, if one is given. |
duke@1 | 371 | Type restype = res == null ? syms.voidType : attr.attribType(res, env); |
duke@1 | 372 | |
duke@1 | 373 | // Attribute thrown exceptions. |
duke@1 | 374 | ListBuffer<Type> thrownbuf = new ListBuffer<Type>(); |
duke@1 | 375 | for (List<JCExpression> l = thrown; l.nonEmpty(); l = l.tail) { |
duke@1 | 376 | Type exc = attr.attribType(l.head, env); |
duke@1 | 377 | if (exc.tag != TYPEVAR) |
duke@1 | 378 | exc = chk.checkClassType(l.head.pos(), exc); |
duke@1 | 379 | thrownbuf.append(exc); |
duke@1 | 380 | } |
duke@1 | 381 | Type mtype = new MethodType(argbuf.toList(), |
duke@1 | 382 | restype, |
duke@1 | 383 | thrownbuf.toList(), |
duke@1 | 384 | syms.methodClass); |
duke@1 | 385 | return tvars.isEmpty() ? mtype : new ForAll(tvars, mtype); |
duke@1 | 386 | } |
duke@1 | 387 | |
duke@1 | 388 | /* ******************************************************************** |
duke@1 | 389 | * Visitor methods for member enter |
duke@1 | 390 | *********************************************************************/ |
duke@1 | 391 | |
duke@1 | 392 | /** Visitor argument: the current environment |
duke@1 | 393 | */ |
duke@1 | 394 | protected Env<AttrContext> env; |
duke@1 | 395 | |
duke@1 | 396 | /** Enter field and method definitions and process import |
duke@1 | 397 | * clauses, catching any completion failure exceptions. |
duke@1 | 398 | */ |
duke@1 | 399 | protected void memberEnter(JCTree tree, Env<AttrContext> env) { |
duke@1 | 400 | Env<AttrContext> prevEnv = this.env; |
duke@1 | 401 | try { |
duke@1 | 402 | this.env = env; |
duke@1 | 403 | tree.accept(this); |
duke@1 | 404 | } catch (CompletionFailure ex) { |
duke@1 | 405 | chk.completionError(tree.pos(), ex); |
duke@1 | 406 | } finally { |
duke@1 | 407 | this.env = prevEnv; |
duke@1 | 408 | } |
duke@1 | 409 | } |
duke@1 | 410 | |
duke@1 | 411 | /** Enter members from a list of trees. |
duke@1 | 412 | */ |
duke@1 | 413 | void memberEnter(List<? extends JCTree> trees, Env<AttrContext> env) { |
duke@1 | 414 | for (List<? extends JCTree> l = trees; l.nonEmpty(); l = l.tail) |
duke@1 | 415 | memberEnter(l.head, env); |
duke@1 | 416 | } |
duke@1 | 417 | |
duke@1 | 418 | /** Enter members for a class. |
duke@1 | 419 | */ |
duke@1 | 420 | void finishClass(JCClassDecl tree, Env<AttrContext> env) { |
duke@1 | 421 | if ((tree.mods.flags & Flags.ENUM) != 0 && |
duke@1 | 422 | (types.supertype(tree.sym.type).tsym.flags() & Flags.ENUM) == 0) { |
duke@1 | 423 | addEnumMembers(tree, env); |
duke@1 | 424 | } |
duke@1 | 425 | memberEnter(tree.defs, env); |
duke@1 | 426 | } |
duke@1 | 427 | |
duke@1 | 428 | /** Add the implicit members for an enum type |
duke@1 | 429 | * to the symbol table. |
duke@1 | 430 | */ |
duke@1 | 431 | private void addEnumMembers(JCClassDecl tree, Env<AttrContext> env) { |
duke@1 | 432 | JCExpression valuesType = make.Type(new ArrayType(tree.sym.type, syms.arrayClass)); |
duke@1 | 433 | |
duke@1 | 434 | // public static T[] values() { return ???; } |
duke@1 | 435 | JCMethodDecl values = make. |
duke@1 | 436 | MethodDef(make.Modifiers(Flags.PUBLIC|Flags.STATIC), |
duke@1 | 437 | names.values, |
duke@1 | 438 | valuesType, |
duke@1 | 439 | List.<JCTypeParameter>nil(), |
duke@1 | 440 | List.<JCVariableDecl>nil(), |
duke@1 | 441 | List.<JCExpression>nil(), // thrown |
duke@1 | 442 | null, //make.Block(0, Tree.emptyList.prepend(make.Return(make.Ident(names._null)))), |
duke@1 | 443 | null); |
duke@1 | 444 | memberEnter(values, env); |
duke@1 | 445 | |
duke@1 | 446 | // public static T valueOf(String name) { return ???; } |
duke@1 | 447 | JCMethodDecl valueOf = make. |
duke@1 | 448 | MethodDef(make.Modifiers(Flags.PUBLIC|Flags.STATIC), |
duke@1 | 449 | names.valueOf, |
duke@1 | 450 | make.Type(tree.sym.type), |
duke@1 | 451 | List.<JCTypeParameter>nil(), |
duke@1 | 452 | List.of(make.VarDef(make.Modifiers(Flags.PARAMETER), |
duke@1 | 453 | names.fromString("name"), |
duke@1 | 454 | make.Type(syms.stringType), null)), |
duke@1 | 455 | List.<JCExpression>nil(), // thrown |
duke@1 | 456 | null, //make.Block(0, Tree.emptyList.prepend(make.Return(make.Ident(names._null)))), |
duke@1 | 457 | null); |
duke@1 | 458 | memberEnter(valueOf, env); |
duke@1 | 459 | |
duke@1 | 460 | // the remaining members are for bootstrapping only |
duke@1 | 461 | if (!target.compilerBootstrap(tree.sym)) return; |
duke@1 | 462 | |
duke@1 | 463 | // public final int ordinal() { return ???; } |
duke@1 | 464 | JCMethodDecl ordinal = make.at(tree.pos). |
duke@1 | 465 | MethodDef(make.Modifiers(Flags.PUBLIC|Flags.FINAL), |
duke@1 | 466 | names.ordinal, |
duke@1 | 467 | make.Type(syms.intType), |
duke@1 | 468 | List.<JCTypeParameter>nil(), |
duke@1 | 469 | List.<JCVariableDecl>nil(), |
duke@1 | 470 | List.<JCExpression>nil(), |
duke@1 | 471 | null, |
duke@1 | 472 | null); |
duke@1 | 473 | memberEnter(ordinal, env); |
duke@1 | 474 | |
duke@1 | 475 | // public final String name() { return ???; } |
duke@1 | 476 | JCMethodDecl name = make. |
duke@1 | 477 | MethodDef(make.Modifiers(Flags.PUBLIC|Flags.FINAL), |
duke@1 | 478 | names._name, |
duke@1 | 479 | make.Type(syms.stringType), |
duke@1 | 480 | List.<JCTypeParameter>nil(), |
duke@1 | 481 | List.<JCVariableDecl>nil(), |
duke@1 | 482 | List.<JCExpression>nil(), |
duke@1 | 483 | null, |
duke@1 | 484 | null); |
duke@1 | 485 | memberEnter(name, env); |
duke@1 | 486 | |
duke@1 | 487 | // public int compareTo(E other) { return ???; } |
duke@1 | 488 | MethodSymbol compareTo = new |
duke@1 | 489 | MethodSymbol(Flags.PUBLIC, |
duke@1 | 490 | names.compareTo, |
duke@1 | 491 | new MethodType(List.of(tree.sym.type), |
duke@1 | 492 | syms.intType, |
duke@1 | 493 | List.<Type>nil(), |
duke@1 | 494 | syms.methodClass), |
duke@1 | 495 | tree.sym); |
duke@1 | 496 | memberEnter(make.MethodDef(compareTo, null), env); |
duke@1 | 497 | } |
duke@1 | 498 | |
duke@1 | 499 | public void visitTopLevel(JCCompilationUnit tree) { |
duke@1 | 500 | if (tree.starImportScope.elems != null) { |
duke@1 | 501 | // we must have already processed this toplevel |
duke@1 | 502 | return; |
duke@1 | 503 | } |
duke@1 | 504 | |
duke@1 | 505 | // check that no class exists with same fully qualified name as |
duke@1 | 506 | // toplevel package |
duke@1 | 507 | if (checkClash && tree.pid != null) { |
duke@1 | 508 | Symbol p = tree.packge; |
duke@1 | 509 | while (p.owner != syms.rootPackage) { |
duke@1 | 510 | p.owner.complete(); // enter all class members of p |
duke@1 | 511 | if (syms.classes.get(p.getQualifiedName()) != null) { |
duke@1 | 512 | log.error(tree.pos, |
duke@1 | 513 | "pkg.clashes.with.class.of.same.name", |
duke@1 | 514 | p); |
duke@1 | 515 | } |
duke@1 | 516 | p = p.owner; |
duke@1 | 517 | } |
duke@1 | 518 | } |
duke@1 | 519 | |
duke@1 | 520 | // process package annotations |
duke@1 | 521 | annotateLater(tree.packageAnnotations, env, tree.packge); |
duke@1 | 522 | |
duke@1 | 523 | // Import-on-demand java.lang. |
duke@1 | 524 | importAll(tree.pos, reader.enterPackage(names.java_lang), env); |
duke@1 | 525 | |
duke@1 | 526 | // Process all import clauses. |
duke@1 | 527 | memberEnter(tree.defs, env); |
duke@1 | 528 | } |
duke@1 | 529 | |
duke@1 | 530 | // process the non-static imports and the static imports of types. |
duke@1 | 531 | public void visitImport(JCImport tree) { |
duke@1 | 532 | JCTree imp = tree.qualid; |
duke@1 | 533 | Name name = TreeInfo.name(imp); |
duke@1 | 534 | TypeSymbol p; |
duke@1 | 535 | |
duke@1 | 536 | // Create a local environment pointing to this tree to disable |
duke@1 | 537 | // effects of other imports in Resolve.findGlobalType |
duke@1 | 538 | Env<AttrContext> localEnv = env.dup(tree); |
duke@1 | 539 | |
duke@1 | 540 | // Attribute qualifying package or class. |
duke@1 | 541 | JCFieldAccess s = (JCFieldAccess) imp; |
duke@1 | 542 | p = attr. |
duke@1 | 543 | attribTree(s.selected, |
duke@1 | 544 | localEnv, |
duke@1 | 545 | tree.staticImport ? TYP : (TYP | PCK), |
duke@1 | 546 | Type.noType).tsym; |
duke@1 | 547 | if (name == names.asterisk) { |
duke@1 | 548 | // Import on demand. |
duke@1 | 549 | chk.checkCanonical(s.selected); |
duke@1 | 550 | if (tree.staticImport) |
duke@1 | 551 | importStaticAll(tree.pos, p, env); |
duke@1 | 552 | else |
duke@1 | 553 | importAll(tree.pos, p, env); |
duke@1 | 554 | } else { |
duke@1 | 555 | // Named type import. |
duke@1 | 556 | if (tree.staticImport) { |
duke@1 | 557 | importNamedStatic(tree.pos(), p, name, localEnv); |
duke@1 | 558 | chk.checkCanonical(s.selected); |
duke@1 | 559 | } else { |
duke@1 | 560 | TypeSymbol c = attribImportType(imp, localEnv).tsym; |
duke@1 | 561 | chk.checkCanonical(imp); |
duke@1 | 562 | importNamed(tree.pos(), c, env); |
duke@1 | 563 | } |
duke@1 | 564 | } |
duke@1 | 565 | } |
duke@1 | 566 | |
duke@1 | 567 | public void visitMethodDef(JCMethodDecl tree) { |
duke@1 | 568 | Scope enclScope = enter.enterScope(env); |
duke@1 | 569 | MethodSymbol m = new MethodSymbol(0, tree.name, null, enclScope.owner); |
duke@1 | 570 | m.flags_field = chk.checkFlags(tree.pos(), tree.mods.flags, m, tree); |
duke@1 | 571 | tree.sym = m; |
duke@1 | 572 | Env<AttrContext> localEnv = methodEnv(tree, env); |
duke@1 | 573 | |
duke@1 | 574 | // Compute the method type |
duke@1 | 575 | m.type = signature(tree.typarams, tree.params, |
duke@1 | 576 | tree.restype, tree.thrown, |
duke@1 | 577 | localEnv); |
duke@1 | 578 | |
duke@1 | 579 | // Set m.params |
duke@1 | 580 | ListBuffer<VarSymbol> params = new ListBuffer<VarSymbol>(); |
duke@1 | 581 | JCVariableDecl lastParam = null; |
duke@1 | 582 | for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) { |
duke@1 | 583 | JCVariableDecl param = lastParam = l.head; |
duke@1 | 584 | assert param.sym != null; |
duke@1 | 585 | params.append(param.sym); |
duke@1 | 586 | } |
duke@1 | 587 | m.params = params.toList(); |
duke@1 | 588 | |
duke@1 | 589 | // mark the method varargs, if necessary |
duke@1 | 590 | if (lastParam != null && (lastParam.mods.flags & Flags.VARARGS) != 0) |
duke@1 | 591 | m.flags_field |= Flags.VARARGS; |
duke@1 | 592 | |
duke@1 | 593 | localEnv.info.scope.leave(); |
duke@1 | 594 | if (chk.checkUnique(tree.pos(), m, enclScope)) { |
duke@1 | 595 | enclScope.enter(m); |
duke@1 | 596 | } |
duke@1 | 597 | annotateLater(tree.mods.annotations, localEnv, m); |
duke@1 | 598 | if (tree.defaultValue != null) |
duke@1 | 599 | annotateDefaultValueLater(tree.defaultValue, localEnv, m); |
duke@1 | 600 | } |
duke@1 | 601 | |
duke@1 | 602 | /** Create a fresh environment for method bodies. |
duke@1 | 603 | * @param tree The method definition. |
duke@1 | 604 | * @param env The environment current outside of the method definition. |
duke@1 | 605 | */ |
duke@1 | 606 | Env<AttrContext> methodEnv(JCMethodDecl tree, Env<AttrContext> env) { |
duke@1 | 607 | Env<AttrContext> localEnv = |
duke@1 | 608 | env.dup(tree, env.info.dup(env.info.scope.dupUnshared())); |
duke@1 | 609 | localEnv.enclMethod = tree; |
duke@1 | 610 | localEnv.info.scope.owner = tree.sym; |
duke@1 | 611 | if ((tree.mods.flags & STATIC) != 0) localEnv.info.staticLevel++; |
duke@1 | 612 | return localEnv; |
duke@1 | 613 | } |
duke@1 | 614 | |
duke@1 | 615 | public void visitVarDef(JCVariableDecl tree) { |
duke@1 | 616 | Env<AttrContext> localEnv = env; |
duke@1 | 617 | if ((tree.mods.flags & STATIC) != 0 || |
duke@1 | 618 | (env.info.scope.owner.flags() & INTERFACE) != 0) { |
duke@1 | 619 | localEnv = env.dup(tree, env.info.dup()); |
duke@1 | 620 | localEnv.info.staticLevel++; |
duke@1 | 621 | } |
duke@1 | 622 | attr.attribType(tree.vartype, localEnv); |
duke@1 | 623 | Scope enclScope = enter.enterScope(env); |
duke@1 | 624 | VarSymbol v = |
duke@1 | 625 | new VarSymbol(0, tree.name, tree.vartype.type, enclScope.owner); |
duke@1 | 626 | v.flags_field = chk.checkFlags(tree.pos(), tree.mods.flags, v, tree); |
duke@1 | 627 | tree.sym = v; |
duke@1 | 628 | if (tree.init != null) { |
duke@1 | 629 | v.flags_field |= HASINIT; |
mcimadamore@94 | 630 | if ((v.flags_field & FINAL) != 0 && tree.init.getTag() != JCTree.NEWCLASS) { |
mcimadamore@94 | 631 | Env<AttrContext> initEnv = getInitEnv(tree, env); |
mcimadamore@94 | 632 | initEnv.info.enclVar = v; |
mcimadamore@94 | 633 | v.setLazyConstValue(initEnv(tree, initEnv), log, attr, tree.init); |
mcimadamore@94 | 634 | } |
duke@1 | 635 | } |
duke@1 | 636 | if (chk.checkUnique(tree.pos(), v, enclScope)) { |
duke@1 | 637 | chk.checkTransparentVar(tree.pos(), v, enclScope); |
duke@1 | 638 | enclScope.enter(v); |
duke@1 | 639 | } |
duke@1 | 640 | annotateLater(tree.mods.annotations, localEnv, v); |
duke@1 | 641 | v.pos = tree.pos; |
duke@1 | 642 | } |
duke@1 | 643 | |
duke@1 | 644 | /** Create a fresh environment for a variable's initializer. |
duke@1 | 645 | * If the variable is a field, the owner of the environment's scope |
duke@1 | 646 | * is be the variable itself, otherwise the owner is the method |
duke@1 | 647 | * enclosing the variable definition. |
duke@1 | 648 | * |
duke@1 | 649 | * @param tree The variable definition. |
duke@1 | 650 | * @param env The environment current outside of the variable definition. |
duke@1 | 651 | */ |
duke@1 | 652 | Env<AttrContext> initEnv(JCVariableDecl tree, Env<AttrContext> env) { |
duke@1 | 653 | Env<AttrContext> localEnv = env.dupto(new AttrContextEnv(tree, env.info.dup())); |
duke@1 | 654 | if (tree.sym.owner.kind == TYP) { |
duke@1 | 655 | localEnv.info.scope = new Scope.DelegatedScope(env.info.scope); |
duke@1 | 656 | localEnv.info.scope.owner = tree.sym; |
duke@1 | 657 | } |
duke@1 | 658 | if ((tree.mods.flags & STATIC) != 0 || |
duke@1 | 659 | (env.enclClass.sym.flags() & INTERFACE) != 0) |
duke@1 | 660 | localEnv.info.staticLevel++; |
duke@1 | 661 | return localEnv; |
duke@1 | 662 | } |
duke@1 | 663 | |
duke@1 | 664 | /** Default member enter visitor method: do nothing |
duke@1 | 665 | */ |
duke@1 | 666 | public void visitTree(JCTree tree) { |
duke@1 | 667 | } |
duke@1 | 668 | |
duke@1 | 669 | |
duke@1 | 670 | public void visitErroneous(JCErroneous tree) { |
duke@1 | 671 | memberEnter(tree.errs, env); |
duke@1 | 672 | } |
duke@1 | 673 | |
duke@1 | 674 | public Env<AttrContext> getMethodEnv(JCMethodDecl tree, Env<AttrContext> env) { |
duke@1 | 675 | Env<AttrContext> mEnv = methodEnv(tree, env); |
duke@1 | 676 | mEnv.info.lint = mEnv.info.lint.augment(tree.sym.attributes_field, tree.sym.flags()); |
duke@1 | 677 | for (List<JCTypeParameter> l = tree.typarams; l.nonEmpty(); l = l.tail) |
duke@1 | 678 | mEnv.info.scope.enterIfAbsent(l.head.type.tsym); |
duke@1 | 679 | for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) |
duke@1 | 680 | mEnv.info.scope.enterIfAbsent(l.head.sym); |
duke@1 | 681 | return mEnv; |
duke@1 | 682 | } |
duke@1 | 683 | |
duke@1 | 684 | public Env<AttrContext> getInitEnv(JCVariableDecl tree, Env<AttrContext> env) { |
duke@1 | 685 | Env<AttrContext> iEnv = initEnv(tree, env); |
duke@1 | 686 | return iEnv; |
duke@1 | 687 | } |
duke@1 | 688 | |
duke@1 | 689 | /* ******************************************************************** |
duke@1 | 690 | * Type completion |
duke@1 | 691 | *********************************************************************/ |
duke@1 | 692 | |
duke@1 | 693 | Type attribImportType(JCTree tree, Env<AttrContext> env) { |
duke@1 | 694 | assert completionEnabled; |
duke@1 | 695 | try { |
duke@1 | 696 | // To prevent deep recursion, suppress completion of some |
duke@1 | 697 | // types. |
duke@1 | 698 | completionEnabled = false; |
duke@1 | 699 | return attr.attribType(tree, env); |
duke@1 | 700 | } finally { |
duke@1 | 701 | completionEnabled = true; |
duke@1 | 702 | } |
duke@1 | 703 | } |
duke@1 | 704 | |
duke@1 | 705 | /* ******************************************************************** |
duke@1 | 706 | * Annotation processing |
duke@1 | 707 | *********************************************************************/ |
duke@1 | 708 | |
duke@1 | 709 | /** Queue annotations for later processing. */ |
duke@1 | 710 | void annotateLater(final List<JCAnnotation> annotations, |
duke@1 | 711 | final Env<AttrContext> localEnv, |
duke@1 | 712 | final Symbol s) { |
duke@1 | 713 | if (annotations.isEmpty()) return; |
duke@1 | 714 | if (s.kind != PCK) s.attributes_field = null; // mark it incomplete for now |
duke@1 | 715 | annotate.later(new Annotate.Annotator() { |
duke@1 | 716 | public String toString() { |
duke@1 | 717 | return "annotate " + annotations + " onto " + s + " in " + s.owner; |
duke@1 | 718 | } |
duke@1 | 719 | public void enterAnnotation() { |
duke@1 | 720 | assert s.kind == PCK || s.attributes_field == null; |
duke@1 | 721 | JavaFileObject prev = log.useSource(localEnv.toplevel.sourcefile); |
duke@1 | 722 | try { |
duke@1 | 723 | if (s.attributes_field != null && |
duke@1 | 724 | s.attributes_field.nonEmpty() && |
duke@1 | 725 | annotations.nonEmpty()) |
duke@1 | 726 | log.error(annotations.head.pos, |
duke@1 | 727 | "already.annotated", |
mcimadamore@80 | 728 | kindName(s), s); |
duke@1 | 729 | enterAnnotations(annotations, localEnv, s); |
duke@1 | 730 | } finally { |
duke@1 | 731 | log.useSource(prev); |
duke@1 | 732 | } |
duke@1 | 733 | } |
duke@1 | 734 | }); |
duke@1 | 735 | } |
duke@1 | 736 | |
duke@1 | 737 | /** |
duke@1 | 738 | * Check if a list of annotations contains a reference to |
duke@1 | 739 | * java.lang.Deprecated. |
duke@1 | 740 | **/ |
duke@1 | 741 | private boolean hasDeprecatedAnnotation(List<JCAnnotation> annotations) { |
duke@1 | 742 | for (List<JCAnnotation> al = annotations; al.nonEmpty(); al = al.tail) { |
duke@1 | 743 | JCAnnotation a = al.head; |
duke@1 | 744 | if (a.annotationType.type == syms.deprecatedType && a.args.isEmpty()) |
duke@1 | 745 | return true; |
duke@1 | 746 | } |
duke@1 | 747 | return false; |
duke@1 | 748 | } |
duke@1 | 749 | |
duke@1 | 750 | |
duke@1 | 751 | /** Enter a set of annotations. */ |
duke@1 | 752 | private void enterAnnotations(List<JCAnnotation> annotations, |
duke@1 | 753 | Env<AttrContext> env, |
duke@1 | 754 | Symbol s) { |
duke@1 | 755 | ListBuffer<Attribute.Compound> buf = |
duke@1 | 756 | new ListBuffer<Attribute.Compound>(); |
duke@1 | 757 | Set<TypeSymbol> annotated = new HashSet<TypeSymbol>(); |
duke@1 | 758 | if (!skipAnnotations) |
duke@1 | 759 | for (List<JCAnnotation> al = annotations; al.nonEmpty(); al = al.tail) { |
duke@1 | 760 | JCAnnotation a = al.head; |
duke@1 | 761 | Attribute.Compound c = annotate.enterAnnotation(a, |
duke@1 | 762 | syms.annotationType, |
duke@1 | 763 | env); |
duke@1 | 764 | if (c == null) continue; |
duke@1 | 765 | buf.append(c); |
duke@1 | 766 | // Note: @Deprecated has no effect on local variables and parameters |
duke@1 | 767 | if (!c.type.isErroneous() |
duke@1 | 768 | && s.owner.kind != MTH |
duke@1 | 769 | && types.isSameType(c.type, syms.deprecatedType)) |
duke@1 | 770 | s.flags_field |= Flags.DEPRECATED; |
duke@1 | 771 | if (!annotated.add(a.type.tsym)) |
duke@1 | 772 | log.error(a.pos, "duplicate.annotation"); |
duke@1 | 773 | } |
duke@1 | 774 | s.attributes_field = buf.toList(); |
duke@1 | 775 | } |
duke@1 | 776 | |
duke@1 | 777 | /** Queue processing of an attribute default value. */ |
duke@1 | 778 | void annotateDefaultValueLater(final JCExpression defaultValue, |
duke@1 | 779 | final Env<AttrContext> localEnv, |
duke@1 | 780 | final MethodSymbol m) { |
duke@1 | 781 | annotate.later(new Annotate.Annotator() { |
duke@1 | 782 | public String toString() { |
duke@1 | 783 | return "annotate " + m.owner + "." + |
duke@1 | 784 | m + " default " + defaultValue; |
duke@1 | 785 | } |
duke@1 | 786 | public void enterAnnotation() { |
duke@1 | 787 | JavaFileObject prev = log.useSource(localEnv.toplevel.sourcefile); |
duke@1 | 788 | try { |
duke@1 | 789 | enterDefaultValue(defaultValue, localEnv, m); |
duke@1 | 790 | } finally { |
duke@1 | 791 | log.useSource(prev); |
duke@1 | 792 | } |
duke@1 | 793 | } |
duke@1 | 794 | }); |
duke@1 | 795 | } |
duke@1 | 796 | |
duke@1 | 797 | /** Enter a default value for an attribute method. */ |
duke@1 | 798 | private void enterDefaultValue(final JCExpression defaultValue, |
duke@1 | 799 | final Env<AttrContext> localEnv, |
duke@1 | 800 | final MethodSymbol m) { |
duke@1 | 801 | m.defaultValue = annotate.enterAttributeValue(m.type.getReturnType(), |
duke@1 | 802 | defaultValue, |
duke@1 | 803 | localEnv); |
duke@1 | 804 | } |
duke@1 | 805 | |
duke@1 | 806 | /* ******************************************************************** |
duke@1 | 807 | * Source completer |
duke@1 | 808 | *********************************************************************/ |
duke@1 | 809 | |
duke@1 | 810 | /** Complete entering a class. |
duke@1 | 811 | * @param sym The symbol of the class to be completed. |
duke@1 | 812 | */ |
duke@1 | 813 | public void complete(Symbol sym) throws CompletionFailure { |
duke@1 | 814 | // Suppress some (recursive) MemberEnter invocations |
duke@1 | 815 | if (!completionEnabled) { |
duke@1 | 816 | // Re-install same completer for next time around and return. |
duke@1 | 817 | assert (sym.flags() & Flags.COMPOUND) == 0; |
duke@1 | 818 | sym.completer = this; |
duke@1 | 819 | return; |
duke@1 | 820 | } |
duke@1 | 821 | |
duke@1 | 822 | ClassSymbol c = (ClassSymbol)sym; |
duke@1 | 823 | ClassType ct = (ClassType)c.type; |
duke@1 | 824 | Env<AttrContext> env = enter.typeEnvs.get(c); |
duke@1 | 825 | JCClassDecl tree = (JCClassDecl)env.tree; |
duke@1 | 826 | boolean wasFirst = isFirst; |
duke@1 | 827 | isFirst = false; |
duke@1 | 828 | |
duke@1 | 829 | JavaFileObject prev = log.useSource(env.toplevel.sourcefile); |
duke@1 | 830 | try { |
duke@1 | 831 | // Save class environment for later member enter (2) processing. |
duke@1 | 832 | halfcompleted.append(env); |
duke@1 | 833 | |
mcimadamore@92 | 834 | // Mark class as not yet attributed. |
mcimadamore@92 | 835 | c.flags_field |= UNATTRIBUTED; |
mcimadamore@92 | 836 | |
duke@1 | 837 | // If this is a toplevel-class, make sure any preceding import |
duke@1 | 838 | // clauses have been seen. |
duke@1 | 839 | if (c.owner.kind == PCK) { |
duke@1 | 840 | memberEnter(env.toplevel, env.enclosing(JCTree.TOPLEVEL)); |
duke@1 | 841 | todo.append(env); |
duke@1 | 842 | } |
duke@1 | 843 | |
duke@1 | 844 | if (c.owner.kind == TYP) |
duke@1 | 845 | c.owner.complete(); |
duke@1 | 846 | |
duke@1 | 847 | // create an environment for evaluating the base clauses |
duke@1 | 848 | Env<AttrContext> baseEnv = baseEnv(tree, env); |
duke@1 | 849 | |
duke@1 | 850 | // Determine supertype. |
duke@1 | 851 | Type supertype = |
duke@1 | 852 | (tree.extending != null) |
duke@1 | 853 | ? attr.attribBase(tree.extending, baseEnv, true, false, true) |
duke@1 | 854 | : ((tree.mods.flags & Flags.ENUM) != 0 && !target.compilerBootstrap(c)) |
duke@1 | 855 | ? attr.attribBase(enumBase(tree.pos, c), baseEnv, |
duke@1 | 856 | true, false, false) |
duke@1 | 857 | : (c.fullname == names.java_lang_Object) |
duke@1 | 858 | ? Type.noType |
duke@1 | 859 | : syms.objectType; |
duke@1 | 860 | ct.supertype_field = supertype; |
duke@1 | 861 | |
duke@1 | 862 | // Determine interfaces. |
duke@1 | 863 | ListBuffer<Type> interfaces = new ListBuffer<Type>(); |
duke@1 | 864 | Set<Type> interfaceSet = new HashSet<Type>(); |
duke@1 | 865 | List<JCExpression> interfaceTrees = tree.implementing; |
duke@1 | 866 | if ((tree.mods.flags & Flags.ENUM) != 0 && target.compilerBootstrap(c)) { |
duke@1 | 867 | // add interface Comparable<T> |
duke@1 | 868 | interfaceTrees = |
duke@1 | 869 | interfaceTrees.prepend(make.Type(new ClassType(syms.comparableType.getEnclosingType(), |
duke@1 | 870 | List.of(c.type), |
duke@1 | 871 | syms.comparableType.tsym))); |
duke@1 | 872 | // add interface Serializable |
duke@1 | 873 | interfaceTrees = |
duke@1 | 874 | interfaceTrees.prepend(make.Type(syms.serializableType)); |
duke@1 | 875 | } |
duke@1 | 876 | for (JCExpression iface : interfaceTrees) { |
duke@1 | 877 | Type i = attr.attribBase(iface, baseEnv, false, true, true); |
duke@1 | 878 | if (i.tag == CLASS) { |
duke@1 | 879 | interfaces.append(i); |
duke@1 | 880 | chk.checkNotRepeated(iface.pos(), types.erasure(i), interfaceSet); |
duke@1 | 881 | } |
duke@1 | 882 | } |
duke@1 | 883 | if ((c.flags_field & ANNOTATION) != 0) |
duke@1 | 884 | ct.interfaces_field = List.of(syms.annotationType); |
duke@1 | 885 | else |
duke@1 | 886 | ct.interfaces_field = interfaces.toList(); |
duke@1 | 887 | |
duke@1 | 888 | if (c.fullname == names.java_lang_Object) { |
duke@1 | 889 | if (tree.extending != null) { |
duke@1 | 890 | chk.checkNonCyclic(tree.extending.pos(), |
duke@1 | 891 | supertype); |
duke@1 | 892 | ct.supertype_field = Type.noType; |
duke@1 | 893 | } |
duke@1 | 894 | else if (tree.implementing.nonEmpty()) { |
duke@1 | 895 | chk.checkNonCyclic(tree.implementing.head.pos(), |
duke@1 | 896 | ct.interfaces_field.head); |
duke@1 | 897 | ct.interfaces_field = List.nil(); |
duke@1 | 898 | } |
duke@1 | 899 | } |
duke@1 | 900 | |
duke@1 | 901 | // Annotations. |
duke@1 | 902 | // In general, we cannot fully process annotations yet, but we |
duke@1 | 903 | // can attribute the annotation types and then check to see if the |
duke@1 | 904 | // @Deprecated annotation is present. |
duke@1 | 905 | attr.attribAnnotationTypes(tree.mods.annotations, baseEnv); |
duke@1 | 906 | if (hasDeprecatedAnnotation(tree.mods.annotations)) |
duke@1 | 907 | c.flags_field |= DEPRECATED; |
duke@1 | 908 | annotateLater(tree.mods.annotations, baseEnv, c); |
duke@1 | 909 | |
mcimadamore@8 | 910 | chk.checkNonCyclic(tree.pos(), c.type); |
mcimadamore@8 | 911 | |
duke@1 | 912 | attr.attribTypeVariables(tree.typarams, baseEnv); |
duke@1 | 913 | |
duke@1 | 914 | // Add default constructor if needed. |
duke@1 | 915 | if ((c.flags() & INTERFACE) == 0 && |
duke@1 | 916 | !TreeInfo.hasConstructors(tree.defs)) { |
duke@1 | 917 | List<Type> argtypes = List.nil(); |
duke@1 | 918 | List<Type> typarams = List.nil(); |
duke@1 | 919 | List<Type> thrown = List.nil(); |
duke@1 | 920 | long ctorFlags = 0; |
duke@1 | 921 | boolean based = false; |
jjg@113 | 922 | if (c.name.isEmpty()) { |
duke@1 | 923 | JCNewClass nc = (JCNewClass)env.next.tree; |
duke@1 | 924 | if (nc.constructor != null) { |
duke@1 | 925 | Type superConstrType = types.memberType(c.type, |
duke@1 | 926 | nc.constructor); |
duke@1 | 927 | argtypes = superConstrType.getParameterTypes(); |
duke@1 | 928 | typarams = superConstrType.getTypeArguments(); |
duke@1 | 929 | ctorFlags = nc.constructor.flags() & VARARGS; |
duke@1 | 930 | if (nc.encl != null) { |
duke@1 | 931 | argtypes = argtypes.prepend(nc.encl.type); |
duke@1 | 932 | based = true; |
duke@1 | 933 | } |
duke@1 | 934 | thrown = superConstrType.getThrownTypes(); |
duke@1 | 935 | } |
duke@1 | 936 | } |
duke@1 | 937 | JCTree constrDef = DefaultConstructor(make.at(tree.pos), c, |
duke@1 | 938 | typarams, argtypes, thrown, |
duke@1 | 939 | ctorFlags, based); |
duke@1 | 940 | tree.defs = tree.defs.prepend(constrDef); |
duke@1 | 941 | } |
duke@1 | 942 | |
duke@1 | 943 | // If this is a class, enter symbols for this and super into |
duke@1 | 944 | // current scope. |
duke@1 | 945 | if ((c.flags_field & INTERFACE) == 0) { |
duke@1 | 946 | VarSymbol thisSym = |
duke@1 | 947 | new VarSymbol(FINAL | HASINIT, names._this, c.type, c); |
duke@1 | 948 | thisSym.pos = Position.FIRSTPOS; |
duke@1 | 949 | env.info.scope.enter(thisSym); |
duke@1 | 950 | if (ct.supertype_field.tag == CLASS) { |
duke@1 | 951 | VarSymbol superSym = |
duke@1 | 952 | new VarSymbol(FINAL | HASINIT, names._super, |
duke@1 | 953 | ct.supertype_field, c); |
duke@1 | 954 | superSym.pos = Position.FIRSTPOS; |
duke@1 | 955 | env.info.scope.enter(superSym); |
duke@1 | 956 | } |
duke@1 | 957 | } |
duke@1 | 958 | |
duke@1 | 959 | // check that no package exists with same fully qualified name, |
duke@1 | 960 | // but admit classes in the unnamed package which have the same |
duke@1 | 961 | // name as a top-level package. |
duke@1 | 962 | if (checkClash && |
duke@1 | 963 | c.owner.kind == PCK && c.owner != syms.unnamedPackage && |
duke@1 | 964 | reader.packageExists(c.fullname)) |
duke@1 | 965 | { |
duke@1 | 966 | log.error(tree.pos, "clash.with.pkg.of.same.name", c); |
duke@1 | 967 | } |
duke@1 | 968 | |
duke@1 | 969 | } catch (CompletionFailure ex) { |
duke@1 | 970 | chk.completionError(tree.pos(), ex); |
duke@1 | 971 | } finally { |
duke@1 | 972 | log.useSource(prev); |
duke@1 | 973 | } |
duke@1 | 974 | |
duke@1 | 975 | // Enter all member fields and methods of a set of half completed |
duke@1 | 976 | // classes in a second phase. |
duke@1 | 977 | if (wasFirst) { |
duke@1 | 978 | try { |
duke@1 | 979 | while (halfcompleted.nonEmpty()) { |
duke@1 | 980 | finish(halfcompleted.next()); |
duke@1 | 981 | } |
duke@1 | 982 | } finally { |
duke@1 | 983 | isFirst = true; |
duke@1 | 984 | } |
duke@1 | 985 | |
duke@1 | 986 | // commit pending annotations |
duke@1 | 987 | annotate.flush(); |
duke@1 | 988 | } |
duke@1 | 989 | } |
duke@1 | 990 | |
duke@1 | 991 | private Env<AttrContext> baseEnv(JCClassDecl tree, Env<AttrContext> env) { |
duke@1 | 992 | Scope typaramScope = new Scope(tree.sym); |
duke@1 | 993 | if (tree.typarams != null) |
duke@1 | 994 | for (List<JCTypeParameter> typarams = tree.typarams; |
duke@1 | 995 | typarams.nonEmpty(); |
duke@1 | 996 | typarams = typarams.tail) |
duke@1 | 997 | typaramScope.enter(typarams.head.type.tsym); |
duke@1 | 998 | Env<AttrContext> outer = env.outer; // the base clause can't see members of this class |
duke@1 | 999 | Env<AttrContext> localEnv = outer.dup(tree, outer.info.dup(typaramScope)); |
duke@1 | 1000 | localEnv.baseClause = true; |
duke@1 | 1001 | localEnv.outer = outer; |
duke@1 | 1002 | localEnv.info.isSelfCall = false; |
duke@1 | 1003 | return localEnv; |
duke@1 | 1004 | } |
duke@1 | 1005 | |
duke@1 | 1006 | /** Enter member fields and methods of a class |
duke@1 | 1007 | * @param env the environment current for the class block. |
duke@1 | 1008 | */ |
duke@1 | 1009 | private void finish(Env<AttrContext> env) { |
duke@1 | 1010 | JavaFileObject prev = log.useSource(env.toplevel.sourcefile); |
duke@1 | 1011 | try { |
duke@1 | 1012 | JCClassDecl tree = (JCClassDecl)env.tree; |
duke@1 | 1013 | finishClass(tree, env); |
duke@1 | 1014 | } finally { |
duke@1 | 1015 | log.useSource(prev); |
duke@1 | 1016 | } |
duke@1 | 1017 | } |
duke@1 | 1018 | |
duke@1 | 1019 | /** Generate a base clause for an enum type. |
duke@1 | 1020 | * @param pos The position for trees and diagnostics, if any |
duke@1 | 1021 | * @param c The class symbol of the enum |
duke@1 | 1022 | */ |
duke@1 | 1023 | private JCExpression enumBase(int pos, ClassSymbol c) { |
duke@1 | 1024 | JCExpression result = make.at(pos). |
duke@1 | 1025 | TypeApply(make.QualIdent(syms.enumSym), |
duke@1 | 1026 | List.<JCExpression>of(make.Type(c.type))); |
duke@1 | 1027 | return result; |
duke@1 | 1028 | } |
duke@1 | 1029 | |
duke@1 | 1030 | /* *************************************************************************** |
duke@1 | 1031 | * tree building |
duke@1 | 1032 | ****************************************************************************/ |
duke@1 | 1033 | |
duke@1 | 1034 | /** Generate default constructor for given class. For classes different |
duke@1 | 1035 | * from java.lang.Object, this is: |
duke@1 | 1036 | * |
duke@1 | 1037 | * c(argtype_0 x_0, ..., argtype_n x_n) throws thrown { |
duke@1 | 1038 | * super(x_0, ..., x_n) |
duke@1 | 1039 | * } |
duke@1 | 1040 | * |
duke@1 | 1041 | * or, if based == true: |
duke@1 | 1042 | * |
duke@1 | 1043 | * c(argtype_0 x_0, ..., argtype_n x_n) throws thrown { |
duke@1 | 1044 | * x_0.super(x_1, ..., x_n) |
duke@1 | 1045 | * } |
duke@1 | 1046 | * |
duke@1 | 1047 | * @param make The tree factory. |
duke@1 | 1048 | * @param c The class owning the default constructor. |
duke@1 | 1049 | * @param argtypes The parameter types of the constructor. |
duke@1 | 1050 | * @param thrown The thrown exceptions of the constructor. |
duke@1 | 1051 | * @param based Is first parameter a this$n? |
duke@1 | 1052 | */ |
duke@1 | 1053 | JCTree DefaultConstructor(TreeMaker make, |
duke@1 | 1054 | ClassSymbol c, |
duke@1 | 1055 | List<Type> typarams, |
duke@1 | 1056 | List<Type> argtypes, |
duke@1 | 1057 | List<Type> thrown, |
duke@1 | 1058 | long flags, |
duke@1 | 1059 | boolean based) { |
duke@1 | 1060 | List<JCVariableDecl> params = make.Params(argtypes, syms.noSymbol); |
duke@1 | 1061 | List<JCStatement> stats = List.nil(); |
duke@1 | 1062 | if (c.type != syms.objectType) |
duke@1 | 1063 | stats = stats.prepend(SuperCall(make, typarams, params, based)); |
duke@1 | 1064 | if ((c.flags() & ENUM) != 0 && |
duke@1 | 1065 | (types.supertype(c.type).tsym == syms.enumSym || |
duke@1 | 1066 | target.compilerBootstrap(c))) { |
duke@1 | 1067 | // constructors of true enums are private |
duke@1 | 1068 | flags = (flags & ~AccessFlags) | PRIVATE | GENERATEDCONSTR; |
duke@1 | 1069 | } else |
duke@1 | 1070 | flags |= (c.flags() & AccessFlags) | GENERATEDCONSTR; |
jjg@113 | 1071 | if (c.name.isEmpty()) flags |= ANONCONSTR; |
duke@1 | 1072 | JCTree result = make.MethodDef( |
duke@1 | 1073 | make.Modifiers(flags), |
duke@1 | 1074 | names.init, |
duke@1 | 1075 | null, |
duke@1 | 1076 | make.TypeParams(typarams), |
duke@1 | 1077 | params, |
duke@1 | 1078 | make.Types(thrown), |
duke@1 | 1079 | make.Block(0, stats), |
duke@1 | 1080 | null); |
duke@1 | 1081 | return result; |
duke@1 | 1082 | } |
duke@1 | 1083 | |
duke@1 | 1084 | /** Generate call to superclass constructor. This is: |
duke@1 | 1085 | * |
duke@1 | 1086 | * super(id_0, ..., id_n) |
duke@1 | 1087 | * |
duke@1 | 1088 | * or, if based == true |
duke@1 | 1089 | * |
duke@1 | 1090 | * id_0.super(id_1,...,id_n) |
duke@1 | 1091 | * |
duke@1 | 1092 | * where id_0, ..., id_n are the names of the given parameters. |
duke@1 | 1093 | * |
duke@1 | 1094 | * @param make The tree factory |
duke@1 | 1095 | * @param params The parameters that need to be passed to super |
duke@1 | 1096 | * @param typarams The type parameters that need to be passed to super |
duke@1 | 1097 | * @param based Is first parameter a this$n? |
duke@1 | 1098 | */ |
duke@1 | 1099 | JCExpressionStatement SuperCall(TreeMaker make, |
duke@1 | 1100 | List<Type> typarams, |
duke@1 | 1101 | List<JCVariableDecl> params, |
duke@1 | 1102 | boolean based) { |
duke@1 | 1103 | JCExpression meth; |
duke@1 | 1104 | if (based) { |
duke@1 | 1105 | meth = make.Select(make.Ident(params.head), names._super); |
duke@1 | 1106 | params = params.tail; |
duke@1 | 1107 | } else { |
duke@1 | 1108 | meth = make.Ident(names._super); |
duke@1 | 1109 | } |
duke@1 | 1110 | List<JCExpression> typeargs = typarams.nonEmpty() ? make.Types(typarams) : null; |
duke@1 | 1111 | return make.Exec(make.Apply(typeargs, meth, make.Idents(params))); |
duke@1 | 1112 | } |
duke@1 | 1113 | } |