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

Mon, 14 Oct 2013 23:07:43 -0700

author
jjg
date
Mon, 14 Oct 2013 23:07:43 -0700
changeset 2114
09a414673570
parent 2111
87b5bfef7edb
child 2133
19e8eebfbe52
permissions
-rw-r--r--

8025998: Missing LV table in lambda bodies
Reviewed-by: vromero

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

mercurial