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

Thu, 04 Aug 2016 23:36:47 -0700

author
asaha
date
Thu, 04 Aug 2016 23:36:47 -0700
changeset 3270
8a30511b2ea4
parent 2596
fa8be3ce18fc
child 2702
9ca8d8713094
permissions
-rw-r--r--

8162511: 8u111 L10n resource file updates
Summary: 8u111 L10n resource file updates
Reviewed-by: coffeys
Contributed-by: li.jiang@oracle.com

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

mercurial