Wed, 27 Apr 2016 01:34:52 +0800
Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.tools.javac.comp; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.util.HashMap; |
aoqi@0 | 29 | import java.util.HashSet; |
aoqi@0 | 30 | import java.util.LinkedHashMap; |
aoqi@0 | 31 | import java.util.Map; |
aoqi@0 | 32 | import java.util.Set; |
aoqi@0 | 33 | |
aoqi@0 | 34 | import javax.tools.JavaFileObject; |
aoqi@0 | 35 | |
aoqi@0 | 36 | import com.sun.tools.javac.code.*; |
aoqi@0 | 37 | import com.sun.tools.javac.jvm.*; |
aoqi@0 | 38 | import com.sun.tools.javac.tree.*; |
aoqi@0 | 39 | import com.sun.tools.javac.util.*; |
aoqi@0 | 40 | |
aoqi@0 | 41 | import com.sun.tools.javac.code.Type.*; |
aoqi@0 | 42 | import com.sun.tools.javac.code.Symbol.*; |
aoqi@0 | 43 | import com.sun.tools.javac.tree.JCTree.*; |
aoqi@0 | 44 | |
aoqi@0 | 45 | import static com.sun.tools.javac.code.Flags.*; |
aoqi@0 | 46 | import static com.sun.tools.javac.code.Flags.ANNOTATION; |
aoqi@0 | 47 | import static com.sun.tools.javac.code.Kinds.*; |
aoqi@0 | 48 | import static com.sun.tools.javac.code.TypeTag.CLASS; |
aoqi@0 | 49 | import static com.sun.tools.javac.code.TypeTag.ERROR; |
aoqi@0 | 50 | import static com.sun.tools.javac.code.TypeTag.TYPEVAR; |
aoqi@0 | 51 | import static com.sun.tools.javac.tree.JCTree.Tag.*; |
aoqi@0 | 52 | import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag; |
aoqi@0 | 53 | import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; |
aoqi@0 | 54 | |
aoqi@0 | 55 | /** This is the second phase of Enter, in which classes are completed |
aoqi@0 | 56 | * by entering their members into the class scope using |
aoqi@0 | 57 | * MemberEnter.complete(). See Enter for an overview. |
aoqi@0 | 58 | * |
aoqi@0 | 59 | * <p><b>This is NOT part of any supported API. |
aoqi@0 | 60 | * If you write code that depends on this, you do so at your own risk. |
aoqi@0 | 61 | * This code and its internal interfaces are subject to change or |
aoqi@0 | 62 | * deletion without notice.</b> |
aoqi@0 | 63 | */ |
aoqi@0 | 64 | public class MemberEnter extends JCTree.Visitor implements Completer { |
aoqi@0 | 65 | protected static final Context.Key<MemberEnter> memberEnterKey = |
aoqi@0 | 66 | new Context.Key<MemberEnter>(); |
aoqi@0 | 67 | |
aoqi@0 | 68 | /** A switch to determine whether we check for package/class conflicts |
aoqi@0 | 69 | */ |
aoqi@0 | 70 | final static boolean checkClash = true; |
aoqi@0 | 71 | |
aoqi@0 | 72 | private final Names names; |
aoqi@0 | 73 | private final Enter enter; |
aoqi@0 | 74 | private final Log log; |
aoqi@0 | 75 | private final Check chk; |
aoqi@0 | 76 | private final Attr attr; |
aoqi@0 | 77 | private final Symtab syms; |
aoqi@0 | 78 | private final TreeMaker make; |
aoqi@0 | 79 | private final ClassReader reader; |
aoqi@0 | 80 | private final Todo todo; |
aoqi@0 | 81 | private final Annotate annotate; |
aoqi@0 | 82 | private final TypeAnnotations typeAnnotations; |
aoqi@0 | 83 | private final Types types; |
aoqi@0 | 84 | private final JCDiagnostic.Factory diags; |
aoqi@0 | 85 | private final Source source; |
aoqi@0 | 86 | private final Target target; |
aoqi@0 | 87 | private final DeferredLintHandler deferredLintHandler; |
aoqi@0 | 88 | private final Lint lint; |
aoqi@0 | 89 | private final TypeEnvs typeEnvs; |
aoqi@0 | 90 | |
aoqi@0 | 91 | public static MemberEnter instance(Context context) { |
aoqi@0 | 92 | MemberEnter instance = context.get(memberEnterKey); |
aoqi@0 | 93 | if (instance == null) |
aoqi@0 | 94 | instance = new MemberEnter(context); |
aoqi@0 | 95 | return instance; |
aoqi@0 | 96 | } |
aoqi@0 | 97 | |
aoqi@0 | 98 | protected MemberEnter(Context context) { |
aoqi@0 | 99 | context.put(memberEnterKey, this); |
aoqi@0 | 100 | names = Names.instance(context); |
aoqi@0 | 101 | enter = Enter.instance(context); |
aoqi@0 | 102 | log = Log.instance(context); |
aoqi@0 | 103 | chk = Check.instance(context); |
aoqi@0 | 104 | attr = Attr.instance(context); |
aoqi@0 | 105 | syms = Symtab.instance(context); |
aoqi@0 | 106 | make = TreeMaker.instance(context); |
aoqi@0 | 107 | reader = ClassReader.instance(context); |
aoqi@0 | 108 | todo = Todo.instance(context); |
aoqi@0 | 109 | annotate = Annotate.instance(context); |
aoqi@0 | 110 | typeAnnotations = TypeAnnotations.instance(context); |
aoqi@0 | 111 | types = Types.instance(context); |
aoqi@0 | 112 | diags = JCDiagnostic.Factory.instance(context); |
aoqi@0 | 113 | source = Source.instance(context); |
aoqi@0 | 114 | target = Target.instance(context); |
aoqi@0 | 115 | deferredLintHandler = DeferredLintHandler.instance(context); |
aoqi@0 | 116 | lint = Lint.instance(context); |
aoqi@0 | 117 | typeEnvs = TypeEnvs.instance(context); |
aoqi@0 | 118 | allowTypeAnnos = source.allowTypeAnnotations(); |
aoqi@0 | 119 | allowRepeatedAnnos = source.allowRepeatedAnnotations(); |
aoqi@0 | 120 | } |
aoqi@0 | 121 | |
aoqi@0 | 122 | /** Switch: support type annotations. |
aoqi@0 | 123 | */ |
aoqi@0 | 124 | boolean allowTypeAnnos; |
aoqi@0 | 125 | |
aoqi@0 | 126 | boolean allowRepeatedAnnos; |
aoqi@0 | 127 | |
aoqi@0 | 128 | /** A queue for classes whose members still need to be entered into the |
aoqi@0 | 129 | * symbol table. |
aoqi@0 | 130 | */ |
aoqi@0 | 131 | ListBuffer<Env<AttrContext>> halfcompleted = new ListBuffer<Env<AttrContext>>(); |
aoqi@0 | 132 | |
aoqi@0 | 133 | /** Set to true only when the first of a set of classes is |
aoqi@0 | 134 | * processed from the half completed queue. |
aoqi@0 | 135 | */ |
aoqi@0 | 136 | boolean isFirst = true; |
aoqi@0 | 137 | |
aoqi@0 | 138 | /** A flag to disable completion from time to time during member |
aoqi@0 | 139 | * enter, as we only need to look up types. This avoids |
aoqi@0 | 140 | * unnecessarily deep recursion. |
aoqi@0 | 141 | */ |
aoqi@0 | 142 | boolean completionEnabled = true; |
aoqi@0 | 143 | |
aoqi@0 | 144 | /* ---------- Processing import clauses ---------------- |
aoqi@0 | 145 | */ |
aoqi@0 | 146 | |
aoqi@0 | 147 | /** Import all classes of a class or package on demand. |
aoqi@0 | 148 | * @param pos Position to be used for error reporting. |
aoqi@0 | 149 | * @param tsym The class or package the members of which are imported. |
aoqi@0 | 150 | * @param env The env in which the imported classes will be entered. |
aoqi@0 | 151 | */ |
aoqi@0 | 152 | private void importAll(int pos, |
aoqi@0 | 153 | final TypeSymbol tsym, |
aoqi@0 | 154 | Env<AttrContext> env) { |
aoqi@0 | 155 | // Check that packages imported from exist (JLS ???). |
aoqi@0 | 156 | if (tsym.kind == PCK && tsym.members().elems == null && !tsym.exists()) { |
aoqi@0 | 157 | // If we can't find java.lang, exit immediately. |
aoqi@0 | 158 | if (((PackageSymbol)tsym).fullname.equals(names.java_lang)) { |
aoqi@0 | 159 | JCDiagnostic msg = diags.fragment("fatal.err.no.java.lang"); |
aoqi@0 | 160 | throw new FatalError(msg); |
aoqi@0 | 161 | } else { |
aoqi@0 | 162 | log.error(DiagnosticFlag.RESOLVE_ERROR, pos, "doesnt.exist", tsym); |
aoqi@0 | 163 | } |
aoqi@0 | 164 | } |
aoqi@0 | 165 | env.toplevel.starImportScope.importAll(tsym.members()); |
aoqi@0 | 166 | } |
aoqi@0 | 167 | |
aoqi@0 | 168 | /** Import all static members of a class or package on demand. |
aoqi@0 | 169 | * @param pos Position to be used for error reporting. |
aoqi@0 | 170 | * @param tsym The class or package the members of which are imported. |
aoqi@0 | 171 | * @param env The env in which the imported classes will be entered. |
aoqi@0 | 172 | */ |
aoqi@0 | 173 | private void importStaticAll(int pos, |
aoqi@0 | 174 | final TypeSymbol tsym, |
aoqi@0 | 175 | Env<AttrContext> env) { |
aoqi@0 | 176 | final JavaFileObject sourcefile = env.toplevel.sourcefile; |
aoqi@0 | 177 | final Scope toScope = env.toplevel.starImportScope; |
aoqi@0 | 178 | final PackageSymbol packge = env.toplevel.packge; |
aoqi@0 | 179 | final TypeSymbol origin = tsym; |
aoqi@0 | 180 | |
aoqi@0 | 181 | // enter imported types immediately |
aoqi@0 | 182 | new Object() { |
aoqi@0 | 183 | Set<Symbol> processed = new HashSet<Symbol>(); |
aoqi@0 | 184 | void importFrom(TypeSymbol tsym) { |
aoqi@0 | 185 | if (tsym == null || !processed.add(tsym)) |
aoqi@0 | 186 | return; |
aoqi@0 | 187 | |
aoqi@0 | 188 | // also import inherited names |
aoqi@0 | 189 | importFrom(types.supertype(tsym.type).tsym); |
aoqi@0 | 190 | for (Type t : types.interfaces(tsym.type)) |
aoqi@0 | 191 | importFrom(t.tsym); |
aoqi@0 | 192 | |
aoqi@0 | 193 | final Scope fromScope = tsym.members(); |
aoqi@0 | 194 | for (Scope.Entry e = fromScope.elems; e != null; e = e.sibling) { |
aoqi@0 | 195 | Symbol sym = e.sym; |
aoqi@0 | 196 | if (sym.kind == TYP && |
aoqi@0 | 197 | (sym.flags() & STATIC) != 0 && |
aoqi@0 | 198 | staticImportAccessible(sym, packge) && |
aoqi@0 | 199 | sym.isMemberOf(origin, types) && |
aoqi@0 | 200 | !toScope.includes(sym)) |
aoqi@0 | 201 | toScope.enter(sym, fromScope, origin.members(), true); |
aoqi@0 | 202 | } |
aoqi@0 | 203 | } |
aoqi@0 | 204 | }.importFrom(tsym); |
aoqi@0 | 205 | |
aoqi@0 | 206 | // enter non-types before annotations that might use them |
aoqi@0 | 207 | annotate.earlier(new Annotate.Worker() { |
aoqi@0 | 208 | Set<Symbol> processed = new HashSet<Symbol>(); |
aoqi@0 | 209 | |
aoqi@0 | 210 | public String toString() { |
aoqi@0 | 211 | return "import static " + tsym + ".*" + " in " + sourcefile; |
aoqi@0 | 212 | } |
aoqi@0 | 213 | void importFrom(TypeSymbol tsym) { |
aoqi@0 | 214 | if (tsym == null || !processed.add(tsym)) |
aoqi@0 | 215 | return; |
aoqi@0 | 216 | |
aoqi@0 | 217 | // also import inherited names |
aoqi@0 | 218 | importFrom(types.supertype(tsym.type).tsym); |
aoqi@0 | 219 | for (Type t : types.interfaces(tsym.type)) |
aoqi@0 | 220 | importFrom(t.tsym); |
aoqi@0 | 221 | |
aoqi@0 | 222 | final Scope fromScope = tsym.members(); |
aoqi@0 | 223 | for (Scope.Entry e = fromScope.elems; e != null; e = e.sibling) { |
aoqi@0 | 224 | Symbol sym = e.sym; |
aoqi@0 | 225 | if (sym.isStatic() && sym.kind != TYP && |
aoqi@0 | 226 | staticImportAccessible(sym, packge) && |
aoqi@0 | 227 | !toScope.includes(sym) && |
aoqi@0 | 228 | sym.isMemberOf(origin, types)) { |
aoqi@0 | 229 | toScope.enter(sym, fromScope, origin.members(), true); |
aoqi@0 | 230 | } |
aoqi@0 | 231 | } |
aoqi@0 | 232 | } |
aoqi@0 | 233 | public void run() { |
aoqi@0 | 234 | importFrom(tsym); |
aoqi@0 | 235 | } |
aoqi@0 | 236 | }); |
aoqi@0 | 237 | } |
aoqi@0 | 238 | |
aoqi@0 | 239 | // is the sym accessible everywhere in packge? |
aoqi@0 | 240 | boolean staticImportAccessible(Symbol sym, PackageSymbol packge) { |
aoqi@0 | 241 | int flags = (int)(sym.flags() & AccessFlags); |
aoqi@0 | 242 | switch (flags) { |
aoqi@0 | 243 | default: |
aoqi@0 | 244 | case PUBLIC: |
aoqi@0 | 245 | return true; |
aoqi@0 | 246 | case PRIVATE: |
aoqi@0 | 247 | return false; |
aoqi@0 | 248 | case 0: |
aoqi@0 | 249 | case PROTECTED: |
aoqi@0 | 250 | return sym.packge() == packge; |
aoqi@0 | 251 | } |
aoqi@0 | 252 | } |
aoqi@0 | 253 | |
aoqi@0 | 254 | /** Import statics types of a given name. Non-types are handled in Attr. |
aoqi@0 | 255 | * @param pos Position to be used for error reporting. |
aoqi@0 | 256 | * @param tsym The class from which the name is imported. |
aoqi@0 | 257 | * @param name The (simple) name being imported. |
aoqi@0 | 258 | * @param env The environment containing the named import |
aoqi@0 | 259 | * scope to add to. |
aoqi@0 | 260 | */ |
aoqi@0 | 261 | private void importNamedStatic(final DiagnosticPosition pos, |
aoqi@0 | 262 | final TypeSymbol tsym, |
aoqi@0 | 263 | final Name name, |
aoqi@0 | 264 | final Env<AttrContext> env) { |
aoqi@0 | 265 | if (tsym.kind != TYP) { |
aoqi@0 | 266 | log.error(DiagnosticFlag.RECOVERABLE, pos, "static.imp.only.classes.and.interfaces"); |
aoqi@0 | 267 | return; |
aoqi@0 | 268 | } |
aoqi@0 | 269 | |
aoqi@0 | 270 | final Scope toScope = env.toplevel.namedImportScope; |
aoqi@0 | 271 | final PackageSymbol packge = env.toplevel.packge; |
aoqi@0 | 272 | final TypeSymbol origin = tsym; |
aoqi@0 | 273 | |
aoqi@0 | 274 | // enter imported types immediately |
aoqi@0 | 275 | new Object() { |
aoqi@0 | 276 | Set<Symbol> processed = new HashSet<Symbol>(); |
aoqi@0 | 277 | void importFrom(TypeSymbol tsym) { |
aoqi@0 | 278 | if (tsym == null || !processed.add(tsym)) |
aoqi@0 | 279 | return; |
aoqi@0 | 280 | |
aoqi@0 | 281 | // also import inherited names |
aoqi@0 | 282 | importFrom(types.supertype(tsym.type).tsym); |
aoqi@0 | 283 | for (Type t : types.interfaces(tsym.type)) |
aoqi@0 | 284 | importFrom(t.tsym); |
aoqi@0 | 285 | |
aoqi@0 | 286 | for (Scope.Entry e = tsym.members().lookup(name); |
aoqi@0 | 287 | e.scope != null; |
aoqi@0 | 288 | e = e.next()) { |
aoqi@0 | 289 | Symbol sym = e.sym; |
aoqi@0 | 290 | if (sym.isStatic() && |
aoqi@0 | 291 | sym.kind == TYP && |
aoqi@0 | 292 | staticImportAccessible(sym, packge) && |
aoqi@0 | 293 | sym.isMemberOf(origin, types) && |
aoqi@0 | 294 | chk.checkUniqueStaticImport(pos, sym, toScope)) |
aoqi@0 | 295 | toScope.enter(sym, sym.owner.members(), origin.members(), true); |
aoqi@0 | 296 | } |
aoqi@0 | 297 | } |
aoqi@0 | 298 | }.importFrom(tsym); |
aoqi@0 | 299 | |
aoqi@0 | 300 | // enter non-types before annotations that might use them |
aoqi@0 | 301 | annotate.earlier(new Annotate.Worker() { |
aoqi@0 | 302 | Set<Symbol> processed = new HashSet<Symbol>(); |
aoqi@0 | 303 | boolean found = false; |
aoqi@0 | 304 | |
aoqi@0 | 305 | public String toString() { |
aoqi@0 | 306 | return "import static " + tsym + "." + name; |
aoqi@0 | 307 | } |
aoqi@0 | 308 | void importFrom(TypeSymbol tsym) { |
aoqi@0 | 309 | if (tsym == null || !processed.add(tsym)) |
aoqi@0 | 310 | return; |
aoqi@0 | 311 | |
aoqi@0 | 312 | // also import inherited names |
aoqi@0 | 313 | importFrom(types.supertype(tsym.type).tsym); |
aoqi@0 | 314 | for (Type t : types.interfaces(tsym.type)) |
aoqi@0 | 315 | importFrom(t.tsym); |
aoqi@0 | 316 | |
aoqi@0 | 317 | for (Scope.Entry e = tsym.members().lookup(name); |
aoqi@0 | 318 | e.scope != null; |
aoqi@0 | 319 | e = e.next()) { |
aoqi@0 | 320 | Symbol sym = e.sym; |
aoqi@0 | 321 | if (sym.isStatic() && |
aoqi@0 | 322 | staticImportAccessible(sym, packge) && |
aoqi@0 | 323 | sym.isMemberOf(origin, types)) { |
aoqi@0 | 324 | found = true; |
aoqi@0 | 325 | if (sym.kind != TYP) { |
aoqi@0 | 326 | toScope.enter(sym, sym.owner.members(), origin.members(), true); |
aoqi@0 | 327 | } |
aoqi@0 | 328 | } |
aoqi@0 | 329 | } |
aoqi@0 | 330 | } |
aoqi@0 | 331 | public void run() { |
aoqi@0 | 332 | JavaFileObject prev = log.useSource(env.toplevel.sourcefile); |
aoqi@0 | 333 | try { |
aoqi@0 | 334 | importFrom(tsym); |
aoqi@0 | 335 | if (!found) { |
aoqi@0 | 336 | log.error(pos, "cant.resolve.location", |
aoqi@0 | 337 | KindName.STATIC, |
aoqi@0 | 338 | name, List.<Type>nil(), List.<Type>nil(), |
aoqi@0 | 339 | Kinds.typeKindName(tsym.type), |
aoqi@0 | 340 | tsym.type); |
aoqi@0 | 341 | } |
aoqi@0 | 342 | } finally { |
aoqi@0 | 343 | log.useSource(prev); |
aoqi@0 | 344 | } |
aoqi@0 | 345 | } |
aoqi@0 | 346 | }); |
aoqi@0 | 347 | } |
aoqi@0 | 348 | /** Import given class. |
aoqi@0 | 349 | * @param pos Position to be used for error reporting. |
aoqi@0 | 350 | * @param tsym The class to be imported. |
aoqi@0 | 351 | * @param env The environment containing the named import |
aoqi@0 | 352 | * scope to add to. |
aoqi@0 | 353 | */ |
aoqi@0 | 354 | private void importNamed(DiagnosticPosition pos, Symbol tsym, Env<AttrContext> env) { |
aoqi@0 | 355 | if (tsym.kind == TYP && |
aoqi@0 | 356 | chk.checkUniqueImport(pos, tsym, env.toplevel.namedImportScope)) |
aoqi@0 | 357 | env.toplevel.namedImportScope.enter(tsym, tsym.owner.members()); |
aoqi@0 | 358 | } |
aoqi@0 | 359 | |
aoqi@0 | 360 | /** Construct method type from method signature. |
aoqi@0 | 361 | * @param typarams The method's type parameters. |
aoqi@0 | 362 | * @param params The method's value parameters. |
aoqi@0 | 363 | * @param res The method's result type, |
aoqi@0 | 364 | * null if it is a constructor. |
aoqi@0 | 365 | * @param recvparam The method's receiver parameter, |
aoqi@0 | 366 | * null if none given; TODO: or already set here? |
aoqi@0 | 367 | * @param thrown The method's thrown exceptions. |
aoqi@0 | 368 | * @param env The method's (local) environment. |
aoqi@0 | 369 | */ |
aoqi@0 | 370 | Type signature(MethodSymbol msym, |
aoqi@0 | 371 | List<JCTypeParameter> typarams, |
aoqi@0 | 372 | List<JCVariableDecl> params, |
aoqi@0 | 373 | JCTree res, |
aoqi@0 | 374 | JCVariableDecl recvparam, |
aoqi@0 | 375 | List<JCExpression> thrown, |
aoqi@0 | 376 | Env<AttrContext> env) { |
aoqi@0 | 377 | |
aoqi@0 | 378 | // Enter and attribute type parameters. |
aoqi@0 | 379 | List<Type> tvars = enter.classEnter(typarams, env); |
aoqi@0 | 380 | attr.attribTypeVariables(typarams, env); |
aoqi@0 | 381 | |
aoqi@0 | 382 | // Enter and attribute value parameters. |
aoqi@0 | 383 | ListBuffer<Type> argbuf = new ListBuffer<Type>(); |
aoqi@0 | 384 | for (List<JCVariableDecl> l = params; l.nonEmpty(); l = l.tail) { |
aoqi@0 | 385 | memberEnter(l.head, env); |
aoqi@0 | 386 | argbuf.append(l.head.vartype.type); |
aoqi@0 | 387 | } |
aoqi@0 | 388 | |
aoqi@0 | 389 | // Attribute result type, if one is given. |
aoqi@0 | 390 | Type restype = res == null ? syms.voidType : attr.attribType(res, env); |
aoqi@0 | 391 | |
aoqi@0 | 392 | // Attribute receiver type, if one is given. |
aoqi@0 | 393 | Type recvtype; |
aoqi@0 | 394 | if (recvparam!=null) { |
aoqi@0 | 395 | memberEnter(recvparam, env); |
aoqi@0 | 396 | recvtype = recvparam.vartype.type; |
aoqi@0 | 397 | } else { |
aoqi@0 | 398 | recvtype = null; |
aoqi@0 | 399 | } |
aoqi@0 | 400 | |
aoqi@0 | 401 | // Attribute thrown exceptions. |
aoqi@0 | 402 | ListBuffer<Type> thrownbuf = new ListBuffer<Type>(); |
aoqi@0 | 403 | for (List<JCExpression> l = thrown; l.nonEmpty(); l = l.tail) { |
aoqi@0 | 404 | Type exc = attr.attribType(l.head, env); |
aoqi@0 | 405 | if (!exc.hasTag(TYPEVAR)) { |
aoqi@0 | 406 | exc = chk.checkClassType(l.head.pos(), exc); |
aoqi@0 | 407 | } else if (exc.tsym.owner == msym) { |
aoqi@0 | 408 | //mark inference variables in 'throws' clause |
aoqi@0 | 409 | exc.tsym.flags_field |= THROWS; |
aoqi@0 | 410 | } |
aoqi@0 | 411 | thrownbuf.append(exc); |
aoqi@0 | 412 | } |
aoqi@0 | 413 | MethodType mtype = new MethodType(argbuf.toList(), |
aoqi@0 | 414 | restype, |
aoqi@0 | 415 | thrownbuf.toList(), |
aoqi@0 | 416 | syms.methodClass); |
aoqi@0 | 417 | mtype.recvtype = recvtype; |
aoqi@0 | 418 | |
aoqi@0 | 419 | return tvars.isEmpty() ? mtype : new ForAll(tvars, mtype); |
aoqi@0 | 420 | } |
aoqi@0 | 421 | |
aoqi@0 | 422 | /* ******************************************************************** |
aoqi@0 | 423 | * Visitor methods for member enter |
aoqi@0 | 424 | *********************************************************************/ |
aoqi@0 | 425 | |
aoqi@0 | 426 | /** Visitor argument: the current environment |
aoqi@0 | 427 | */ |
aoqi@0 | 428 | protected Env<AttrContext> env; |
aoqi@0 | 429 | |
aoqi@0 | 430 | /** Enter field and method definitions and process import |
aoqi@0 | 431 | * clauses, catching any completion failure exceptions. |
aoqi@0 | 432 | */ |
aoqi@0 | 433 | protected void memberEnter(JCTree tree, Env<AttrContext> env) { |
aoqi@0 | 434 | Env<AttrContext> prevEnv = this.env; |
aoqi@0 | 435 | try { |
aoqi@0 | 436 | this.env = env; |
aoqi@0 | 437 | tree.accept(this); |
aoqi@0 | 438 | } catch (CompletionFailure ex) { |
aoqi@0 | 439 | chk.completionError(tree.pos(), ex); |
aoqi@0 | 440 | } finally { |
aoqi@0 | 441 | this.env = prevEnv; |
aoqi@0 | 442 | } |
aoqi@0 | 443 | } |
aoqi@0 | 444 | |
aoqi@0 | 445 | /** Enter members from a list of trees. |
aoqi@0 | 446 | */ |
aoqi@0 | 447 | void memberEnter(List<? extends JCTree> trees, Env<AttrContext> env) { |
aoqi@0 | 448 | for (List<? extends JCTree> l = trees; l.nonEmpty(); l = l.tail) |
aoqi@0 | 449 | memberEnter(l.head, env); |
aoqi@0 | 450 | } |
aoqi@0 | 451 | |
aoqi@0 | 452 | /** Enter members for a class. |
aoqi@0 | 453 | */ |
aoqi@0 | 454 | void finishClass(JCClassDecl tree, Env<AttrContext> env) { |
aoqi@0 | 455 | if ((tree.mods.flags & Flags.ENUM) != 0 && |
aoqi@0 | 456 | (types.supertype(tree.sym.type).tsym.flags() & Flags.ENUM) == 0) { |
aoqi@0 | 457 | addEnumMembers(tree, env); |
aoqi@0 | 458 | } |
aoqi@0 | 459 | memberEnter(tree.defs, env); |
aoqi@0 | 460 | } |
aoqi@0 | 461 | |
aoqi@0 | 462 | /** Add the implicit members for an enum type |
aoqi@0 | 463 | * to the symbol table. |
aoqi@0 | 464 | */ |
aoqi@0 | 465 | private void addEnumMembers(JCClassDecl tree, Env<AttrContext> env) { |
aoqi@0 | 466 | JCExpression valuesType = make.Type(new ArrayType(tree.sym.type, syms.arrayClass)); |
aoqi@0 | 467 | |
aoqi@0 | 468 | // public static T[] values() { return ???; } |
aoqi@0 | 469 | JCMethodDecl values = make. |
aoqi@0 | 470 | MethodDef(make.Modifiers(Flags.PUBLIC|Flags.STATIC), |
aoqi@0 | 471 | names.values, |
aoqi@0 | 472 | valuesType, |
aoqi@0 | 473 | List.<JCTypeParameter>nil(), |
aoqi@0 | 474 | List.<JCVariableDecl>nil(), |
aoqi@0 | 475 | List.<JCExpression>nil(), // thrown |
aoqi@0 | 476 | null, //make.Block(0, Tree.emptyList.prepend(make.Return(make.Ident(names._null)))), |
aoqi@0 | 477 | null); |
aoqi@0 | 478 | memberEnter(values, env); |
aoqi@0 | 479 | |
aoqi@0 | 480 | // public static T valueOf(String name) { return ???; } |
aoqi@0 | 481 | JCMethodDecl valueOf = make. |
aoqi@0 | 482 | MethodDef(make.Modifiers(Flags.PUBLIC|Flags.STATIC), |
aoqi@0 | 483 | names.valueOf, |
aoqi@0 | 484 | make.Type(tree.sym.type), |
aoqi@0 | 485 | List.<JCTypeParameter>nil(), |
aoqi@0 | 486 | List.of(make.VarDef(make.Modifiers(Flags.PARAMETER | |
aoqi@0 | 487 | Flags.MANDATED), |
aoqi@0 | 488 | names.fromString("name"), |
aoqi@0 | 489 | make.Type(syms.stringType), null)), |
aoqi@0 | 490 | List.<JCExpression>nil(), // thrown |
aoqi@0 | 491 | null, //make.Block(0, Tree.emptyList.prepend(make.Return(make.Ident(names._null)))), |
aoqi@0 | 492 | null); |
aoqi@0 | 493 | memberEnter(valueOf, env); |
aoqi@0 | 494 | } |
aoqi@0 | 495 | |
aoqi@0 | 496 | public void visitTopLevel(JCCompilationUnit tree) { |
aoqi@0 | 497 | if (tree.starImportScope.elems != null) { |
aoqi@0 | 498 | // we must have already processed this toplevel |
aoqi@0 | 499 | return; |
aoqi@0 | 500 | } |
aoqi@0 | 501 | |
aoqi@0 | 502 | // check that no class exists with same fully qualified name as |
aoqi@0 | 503 | // toplevel package |
aoqi@0 | 504 | if (checkClash && tree.pid != null) { |
aoqi@0 | 505 | Symbol p = tree.packge; |
aoqi@0 | 506 | while (p.owner != syms.rootPackage) { |
aoqi@0 | 507 | p.owner.complete(); // enter all class members of p |
aoqi@0 | 508 | if (syms.classes.get(p.getQualifiedName()) != null) { |
aoqi@0 | 509 | log.error(tree.pos, |
aoqi@0 | 510 | "pkg.clashes.with.class.of.same.name", |
aoqi@0 | 511 | p); |
aoqi@0 | 512 | } |
aoqi@0 | 513 | p = p.owner; |
aoqi@0 | 514 | } |
aoqi@0 | 515 | } |
aoqi@0 | 516 | |
aoqi@0 | 517 | // process package annotations |
aoqi@0 | 518 | annotateLater(tree.packageAnnotations, env, tree.packge, null); |
aoqi@0 | 519 | |
aoqi@0 | 520 | DiagnosticPosition prevLintPos = deferredLintHandler.immediate(); |
aoqi@0 | 521 | Lint prevLint = chk.setLint(lint); |
aoqi@0 | 522 | |
aoqi@0 | 523 | try { |
aoqi@0 | 524 | // Import-on-demand java.lang. |
aoqi@0 | 525 | importAll(tree.pos, reader.enterPackage(names.java_lang), env); |
aoqi@0 | 526 | |
aoqi@0 | 527 | // Process all import clauses. |
aoqi@0 | 528 | memberEnter(tree.defs, env); |
aoqi@0 | 529 | } finally { |
aoqi@0 | 530 | chk.setLint(prevLint); |
aoqi@0 | 531 | deferredLintHandler.setPos(prevLintPos); |
aoqi@0 | 532 | } |
aoqi@0 | 533 | } |
aoqi@0 | 534 | |
aoqi@0 | 535 | // process the non-static imports and the static imports of types. |
aoqi@0 | 536 | public void visitImport(JCImport tree) { |
aoqi@0 | 537 | JCFieldAccess imp = (JCFieldAccess)tree.qualid; |
aoqi@0 | 538 | Name name = TreeInfo.name(imp); |
aoqi@0 | 539 | |
aoqi@0 | 540 | // Create a local environment pointing to this tree to disable |
aoqi@0 | 541 | // effects of other imports in Resolve.findGlobalType |
aoqi@0 | 542 | Env<AttrContext> localEnv = env.dup(tree); |
aoqi@0 | 543 | |
aoqi@0 | 544 | TypeSymbol p = attr.attribImportQualifier(tree, localEnv).tsym; |
aoqi@0 | 545 | if (name == names.asterisk) { |
aoqi@0 | 546 | // Import on demand. |
aoqi@0 | 547 | chk.checkCanonical(imp.selected); |
aoqi@0 | 548 | if (tree.staticImport) |
aoqi@0 | 549 | importStaticAll(tree.pos, p, env); |
aoqi@0 | 550 | else |
aoqi@0 | 551 | importAll(tree.pos, p, env); |
aoqi@0 | 552 | } else { |
aoqi@0 | 553 | // Named type import. |
aoqi@0 | 554 | if (tree.staticImport) { |
aoqi@0 | 555 | importNamedStatic(tree.pos(), p, name, localEnv); |
aoqi@0 | 556 | chk.checkCanonical(imp.selected); |
aoqi@0 | 557 | } else { |
aoqi@0 | 558 | TypeSymbol c = attribImportType(imp, localEnv).tsym; |
aoqi@0 | 559 | chk.checkCanonical(imp); |
aoqi@0 | 560 | importNamed(tree.pos(), c, env); |
aoqi@0 | 561 | } |
aoqi@0 | 562 | } |
aoqi@0 | 563 | } |
aoqi@0 | 564 | |
aoqi@0 | 565 | public void visitMethodDef(JCMethodDecl tree) { |
aoqi@0 | 566 | Scope enclScope = enter.enterScope(env); |
aoqi@0 | 567 | MethodSymbol m = new MethodSymbol(0, tree.name, null, enclScope.owner); |
aoqi@0 | 568 | m.flags_field = chk.checkFlags(tree.pos(), tree.mods.flags, m, tree); |
aoqi@0 | 569 | tree.sym = m; |
aoqi@0 | 570 | |
aoqi@0 | 571 | //if this is a default method, add the DEFAULT flag to the enclosing interface |
aoqi@0 | 572 | if ((tree.mods.flags & DEFAULT) != 0) { |
aoqi@0 | 573 | m.enclClass().flags_field |= DEFAULT; |
aoqi@0 | 574 | } |
aoqi@0 | 575 | |
aoqi@0 | 576 | Env<AttrContext> localEnv = methodEnv(tree, env); |
aoqi@0 | 577 | |
aoqi@0 | 578 | annotate.enterStart(); |
aoqi@0 | 579 | try { |
aoqi@0 | 580 | DiagnosticPosition prevLintPos = deferredLintHandler.setPos(tree.pos()); |
aoqi@0 | 581 | try { |
aoqi@0 | 582 | // Compute the method type |
aoqi@0 | 583 | m.type = signature(m, tree.typarams, tree.params, |
aoqi@0 | 584 | tree.restype, tree.recvparam, |
aoqi@0 | 585 | tree.thrown, |
aoqi@0 | 586 | localEnv); |
aoqi@0 | 587 | } finally { |
aoqi@0 | 588 | deferredLintHandler.setPos(prevLintPos); |
aoqi@0 | 589 | } |
aoqi@0 | 590 | |
aoqi@0 | 591 | if (types.isSignaturePolymorphic(m)) { |
aoqi@0 | 592 | m.flags_field |= SIGNATURE_POLYMORPHIC; |
aoqi@0 | 593 | } |
aoqi@0 | 594 | |
aoqi@0 | 595 | // Set m.params |
aoqi@0 | 596 | ListBuffer<VarSymbol> params = new ListBuffer<VarSymbol>(); |
aoqi@0 | 597 | JCVariableDecl lastParam = null; |
aoqi@0 | 598 | for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) { |
aoqi@0 | 599 | JCVariableDecl param = lastParam = l.head; |
aoqi@0 | 600 | params.append(Assert.checkNonNull(param.sym)); |
aoqi@0 | 601 | } |
aoqi@0 | 602 | m.params = params.toList(); |
aoqi@0 | 603 | |
aoqi@0 | 604 | // mark the method varargs, if necessary |
aoqi@0 | 605 | if (lastParam != null && (lastParam.mods.flags & Flags.VARARGS) != 0) |
aoqi@0 | 606 | m.flags_field |= Flags.VARARGS; |
aoqi@0 | 607 | |
aoqi@0 | 608 | localEnv.info.scope.leave(); |
aoqi@0 | 609 | if (chk.checkUnique(tree.pos(), m, enclScope)) { |
aoqi@0 | 610 | enclScope.enter(m); |
aoqi@0 | 611 | } |
aoqi@0 | 612 | |
aoqi@0 | 613 | annotateLater(tree.mods.annotations, localEnv, m, tree.pos()); |
aoqi@0 | 614 | // Visit the signature of the method. Note that |
aoqi@0 | 615 | // TypeAnnotate doesn't descend into the body. |
aoqi@0 | 616 | typeAnnotate(tree, localEnv, m, tree.pos()); |
aoqi@0 | 617 | |
aoqi@0 | 618 | if (tree.defaultValue != null) |
aoqi@0 | 619 | annotateDefaultValueLater(tree.defaultValue, localEnv, m); |
aoqi@0 | 620 | } finally { |
aoqi@0 | 621 | annotate.enterDone(); |
aoqi@0 | 622 | } |
aoqi@0 | 623 | } |
aoqi@0 | 624 | |
aoqi@0 | 625 | /** Create a fresh environment for method bodies. |
aoqi@0 | 626 | * @param tree The method definition. |
aoqi@0 | 627 | * @param env The environment current outside of the method definition. |
aoqi@0 | 628 | */ |
aoqi@0 | 629 | Env<AttrContext> methodEnv(JCMethodDecl tree, Env<AttrContext> env) { |
aoqi@0 | 630 | Env<AttrContext> localEnv = |
aoqi@0 | 631 | env.dup(tree, env.info.dup(env.info.scope.dupUnshared())); |
aoqi@0 | 632 | localEnv.enclMethod = tree; |
aoqi@0 | 633 | localEnv.info.scope.owner = tree.sym; |
aoqi@0 | 634 | if (tree.sym.type != null) { |
aoqi@0 | 635 | //when this is called in the enter stage, there's no type to be set |
aoqi@0 | 636 | localEnv.info.returnResult = attr.new ResultInfo(VAL, tree.sym.type.getReturnType()); |
aoqi@0 | 637 | } |
aoqi@0 | 638 | if ((tree.mods.flags & STATIC) != 0) localEnv.info.staticLevel++; |
aoqi@0 | 639 | return localEnv; |
aoqi@0 | 640 | } |
aoqi@0 | 641 | |
aoqi@0 | 642 | public void visitVarDef(JCVariableDecl tree) { |
aoqi@0 | 643 | Env<AttrContext> localEnv = env; |
aoqi@0 | 644 | if ((tree.mods.flags & STATIC) != 0 || |
aoqi@0 | 645 | (env.info.scope.owner.flags() & INTERFACE) != 0) { |
aoqi@0 | 646 | localEnv = env.dup(tree, env.info.dup()); |
aoqi@0 | 647 | localEnv.info.staticLevel++; |
aoqi@0 | 648 | } |
aoqi@0 | 649 | DiagnosticPosition prevLintPos = deferredLintHandler.setPos(tree.pos()); |
aoqi@0 | 650 | annotate.enterStart(); |
aoqi@0 | 651 | try { |
aoqi@0 | 652 | try { |
aoqi@0 | 653 | if (TreeInfo.isEnumInit(tree)) { |
aoqi@0 | 654 | attr.attribIdentAsEnumType(localEnv, (JCIdent)tree.vartype); |
aoqi@0 | 655 | } else { |
aoqi@0 | 656 | attr.attribType(tree.vartype, localEnv); |
aoqi@0 | 657 | if (TreeInfo.isReceiverParam(tree)) |
aoqi@0 | 658 | checkReceiver(tree, localEnv); |
aoqi@0 | 659 | } |
aoqi@0 | 660 | } finally { |
aoqi@0 | 661 | deferredLintHandler.setPos(prevLintPos); |
aoqi@0 | 662 | } |
aoqi@0 | 663 | |
aoqi@0 | 664 | if ((tree.mods.flags & VARARGS) != 0) { |
aoqi@0 | 665 | //if we are entering a varargs parameter, we need to |
aoqi@0 | 666 | //replace its type (a plain array type) with the more |
aoqi@0 | 667 | //precise VarargsType --- we need to do it this way |
aoqi@0 | 668 | //because varargs is represented in the tree as a |
aoqi@0 | 669 | //modifier on the parameter declaration, and not as a |
aoqi@0 | 670 | //distinct type of array node. |
aoqi@0 | 671 | ArrayType atype = (ArrayType)tree.vartype.type.unannotatedType(); |
aoqi@0 | 672 | tree.vartype.type = atype.makeVarargs(); |
aoqi@0 | 673 | } |
aoqi@0 | 674 | Scope enclScope = enter.enterScope(env); |
aoqi@0 | 675 | VarSymbol v = |
aoqi@0 | 676 | new VarSymbol(0, tree.name, tree.vartype.type, enclScope.owner); |
aoqi@0 | 677 | v.flags_field = chk.checkFlags(tree.pos(), tree.mods.flags, v, tree); |
aoqi@0 | 678 | tree.sym = v; |
aoqi@0 | 679 | if (tree.init != null) { |
aoqi@0 | 680 | v.flags_field |= HASINIT; |
aoqi@0 | 681 | if ((v.flags_field & FINAL) != 0 && |
aoqi@0 | 682 | needsLazyConstValue(tree.init)) { |
aoqi@0 | 683 | Env<AttrContext> initEnv = getInitEnv(tree, env); |
aoqi@0 | 684 | initEnv.info.enclVar = v; |
aoqi@0 | 685 | v.setLazyConstValue(initEnv(tree, initEnv), attr, tree); |
aoqi@0 | 686 | } |
aoqi@0 | 687 | } |
aoqi@0 | 688 | if (chk.checkUnique(tree.pos(), v, enclScope)) { |
aoqi@0 | 689 | chk.checkTransparentVar(tree.pos(), v, enclScope); |
aoqi@0 | 690 | enclScope.enter(v); |
aoqi@0 | 691 | } |
aoqi@0 | 692 | annotateLater(tree.mods.annotations, localEnv, v, tree.pos()); |
aoqi@0 | 693 | typeAnnotate(tree.vartype, env, v, tree.pos()); |
aoqi@0 | 694 | v.pos = tree.pos; |
aoqi@0 | 695 | } finally { |
aoqi@0 | 696 | annotate.enterDone(); |
aoqi@0 | 697 | } |
aoqi@0 | 698 | } |
aoqi@0 | 699 | // where |
aoqi@0 | 700 | void checkType(JCTree tree, Type type, String diag) { |
aoqi@0 | 701 | if (!tree.type.isErroneous() && !types.isSameType(tree.type, type)) { |
aoqi@0 | 702 | log.error(tree, diag, type, tree.type); |
aoqi@0 | 703 | } |
aoqi@0 | 704 | } |
aoqi@0 | 705 | void checkReceiver(JCVariableDecl tree, Env<AttrContext> localEnv) { |
aoqi@0 | 706 | attr.attribExpr(tree.nameexpr, localEnv); |
aoqi@0 | 707 | MethodSymbol m = localEnv.enclMethod.sym; |
aoqi@0 | 708 | if (m.isConstructor()) { |
aoqi@0 | 709 | Type outertype = m.owner.owner.type; |
aoqi@0 | 710 | if (outertype.hasTag(TypeTag.METHOD)) { |
aoqi@0 | 711 | // we have a local inner class |
aoqi@0 | 712 | outertype = m.owner.owner.owner.type; |
aoqi@0 | 713 | } |
aoqi@0 | 714 | if (outertype.hasTag(TypeTag.CLASS)) { |
aoqi@0 | 715 | checkType(tree.vartype, outertype, "incorrect.constructor.receiver.type"); |
aoqi@0 | 716 | checkType(tree.nameexpr, outertype, "incorrect.constructor.receiver.name"); |
aoqi@0 | 717 | } else { |
aoqi@0 | 718 | log.error(tree, "receiver.parameter.not.applicable.constructor.toplevel.class"); |
aoqi@0 | 719 | } |
aoqi@0 | 720 | } else { |
aoqi@0 | 721 | checkType(tree.vartype, m.owner.type, "incorrect.receiver.type"); |
aoqi@0 | 722 | checkType(tree.nameexpr, m.owner.type, "incorrect.receiver.name"); |
aoqi@0 | 723 | } |
aoqi@0 | 724 | } |
aoqi@0 | 725 | |
aoqi@0 | 726 | public boolean needsLazyConstValue(JCTree tree) { |
aoqi@0 | 727 | InitTreeVisitor initTreeVisitor = new InitTreeVisitor(); |
aoqi@0 | 728 | tree.accept(initTreeVisitor); |
aoqi@0 | 729 | return initTreeVisitor.result; |
aoqi@0 | 730 | } |
aoqi@0 | 731 | |
aoqi@0 | 732 | /** Visitor class for expressions which might be constant expressions. |
aoqi@0 | 733 | */ |
aoqi@0 | 734 | static class InitTreeVisitor extends JCTree.Visitor { |
aoqi@0 | 735 | |
aoqi@0 | 736 | private boolean result = true; |
aoqi@0 | 737 | |
aoqi@0 | 738 | @Override |
aoqi@0 | 739 | public void visitTree(JCTree tree) {} |
aoqi@0 | 740 | |
aoqi@0 | 741 | @Override |
aoqi@0 | 742 | public void visitNewClass(JCNewClass that) { |
aoqi@0 | 743 | result = false; |
aoqi@0 | 744 | } |
aoqi@0 | 745 | |
aoqi@0 | 746 | @Override |
aoqi@0 | 747 | public void visitNewArray(JCNewArray that) { |
aoqi@0 | 748 | result = false; |
aoqi@0 | 749 | } |
aoqi@0 | 750 | |
aoqi@0 | 751 | @Override |
aoqi@0 | 752 | public void visitLambda(JCLambda that) { |
aoqi@0 | 753 | result = false; |
aoqi@0 | 754 | } |
aoqi@0 | 755 | |
aoqi@0 | 756 | @Override |
aoqi@0 | 757 | public void visitReference(JCMemberReference that) { |
aoqi@0 | 758 | result = false; |
aoqi@0 | 759 | } |
aoqi@0 | 760 | |
aoqi@0 | 761 | @Override |
aoqi@0 | 762 | public void visitApply(JCMethodInvocation that) { |
aoqi@0 | 763 | result = false; |
aoqi@0 | 764 | } |
aoqi@0 | 765 | |
aoqi@0 | 766 | @Override |
aoqi@0 | 767 | public void visitSelect(JCFieldAccess tree) { |
aoqi@0 | 768 | tree.selected.accept(this); |
aoqi@0 | 769 | } |
aoqi@0 | 770 | |
aoqi@0 | 771 | @Override |
aoqi@0 | 772 | public void visitConditional(JCConditional tree) { |
aoqi@0 | 773 | tree.cond.accept(this); |
aoqi@0 | 774 | tree.truepart.accept(this); |
aoqi@0 | 775 | tree.falsepart.accept(this); |
aoqi@0 | 776 | } |
aoqi@0 | 777 | |
aoqi@0 | 778 | @Override |
aoqi@0 | 779 | public void visitParens(JCParens tree) { |
aoqi@0 | 780 | tree.expr.accept(this); |
aoqi@0 | 781 | } |
aoqi@0 | 782 | |
aoqi@0 | 783 | @Override |
aoqi@0 | 784 | public void visitTypeCast(JCTypeCast tree) { |
aoqi@0 | 785 | tree.expr.accept(this); |
aoqi@0 | 786 | } |
aoqi@0 | 787 | } |
aoqi@0 | 788 | |
aoqi@0 | 789 | /** Create a fresh environment for a variable's initializer. |
aoqi@0 | 790 | * If the variable is a field, the owner of the environment's scope |
aoqi@0 | 791 | * is be the variable itself, otherwise the owner is the method |
aoqi@0 | 792 | * enclosing the variable definition. |
aoqi@0 | 793 | * |
aoqi@0 | 794 | * @param tree The variable definition. |
aoqi@0 | 795 | * @param env The environment current outside of the variable definition. |
aoqi@0 | 796 | */ |
aoqi@0 | 797 | Env<AttrContext> initEnv(JCVariableDecl tree, Env<AttrContext> env) { |
aoqi@0 | 798 | Env<AttrContext> localEnv = env.dupto(new AttrContextEnv(tree, env.info.dup())); |
aoqi@0 | 799 | if (tree.sym.owner.kind == TYP) { |
aoqi@0 | 800 | localEnv.info.scope = env.info.scope.dupUnshared(); |
aoqi@0 | 801 | localEnv.info.scope.owner = tree.sym; |
aoqi@0 | 802 | } |
aoqi@0 | 803 | if ((tree.mods.flags & STATIC) != 0 || |
aoqi@0 | 804 | ((env.enclClass.sym.flags() & INTERFACE) != 0 && env.enclMethod == null)) |
aoqi@0 | 805 | localEnv.info.staticLevel++; |
aoqi@0 | 806 | return localEnv; |
aoqi@0 | 807 | } |
aoqi@0 | 808 | |
aoqi@0 | 809 | /** Default member enter visitor method: do nothing |
aoqi@0 | 810 | */ |
aoqi@0 | 811 | public void visitTree(JCTree tree) { |
aoqi@0 | 812 | } |
aoqi@0 | 813 | |
aoqi@0 | 814 | public void visitErroneous(JCErroneous tree) { |
aoqi@0 | 815 | if (tree.errs != null) |
aoqi@0 | 816 | memberEnter(tree.errs, env); |
aoqi@0 | 817 | } |
aoqi@0 | 818 | |
aoqi@0 | 819 | public Env<AttrContext> getMethodEnv(JCMethodDecl tree, Env<AttrContext> env) { |
aoqi@0 | 820 | Env<AttrContext> mEnv = methodEnv(tree, env); |
aoqi@0 | 821 | mEnv.info.lint = mEnv.info.lint.augment(tree.sym); |
aoqi@0 | 822 | for (List<JCTypeParameter> l = tree.typarams; l.nonEmpty(); l = l.tail) |
aoqi@0 | 823 | mEnv.info.scope.enterIfAbsent(l.head.type.tsym); |
aoqi@0 | 824 | for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) |
aoqi@0 | 825 | mEnv.info.scope.enterIfAbsent(l.head.sym); |
aoqi@0 | 826 | return mEnv; |
aoqi@0 | 827 | } |
aoqi@0 | 828 | |
aoqi@0 | 829 | public Env<AttrContext> getInitEnv(JCVariableDecl tree, Env<AttrContext> env) { |
aoqi@0 | 830 | Env<AttrContext> iEnv = initEnv(tree, env); |
aoqi@0 | 831 | return iEnv; |
aoqi@0 | 832 | } |
aoqi@0 | 833 | |
aoqi@0 | 834 | /* ******************************************************************** |
aoqi@0 | 835 | * Type completion |
aoqi@0 | 836 | *********************************************************************/ |
aoqi@0 | 837 | |
aoqi@0 | 838 | Type attribImportType(JCTree tree, Env<AttrContext> env) { |
aoqi@0 | 839 | Assert.check(completionEnabled); |
aoqi@0 | 840 | try { |
aoqi@0 | 841 | // To prevent deep recursion, suppress completion of some |
aoqi@0 | 842 | // types. |
aoqi@0 | 843 | completionEnabled = false; |
aoqi@0 | 844 | return attr.attribType(tree, env); |
aoqi@0 | 845 | } finally { |
aoqi@0 | 846 | completionEnabled = true; |
aoqi@0 | 847 | } |
aoqi@0 | 848 | } |
aoqi@0 | 849 | |
aoqi@0 | 850 | /* ******************************************************************** |
aoqi@0 | 851 | * Annotation processing |
aoqi@0 | 852 | *********************************************************************/ |
aoqi@0 | 853 | |
aoqi@0 | 854 | /** Queue annotations for later processing. */ |
aoqi@0 | 855 | void annotateLater(final List<JCAnnotation> annotations, |
aoqi@0 | 856 | final Env<AttrContext> localEnv, |
aoqi@0 | 857 | final Symbol s, |
aoqi@0 | 858 | final DiagnosticPosition deferPos) { |
aoqi@0 | 859 | if (annotations.isEmpty()) { |
aoqi@0 | 860 | return; |
aoqi@0 | 861 | } |
aoqi@0 | 862 | if (s.kind != PCK) { |
aoqi@0 | 863 | s.resetAnnotations(); // mark Annotations as incomplete for now |
aoqi@0 | 864 | } |
aoqi@0 | 865 | annotate.normal(new Annotate.Worker() { |
aoqi@0 | 866 | @Override |
aoqi@0 | 867 | public String toString() { |
aoqi@0 | 868 | return "annotate " + annotations + " onto " + s + " in " + s.owner; |
aoqi@0 | 869 | } |
aoqi@0 | 870 | |
aoqi@0 | 871 | @Override |
aoqi@0 | 872 | public void run() { |
aoqi@0 | 873 | Assert.check(s.kind == PCK || s.annotationsPendingCompletion()); |
aoqi@0 | 874 | JavaFileObject prev = log.useSource(localEnv.toplevel.sourcefile); |
aoqi@0 | 875 | DiagnosticPosition prevLintPos = |
aoqi@0 | 876 | deferPos != null |
aoqi@0 | 877 | ? deferredLintHandler.setPos(deferPos) |
aoqi@0 | 878 | : deferredLintHandler.immediate(); |
aoqi@0 | 879 | Lint prevLint = deferPos != null ? null : chk.setLint(lint); |
aoqi@0 | 880 | try { |
aoqi@0 | 881 | if (s.hasAnnotations() && |
aoqi@0 | 882 | annotations.nonEmpty()) |
aoqi@0 | 883 | log.error(annotations.head.pos, |
aoqi@0 | 884 | "already.annotated", |
aoqi@0 | 885 | kindName(s), s); |
aoqi@0 | 886 | actualEnterAnnotations(annotations, localEnv, s); |
aoqi@0 | 887 | } finally { |
aoqi@0 | 888 | if (prevLint != null) |
aoqi@0 | 889 | chk.setLint(prevLint); |
aoqi@0 | 890 | deferredLintHandler.setPos(prevLintPos); |
aoqi@0 | 891 | log.useSource(prev); |
aoqi@0 | 892 | } |
aoqi@0 | 893 | } |
aoqi@0 | 894 | }); |
aoqi@0 | 895 | |
aoqi@0 | 896 | annotate.validate(new Annotate.Worker() { //validate annotations |
aoqi@0 | 897 | @Override |
aoqi@0 | 898 | public void run() { |
aoqi@0 | 899 | JavaFileObject prev = log.useSource(localEnv.toplevel.sourcefile); |
aoqi@0 | 900 | try { |
aoqi@0 | 901 | chk.validateAnnotations(annotations, s); |
aoqi@0 | 902 | } finally { |
aoqi@0 | 903 | log.useSource(prev); |
aoqi@0 | 904 | } |
aoqi@0 | 905 | } |
aoqi@0 | 906 | }); |
aoqi@0 | 907 | } |
aoqi@0 | 908 | |
aoqi@0 | 909 | /** |
aoqi@0 | 910 | * Check if a list of annotations contains a reference to |
aoqi@0 | 911 | * java.lang.Deprecated. |
aoqi@0 | 912 | **/ |
aoqi@0 | 913 | private boolean hasDeprecatedAnnotation(List<JCAnnotation> annotations) { |
aoqi@0 | 914 | for (List<JCAnnotation> al = annotations; !al.isEmpty(); al = al.tail) { |
aoqi@0 | 915 | JCAnnotation a = al.head; |
aoqi@0 | 916 | if (a.annotationType.type == syms.deprecatedType && a.args.isEmpty()) |
aoqi@0 | 917 | return true; |
aoqi@0 | 918 | } |
aoqi@0 | 919 | return false; |
aoqi@0 | 920 | } |
aoqi@0 | 921 | |
aoqi@0 | 922 | /** Enter a set of annotations. */ |
aoqi@0 | 923 | private void actualEnterAnnotations(List<JCAnnotation> annotations, |
aoqi@0 | 924 | Env<AttrContext> env, |
aoqi@0 | 925 | Symbol s) { |
aoqi@0 | 926 | Map<TypeSymbol, ListBuffer<Attribute.Compound>> annotated = |
aoqi@0 | 927 | new LinkedHashMap<TypeSymbol, ListBuffer<Attribute.Compound>>(); |
aoqi@0 | 928 | Map<Attribute.Compound, DiagnosticPosition> pos = |
aoqi@0 | 929 | new HashMap<Attribute.Compound, DiagnosticPosition>(); |
aoqi@0 | 930 | |
aoqi@0 | 931 | for (List<JCAnnotation> al = annotations; !al.isEmpty(); al = al.tail) { |
aoqi@0 | 932 | JCAnnotation a = al.head; |
aoqi@0 | 933 | Attribute.Compound c = annotate.enterAnnotation(a, |
aoqi@0 | 934 | syms.annotationType, |
aoqi@0 | 935 | env); |
aoqi@0 | 936 | if (c == null) { |
aoqi@0 | 937 | continue; |
aoqi@0 | 938 | } |
aoqi@0 | 939 | |
aoqi@0 | 940 | if (annotated.containsKey(a.type.tsym)) { |
aoqi@0 | 941 | if (!allowRepeatedAnnos) { |
aoqi@0 | 942 | log.error(a.pos(), "repeatable.annotations.not.supported.in.source"); |
aoqi@0 | 943 | allowRepeatedAnnos = true; |
aoqi@0 | 944 | } |
aoqi@0 | 945 | ListBuffer<Attribute.Compound> l = annotated.get(a.type.tsym); |
aoqi@0 | 946 | l = l.append(c); |
aoqi@0 | 947 | annotated.put(a.type.tsym, l); |
aoqi@0 | 948 | pos.put(c, a.pos()); |
aoqi@0 | 949 | } else { |
aoqi@0 | 950 | annotated.put(a.type.tsym, ListBuffer.of(c)); |
aoqi@0 | 951 | pos.put(c, a.pos()); |
aoqi@0 | 952 | } |
aoqi@0 | 953 | |
aoqi@0 | 954 | // Note: @Deprecated has no effect on local variables and parameters |
aoqi@0 | 955 | if (!c.type.isErroneous() |
aoqi@0 | 956 | && s.owner.kind != MTH |
aoqi@0 | 957 | && types.isSameType(c.type, syms.deprecatedType)) { |
aoqi@0 | 958 | s.flags_field |= Flags.DEPRECATED; |
aoqi@0 | 959 | } |
aoqi@0 | 960 | } |
aoqi@0 | 961 | |
aoqi@0 | 962 | s.setDeclarationAttributesWithCompletion( |
aoqi@0 | 963 | annotate.new AnnotateRepeatedContext<Attribute.Compound>(env, annotated, pos, log, false)); |
aoqi@0 | 964 | } |
aoqi@0 | 965 | |
aoqi@0 | 966 | /** Queue processing of an attribute default value. */ |
aoqi@0 | 967 | void annotateDefaultValueLater(final JCExpression defaultValue, |
aoqi@0 | 968 | final Env<AttrContext> localEnv, |
aoqi@0 | 969 | final MethodSymbol m) { |
aoqi@0 | 970 | annotate.normal(new Annotate.Worker() { |
aoqi@0 | 971 | @Override |
aoqi@0 | 972 | public String toString() { |
aoqi@0 | 973 | return "annotate " + m.owner + "." + |
aoqi@0 | 974 | m + " default " + defaultValue; |
aoqi@0 | 975 | } |
aoqi@0 | 976 | |
aoqi@0 | 977 | @Override |
aoqi@0 | 978 | public void run() { |
aoqi@0 | 979 | JavaFileObject prev = log.useSource(localEnv.toplevel.sourcefile); |
aoqi@0 | 980 | try { |
aoqi@0 | 981 | enterDefaultValue(defaultValue, localEnv, m); |
aoqi@0 | 982 | } finally { |
aoqi@0 | 983 | log.useSource(prev); |
aoqi@0 | 984 | } |
aoqi@0 | 985 | } |
aoqi@0 | 986 | }); |
aoqi@0 | 987 | annotate.validate(new Annotate.Worker() { //validate annotations |
aoqi@0 | 988 | @Override |
aoqi@0 | 989 | public void run() { |
aoqi@0 | 990 | JavaFileObject prev = log.useSource(localEnv.toplevel.sourcefile); |
aoqi@0 | 991 | try { |
aoqi@0 | 992 | // if default value is an annotation, check it is a well-formed |
aoqi@0 | 993 | // annotation value (e.g. no duplicate values, no missing values, etc.) |
aoqi@0 | 994 | chk.validateAnnotationTree(defaultValue); |
aoqi@0 | 995 | } finally { |
aoqi@0 | 996 | log.useSource(prev); |
aoqi@0 | 997 | } |
aoqi@0 | 998 | } |
aoqi@0 | 999 | }); |
aoqi@0 | 1000 | } |
aoqi@0 | 1001 | |
aoqi@0 | 1002 | /** Enter a default value for an attribute method. */ |
aoqi@0 | 1003 | private void enterDefaultValue(final JCExpression defaultValue, |
aoqi@0 | 1004 | final Env<AttrContext> localEnv, |
aoqi@0 | 1005 | final MethodSymbol m) { |
aoqi@0 | 1006 | m.defaultValue = annotate.enterAttributeValue(m.type.getReturnType(), |
aoqi@0 | 1007 | defaultValue, |
aoqi@0 | 1008 | localEnv); |
aoqi@0 | 1009 | } |
aoqi@0 | 1010 | |
aoqi@0 | 1011 | /* ******************************************************************** |
aoqi@0 | 1012 | * Source completer |
aoqi@0 | 1013 | *********************************************************************/ |
aoqi@0 | 1014 | |
aoqi@0 | 1015 | /** Complete entering a class. |
aoqi@0 | 1016 | * @param sym The symbol of the class to be completed. |
aoqi@0 | 1017 | */ |
aoqi@0 | 1018 | public void complete(Symbol sym) throws CompletionFailure { |
aoqi@0 | 1019 | // Suppress some (recursive) MemberEnter invocations |
aoqi@0 | 1020 | if (!completionEnabled) { |
aoqi@0 | 1021 | // Re-install same completer for next time around and return. |
aoqi@0 | 1022 | Assert.check((sym.flags() & Flags.COMPOUND) == 0); |
aoqi@0 | 1023 | sym.completer = this; |
aoqi@0 | 1024 | return; |
aoqi@0 | 1025 | } |
aoqi@0 | 1026 | |
aoqi@0 | 1027 | ClassSymbol c = (ClassSymbol)sym; |
aoqi@0 | 1028 | ClassType ct = (ClassType)c.type; |
aoqi@0 | 1029 | Env<AttrContext> env = typeEnvs.get(c); |
aoqi@0 | 1030 | JCClassDecl tree = (JCClassDecl)env.tree; |
aoqi@0 | 1031 | boolean wasFirst = isFirst; |
aoqi@0 | 1032 | isFirst = false; |
aoqi@0 | 1033 | |
aoqi@0 | 1034 | JavaFileObject prev = log.useSource(env.toplevel.sourcefile); |
aoqi@0 | 1035 | DiagnosticPosition prevLintPos = deferredLintHandler.setPos(tree.pos()); |
aoqi@0 | 1036 | try { |
aoqi@0 | 1037 | // Save class environment for later member enter (2) processing. |
aoqi@0 | 1038 | halfcompleted.append(env); |
aoqi@0 | 1039 | |
aoqi@0 | 1040 | // Mark class as not yet attributed. |
aoqi@0 | 1041 | c.flags_field |= UNATTRIBUTED; |
aoqi@0 | 1042 | |
aoqi@0 | 1043 | // If this is a toplevel-class, make sure any preceding import |
aoqi@0 | 1044 | // clauses have been seen. |
aoqi@0 | 1045 | if (c.owner.kind == PCK) { |
aoqi@0 | 1046 | memberEnter(env.toplevel, env.enclosing(TOPLEVEL)); |
aoqi@0 | 1047 | todo.append(env); |
aoqi@0 | 1048 | } |
aoqi@0 | 1049 | |
aoqi@0 | 1050 | if (c.owner.kind == TYP) |
aoqi@0 | 1051 | c.owner.complete(); |
aoqi@0 | 1052 | |
aoqi@0 | 1053 | // create an environment for evaluating the base clauses |
aoqi@0 | 1054 | Env<AttrContext> baseEnv = baseEnv(tree, env); |
aoqi@0 | 1055 | |
aoqi@0 | 1056 | if (tree.extending != null) |
aoqi@0 | 1057 | typeAnnotate(tree.extending, baseEnv, sym, tree.pos()); |
aoqi@0 | 1058 | for (JCExpression impl : tree.implementing) |
aoqi@0 | 1059 | typeAnnotate(impl, baseEnv, sym, tree.pos()); |
aoqi@0 | 1060 | annotate.flush(); |
aoqi@0 | 1061 | |
aoqi@0 | 1062 | // Determine supertype. |
aoqi@0 | 1063 | Type supertype = |
aoqi@0 | 1064 | (tree.extending != null) |
aoqi@0 | 1065 | ? attr.attribBase(tree.extending, baseEnv, true, false, true) |
aoqi@0 | 1066 | : ((tree.mods.flags & Flags.ENUM) != 0) |
aoqi@0 | 1067 | ? attr.attribBase(enumBase(tree.pos, c), baseEnv, |
aoqi@0 | 1068 | true, false, false) |
aoqi@0 | 1069 | : (c.fullname == names.java_lang_Object) |
aoqi@0 | 1070 | ? Type.noType |
aoqi@0 | 1071 | : syms.objectType; |
aoqi@0 | 1072 | ct.supertype_field = modelMissingTypes(supertype, tree.extending, false); |
aoqi@0 | 1073 | |
aoqi@0 | 1074 | // Determine interfaces. |
aoqi@0 | 1075 | ListBuffer<Type> interfaces = new ListBuffer<Type>(); |
aoqi@0 | 1076 | ListBuffer<Type> all_interfaces = null; // lazy init |
aoqi@0 | 1077 | Set<Type> interfaceSet = new HashSet<Type>(); |
aoqi@0 | 1078 | List<JCExpression> interfaceTrees = tree.implementing; |
aoqi@0 | 1079 | for (JCExpression iface : interfaceTrees) { |
aoqi@0 | 1080 | Type i = attr.attribBase(iface, baseEnv, false, true, true); |
aoqi@0 | 1081 | if (i.hasTag(CLASS)) { |
aoqi@0 | 1082 | interfaces.append(i); |
aoqi@0 | 1083 | if (all_interfaces != null) all_interfaces.append(i); |
aoqi@0 | 1084 | chk.checkNotRepeated(iface.pos(), types.erasure(i), interfaceSet); |
aoqi@0 | 1085 | } else { |
aoqi@0 | 1086 | if (all_interfaces == null) |
aoqi@0 | 1087 | all_interfaces = new ListBuffer<Type>().appendList(interfaces); |
aoqi@0 | 1088 | all_interfaces.append(modelMissingTypes(i, iface, true)); |
aoqi@0 | 1089 | } |
aoqi@0 | 1090 | } |
aoqi@0 | 1091 | if ((c.flags_field & ANNOTATION) != 0) { |
aoqi@0 | 1092 | ct.interfaces_field = List.of(syms.annotationType); |
aoqi@0 | 1093 | ct.all_interfaces_field = ct.interfaces_field; |
aoqi@0 | 1094 | } else { |
aoqi@0 | 1095 | ct.interfaces_field = interfaces.toList(); |
aoqi@0 | 1096 | ct.all_interfaces_field = (all_interfaces == null) |
aoqi@0 | 1097 | ? ct.interfaces_field : all_interfaces.toList(); |
aoqi@0 | 1098 | } |
aoqi@0 | 1099 | |
aoqi@0 | 1100 | if (c.fullname == names.java_lang_Object) { |
aoqi@0 | 1101 | if (tree.extending != null) { |
aoqi@0 | 1102 | chk.checkNonCyclic(tree.extending.pos(), |
aoqi@0 | 1103 | supertype); |
aoqi@0 | 1104 | ct.supertype_field = Type.noType; |
aoqi@0 | 1105 | } |
aoqi@0 | 1106 | else if (tree.implementing.nonEmpty()) { |
aoqi@0 | 1107 | chk.checkNonCyclic(tree.implementing.head.pos(), |
aoqi@0 | 1108 | ct.interfaces_field.head); |
aoqi@0 | 1109 | ct.interfaces_field = List.nil(); |
aoqi@0 | 1110 | } |
aoqi@0 | 1111 | } |
aoqi@0 | 1112 | |
aoqi@0 | 1113 | // Annotations. |
aoqi@0 | 1114 | // In general, we cannot fully process annotations yet, but we |
aoqi@0 | 1115 | // can attribute the annotation types and then check to see if the |
aoqi@0 | 1116 | // @Deprecated annotation is present. |
aoqi@0 | 1117 | attr.attribAnnotationTypes(tree.mods.annotations, baseEnv); |
aoqi@0 | 1118 | if (hasDeprecatedAnnotation(tree.mods.annotations)) |
aoqi@0 | 1119 | c.flags_field |= DEPRECATED; |
aoqi@0 | 1120 | annotateLater(tree.mods.annotations, baseEnv, c, tree.pos()); |
aoqi@0 | 1121 | // class type parameters use baseEnv but everything uses env |
aoqi@0 | 1122 | |
aoqi@0 | 1123 | chk.checkNonCyclicDecl(tree); |
aoqi@0 | 1124 | |
aoqi@0 | 1125 | attr.attribTypeVariables(tree.typarams, baseEnv); |
aoqi@0 | 1126 | // Do this here, where we have the symbol. |
aoqi@0 | 1127 | for (JCTypeParameter tp : tree.typarams) |
aoqi@0 | 1128 | typeAnnotate(tp, baseEnv, sym, tree.pos()); |
aoqi@0 | 1129 | |
aoqi@0 | 1130 | // Add default constructor if needed. |
aoqi@0 | 1131 | if ((c.flags() & INTERFACE) == 0 && |
aoqi@0 | 1132 | !TreeInfo.hasConstructors(tree.defs)) { |
aoqi@0 | 1133 | List<Type> argtypes = List.nil(); |
aoqi@0 | 1134 | List<Type> typarams = List.nil(); |
aoqi@0 | 1135 | List<Type> thrown = List.nil(); |
aoqi@0 | 1136 | long ctorFlags = 0; |
aoqi@0 | 1137 | boolean based = false; |
aoqi@0 | 1138 | boolean addConstructor = true; |
aoqi@0 | 1139 | JCNewClass nc = null; |
aoqi@0 | 1140 | if (c.name.isEmpty()) { |
aoqi@0 | 1141 | nc = (JCNewClass)env.next.tree; |
aoqi@0 | 1142 | if (nc.constructor != null) { |
aoqi@0 | 1143 | addConstructor = nc.constructor.kind != ERR; |
aoqi@0 | 1144 | Type superConstrType = types.memberType(c.type, |
aoqi@0 | 1145 | nc.constructor); |
aoqi@0 | 1146 | argtypes = superConstrType.getParameterTypes(); |
aoqi@0 | 1147 | typarams = superConstrType.getTypeArguments(); |
aoqi@0 | 1148 | ctorFlags = nc.constructor.flags() & VARARGS; |
aoqi@0 | 1149 | if (nc.encl != null) { |
aoqi@0 | 1150 | argtypes = argtypes.prepend(nc.encl.type); |
aoqi@0 | 1151 | based = true; |
aoqi@0 | 1152 | } |
aoqi@0 | 1153 | thrown = superConstrType.getThrownTypes(); |
aoqi@0 | 1154 | } |
aoqi@0 | 1155 | } |
aoqi@0 | 1156 | if (addConstructor) { |
aoqi@0 | 1157 | MethodSymbol basedConstructor = nc != null ? |
aoqi@0 | 1158 | (MethodSymbol)nc.constructor : null; |
aoqi@0 | 1159 | JCTree constrDef = DefaultConstructor(make.at(tree.pos), c, |
aoqi@0 | 1160 | basedConstructor, |
aoqi@0 | 1161 | typarams, argtypes, thrown, |
aoqi@0 | 1162 | ctorFlags, based); |
aoqi@0 | 1163 | tree.defs = tree.defs.prepend(constrDef); |
aoqi@0 | 1164 | } |
aoqi@0 | 1165 | } |
aoqi@0 | 1166 | |
aoqi@0 | 1167 | // enter symbols for 'this' into current scope. |
aoqi@0 | 1168 | VarSymbol thisSym = |
aoqi@0 | 1169 | new VarSymbol(FINAL | HASINIT, names._this, c.type, c); |
aoqi@0 | 1170 | thisSym.pos = Position.FIRSTPOS; |
aoqi@0 | 1171 | env.info.scope.enter(thisSym); |
aoqi@0 | 1172 | // if this is a class, enter symbol for 'super' into current scope. |
aoqi@0 | 1173 | if ((c.flags_field & INTERFACE) == 0 && |
aoqi@0 | 1174 | ct.supertype_field.hasTag(CLASS)) { |
aoqi@0 | 1175 | VarSymbol superSym = |
aoqi@0 | 1176 | new VarSymbol(FINAL | HASINIT, names._super, |
aoqi@0 | 1177 | ct.supertype_field, c); |
aoqi@0 | 1178 | superSym.pos = Position.FIRSTPOS; |
aoqi@0 | 1179 | env.info.scope.enter(superSym); |
aoqi@0 | 1180 | } |
aoqi@0 | 1181 | |
aoqi@0 | 1182 | // check that no package exists with same fully qualified name, |
aoqi@0 | 1183 | // but admit classes in the unnamed package which have the same |
aoqi@0 | 1184 | // name as a top-level package. |
aoqi@0 | 1185 | if (checkClash && |
aoqi@0 | 1186 | c.owner.kind == PCK && c.owner != syms.unnamedPackage && |
aoqi@0 | 1187 | reader.packageExists(c.fullname)) { |
aoqi@0 | 1188 | log.error(tree.pos, "clash.with.pkg.of.same.name", Kinds.kindName(sym), c); |
aoqi@0 | 1189 | } |
aoqi@0 | 1190 | if (c.owner.kind == PCK && (c.flags_field & PUBLIC) == 0 && |
aoqi@0 | 1191 | !env.toplevel.sourcefile.isNameCompatible(c.name.toString(),JavaFileObject.Kind.SOURCE)) { |
aoqi@0 | 1192 | c.flags_field |= AUXILIARY; |
aoqi@0 | 1193 | } |
aoqi@0 | 1194 | } catch (CompletionFailure ex) { |
aoqi@0 | 1195 | chk.completionError(tree.pos(), ex); |
aoqi@0 | 1196 | } finally { |
aoqi@0 | 1197 | deferredLintHandler.setPos(prevLintPos); |
aoqi@0 | 1198 | log.useSource(prev); |
aoqi@0 | 1199 | } |
aoqi@0 | 1200 | |
aoqi@0 | 1201 | // Enter all member fields and methods of a set of half completed |
aoqi@0 | 1202 | // classes in a second phase. |
aoqi@0 | 1203 | if (wasFirst) { |
aoqi@0 | 1204 | try { |
aoqi@0 | 1205 | while (halfcompleted.nonEmpty()) { |
aoqi@0 | 1206 | Env<AttrContext> toFinish = halfcompleted.next(); |
aoqi@0 | 1207 | finish(toFinish); |
aoqi@0 | 1208 | if (allowTypeAnnos) { |
aoqi@0 | 1209 | typeAnnotations.organizeTypeAnnotationsSignatures(toFinish, (JCClassDecl)toFinish.tree); |
aoqi@0 | 1210 | typeAnnotations.validateTypeAnnotationsSignatures(toFinish, (JCClassDecl)toFinish.tree); |
aoqi@0 | 1211 | } |
aoqi@0 | 1212 | } |
aoqi@0 | 1213 | } finally { |
aoqi@0 | 1214 | isFirst = true; |
aoqi@0 | 1215 | } |
aoqi@0 | 1216 | } |
aoqi@0 | 1217 | } |
aoqi@0 | 1218 | |
aoqi@0 | 1219 | /* |
aoqi@0 | 1220 | * If the symbol is non-null, attach the type annotation to it. |
aoqi@0 | 1221 | */ |
aoqi@0 | 1222 | private void actualEnterTypeAnnotations(final List<JCAnnotation> annotations, |
aoqi@0 | 1223 | final Env<AttrContext> env, |
aoqi@0 | 1224 | final Symbol s) { |
aoqi@0 | 1225 | Map<TypeSymbol, ListBuffer<Attribute.TypeCompound>> annotated = |
aoqi@0 | 1226 | new LinkedHashMap<TypeSymbol, ListBuffer<Attribute.TypeCompound>>(); |
aoqi@0 | 1227 | Map<Attribute.TypeCompound, DiagnosticPosition> pos = |
aoqi@0 | 1228 | new HashMap<Attribute.TypeCompound, DiagnosticPosition>(); |
aoqi@0 | 1229 | |
aoqi@0 | 1230 | for (List<JCAnnotation> al = annotations; !al.isEmpty(); al = al.tail) { |
aoqi@0 | 1231 | JCAnnotation a = al.head; |
aoqi@0 | 1232 | Attribute.TypeCompound tc = annotate.enterTypeAnnotation(a, |
aoqi@0 | 1233 | syms.annotationType, |
aoqi@0 | 1234 | env); |
aoqi@0 | 1235 | if (tc == null) { |
aoqi@0 | 1236 | continue; |
aoqi@0 | 1237 | } |
aoqi@0 | 1238 | |
aoqi@0 | 1239 | if (annotated.containsKey(a.type.tsym)) { |
aoqi@0 | 1240 | if (source.allowRepeatedAnnotations()) { |
aoqi@0 | 1241 | ListBuffer<Attribute.TypeCompound> l = annotated.get(a.type.tsym); |
aoqi@0 | 1242 | l = l.append(tc); |
aoqi@0 | 1243 | annotated.put(a.type.tsym, l); |
aoqi@0 | 1244 | pos.put(tc, a.pos()); |
aoqi@0 | 1245 | } else { |
aoqi@0 | 1246 | log.error(a.pos(), "repeatable.annotations.not.supported.in.source"); |
aoqi@0 | 1247 | } |
aoqi@0 | 1248 | } else { |
aoqi@0 | 1249 | annotated.put(a.type.tsym, ListBuffer.of(tc)); |
aoqi@0 | 1250 | pos.put(tc, a.pos()); |
aoqi@0 | 1251 | } |
aoqi@0 | 1252 | } |
aoqi@0 | 1253 | |
aoqi@0 | 1254 | if (s != null) { |
aoqi@0 | 1255 | s.appendTypeAttributesWithCompletion( |
aoqi@0 | 1256 | annotate.new AnnotateRepeatedContext<Attribute.TypeCompound>(env, annotated, pos, log, true)); |
aoqi@0 | 1257 | } |
aoqi@0 | 1258 | } |
aoqi@0 | 1259 | |
aoqi@0 | 1260 | public void typeAnnotate(final JCTree tree, final Env<AttrContext> env, final Symbol sym, DiagnosticPosition deferPos) { |
aoqi@0 | 1261 | if (allowTypeAnnos) { |
aoqi@0 | 1262 | tree.accept(new TypeAnnotate(env, sym, deferPos)); |
aoqi@0 | 1263 | } |
aoqi@0 | 1264 | } |
aoqi@0 | 1265 | |
aoqi@0 | 1266 | /** |
aoqi@0 | 1267 | * We need to use a TreeScanner, because it is not enough to visit the top-level |
aoqi@0 | 1268 | * annotations. We also need to visit type arguments, etc. |
aoqi@0 | 1269 | */ |
aoqi@0 | 1270 | private class TypeAnnotate extends TreeScanner { |
aoqi@0 | 1271 | private Env<AttrContext> env; |
aoqi@0 | 1272 | private Symbol sym; |
aoqi@0 | 1273 | private DiagnosticPosition deferPos; |
aoqi@0 | 1274 | |
aoqi@0 | 1275 | public TypeAnnotate(final Env<AttrContext> env, final Symbol sym, DiagnosticPosition deferPos) { |
aoqi@0 | 1276 | this.env = env; |
aoqi@0 | 1277 | this.sym = sym; |
aoqi@0 | 1278 | this.deferPos = deferPos; |
aoqi@0 | 1279 | } |
aoqi@0 | 1280 | |
aoqi@0 | 1281 | void annotateTypeLater(final List<JCAnnotation> annotations) { |
aoqi@0 | 1282 | if (annotations.isEmpty()) { |
aoqi@0 | 1283 | return; |
aoqi@0 | 1284 | } |
aoqi@0 | 1285 | |
aoqi@0 | 1286 | final DiagnosticPosition deferPos = this.deferPos; |
aoqi@0 | 1287 | |
aoqi@0 | 1288 | annotate.normal(new Annotate.Worker() { |
aoqi@0 | 1289 | @Override |
aoqi@0 | 1290 | public String toString() { |
aoqi@0 | 1291 | return "type annotate " + annotations + " onto " + sym + " in " + sym.owner; |
aoqi@0 | 1292 | } |
aoqi@0 | 1293 | @Override |
aoqi@0 | 1294 | public void run() { |
aoqi@0 | 1295 | JavaFileObject prev = log.useSource(env.toplevel.sourcefile); |
aoqi@0 | 1296 | DiagnosticPosition prevLintPos = null; |
aoqi@0 | 1297 | |
aoqi@0 | 1298 | if (deferPos != null) { |
aoqi@0 | 1299 | prevLintPos = deferredLintHandler.setPos(deferPos); |
aoqi@0 | 1300 | } |
aoqi@0 | 1301 | try { |
aoqi@0 | 1302 | actualEnterTypeAnnotations(annotations, env, sym); |
aoqi@0 | 1303 | } finally { |
aoqi@0 | 1304 | if (prevLintPos != null) |
aoqi@0 | 1305 | deferredLintHandler.setPos(prevLintPos); |
aoqi@0 | 1306 | log.useSource(prev); |
aoqi@0 | 1307 | } |
aoqi@0 | 1308 | } |
aoqi@0 | 1309 | }); |
aoqi@0 | 1310 | } |
aoqi@0 | 1311 | |
aoqi@0 | 1312 | @Override |
aoqi@0 | 1313 | public void visitAnnotatedType(final JCAnnotatedType tree) { |
aoqi@0 | 1314 | annotateTypeLater(tree.annotations); |
aoqi@0 | 1315 | super.visitAnnotatedType(tree); |
aoqi@0 | 1316 | } |
aoqi@0 | 1317 | |
aoqi@0 | 1318 | @Override |
aoqi@0 | 1319 | public void visitTypeParameter(final JCTypeParameter tree) { |
aoqi@0 | 1320 | annotateTypeLater(tree.annotations); |
aoqi@0 | 1321 | super.visitTypeParameter(tree); |
aoqi@0 | 1322 | } |
aoqi@0 | 1323 | |
aoqi@0 | 1324 | @Override |
aoqi@0 | 1325 | public void visitNewArray(final JCNewArray tree) { |
aoqi@0 | 1326 | annotateTypeLater(tree.annotations); |
aoqi@0 | 1327 | for (List<JCAnnotation> dimAnnos : tree.dimAnnotations) |
aoqi@0 | 1328 | annotateTypeLater(dimAnnos); |
aoqi@0 | 1329 | super.visitNewArray(tree); |
aoqi@0 | 1330 | } |
aoqi@0 | 1331 | |
aoqi@0 | 1332 | @Override |
aoqi@0 | 1333 | public void visitMethodDef(final JCMethodDecl tree) { |
aoqi@0 | 1334 | scan(tree.mods); |
aoqi@0 | 1335 | scan(tree.restype); |
aoqi@0 | 1336 | scan(tree.typarams); |
aoqi@0 | 1337 | scan(tree.recvparam); |
aoqi@0 | 1338 | scan(tree.params); |
aoqi@0 | 1339 | scan(tree.thrown); |
aoqi@0 | 1340 | scan(tree.defaultValue); |
aoqi@0 | 1341 | // Do not annotate the body, just the signature. |
aoqi@0 | 1342 | // scan(tree.body); |
aoqi@0 | 1343 | } |
aoqi@0 | 1344 | |
aoqi@0 | 1345 | @Override |
aoqi@0 | 1346 | public void visitVarDef(final JCVariableDecl tree) { |
aoqi@0 | 1347 | DiagnosticPosition prevPos = deferPos; |
aoqi@0 | 1348 | deferPos = tree.pos(); |
aoqi@0 | 1349 | try { |
aoqi@0 | 1350 | if (sym != null && sym.kind == Kinds.VAR) { |
aoqi@0 | 1351 | // Don't visit a parameter once when the sym is the method |
aoqi@0 | 1352 | // and once when the sym is the parameter. |
aoqi@0 | 1353 | scan(tree.mods); |
aoqi@0 | 1354 | scan(tree.vartype); |
aoqi@0 | 1355 | } |
aoqi@0 | 1356 | scan(tree.init); |
aoqi@0 | 1357 | } finally { |
aoqi@0 | 1358 | deferPos = prevPos; |
aoqi@0 | 1359 | } |
aoqi@0 | 1360 | } |
aoqi@0 | 1361 | |
aoqi@0 | 1362 | @Override |
aoqi@0 | 1363 | public void visitClassDef(JCClassDecl tree) { |
aoqi@0 | 1364 | // We can only hit a classdef if it is declared within |
aoqi@0 | 1365 | // a method. Ignore it - the class will be visited |
aoqi@0 | 1366 | // separately later. |
aoqi@0 | 1367 | } |
aoqi@0 | 1368 | |
aoqi@0 | 1369 | @Override |
aoqi@0 | 1370 | public void visitNewClass(JCNewClass tree) { |
aoqi@0 | 1371 | if (tree.def == null) { |
aoqi@0 | 1372 | // For an anonymous class instantiation the class |
aoqi@0 | 1373 | // will be visited separately. |
aoqi@0 | 1374 | super.visitNewClass(tree); |
aoqi@0 | 1375 | } |
aoqi@0 | 1376 | } |
aoqi@0 | 1377 | } |
aoqi@0 | 1378 | |
aoqi@0 | 1379 | |
aoqi@0 | 1380 | private Env<AttrContext> baseEnv(JCClassDecl tree, Env<AttrContext> env) { |
aoqi@0 | 1381 | Scope baseScope = new Scope(tree.sym); |
aoqi@0 | 1382 | //import already entered local classes into base scope |
aoqi@0 | 1383 | for (Scope.Entry e = env.outer.info.scope.elems ; e != null ; e = e.sibling) { |
aoqi@0 | 1384 | if (e.sym.isLocal()) { |
aoqi@0 | 1385 | baseScope.enter(e.sym); |
aoqi@0 | 1386 | } |
aoqi@0 | 1387 | } |
aoqi@0 | 1388 | //import current type-parameters into base scope |
aoqi@0 | 1389 | if (tree.typarams != null) |
aoqi@0 | 1390 | for (List<JCTypeParameter> typarams = tree.typarams; |
aoqi@0 | 1391 | typarams.nonEmpty(); |
aoqi@0 | 1392 | typarams = typarams.tail) |
aoqi@0 | 1393 | baseScope.enter(typarams.head.type.tsym); |
aoqi@0 | 1394 | Env<AttrContext> outer = env.outer; // the base clause can't see members of this class |
aoqi@0 | 1395 | Env<AttrContext> localEnv = outer.dup(tree, outer.info.dup(baseScope)); |
aoqi@0 | 1396 | localEnv.baseClause = true; |
aoqi@0 | 1397 | localEnv.outer = outer; |
aoqi@0 | 1398 | localEnv.info.isSelfCall = false; |
aoqi@0 | 1399 | return localEnv; |
aoqi@0 | 1400 | } |
aoqi@0 | 1401 | |
aoqi@0 | 1402 | /** Enter member fields and methods of a class |
aoqi@0 | 1403 | * @param env the environment current for the class block. |
aoqi@0 | 1404 | */ |
aoqi@0 | 1405 | private void finish(Env<AttrContext> env) { |
aoqi@0 | 1406 | JavaFileObject prev = log.useSource(env.toplevel.sourcefile); |
aoqi@0 | 1407 | try { |
aoqi@0 | 1408 | JCClassDecl tree = (JCClassDecl)env.tree; |
aoqi@0 | 1409 | finishClass(tree, env); |
aoqi@0 | 1410 | } finally { |
aoqi@0 | 1411 | log.useSource(prev); |
aoqi@0 | 1412 | } |
aoqi@0 | 1413 | } |
aoqi@0 | 1414 | |
aoqi@0 | 1415 | /** Generate a base clause for an enum type. |
aoqi@0 | 1416 | * @param pos The position for trees and diagnostics, if any |
aoqi@0 | 1417 | * @param c The class symbol of the enum |
aoqi@0 | 1418 | */ |
aoqi@0 | 1419 | private JCExpression enumBase(int pos, ClassSymbol c) { |
aoqi@0 | 1420 | JCExpression result = make.at(pos). |
aoqi@0 | 1421 | TypeApply(make.QualIdent(syms.enumSym), |
aoqi@0 | 1422 | List.<JCExpression>of(make.Type(c.type))); |
aoqi@0 | 1423 | return result; |
aoqi@0 | 1424 | } |
aoqi@0 | 1425 | |
aoqi@0 | 1426 | Type modelMissingTypes(Type t, final JCExpression tree, final boolean interfaceExpected) { |
aoqi@0 | 1427 | if (!t.hasTag(ERROR)) |
aoqi@0 | 1428 | return t; |
aoqi@0 | 1429 | |
aoqi@0 | 1430 | return new ErrorType(t.getOriginalType(), t.tsym) { |
aoqi@0 | 1431 | private Type modelType; |
aoqi@0 | 1432 | |
aoqi@0 | 1433 | @Override |
aoqi@0 | 1434 | public Type getModelType() { |
aoqi@0 | 1435 | if (modelType == null) |
aoqi@0 | 1436 | modelType = new Synthesizer(getOriginalType(), interfaceExpected).visit(tree); |
aoqi@0 | 1437 | return modelType; |
aoqi@0 | 1438 | } |
aoqi@0 | 1439 | }; |
aoqi@0 | 1440 | } |
aoqi@0 | 1441 | // where |
aoqi@0 | 1442 | private class Synthesizer extends JCTree.Visitor { |
aoqi@0 | 1443 | Type originalType; |
aoqi@0 | 1444 | boolean interfaceExpected; |
aoqi@0 | 1445 | List<ClassSymbol> synthesizedSymbols = List.nil(); |
aoqi@0 | 1446 | Type result; |
aoqi@0 | 1447 | |
aoqi@0 | 1448 | Synthesizer(Type originalType, boolean interfaceExpected) { |
aoqi@0 | 1449 | this.originalType = originalType; |
aoqi@0 | 1450 | this.interfaceExpected = interfaceExpected; |
aoqi@0 | 1451 | } |
aoqi@0 | 1452 | |
aoqi@0 | 1453 | Type visit(JCTree tree) { |
aoqi@0 | 1454 | tree.accept(this); |
aoqi@0 | 1455 | return result; |
aoqi@0 | 1456 | } |
aoqi@0 | 1457 | |
aoqi@0 | 1458 | List<Type> visit(List<? extends JCTree> trees) { |
aoqi@0 | 1459 | ListBuffer<Type> lb = new ListBuffer<Type>(); |
aoqi@0 | 1460 | for (JCTree t: trees) |
aoqi@0 | 1461 | lb.append(visit(t)); |
aoqi@0 | 1462 | return lb.toList(); |
aoqi@0 | 1463 | } |
aoqi@0 | 1464 | |
aoqi@0 | 1465 | @Override |
aoqi@0 | 1466 | public void visitTree(JCTree tree) { |
aoqi@0 | 1467 | result = syms.errType; |
aoqi@0 | 1468 | } |
aoqi@0 | 1469 | |
aoqi@0 | 1470 | @Override |
aoqi@0 | 1471 | public void visitIdent(JCIdent tree) { |
aoqi@0 | 1472 | if (!tree.type.hasTag(ERROR)) { |
aoqi@0 | 1473 | result = tree.type; |
aoqi@0 | 1474 | } else { |
aoqi@0 | 1475 | result = synthesizeClass(tree.name, syms.unnamedPackage).type; |
aoqi@0 | 1476 | } |
aoqi@0 | 1477 | } |
aoqi@0 | 1478 | |
aoqi@0 | 1479 | @Override |
aoqi@0 | 1480 | public void visitSelect(JCFieldAccess tree) { |
aoqi@0 | 1481 | if (!tree.type.hasTag(ERROR)) { |
aoqi@0 | 1482 | result = tree.type; |
aoqi@0 | 1483 | } else { |
aoqi@0 | 1484 | Type selectedType; |
aoqi@0 | 1485 | boolean prev = interfaceExpected; |
aoqi@0 | 1486 | try { |
aoqi@0 | 1487 | interfaceExpected = false; |
aoqi@0 | 1488 | selectedType = visit(tree.selected); |
aoqi@0 | 1489 | } finally { |
aoqi@0 | 1490 | interfaceExpected = prev; |
aoqi@0 | 1491 | } |
aoqi@0 | 1492 | ClassSymbol c = synthesizeClass(tree.name, selectedType.tsym); |
aoqi@0 | 1493 | result = c.type; |
aoqi@0 | 1494 | } |
aoqi@0 | 1495 | } |
aoqi@0 | 1496 | |
aoqi@0 | 1497 | @Override |
aoqi@0 | 1498 | public void visitTypeApply(JCTypeApply tree) { |
aoqi@0 | 1499 | if (!tree.type.hasTag(ERROR)) { |
aoqi@0 | 1500 | result = tree.type; |
aoqi@0 | 1501 | } else { |
aoqi@0 | 1502 | ClassType clazzType = (ClassType) visit(tree.clazz); |
aoqi@0 | 1503 | if (synthesizedSymbols.contains(clazzType.tsym)) |
aoqi@0 | 1504 | synthesizeTyparams((ClassSymbol) clazzType.tsym, tree.arguments.size()); |
aoqi@0 | 1505 | final List<Type> actuals = visit(tree.arguments); |
aoqi@0 | 1506 | result = new ErrorType(tree.type, clazzType.tsym) { |
aoqi@0 | 1507 | @Override |
aoqi@0 | 1508 | public List<Type> getTypeArguments() { |
aoqi@0 | 1509 | return actuals; |
aoqi@0 | 1510 | } |
aoqi@0 | 1511 | }; |
aoqi@0 | 1512 | } |
aoqi@0 | 1513 | } |
aoqi@0 | 1514 | |
aoqi@0 | 1515 | ClassSymbol synthesizeClass(Name name, Symbol owner) { |
aoqi@0 | 1516 | int flags = interfaceExpected ? INTERFACE : 0; |
aoqi@0 | 1517 | ClassSymbol c = new ClassSymbol(flags, name, owner); |
aoqi@0 | 1518 | c.members_field = new Scope.ErrorScope(c); |
aoqi@0 | 1519 | c.type = new ErrorType(originalType, c) { |
aoqi@0 | 1520 | @Override |
aoqi@0 | 1521 | public List<Type> getTypeArguments() { |
aoqi@0 | 1522 | return typarams_field; |
aoqi@0 | 1523 | } |
aoqi@0 | 1524 | }; |
aoqi@0 | 1525 | synthesizedSymbols = synthesizedSymbols.prepend(c); |
aoqi@0 | 1526 | return c; |
aoqi@0 | 1527 | } |
aoqi@0 | 1528 | |
aoqi@0 | 1529 | void synthesizeTyparams(ClassSymbol sym, int n) { |
aoqi@0 | 1530 | ClassType ct = (ClassType) sym.type; |
aoqi@0 | 1531 | Assert.check(ct.typarams_field.isEmpty()); |
aoqi@0 | 1532 | if (n == 1) { |
aoqi@0 | 1533 | TypeVar v = new TypeVar(names.fromString("T"), sym, syms.botType); |
aoqi@0 | 1534 | ct.typarams_field = ct.typarams_field.prepend(v); |
aoqi@0 | 1535 | } else { |
aoqi@0 | 1536 | for (int i = n; i > 0; i--) { |
aoqi@0 | 1537 | TypeVar v = new TypeVar(names.fromString("T" + i), sym, syms.botType); |
aoqi@0 | 1538 | ct.typarams_field = ct.typarams_field.prepend(v); |
aoqi@0 | 1539 | } |
aoqi@0 | 1540 | } |
aoqi@0 | 1541 | } |
aoqi@0 | 1542 | } |
aoqi@0 | 1543 | |
aoqi@0 | 1544 | |
aoqi@0 | 1545 | /* *************************************************************************** |
aoqi@0 | 1546 | * tree building |
aoqi@0 | 1547 | ****************************************************************************/ |
aoqi@0 | 1548 | |
aoqi@0 | 1549 | /** Generate default constructor for given class. For classes different |
aoqi@0 | 1550 | * from java.lang.Object, this is: |
aoqi@0 | 1551 | * |
aoqi@0 | 1552 | * c(argtype_0 x_0, ..., argtype_n x_n) throws thrown { |
aoqi@0 | 1553 | * super(x_0, ..., x_n) |
aoqi@0 | 1554 | * } |
aoqi@0 | 1555 | * |
aoqi@0 | 1556 | * or, if based == true: |
aoqi@0 | 1557 | * |
aoqi@0 | 1558 | * c(argtype_0 x_0, ..., argtype_n x_n) throws thrown { |
aoqi@0 | 1559 | * x_0.super(x_1, ..., x_n) |
aoqi@0 | 1560 | * } |
aoqi@0 | 1561 | * |
aoqi@0 | 1562 | * @param make The tree factory. |
aoqi@0 | 1563 | * @param c The class owning the default constructor. |
aoqi@0 | 1564 | * @param argtypes The parameter types of the constructor. |
aoqi@0 | 1565 | * @param thrown The thrown exceptions of the constructor. |
aoqi@0 | 1566 | * @param based Is first parameter a this$n? |
aoqi@0 | 1567 | */ |
aoqi@0 | 1568 | JCTree DefaultConstructor(TreeMaker make, |
aoqi@0 | 1569 | ClassSymbol c, |
aoqi@0 | 1570 | MethodSymbol baseInit, |
aoqi@0 | 1571 | List<Type> typarams, |
aoqi@0 | 1572 | List<Type> argtypes, |
aoqi@0 | 1573 | List<Type> thrown, |
aoqi@0 | 1574 | long flags, |
aoqi@0 | 1575 | boolean based) { |
aoqi@0 | 1576 | JCTree result; |
aoqi@0 | 1577 | if ((c.flags() & ENUM) != 0 && |
aoqi@0 | 1578 | (types.supertype(c.type).tsym == syms.enumSym)) { |
aoqi@0 | 1579 | // constructors of true enums are private |
aoqi@0 | 1580 | flags = (flags & ~AccessFlags) | PRIVATE | GENERATEDCONSTR; |
aoqi@0 | 1581 | } else |
aoqi@0 | 1582 | flags |= (c.flags() & AccessFlags) | GENERATEDCONSTR; |
aoqi@0 | 1583 | if (c.name.isEmpty()) { |
aoqi@0 | 1584 | flags |= ANONCONSTR; |
aoqi@0 | 1585 | } |
aoqi@0 | 1586 | Type mType = new MethodType(argtypes, null, thrown, c); |
aoqi@0 | 1587 | Type initType = typarams.nonEmpty() ? |
aoqi@0 | 1588 | new ForAll(typarams, mType) : |
aoqi@0 | 1589 | mType; |
aoqi@0 | 1590 | MethodSymbol init = new MethodSymbol(flags, names.init, |
aoqi@0 | 1591 | initType, c); |
aoqi@0 | 1592 | init.params = createDefaultConstructorParams(make, baseInit, init, |
aoqi@0 | 1593 | argtypes, based); |
aoqi@0 | 1594 | List<JCVariableDecl> params = make.Params(argtypes, init); |
aoqi@0 | 1595 | List<JCStatement> stats = List.nil(); |
aoqi@0 | 1596 | if (c.type != syms.objectType) { |
aoqi@0 | 1597 | stats = stats.prepend(SuperCall(make, typarams, params, based)); |
aoqi@0 | 1598 | } |
aoqi@0 | 1599 | result = make.MethodDef(init, make.Block(0, stats)); |
aoqi@0 | 1600 | return result; |
aoqi@0 | 1601 | } |
aoqi@0 | 1602 | |
aoqi@0 | 1603 | private List<VarSymbol> createDefaultConstructorParams( |
aoqi@0 | 1604 | TreeMaker make, |
aoqi@0 | 1605 | MethodSymbol baseInit, |
aoqi@0 | 1606 | MethodSymbol init, |
aoqi@0 | 1607 | List<Type> argtypes, |
aoqi@0 | 1608 | boolean based) { |
aoqi@0 | 1609 | List<VarSymbol> initParams = null; |
aoqi@0 | 1610 | List<Type> argTypesList = argtypes; |
aoqi@0 | 1611 | if (based) { |
aoqi@0 | 1612 | /* In this case argtypes will have an extra type, compared to baseInit, |
aoqi@0 | 1613 | * corresponding to the type of the enclosing instance i.e.: |
aoqi@0 | 1614 | * |
aoqi@0 | 1615 | * Inner i = outer.new Inner(1){} |
aoqi@0 | 1616 | * |
aoqi@0 | 1617 | * in the above example argtypes will be (Outer, int) and baseInit |
aoqi@0 | 1618 | * will have parameter's types (int). So in this case we have to add |
aoqi@0 | 1619 | * first the extra type in argtypes and then get the names of the |
aoqi@0 | 1620 | * parameters from baseInit. |
aoqi@0 | 1621 | */ |
aoqi@0 | 1622 | initParams = List.nil(); |
aoqi@0 | 1623 | VarSymbol param = new VarSymbol(PARAMETER, make.paramName(0), argtypes.head, init); |
aoqi@0 | 1624 | initParams = initParams.append(param); |
aoqi@0 | 1625 | argTypesList = argTypesList.tail; |
aoqi@0 | 1626 | } |
aoqi@0 | 1627 | if (baseInit != null && baseInit.params != null && |
aoqi@0 | 1628 | baseInit.params.nonEmpty() && argTypesList.nonEmpty()) { |
aoqi@0 | 1629 | initParams = (initParams == null) ? List.<VarSymbol>nil() : initParams; |
aoqi@0 | 1630 | List<VarSymbol> baseInitParams = baseInit.params; |
aoqi@0 | 1631 | while (baseInitParams.nonEmpty() && argTypesList.nonEmpty()) { |
aoqi@0 | 1632 | VarSymbol param = new VarSymbol(baseInitParams.head.flags() | PARAMETER, |
aoqi@0 | 1633 | baseInitParams.head.name, argTypesList.head, init); |
aoqi@0 | 1634 | initParams = initParams.append(param); |
aoqi@0 | 1635 | baseInitParams = baseInitParams.tail; |
aoqi@0 | 1636 | argTypesList = argTypesList.tail; |
aoqi@0 | 1637 | } |
aoqi@0 | 1638 | } |
aoqi@0 | 1639 | return initParams; |
aoqi@0 | 1640 | } |
aoqi@0 | 1641 | |
aoqi@0 | 1642 | /** Generate call to superclass constructor. This is: |
aoqi@0 | 1643 | * |
aoqi@0 | 1644 | * super(id_0, ..., id_n) |
aoqi@0 | 1645 | * |
aoqi@0 | 1646 | * or, if based == true |
aoqi@0 | 1647 | * |
aoqi@0 | 1648 | * id_0.super(id_1,...,id_n) |
aoqi@0 | 1649 | * |
aoqi@0 | 1650 | * where id_0, ..., id_n are the names of the given parameters. |
aoqi@0 | 1651 | * |
aoqi@0 | 1652 | * @param make The tree factory |
aoqi@0 | 1653 | * @param params The parameters that need to be passed to super |
aoqi@0 | 1654 | * @param typarams The type parameters that need to be passed to super |
aoqi@0 | 1655 | * @param based Is first parameter a this$n? |
aoqi@0 | 1656 | */ |
aoqi@0 | 1657 | JCExpressionStatement SuperCall(TreeMaker make, |
aoqi@0 | 1658 | List<Type> typarams, |
aoqi@0 | 1659 | List<JCVariableDecl> params, |
aoqi@0 | 1660 | boolean based) { |
aoqi@0 | 1661 | JCExpression meth; |
aoqi@0 | 1662 | if (based) { |
aoqi@0 | 1663 | meth = make.Select(make.Ident(params.head), names._super); |
aoqi@0 | 1664 | params = params.tail; |
aoqi@0 | 1665 | } else { |
aoqi@0 | 1666 | meth = make.Ident(names._super); |
aoqi@0 | 1667 | } |
aoqi@0 | 1668 | List<JCExpression> typeargs = typarams.nonEmpty() ? make.Types(typarams) : null; |
aoqi@0 | 1669 | return make.Exec(make.Apply(typeargs, meth, make.Idents(params))); |
aoqi@0 | 1670 | } |
aoqi@0 | 1671 | } |