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