src/share/classes/com/sun/tools/javac/comp/MemberEnter.java

Sat, 07 Nov 2020 10:30:02 +0800

author
aoqi
date
Sat, 07 Nov 2020 10:30:02 +0800
changeset 3938
93012e2a5d1d
parent 2702
9ca8d8713094
permissions
-rw-r--r--

Added tag mips-jdk8u275-b01 for changeset eb6ee6a5f2fe

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
jfranck@2596 578 DiagnosticPosition prevLintPos = deferredLintHandler.setPos(tree.pos());
aoqi@0 579 try {
jfranck@2596 580 // Compute the method type
jfranck@2596 581 m.type = signature(m, tree.typarams, tree.params,
jfranck@2596 582 tree.restype, tree.recvparam,
jfranck@2596 583 tree.thrown,
jfranck@2596 584 localEnv);
jfranck@2596 585 } finally {
jfranck@2596 586 deferredLintHandler.setPos(prevLintPos);
jfranck@2596 587 }
aoqi@0 588
jfranck@2596 589 if (types.isSignaturePolymorphic(m)) {
jfranck@2596 590 m.flags_field |= SIGNATURE_POLYMORPHIC;
jfranck@2596 591 }
aoqi@0 592
jfranck@2596 593 // Set m.params
jfranck@2596 594 ListBuffer<VarSymbol> params = new ListBuffer<VarSymbol>();
jfranck@2596 595 JCVariableDecl lastParam = null;
jfranck@2596 596 for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) {
jfranck@2596 597 JCVariableDecl param = lastParam = l.head;
jfranck@2596 598 params.append(Assert.checkNonNull(param.sym));
jfranck@2596 599 }
jfranck@2596 600 m.params = params.toList();
aoqi@0 601
jfranck@2596 602 // mark the method varargs, if necessary
jfranck@2596 603 if (lastParam != null && (lastParam.mods.flags & Flags.VARARGS) != 0)
jfranck@2596 604 m.flags_field |= Flags.VARARGS;
aoqi@0 605
jfranck@2596 606 localEnv.info.scope.leave();
jfranck@2596 607 if (chk.checkUnique(tree.pos(), m, enclScope)) {
jfranck@2596 608 enclScope.enter(m);
jfranck@2596 609 }
aoqi@0 610
jfranck@2596 611 annotateLater(tree.mods.annotations, localEnv, m, tree.pos());
jfranck@2596 612 // Visit the signature of the method. Note that
jfranck@2596 613 // TypeAnnotate doesn't descend into the body.
jfranck@2596 614 typeAnnotate(tree, localEnv, m, tree.pos());
aoqi@0 615
jfranck@2596 616 if (tree.defaultValue != null)
jfranck@2596 617 annotateDefaultValueLater(tree.defaultValue, localEnv, m);
aoqi@0 618 }
aoqi@0 619
aoqi@0 620 /** Create a fresh environment for method bodies.
aoqi@0 621 * @param tree The method definition.
aoqi@0 622 * @param env The environment current outside of the method definition.
aoqi@0 623 */
aoqi@0 624 Env<AttrContext> methodEnv(JCMethodDecl tree, Env<AttrContext> env) {
aoqi@0 625 Env<AttrContext> localEnv =
aoqi@0 626 env.dup(tree, env.info.dup(env.info.scope.dupUnshared()));
aoqi@0 627 localEnv.enclMethod = tree;
aoqi@0 628 localEnv.info.scope.owner = tree.sym;
aoqi@0 629 if (tree.sym.type != null) {
aoqi@0 630 //when this is called in the enter stage, there's no type to be set
aoqi@0 631 localEnv.info.returnResult = attr.new ResultInfo(VAL, tree.sym.type.getReturnType());
aoqi@0 632 }
aoqi@0 633 if ((tree.mods.flags & STATIC) != 0) localEnv.info.staticLevel++;
aoqi@0 634 return localEnv;
aoqi@0 635 }
aoqi@0 636
aoqi@0 637 public void visitVarDef(JCVariableDecl tree) {
aoqi@0 638 Env<AttrContext> localEnv = env;
aoqi@0 639 if ((tree.mods.flags & STATIC) != 0 ||
aoqi@0 640 (env.info.scope.owner.flags() & INTERFACE) != 0) {
aoqi@0 641 localEnv = env.dup(tree, env.info.dup());
aoqi@0 642 localEnv.info.staticLevel++;
aoqi@0 643 }
aoqi@0 644 DiagnosticPosition prevLintPos = deferredLintHandler.setPos(tree.pos());
aoqi@0 645 try {
jfranck@2596 646 if (TreeInfo.isEnumInit(tree)) {
jfranck@2596 647 attr.attribIdentAsEnumType(localEnv, (JCIdent)tree.vartype);
jfranck@2596 648 } else {
jfranck@2596 649 attr.attribType(tree.vartype, localEnv);
jfranck@2596 650 if (TreeInfo.isReceiverParam(tree))
jfranck@2596 651 checkReceiver(tree, localEnv);
aoqi@0 652 }
jfranck@2596 653 } finally {
jfranck@2596 654 deferredLintHandler.setPos(prevLintPos);
jfranck@2596 655 }
aoqi@0 656
jfranck@2596 657 if ((tree.mods.flags & VARARGS) != 0) {
jfranck@2596 658 //if we are entering a varargs parameter, we need to
jfranck@2596 659 //replace its type (a plain array type) with the more
jfranck@2596 660 //precise VarargsType --- we need to do it this way
jfranck@2596 661 //because varargs is represented in the tree as a
jfranck@2596 662 //modifier on the parameter declaration, and not as a
jfranck@2596 663 //distinct type of array node.
jfranck@2596 664 ArrayType atype = (ArrayType)tree.vartype.type.unannotatedType();
jfranck@2596 665 tree.vartype.type = atype.makeVarargs();
jfranck@2596 666 }
jfranck@2596 667 Scope enclScope = enter.enterScope(env);
jfranck@2596 668 VarSymbol v =
jfranck@2596 669 new VarSymbol(0, tree.name, tree.vartype.type, enclScope.owner);
jfranck@2596 670 v.flags_field = chk.checkFlags(tree.pos(), tree.mods.flags, v, tree);
jfranck@2596 671 tree.sym = v;
jfranck@2596 672 if (tree.init != null) {
jfranck@2596 673 v.flags_field |= HASINIT;
jfranck@2596 674 if ((v.flags_field & FINAL) != 0 &&
jfranck@2596 675 needsLazyConstValue(tree.init)) {
jfranck@2596 676 Env<AttrContext> initEnv = getInitEnv(tree, env);
jfranck@2596 677 initEnv.info.enclVar = v;
jfranck@2596 678 v.setLazyConstValue(initEnv(tree, initEnv), attr, tree);
aoqi@0 679 }
aoqi@0 680 }
jfranck@2596 681 if (chk.checkUnique(tree.pos(), v, enclScope)) {
jfranck@2596 682 chk.checkTransparentVar(tree.pos(), v, enclScope);
jfranck@2596 683 enclScope.enter(v);
jfranck@2596 684 }
jfranck@2596 685 annotateLater(tree.mods.annotations, localEnv, v, tree.pos());
jfranck@2596 686 typeAnnotate(tree.vartype, env, v, tree.pos());
jfranck@2596 687 v.pos = tree.pos;
aoqi@0 688 }
aoqi@0 689 // where
aoqi@0 690 void checkType(JCTree tree, Type type, String diag) {
aoqi@0 691 if (!tree.type.isErroneous() && !types.isSameType(tree.type, type)) {
aoqi@0 692 log.error(tree, diag, type, tree.type);
aoqi@0 693 }
aoqi@0 694 }
aoqi@0 695 void checkReceiver(JCVariableDecl tree, Env<AttrContext> localEnv) {
aoqi@0 696 attr.attribExpr(tree.nameexpr, localEnv);
aoqi@0 697 MethodSymbol m = localEnv.enclMethod.sym;
aoqi@0 698 if (m.isConstructor()) {
aoqi@0 699 Type outertype = m.owner.owner.type;
aoqi@0 700 if (outertype.hasTag(TypeTag.METHOD)) {
aoqi@0 701 // we have a local inner class
aoqi@0 702 outertype = m.owner.owner.owner.type;
aoqi@0 703 }
aoqi@0 704 if (outertype.hasTag(TypeTag.CLASS)) {
aoqi@0 705 checkType(tree.vartype, outertype, "incorrect.constructor.receiver.type");
aoqi@0 706 checkType(tree.nameexpr, outertype, "incorrect.constructor.receiver.name");
aoqi@0 707 } else {
aoqi@0 708 log.error(tree, "receiver.parameter.not.applicable.constructor.toplevel.class");
aoqi@0 709 }
aoqi@0 710 } else {
aoqi@0 711 checkType(tree.vartype, m.owner.type, "incorrect.receiver.type");
aoqi@0 712 checkType(tree.nameexpr, m.owner.type, "incorrect.receiver.name");
aoqi@0 713 }
aoqi@0 714 }
aoqi@0 715
aoqi@0 716 public boolean needsLazyConstValue(JCTree tree) {
aoqi@0 717 InitTreeVisitor initTreeVisitor = new InitTreeVisitor();
aoqi@0 718 tree.accept(initTreeVisitor);
aoqi@0 719 return initTreeVisitor.result;
aoqi@0 720 }
aoqi@0 721
aoqi@0 722 /** Visitor class for expressions which might be constant expressions.
aoqi@0 723 */
aoqi@0 724 static class InitTreeVisitor extends JCTree.Visitor {
aoqi@0 725
aoqi@0 726 private boolean result = true;
aoqi@0 727
aoqi@0 728 @Override
aoqi@0 729 public void visitTree(JCTree tree) {}
aoqi@0 730
aoqi@0 731 @Override
aoqi@0 732 public void visitNewClass(JCNewClass that) {
aoqi@0 733 result = false;
aoqi@0 734 }
aoqi@0 735
aoqi@0 736 @Override
aoqi@0 737 public void visitNewArray(JCNewArray that) {
aoqi@0 738 result = false;
aoqi@0 739 }
aoqi@0 740
aoqi@0 741 @Override
aoqi@0 742 public void visitLambda(JCLambda that) {
aoqi@0 743 result = false;
aoqi@0 744 }
aoqi@0 745
aoqi@0 746 @Override
aoqi@0 747 public void visitReference(JCMemberReference that) {
aoqi@0 748 result = false;
aoqi@0 749 }
aoqi@0 750
aoqi@0 751 @Override
aoqi@0 752 public void visitApply(JCMethodInvocation that) {
aoqi@0 753 result = false;
aoqi@0 754 }
aoqi@0 755
aoqi@0 756 @Override
aoqi@0 757 public void visitSelect(JCFieldAccess tree) {
aoqi@0 758 tree.selected.accept(this);
aoqi@0 759 }
aoqi@0 760
aoqi@0 761 @Override
aoqi@0 762 public void visitConditional(JCConditional tree) {
aoqi@0 763 tree.cond.accept(this);
aoqi@0 764 tree.truepart.accept(this);
aoqi@0 765 tree.falsepart.accept(this);
aoqi@0 766 }
aoqi@0 767
aoqi@0 768 @Override
aoqi@0 769 public void visitParens(JCParens tree) {
aoqi@0 770 tree.expr.accept(this);
aoqi@0 771 }
aoqi@0 772
aoqi@0 773 @Override
aoqi@0 774 public void visitTypeCast(JCTypeCast tree) {
aoqi@0 775 tree.expr.accept(this);
aoqi@0 776 }
aoqi@0 777 }
aoqi@0 778
aoqi@0 779 /** Create a fresh environment for a variable's initializer.
aoqi@0 780 * If the variable is a field, the owner of the environment's scope
aoqi@0 781 * is be the variable itself, otherwise the owner is the method
aoqi@0 782 * enclosing the variable definition.
aoqi@0 783 *
aoqi@0 784 * @param tree The variable definition.
aoqi@0 785 * @param env The environment current outside of the variable definition.
aoqi@0 786 */
aoqi@0 787 Env<AttrContext> initEnv(JCVariableDecl tree, Env<AttrContext> env) {
aoqi@0 788 Env<AttrContext> localEnv = env.dupto(new AttrContextEnv(tree, env.info.dup()));
aoqi@0 789 if (tree.sym.owner.kind == TYP) {
aoqi@0 790 localEnv.info.scope = env.info.scope.dupUnshared();
aoqi@0 791 localEnv.info.scope.owner = tree.sym;
aoqi@0 792 }
aoqi@0 793 if ((tree.mods.flags & STATIC) != 0 ||
aoqi@0 794 ((env.enclClass.sym.flags() & INTERFACE) != 0 && env.enclMethod == null))
aoqi@0 795 localEnv.info.staticLevel++;
aoqi@0 796 return localEnv;
aoqi@0 797 }
aoqi@0 798
aoqi@0 799 /** Default member enter visitor method: do nothing
aoqi@0 800 */
aoqi@0 801 public void visitTree(JCTree tree) {
aoqi@0 802 }
aoqi@0 803
aoqi@0 804 public void visitErroneous(JCErroneous tree) {
aoqi@0 805 if (tree.errs != null)
aoqi@0 806 memberEnter(tree.errs, env);
aoqi@0 807 }
aoqi@0 808
aoqi@0 809 public Env<AttrContext> getMethodEnv(JCMethodDecl tree, Env<AttrContext> env) {
aoqi@0 810 Env<AttrContext> mEnv = methodEnv(tree, env);
aoqi@0 811 mEnv.info.lint = mEnv.info.lint.augment(tree.sym);
aoqi@0 812 for (List<JCTypeParameter> l = tree.typarams; l.nonEmpty(); l = l.tail)
aoqi@0 813 mEnv.info.scope.enterIfAbsent(l.head.type.tsym);
aoqi@0 814 for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail)
aoqi@0 815 mEnv.info.scope.enterIfAbsent(l.head.sym);
aoqi@0 816 return mEnv;
aoqi@0 817 }
aoqi@0 818
aoqi@0 819 public Env<AttrContext> getInitEnv(JCVariableDecl tree, Env<AttrContext> env) {
aoqi@0 820 Env<AttrContext> iEnv = initEnv(tree, env);
aoqi@0 821 return iEnv;
aoqi@0 822 }
aoqi@0 823
aoqi@0 824 /* ********************************************************************
aoqi@0 825 * Type completion
aoqi@0 826 *********************************************************************/
aoqi@0 827
aoqi@0 828 Type attribImportType(JCTree tree, Env<AttrContext> env) {
aoqi@0 829 Assert.check(completionEnabled);
aoqi@0 830 try {
aoqi@0 831 // To prevent deep recursion, suppress completion of some
aoqi@0 832 // types.
aoqi@0 833 completionEnabled = false;
aoqi@0 834 return attr.attribType(tree, env);
aoqi@0 835 } finally {
aoqi@0 836 completionEnabled = true;
aoqi@0 837 }
aoqi@0 838 }
aoqi@0 839
aoqi@0 840 /* ********************************************************************
aoqi@0 841 * Annotation processing
aoqi@0 842 *********************************************************************/
aoqi@0 843
aoqi@0 844 /** Queue annotations for later processing. */
aoqi@0 845 void annotateLater(final List<JCAnnotation> annotations,
aoqi@0 846 final Env<AttrContext> localEnv,
aoqi@0 847 final Symbol s,
aoqi@0 848 final DiagnosticPosition deferPos) {
aoqi@0 849 if (annotations.isEmpty()) {
aoqi@0 850 return;
aoqi@0 851 }
aoqi@0 852 if (s.kind != PCK) {
aoqi@0 853 s.resetAnnotations(); // mark Annotations as incomplete for now
aoqi@0 854 }
aoqi@0 855 annotate.normal(new Annotate.Worker() {
aoqi@0 856 @Override
aoqi@0 857 public String toString() {
aoqi@0 858 return "annotate " + annotations + " onto " + s + " in " + s.owner;
aoqi@0 859 }
aoqi@0 860
aoqi@0 861 @Override
aoqi@0 862 public void run() {
aoqi@0 863 Assert.check(s.kind == PCK || s.annotationsPendingCompletion());
aoqi@0 864 JavaFileObject prev = log.useSource(localEnv.toplevel.sourcefile);
aoqi@0 865 DiagnosticPosition prevLintPos =
aoqi@0 866 deferPos != null
aoqi@0 867 ? deferredLintHandler.setPos(deferPos)
aoqi@0 868 : deferredLintHandler.immediate();
aoqi@0 869 Lint prevLint = deferPos != null ? null : chk.setLint(lint);
aoqi@0 870 try {
aoqi@0 871 if (s.hasAnnotations() &&
aoqi@0 872 annotations.nonEmpty())
aoqi@0 873 log.error(annotations.head.pos,
aoqi@0 874 "already.annotated",
aoqi@0 875 kindName(s), s);
aoqi@0 876 actualEnterAnnotations(annotations, localEnv, s);
aoqi@0 877 } finally {
aoqi@0 878 if (prevLint != null)
aoqi@0 879 chk.setLint(prevLint);
aoqi@0 880 deferredLintHandler.setPos(prevLintPos);
aoqi@0 881 log.useSource(prev);
aoqi@0 882 }
aoqi@0 883 }
aoqi@0 884 });
aoqi@0 885
aoqi@0 886 annotate.validate(new Annotate.Worker() { //validate annotations
aoqi@0 887 @Override
aoqi@0 888 public void run() {
aoqi@0 889 JavaFileObject prev = log.useSource(localEnv.toplevel.sourcefile);
aoqi@0 890 try {
aoqi@0 891 chk.validateAnnotations(annotations, s);
aoqi@0 892 } finally {
aoqi@0 893 log.useSource(prev);
aoqi@0 894 }
aoqi@0 895 }
aoqi@0 896 });
aoqi@0 897 }
aoqi@0 898
aoqi@0 899 /**
aoqi@0 900 * Check if a list of annotations contains a reference to
aoqi@0 901 * java.lang.Deprecated.
aoqi@0 902 **/
aoqi@0 903 private boolean hasDeprecatedAnnotation(List<JCAnnotation> annotations) {
aoqi@0 904 for (List<JCAnnotation> al = annotations; !al.isEmpty(); al = al.tail) {
aoqi@0 905 JCAnnotation a = al.head;
aoqi@0 906 if (a.annotationType.type == syms.deprecatedType && a.args.isEmpty())
aoqi@0 907 return true;
aoqi@0 908 }
aoqi@0 909 return false;
aoqi@0 910 }
aoqi@0 911
aoqi@0 912 /** Enter a set of annotations. */
aoqi@0 913 private void actualEnterAnnotations(List<JCAnnotation> annotations,
aoqi@0 914 Env<AttrContext> env,
aoqi@0 915 Symbol s) {
aoqi@0 916 Map<TypeSymbol, ListBuffer<Attribute.Compound>> annotated =
aoqi@0 917 new LinkedHashMap<TypeSymbol, ListBuffer<Attribute.Compound>>();
aoqi@0 918 Map<Attribute.Compound, DiagnosticPosition> pos =
aoqi@0 919 new HashMap<Attribute.Compound, DiagnosticPosition>();
aoqi@0 920
aoqi@0 921 for (List<JCAnnotation> al = annotations; !al.isEmpty(); al = al.tail) {
aoqi@0 922 JCAnnotation a = al.head;
aoqi@0 923 Attribute.Compound c = annotate.enterAnnotation(a,
aoqi@0 924 syms.annotationType,
aoqi@0 925 env);
aoqi@0 926 if (c == null) {
aoqi@0 927 continue;
aoqi@0 928 }
aoqi@0 929
aoqi@0 930 if (annotated.containsKey(a.type.tsym)) {
aoqi@0 931 if (!allowRepeatedAnnos) {
aoqi@0 932 log.error(a.pos(), "repeatable.annotations.not.supported.in.source");
aoqi@0 933 allowRepeatedAnnos = true;
aoqi@0 934 }
aoqi@0 935 ListBuffer<Attribute.Compound> l = annotated.get(a.type.tsym);
aoqi@0 936 l = l.append(c);
aoqi@0 937 annotated.put(a.type.tsym, l);
aoqi@0 938 pos.put(c, a.pos());
aoqi@0 939 } else {
aoqi@0 940 annotated.put(a.type.tsym, ListBuffer.of(c));
aoqi@0 941 pos.put(c, a.pos());
aoqi@0 942 }
aoqi@0 943
aoqi@0 944 // Note: @Deprecated has no effect on local variables and parameters
aoqi@0 945 if (!c.type.isErroneous()
aoqi@0 946 && s.owner.kind != MTH
aoqi@0 947 && types.isSameType(c.type, syms.deprecatedType)) {
aoqi@0 948 s.flags_field |= Flags.DEPRECATED;
aoqi@0 949 }
aoqi@0 950 }
aoqi@0 951
aoqi@0 952 s.setDeclarationAttributesWithCompletion(
aoqi@0 953 annotate.new AnnotateRepeatedContext<Attribute.Compound>(env, annotated, pos, log, false));
aoqi@0 954 }
aoqi@0 955
aoqi@0 956 /** Queue processing of an attribute default value. */
aoqi@0 957 void annotateDefaultValueLater(final JCExpression defaultValue,
aoqi@0 958 final Env<AttrContext> localEnv,
aoqi@0 959 final MethodSymbol m) {
aoqi@0 960 annotate.normal(new Annotate.Worker() {
aoqi@0 961 @Override
aoqi@0 962 public String toString() {
aoqi@0 963 return "annotate " + m.owner + "." +
aoqi@0 964 m + " default " + defaultValue;
aoqi@0 965 }
aoqi@0 966
aoqi@0 967 @Override
aoqi@0 968 public void run() {
aoqi@0 969 JavaFileObject prev = log.useSource(localEnv.toplevel.sourcefile);
aoqi@0 970 try {
aoqi@0 971 enterDefaultValue(defaultValue, localEnv, m);
aoqi@0 972 } finally {
aoqi@0 973 log.useSource(prev);
aoqi@0 974 }
aoqi@0 975 }
aoqi@0 976 });
aoqi@0 977 annotate.validate(new Annotate.Worker() { //validate annotations
aoqi@0 978 @Override
aoqi@0 979 public void run() {
aoqi@0 980 JavaFileObject prev = log.useSource(localEnv.toplevel.sourcefile);
aoqi@0 981 try {
aoqi@0 982 // if default value is an annotation, check it is a well-formed
aoqi@0 983 // annotation value (e.g. no duplicate values, no missing values, etc.)
aoqi@0 984 chk.validateAnnotationTree(defaultValue);
aoqi@0 985 } finally {
aoqi@0 986 log.useSource(prev);
aoqi@0 987 }
aoqi@0 988 }
aoqi@0 989 });
aoqi@0 990 }
aoqi@0 991
aoqi@0 992 /** Enter a default value for an attribute method. */
aoqi@0 993 private void enterDefaultValue(final JCExpression defaultValue,
aoqi@0 994 final Env<AttrContext> localEnv,
aoqi@0 995 final MethodSymbol m) {
aoqi@0 996 m.defaultValue = annotate.enterAttributeValue(m.type.getReturnType(),
aoqi@0 997 defaultValue,
aoqi@0 998 localEnv);
aoqi@0 999 }
aoqi@0 1000
aoqi@0 1001 /* ********************************************************************
aoqi@0 1002 * Source completer
aoqi@0 1003 *********************************************************************/
aoqi@0 1004
aoqi@0 1005 /** Complete entering a class.
aoqi@0 1006 * @param sym The symbol of the class to be completed.
aoqi@0 1007 */
aoqi@0 1008 public void complete(Symbol sym) throws CompletionFailure {
aoqi@0 1009 // Suppress some (recursive) MemberEnter invocations
aoqi@0 1010 if (!completionEnabled) {
aoqi@0 1011 // Re-install same completer for next time around and return.
aoqi@0 1012 Assert.check((sym.flags() & Flags.COMPOUND) == 0);
aoqi@0 1013 sym.completer = this;
aoqi@0 1014 return;
aoqi@0 1015 }
aoqi@0 1016
aoqi@0 1017 ClassSymbol c = (ClassSymbol)sym;
aoqi@0 1018 ClassType ct = (ClassType)c.type;
aoqi@0 1019 Env<AttrContext> env = typeEnvs.get(c);
aoqi@0 1020 JCClassDecl tree = (JCClassDecl)env.tree;
aoqi@0 1021 boolean wasFirst = isFirst;
aoqi@0 1022 isFirst = false;
jfranck@2596 1023 try {
jfranck@2596 1024 annotate.enterStart();
aoqi@0 1025
jfranck@2596 1026 JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
jfranck@2596 1027 DiagnosticPosition prevLintPos = deferredLintHandler.setPos(tree.pos());
jfranck@2596 1028 try {
jfranck@2596 1029 // Save class environment for later member enter (2) processing.
jfranck@2596 1030 halfcompleted.append(env);
aoqi@0 1031
jfranck@2596 1032 // Mark class as not yet attributed.
jfranck@2596 1033 c.flags_field |= UNATTRIBUTED;
aoqi@0 1034
jfranck@2596 1035 // If this is a toplevel-class, make sure any preceding import
jfranck@2596 1036 // clauses have been seen.
jfranck@2596 1037 if (c.owner.kind == PCK) {
jfranck@2596 1038 memberEnter(env.toplevel, env.enclosing(TOPLEVEL));
jfranck@2596 1039 todo.append(env);
jfranck@2596 1040 }
jfranck@2596 1041
jfranck@2596 1042 if (c.owner.kind == TYP)
jfranck@2596 1043 c.owner.complete();
jfranck@2596 1044
jfranck@2596 1045 // create an environment for evaluating the base clauses
jfranck@2596 1046 Env<AttrContext> baseEnv = baseEnv(tree, env);
jfranck@2596 1047
jfranck@2596 1048 if (tree.extending != null)
jfranck@2596 1049 typeAnnotate(tree.extending, baseEnv, sym, tree.pos());
jfranck@2596 1050 for (JCExpression impl : tree.implementing)
jfranck@2596 1051 typeAnnotate(impl, baseEnv, sym, tree.pos());
jfranck@2596 1052 annotate.flush();
jfranck@2596 1053
jfranck@2596 1054 // Determine supertype.
jfranck@2596 1055 Type supertype =
jfranck@2596 1056 (tree.extending != null)
jfranck@2596 1057 ? attr.attribBase(tree.extending, baseEnv, true, false, true)
jfranck@2596 1058 : ((tree.mods.flags & Flags.ENUM) != 0)
jfranck@2596 1059 ? attr.attribBase(enumBase(tree.pos, c), baseEnv,
jfranck@2596 1060 true, false, false)
jfranck@2596 1061 : (c.fullname == names.java_lang_Object)
jfranck@2596 1062 ? Type.noType
jfranck@2596 1063 : syms.objectType;
jfranck@2596 1064 ct.supertype_field = modelMissingTypes(supertype, tree.extending, false);
jfranck@2596 1065
jfranck@2596 1066 // Determine interfaces.
jfranck@2596 1067 ListBuffer<Type> interfaces = new ListBuffer<Type>();
jfranck@2596 1068 ListBuffer<Type> all_interfaces = null; // lazy init
jfranck@2596 1069 Set<Type> interfaceSet = new HashSet<Type>();
jfranck@2596 1070 List<JCExpression> interfaceTrees = tree.implementing;
jfranck@2596 1071 for (JCExpression iface : interfaceTrees) {
jfranck@2596 1072 Type i = attr.attribBase(iface, baseEnv, false, true, true);
jfranck@2596 1073 if (i.hasTag(CLASS)) {
jfranck@2596 1074 interfaces.append(i);
jfranck@2596 1075 if (all_interfaces != null) all_interfaces.append(i);
jfranck@2596 1076 chk.checkNotRepeated(iface.pos(), types.erasure(i), interfaceSet);
jfranck@2596 1077 } else {
jfranck@2596 1078 if (all_interfaces == null)
jfranck@2596 1079 all_interfaces = new ListBuffer<Type>().appendList(interfaces);
jfranck@2596 1080 all_interfaces.append(modelMissingTypes(i, iface, true));
jfranck@2596 1081 }
jfranck@2596 1082 }
jfranck@2596 1083 if ((c.flags_field & ANNOTATION) != 0) {
jfranck@2596 1084 ct.interfaces_field = List.of(syms.annotationType);
jfranck@2596 1085 ct.all_interfaces_field = ct.interfaces_field;
jfranck@2596 1086 } else {
jfranck@2596 1087 ct.interfaces_field = interfaces.toList();
jfranck@2596 1088 ct.all_interfaces_field = (all_interfaces == null)
jfranck@2596 1089 ? ct.interfaces_field : all_interfaces.toList();
jfranck@2596 1090 }
jfranck@2596 1091
jfranck@2596 1092 if (c.fullname == names.java_lang_Object) {
jfranck@2596 1093 if (tree.extending != null) {
jfranck@2596 1094 chk.checkNonCyclic(tree.extending.pos(),
jfranck@2596 1095 supertype);
jfranck@2596 1096 ct.supertype_field = Type.noType;
jfranck@2596 1097 }
jfranck@2596 1098 else if (tree.implementing.nonEmpty()) {
jfranck@2596 1099 chk.checkNonCyclic(tree.implementing.head.pos(),
jfranck@2596 1100 ct.interfaces_field.head);
jfranck@2596 1101 ct.interfaces_field = List.nil();
jfranck@2596 1102 }
jfranck@2596 1103 }
jfranck@2596 1104
jfranck@2596 1105 // Annotations.
jfranck@2596 1106 // In general, we cannot fully process annotations yet, but we
jfranck@2596 1107 // can attribute the annotation types and then check to see if the
jfranck@2596 1108 // @Deprecated annotation is present.
jfranck@2596 1109 attr.attribAnnotationTypes(tree.mods.annotations, baseEnv);
jfranck@2596 1110 if (hasDeprecatedAnnotation(tree.mods.annotations))
jfranck@2596 1111 c.flags_field |= DEPRECATED;
jfranck@2596 1112 annotateLater(tree.mods.annotations, baseEnv, c, tree.pos());
jfranck@2596 1113 // class type parameters use baseEnv but everything uses env
jfranck@2596 1114
jfranck@2596 1115 chk.checkNonCyclicDecl(tree);
jfranck@2596 1116
jfranck@2596 1117 attr.attribTypeVariables(tree.typarams, baseEnv);
jfranck@2596 1118 // Do this here, where we have the symbol.
jfranck@2596 1119 for (JCTypeParameter tp : tree.typarams)
jfranck@2596 1120 typeAnnotate(tp, baseEnv, sym, tree.pos());
jfranck@2596 1121
jfranck@2596 1122 // Add default constructor if needed.
jfranck@2596 1123 if ((c.flags() & INTERFACE) == 0 &&
jfranck@2596 1124 !TreeInfo.hasConstructors(tree.defs)) {
jfranck@2596 1125 List<Type> argtypes = List.nil();
jfranck@2596 1126 List<Type> typarams = List.nil();
jfranck@2596 1127 List<Type> thrown = List.nil();
jfranck@2596 1128 long ctorFlags = 0;
jfranck@2596 1129 boolean based = false;
jfranck@2596 1130 boolean addConstructor = true;
jfranck@2596 1131 JCNewClass nc = null;
jfranck@2596 1132 if (c.name.isEmpty()) {
jfranck@2596 1133 nc = (JCNewClass)env.next.tree;
jfranck@2596 1134 if (nc.constructor != null) {
jfranck@2596 1135 addConstructor = nc.constructor.kind != ERR;
jfranck@2596 1136 Type superConstrType = types.memberType(c.type,
jfranck@2596 1137 nc.constructor);
jfranck@2596 1138 argtypes = superConstrType.getParameterTypes();
jfranck@2596 1139 typarams = superConstrType.getTypeArguments();
jfranck@2596 1140 ctorFlags = nc.constructor.flags() & VARARGS;
jfranck@2596 1141 if (nc.encl != null) {
jfranck@2596 1142 argtypes = argtypes.prepend(nc.encl.type);
jfranck@2596 1143 based = true;
jfranck@2596 1144 }
jfranck@2596 1145 thrown = superConstrType.getThrownTypes();
jfranck@2596 1146 }
jfranck@2596 1147 }
jfranck@2596 1148 if (addConstructor) {
jfranck@2596 1149 MethodSymbol basedConstructor = nc != null ?
jfranck@2596 1150 (MethodSymbol)nc.constructor : null;
jfranck@2596 1151 JCTree constrDef = DefaultConstructor(make.at(tree.pos), c,
jfranck@2596 1152 basedConstructor,
jfranck@2596 1153 typarams, argtypes, thrown,
jfranck@2596 1154 ctorFlags, based);
jfranck@2596 1155 tree.defs = tree.defs.prepend(constrDef);
jfranck@2596 1156 }
jfranck@2596 1157 }
jfranck@2596 1158
jfranck@2596 1159 // enter symbols for 'this' into current scope.
jfranck@2596 1160 VarSymbol thisSym =
jfranck@2596 1161 new VarSymbol(FINAL | HASINIT, names._this, c.type, c);
jfranck@2596 1162 thisSym.pos = Position.FIRSTPOS;
jfranck@2596 1163 env.info.scope.enter(thisSym);
jfranck@2596 1164 // if this is a class, enter symbol for 'super' into current scope.
jfranck@2596 1165 if ((c.flags_field & INTERFACE) == 0 &&
jfranck@2596 1166 ct.supertype_field.hasTag(CLASS)) {
jfranck@2596 1167 VarSymbol superSym =
jfranck@2596 1168 new VarSymbol(FINAL | HASINIT, names._super,
jfranck@2596 1169 ct.supertype_field, c);
jfranck@2596 1170 superSym.pos = Position.FIRSTPOS;
jfranck@2596 1171 env.info.scope.enter(superSym);
jfranck@2596 1172 }
jfranck@2596 1173
jfranck@2596 1174 // check that no package exists with same fully qualified name,
jfranck@2596 1175 // but admit classes in the unnamed package which have the same
jfranck@2596 1176 // name as a top-level package.
jfranck@2596 1177 if (checkClash &&
jfranck@2596 1178 c.owner.kind == PCK && c.owner != syms.unnamedPackage &&
jfranck@2596 1179 reader.packageExists(c.fullname)) {
jfranck@2596 1180 log.error(tree.pos, "clash.with.pkg.of.same.name", Kinds.kindName(sym), c);
jfranck@2596 1181 }
jfranck@2596 1182 if (c.owner.kind == PCK && (c.flags_field & PUBLIC) == 0 &&
jfranck@2596 1183 !env.toplevel.sourcefile.isNameCompatible(c.name.toString(),JavaFileObject.Kind.SOURCE)) {
jfranck@2596 1184 c.flags_field |= AUXILIARY;
jfranck@2596 1185 }
jfranck@2596 1186 } catch (CompletionFailure ex) {
jfranck@2596 1187 chk.completionError(tree.pos(), ex);
jfranck@2596 1188 } finally {
jfranck@2596 1189 deferredLintHandler.setPos(prevLintPos);
jfranck@2596 1190 log.useSource(prev);
aoqi@0 1191 }
aoqi@0 1192
jfranck@2596 1193 // Enter all member fields and methods of a set of half completed
jfranck@2596 1194 // classes in a second phase.
jfranck@2596 1195 if (wasFirst) {
jfranck@2596 1196 try {
jfranck@2596 1197 while (halfcompleted.nonEmpty()) {
jfranck@2596 1198 Env<AttrContext> toFinish = halfcompleted.next();
jfranck@2596 1199 finish(toFinish);
jfranck@2596 1200 if (allowTypeAnnos) {
jfranck@2596 1201 typeAnnotations.organizeTypeAnnotationsSignatures(toFinish, (JCClassDecl)toFinish.tree);
jfranck@2596 1202 typeAnnotations.validateTypeAnnotationsSignatures(toFinish, (JCClassDecl)toFinish.tree);
jfranck@2596 1203 }
jfranck@2596 1204 }
jfranck@2596 1205 } finally {
jfranck@2596 1206 isFirst = true;
aoqi@0 1207 }
aoqi@0 1208 }
aoqi@0 1209 } finally {
jfranck@2596 1210 annotate.enterDone();
aoqi@0 1211 }
aoqi@0 1212 }
aoqi@0 1213
aoqi@0 1214 /*
aoqi@0 1215 * If the symbol is non-null, attach the type annotation to it.
aoqi@0 1216 */
aoqi@0 1217 private void actualEnterTypeAnnotations(final List<JCAnnotation> annotations,
aoqi@0 1218 final Env<AttrContext> env,
aoqi@0 1219 final Symbol s) {
aoqi@0 1220 Map<TypeSymbol, ListBuffer<Attribute.TypeCompound>> annotated =
aoqi@0 1221 new LinkedHashMap<TypeSymbol, ListBuffer<Attribute.TypeCompound>>();
aoqi@0 1222 Map<Attribute.TypeCompound, DiagnosticPosition> pos =
aoqi@0 1223 new HashMap<Attribute.TypeCompound, DiagnosticPosition>();
aoqi@0 1224
aoqi@0 1225 for (List<JCAnnotation> al = annotations; !al.isEmpty(); al = al.tail) {
aoqi@0 1226 JCAnnotation a = al.head;
aoqi@0 1227 Attribute.TypeCompound tc = annotate.enterTypeAnnotation(a,
aoqi@0 1228 syms.annotationType,
aoqi@0 1229 env);
aoqi@0 1230 if (tc == null) {
aoqi@0 1231 continue;
aoqi@0 1232 }
aoqi@0 1233
aoqi@0 1234 if (annotated.containsKey(a.type.tsym)) {
aoqi@0 1235 if (source.allowRepeatedAnnotations()) {
aoqi@0 1236 ListBuffer<Attribute.TypeCompound> l = annotated.get(a.type.tsym);
aoqi@0 1237 l = l.append(tc);
aoqi@0 1238 annotated.put(a.type.tsym, l);
aoqi@0 1239 pos.put(tc, a.pos());
aoqi@0 1240 } else {
aoqi@0 1241 log.error(a.pos(), "repeatable.annotations.not.supported.in.source");
aoqi@0 1242 }
aoqi@0 1243 } else {
aoqi@0 1244 annotated.put(a.type.tsym, ListBuffer.of(tc));
aoqi@0 1245 pos.put(tc, a.pos());
aoqi@0 1246 }
aoqi@0 1247 }
aoqi@0 1248
aoqi@0 1249 if (s != null) {
aoqi@0 1250 s.appendTypeAttributesWithCompletion(
aoqi@0 1251 annotate.new AnnotateRepeatedContext<Attribute.TypeCompound>(env, annotated, pos, log, true));
aoqi@0 1252 }
aoqi@0 1253 }
aoqi@0 1254
aoqi@0 1255 public void typeAnnotate(final JCTree tree, final Env<AttrContext> env, final Symbol sym, DiagnosticPosition deferPos) {
aoqi@0 1256 if (allowTypeAnnos) {
aoqi@0 1257 tree.accept(new TypeAnnotate(env, sym, deferPos));
aoqi@0 1258 }
aoqi@0 1259 }
aoqi@0 1260
aoqi@0 1261 /**
aoqi@0 1262 * We need to use a TreeScanner, because it is not enough to visit the top-level
aoqi@0 1263 * annotations. We also need to visit type arguments, etc.
aoqi@0 1264 */
aoqi@0 1265 private class TypeAnnotate extends TreeScanner {
aoqi@0 1266 private Env<AttrContext> env;
aoqi@0 1267 private Symbol sym;
aoqi@0 1268 private DiagnosticPosition deferPos;
aoqi@0 1269
aoqi@0 1270 public TypeAnnotate(final Env<AttrContext> env, final Symbol sym, DiagnosticPosition deferPos) {
aoqi@0 1271 this.env = env;
aoqi@0 1272 this.sym = sym;
aoqi@0 1273 this.deferPos = deferPos;
aoqi@0 1274 }
aoqi@0 1275
aoqi@0 1276 void annotateTypeLater(final List<JCAnnotation> annotations) {
aoqi@0 1277 if (annotations.isEmpty()) {
aoqi@0 1278 return;
aoqi@0 1279 }
aoqi@0 1280
aoqi@0 1281 final DiagnosticPosition deferPos = this.deferPos;
aoqi@0 1282
aoqi@0 1283 annotate.normal(new Annotate.Worker() {
aoqi@0 1284 @Override
aoqi@0 1285 public String toString() {
aoqi@0 1286 return "type annotate " + annotations + " onto " + sym + " in " + sym.owner;
aoqi@0 1287 }
aoqi@0 1288 @Override
aoqi@0 1289 public void run() {
aoqi@0 1290 JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
aoqi@0 1291 DiagnosticPosition prevLintPos = null;
aoqi@0 1292
aoqi@0 1293 if (deferPos != null) {
aoqi@0 1294 prevLintPos = deferredLintHandler.setPos(deferPos);
aoqi@0 1295 }
aoqi@0 1296 try {
aoqi@0 1297 actualEnterTypeAnnotations(annotations, env, sym);
aoqi@0 1298 } finally {
aoqi@0 1299 if (prevLintPos != null)
aoqi@0 1300 deferredLintHandler.setPos(prevLintPos);
aoqi@0 1301 log.useSource(prev);
aoqi@0 1302 }
aoqi@0 1303 }
aoqi@0 1304 });
aoqi@0 1305 }
aoqi@0 1306
aoqi@0 1307 @Override
aoqi@0 1308 public void visitAnnotatedType(final JCAnnotatedType tree) {
aoqi@0 1309 annotateTypeLater(tree.annotations);
aoqi@0 1310 super.visitAnnotatedType(tree);
aoqi@0 1311 }
aoqi@0 1312
aoqi@0 1313 @Override
aoqi@0 1314 public void visitTypeParameter(final JCTypeParameter tree) {
aoqi@0 1315 annotateTypeLater(tree.annotations);
aoqi@0 1316 super.visitTypeParameter(tree);
aoqi@0 1317 }
aoqi@0 1318
aoqi@0 1319 @Override
aoqi@0 1320 public void visitNewArray(final JCNewArray tree) {
aoqi@0 1321 annotateTypeLater(tree.annotations);
aoqi@0 1322 for (List<JCAnnotation> dimAnnos : tree.dimAnnotations)
aoqi@0 1323 annotateTypeLater(dimAnnos);
aoqi@0 1324 super.visitNewArray(tree);
aoqi@0 1325 }
aoqi@0 1326
aoqi@0 1327 @Override
aoqi@0 1328 public void visitMethodDef(final JCMethodDecl tree) {
aoqi@0 1329 scan(tree.mods);
aoqi@0 1330 scan(tree.restype);
aoqi@0 1331 scan(tree.typarams);
aoqi@0 1332 scan(tree.recvparam);
aoqi@0 1333 scan(tree.params);
aoqi@0 1334 scan(tree.thrown);
aoqi@0 1335 scan(tree.defaultValue);
aoqi@0 1336 // Do not annotate the body, just the signature.
aoqi@0 1337 // scan(tree.body);
aoqi@0 1338 }
aoqi@0 1339
aoqi@0 1340 @Override
aoqi@0 1341 public void visitVarDef(final JCVariableDecl tree) {
aoqi@0 1342 DiagnosticPosition prevPos = deferPos;
aoqi@0 1343 deferPos = tree.pos();
aoqi@0 1344 try {
aoqi@0 1345 if (sym != null && sym.kind == Kinds.VAR) {
aoqi@0 1346 // Don't visit a parameter once when the sym is the method
aoqi@0 1347 // and once when the sym is the parameter.
aoqi@0 1348 scan(tree.mods);
aoqi@0 1349 scan(tree.vartype);
aoqi@0 1350 }
aoqi@0 1351 scan(tree.init);
aoqi@0 1352 } finally {
aoqi@0 1353 deferPos = prevPos;
aoqi@0 1354 }
aoqi@0 1355 }
aoqi@0 1356
aoqi@0 1357 @Override
aoqi@0 1358 public void visitClassDef(JCClassDecl tree) {
aoqi@0 1359 // We can only hit a classdef if it is declared within
aoqi@0 1360 // a method. Ignore it - the class will be visited
aoqi@0 1361 // separately later.
aoqi@0 1362 }
aoqi@0 1363
aoqi@0 1364 @Override
aoqi@0 1365 public void visitNewClass(JCNewClass tree) {
aoqi@0 1366 if (tree.def == null) {
aoqi@0 1367 // For an anonymous class instantiation the class
aoqi@0 1368 // will be visited separately.
aoqi@0 1369 super.visitNewClass(tree);
aoqi@0 1370 }
aoqi@0 1371 }
aoqi@0 1372 }
aoqi@0 1373
aoqi@0 1374
aoqi@0 1375 private Env<AttrContext> baseEnv(JCClassDecl tree, Env<AttrContext> env) {
aoqi@0 1376 Scope baseScope = new Scope(tree.sym);
aoqi@0 1377 //import already entered local classes into base scope
aoqi@0 1378 for (Scope.Entry e = env.outer.info.scope.elems ; e != null ; e = e.sibling) {
aoqi@0 1379 if (e.sym.isLocal()) {
aoqi@0 1380 baseScope.enter(e.sym);
aoqi@0 1381 }
aoqi@0 1382 }
aoqi@0 1383 //import current type-parameters into base scope
aoqi@0 1384 if (tree.typarams != null)
aoqi@0 1385 for (List<JCTypeParameter> typarams = tree.typarams;
aoqi@0 1386 typarams.nonEmpty();
aoqi@0 1387 typarams = typarams.tail)
aoqi@0 1388 baseScope.enter(typarams.head.type.tsym);
aoqi@0 1389 Env<AttrContext> outer = env.outer; // the base clause can't see members of this class
aoqi@0 1390 Env<AttrContext> localEnv = outer.dup(tree, outer.info.dup(baseScope));
aoqi@0 1391 localEnv.baseClause = true;
aoqi@0 1392 localEnv.outer = outer;
aoqi@0 1393 localEnv.info.isSelfCall = false;
aoqi@0 1394 return localEnv;
aoqi@0 1395 }
aoqi@0 1396
aoqi@0 1397 /** Enter member fields and methods of a class
aoqi@0 1398 * @param env the environment current for the class block.
aoqi@0 1399 */
aoqi@0 1400 private void finish(Env<AttrContext> env) {
aoqi@0 1401 JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
aoqi@0 1402 try {
aoqi@0 1403 JCClassDecl tree = (JCClassDecl)env.tree;
aoqi@0 1404 finishClass(tree, env);
aoqi@0 1405 } finally {
aoqi@0 1406 log.useSource(prev);
aoqi@0 1407 }
aoqi@0 1408 }
aoqi@0 1409
aoqi@0 1410 /** Generate a base clause for an enum type.
aoqi@0 1411 * @param pos The position for trees and diagnostics, if any
aoqi@0 1412 * @param c The class symbol of the enum
aoqi@0 1413 */
aoqi@0 1414 private JCExpression enumBase(int pos, ClassSymbol c) {
aoqi@0 1415 JCExpression result = make.at(pos).
aoqi@0 1416 TypeApply(make.QualIdent(syms.enumSym),
aoqi@0 1417 List.<JCExpression>of(make.Type(c.type)));
aoqi@0 1418 return result;
aoqi@0 1419 }
aoqi@0 1420
aoqi@0 1421 Type modelMissingTypes(Type t, final JCExpression tree, final boolean interfaceExpected) {
aoqi@0 1422 if (!t.hasTag(ERROR))
aoqi@0 1423 return t;
aoqi@0 1424
aoqi@0 1425 return new ErrorType(t.getOriginalType(), t.tsym) {
aoqi@0 1426 private Type modelType;
aoqi@0 1427
aoqi@0 1428 @Override
aoqi@0 1429 public Type getModelType() {
aoqi@0 1430 if (modelType == null)
aoqi@0 1431 modelType = new Synthesizer(getOriginalType(), interfaceExpected).visit(tree);
aoqi@0 1432 return modelType;
aoqi@0 1433 }
aoqi@0 1434 };
aoqi@0 1435 }
aoqi@0 1436 // where
aoqi@0 1437 private class Synthesizer extends JCTree.Visitor {
aoqi@0 1438 Type originalType;
aoqi@0 1439 boolean interfaceExpected;
aoqi@0 1440 List<ClassSymbol> synthesizedSymbols = List.nil();
aoqi@0 1441 Type result;
aoqi@0 1442
aoqi@0 1443 Synthesizer(Type originalType, boolean interfaceExpected) {
aoqi@0 1444 this.originalType = originalType;
aoqi@0 1445 this.interfaceExpected = interfaceExpected;
aoqi@0 1446 }
aoqi@0 1447
aoqi@0 1448 Type visit(JCTree tree) {
aoqi@0 1449 tree.accept(this);
aoqi@0 1450 return result;
aoqi@0 1451 }
aoqi@0 1452
aoqi@0 1453 List<Type> visit(List<? extends JCTree> trees) {
aoqi@0 1454 ListBuffer<Type> lb = new ListBuffer<Type>();
aoqi@0 1455 for (JCTree t: trees)
aoqi@0 1456 lb.append(visit(t));
aoqi@0 1457 return lb.toList();
aoqi@0 1458 }
aoqi@0 1459
aoqi@0 1460 @Override
aoqi@0 1461 public void visitTree(JCTree tree) {
aoqi@0 1462 result = syms.errType;
aoqi@0 1463 }
aoqi@0 1464
aoqi@0 1465 @Override
aoqi@0 1466 public void visitIdent(JCIdent tree) {
aoqi@0 1467 if (!tree.type.hasTag(ERROR)) {
aoqi@0 1468 result = tree.type;
aoqi@0 1469 } else {
aoqi@0 1470 result = synthesizeClass(tree.name, syms.unnamedPackage).type;
aoqi@0 1471 }
aoqi@0 1472 }
aoqi@0 1473
aoqi@0 1474 @Override
aoqi@0 1475 public void visitSelect(JCFieldAccess tree) {
aoqi@0 1476 if (!tree.type.hasTag(ERROR)) {
aoqi@0 1477 result = tree.type;
aoqi@0 1478 } else {
aoqi@0 1479 Type selectedType;
aoqi@0 1480 boolean prev = interfaceExpected;
aoqi@0 1481 try {
aoqi@0 1482 interfaceExpected = false;
aoqi@0 1483 selectedType = visit(tree.selected);
aoqi@0 1484 } finally {
aoqi@0 1485 interfaceExpected = prev;
aoqi@0 1486 }
aoqi@0 1487 ClassSymbol c = synthesizeClass(tree.name, selectedType.tsym);
aoqi@0 1488 result = c.type;
aoqi@0 1489 }
aoqi@0 1490 }
aoqi@0 1491
aoqi@0 1492 @Override
aoqi@0 1493 public void visitTypeApply(JCTypeApply tree) {
aoqi@0 1494 if (!tree.type.hasTag(ERROR)) {
aoqi@0 1495 result = tree.type;
aoqi@0 1496 } else {
aoqi@0 1497 ClassType clazzType = (ClassType) visit(tree.clazz);
aoqi@0 1498 if (synthesizedSymbols.contains(clazzType.tsym))
aoqi@0 1499 synthesizeTyparams((ClassSymbol) clazzType.tsym, tree.arguments.size());
aoqi@0 1500 final List<Type> actuals = visit(tree.arguments);
aoqi@0 1501 result = new ErrorType(tree.type, clazzType.tsym) {
aoqi@0 1502 @Override
aoqi@0 1503 public List<Type> getTypeArguments() {
aoqi@0 1504 return actuals;
aoqi@0 1505 }
aoqi@0 1506 };
aoqi@0 1507 }
aoqi@0 1508 }
aoqi@0 1509
aoqi@0 1510 ClassSymbol synthesizeClass(Name name, Symbol owner) {
aoqi@0 1511 int flags = interfaceExpected ? INTERFACE : 0;
aoqi@0 1512 ClassSymbol c = new ClassSymbol(flags, name, owner);
aoqi@0 1513 c.members_field = new Scope.ErrorScope(c);
aoqi@0 1514 c.type = new ErrorType(originalType, c) {
aoqi@0 1515 @Override
aoqi@0 1516 public List<Type> getTypeArguments() {
aoqi@0 1517 return typarams_field;
aoqi@0 1518 }
aoqi@0 1519 };
aoqi@0 1520 synthesizedSymbols = synthesizedSymbols.prepend(c);
aoqi@0 1521 return c;
aoqi@0 1522 }
aoqi@0 1523
aoqi@0 1524 void synthesizeTyparams(ClassSymbol sym, int n) {
aoqi@0 1525 ClassType ct = (ClassType) sym.type;
aoqi@0 1526 Assert.check(ct.typarams_field.isEmpty());
aoqi@0 1527 if (n == 1) {
aoqi@0 1528 TypeVar v = new TypeVar(names.fromString("T"), sym, syms.botType);
aoqi@0 1529 ct.typarams_field = ct.typarams_field.prepend(v);
aoqi@0 1530 } else {
aoqi@0 1531 for (int i = n; i > 0; i--) {
aoqi@0 1532 TypeVar v = new TypeVar(names.fromString("T" + i), sym, syms.botType);
aoqi@0 1533 ct.typarams_field = ct.typarams_field.prepend(v);
aoqi@0 1534 }
aoqi@0 1535 }
aoqi@0 1536 }
aoqi@0 1537 }
aoqi@0 1538
aoqi@0 1539
aoqi@0 1540 /* ***************************************************************************
aoqi@0 1541 * tree building
aoqi@0 1542 ****************************************************************************/
aoqi@0 1543
aoqi@0 1544 /** Generate default constructor for given class. For classes different
aoqi@0 1545 * from java.lang.Object, this is:
aoqi@0 1546 *
aoqi@0 1547 * c(argtype_0 x_0, ..., argtype_n x_n) throws thrown {
aoqi@0 1548 * super(x_0, ..., x_n)
aoqi@0 1549 * }
aoqi@0 1550 *
aoqi@0 1551 * or, if based == true:
aoqi@0 1552 *
aoqi@0 1553 * c(argtype_0 x_0, ..., argtype_n x_n) throws thrown {
aoqi@0 1554 * x_0.super(x_1, ..., x_n)
aoqi@0 1555 * }
aoqi@0 1556 *
aoqi@0 1557 * @param make The tree factory.
aoqi@0 1558 * @param c The class owning the default constructor.
aoqi@0 1559 * @param argtypes The parameter types of the constructor.
aoqi@0 1560 * @param thrown The thrown exceptions of the constructor.
aoqi@0 1561 * @param based Is first parameter a this$n?
aoqi@0 1562 */
aoqi@0 1563 JCTree DefaultConstructor(TreeMaker make,
aoqi@0 1564 ClassSymbol c,
aoqi@0 1565 MethodSymbol baseInit,
aoqi@0 1566 List<Type> typarams,
aoqi@0 1567 List<Type> argtypes,
aoqi@0 1568 List<Type> thrown,
aoqi@0 1569 long flags,
aoqi@0 1570 boolean based) {
aoqi@0 1571 JCTree result;
aoqi@0 1572 if ((c.flags() & ENUM) != 0 &&
aoqi@0 1573 (types.supertype(c.type).tsym == syms.enumSym)) {
aoqi@0 1574 // constructors of true enums are private
aoqi@0 1575 flags = (flags & ~AccessFlags) | PRIVATE | GENERATEDCONSTR;
aoqi@0 1576 } else
aoqi@0 1577 flags |= (c.flags() & AccessFlags) | GENERATEDCONSTR;
aoqi@0 1578 if (c.name.isEmpty()) {
aoqi@0 1579 flags |= ANONCONSTR;
aoqi@0 1580 }
aoqi@0 1581 Type mType = new MethodType(argtypes, null, thrown, c);
aoqi@0 1582 Type initType = typarams.nonEmpty() ?
aoqi@0 1583 new ForAll(typarams, mType) :
aoqi@0 1584 mType;
aoqi@0 1585 MethodSymbol init = new MethodSymbol(flags, names.init,
aoqi@0 1586 initType, c);
aoqi@0 1587 init.params = createDefaultConstructorParams(make, baseInit, init,
aoqi@0 1588 argtypes, based);
aoqi@0 1589 List<JCVariableDecl> params = make.Params(argtypes, init);
aoqi@0 1590 List<JCStatement> stats = List.nil();
aoqi@0 1591 if (c.type != syms.objectType) {
aoqi@0 1592 stats = stats.prepend(SuperCall(make, typarams, params, based));
aoqi@0 1593 }
aoqi@0 1594 result = make.MethodDef(init, make.Block(0, stats));
aoqi@0 1595 return result;
aoqi@0 1596 }
aoqi@0 1597
aoqi@0 1598 private List<VarSymbol> createDefaultConstructorParams(
aoqi@0 1599 TreeMaker make,
aoqi@0 1600 MethodSymbol baseInit,
aoqi@0 1601 MethodSymbol init,
aoqi@0 1602 List<Type> argtypes,
aoqi@0 1603 boolean based) {
aoqi@0 1604 List<VarSymbol> initParams = null;
aoqi@0 1605 List<Type> argTypesList = argtypes;
aoqi@0 1606 if (based) {
aoqi@0 1607 /* In this case argtypes will have an extra type, compared to baseInit,
aoqi@0 1608 * corresponding to the type of the enclosing instance i.e.:
aoqi@0 1609 *
aoqi@0 1610 * Inner i = outer.new Inner(1){}
aoqi@0 1611 *
aoqi@0 1612 * in the above example argtypes will be (Outer, int) and baseInit
aoqi@0 1613 * will have parameter's types (int). So in this case we have to add
aoqi@0 1614 * first the extra type in argtypes and then get the names of the
aoqi@0 1615 * parameters from baseInit.
aoqi@0 1616 */
aoqi@0 1617 initParams = List.nil();
aoqi@0 1618 VarSymbol param = new VarSymbol(PARAMETER, make.paramName(0), argtypes.head, init);
aoqi@0 1619 initParams = initParams.append(param);
aoqi@0 1620 argTypesList = argTypesList.tail;
aoqi@0 1621 }
aoqi@0 1622 if (baseInit != null && baseInit.params != null &&
aoqi@0 1623 baseInit.params.nonEmpty() && argTypesList.nonEmpty()) {
aoqi@0 1624 initParams = (initParams == null) ? List.<VarSymbol>nil() : initParams;
aoqi@0 1625 List<VarSymbol> baseInitParams = baseInit.params;
aoqi@0 1626 while (baseInitParams.nonEmpty() && argTypesList.nonEmpty()) {
aoqi@0 1627 VarSymbol param = new VarSymbol(baseInitParams.head.flags() | PARAMETER,
aoqi@0 1628 baseInitParams.head.name, argTypesList.head, init);
aoqi@0 1629 initParams = initParams.append(param);
aoqi@0 1630 baseInitParams = baseInitParams.tail;
aoqi@0 1631 argTypesList = argTypesList.tail;
aoqi@0 1632 }
aoqi@0 1633 }
aoqi@0 1634 return initParams;
aoqi@0 1635 }
aoqi@0 1636
aoqi@0 1637 /** Generate call to superclass constructor. This is:
aoqi@0 1638 *
aoqi@0 1639 * super(id_0, ..., id_n)
aoqi@0 1640 *
aoqi@0 1641 * or, if based == true
aoqi@0 1642 *
aoqi@0 1643 * id_0.super(id_1,...,id_n)
aoqi@0 1644 *
aoqi@0 1645 * where id_0, ..., id_n are the names of the given parameters.
aoqi@0 1646 *
aoqi@0 1647 * @param make The tree factory
aoqi@0 1648 * @param params The parameters that need to be passed to super
aoqi@0 1649 * @param typarams The type parameters that need to be passed to super
aoqi@0 1650 * @param based Is first parameter a this$n?
aoqi@0 1651 */
aoqi@0 1652 JCExpressionStatement SuperCall(TreeMaker make,
aoqi@0 1653 List<Type> typarams,
aoqi@0 1654 List<JCVariableDecl> params,
aoqi@0 1655 boolean based) {
aoqi@0 1656 JCExpression meth;
aoqi@0 1657 if (based) {
aoqi@0 1658 meth = make.Select(make.Ident(params.head), names._super);
aoqi@0 1659 params = params.tail;
aoqi@0 1660 } else {
aoqi@0 1661 meth = make.Ident(names._super);
aoqi@0 1662 }
aoqi@0 1663 List<JCExpression> typeargs = typarams.nonEmpty() ? make.Types(typarams) : null;
aoqi@0 1664 return make.Exec(make.Apply(typeargs, meth, make.Idents(params)));
aoqi@0 1665 }
aoqi@0 1666 }

mercurial