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

Sun, 08 Sep 2013 11:54:21 +0100

author
vromero
date
Sun, 08 Sep 2013 11:54:21 +0100
changeset 2013
2de3750d65a5
parent 1974
25aaff78d754
child 2027
4932bb04c4b8
permissions
-rw-r--r--

8024398: javac, compiler crashes with try with empty body
Reviewed-by: jjg

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

mercurial