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

Sat, 01 Jun 2013 21:57:56 +0100

author
vromero
date
Sat, 01 Jun 2013 21:57:56 +0100
changeset 1791
e9855150c5b0
parent 1755
ddb4a2bfcd82
child 1802
8fb68f73d4b1
permissions
-rw-r--r--

8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
Reviewed-by: mcimadamore

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

mercurial