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

Tue, 18 Jun 2013 19:02:48 +0100

author
vromero
date
Tue, 18 Jun 2013 19:02:48 +0100
changeset 1826
9851071b551a
parent 1820
6b48ebae2569
child 1850
6debfa63a4a1
permissions
-rw-r--r--

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

mercurial