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

Fri, 26 Jun 2009 18:51:39 -0700

author
jjg
date
Fri, 26 Jun 2009 18:51:39 -0700
changeset 308
03944ee4fac4
parent 299
22872b24d38c
child 359
8227961c64d3
permissions
-rw-r--r--

6843077: JSR 308: Annotations on types
Reviewed-by: jjg, mcimadamore, darcy
Contributed-by: mernst@cs.washington.edu, mali@csail.mit.edu, mpapi@csail.mit.edu

duke@1 1 /*
xdono@229 2 * Copyright 1999-2009 Sun Microsystems, Inc. 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
duke@1 7 * published by the Free Software Foundation. Sun designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
duke@1 9 * by Sun 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 *
duke@1 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@1 22 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@1 23 * have any questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javac.comp;
duke@1 27
duke@1 28 import java.util.*;
duke@1 29 import java.util.Set;
duke@1 30
duke@1 31 import com.sun.tools.javac.code.*;
duke@1 32 import com.sun.tools.javac.jvm.*;
duke@1 33 import com.sun.tools.javac.tree.*;
duke@1 34 import com.sun.tools.javac.util.*;
duke@1 35 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
duke@1 36 import com.sun.tools.javac.util.List;
duke@1 37
duke@1 38 import com.sun.tools.javac.tree.JCTree.*;
duke@1 39 import com.sun.tools.javac.code.Lint;
duke@1 40 import com.sun.tools.javac.code.Lint.LintCategory;
duke@1 41 import com.sun.tools.javac.code.Type.*;
duke@1 42 import com.sun.tools.javac.code.Symbol.*;
duke@1 43
duke@1 44 import static com.sun.tools.javac.code.Flags.*;
duke@1 45 import static com.sun.tools.javac.code.Kinds.*;
duke@1 46 import static com.sun.tools.javac.code.TypeTags.*;
duke@1 47
duke@1 48 /** Type checking helper class for the attribution phase.
duke@1 49 *
duke@1 50 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
duke@1 51 * you write code that depends on this, you do so at your own risk.
duke@1 52 * This code and its internal interfaces are subject to change or
duke@1 53 * deletion without notice.</b>
duke@1 54 */
duke@1 55 public class Check {
duke@1 56 protected static final Context.Key<Check> checkKey =
duke@1 57 new Context.Key<Check>();
duke@1 58
jjg@113 59 private final Names names;
duke@1 60 private final Log log;
duke@1 61 private final Symtab syms;
duke@1 62 private final Infer infer;
duke@1 63 private final Target target;
duke@1 64 private final Source source;
duke@1 65 private final Types types;
mcimadamore@89 66 private final JCDiagnostic.Factory diags;
duke@1 67 private final boolean skipAnnotations;
duke@1 68 private final TreeInfo treeinfo;
duke@1 69
duke@1 70 // The set of lint options currently in effect. It is initialized
duke@1 71 // from the context, and then is set/reset as needed by Attr as it
duke@1 72 // visits all the various parts of the trees during attribution.
duke@1 73 private Lint lint;
duke@1 74
duke@1 75 public static Check instance(Context context) {
duke@1 76 Check instance = context.get(checkKey);
duke@1 77 if (instance == null)
duke@1 78 instance = new Check(context);
duke@1 79 return instance;
duke@1 80 }
duke@1 81
duke@1 82 protected Check(Context context) {
duke@1 83 context.put(checkKey, this);
duke@1 84
jjg@113 85 names = Names.instance(context);
duke@1 86 log = Log.instance(context);
duke@1 87 syms = Symtab.instance(context);
duke@1 88 infer = Infer.instance(context);
duke@1 89 this.types = Types.instance(context);
mcimadamore@89 90 diags = JCDiagnostic.Factory.instance(context);
duke@1 91 Options options = Options.instance(context);
duke@1 92 target = Target.instance(context);
duke@1 93 source = Source.instance(context);
duke@1 94 lint = Lint.instance(context);
duke@1 95 treeinfo = TreeInfo.instance(context);
duke@1 96
duke@1 97 Source source = Source.instance(context);
duke@1 98 allowGenerics = source.allowGenerics();
duke@1 99 allowAnnotations = source.allowAnnotations();
duke@1 100 complexInference = options.get("-complexinference") != null;
duke@1 101 skipAnnotations = options.get("skipAnnotations") != null;
duke@1 102
duke@1 103 boolean verboseDeprecated = lint.isEnabled(LintCategory.DEPRECATION);
duke@1 104 boolean verboseUnchecked = lint.isEnabled(LintCategory.UNCHECKED);
jjg@60 105 boolean enforceMandatoryWarnings = source.enforceMandatoryWarnings();
duke@1 106
jjg@60 107 deprecationHandler = new MandatoryWarningHandler(log, verboseDeprecated,
jjg@60 108 enforceMandatoryWarnings, "deprecated");
jjg@60 109 uncheckedHandler = new MandatoryWarningHandler(log, verboseUnchecked,
jjg@60 110 enforceMandatoryWarnings, "unchecked");
duke@1 111 }
duke@1 112
duke@1 113 /** Switch: generics enabled?
duke@1 114 */
duke@1 115 boolean allowGenerics;
duke@1 116
duke@1 117 /** Switch: annotations enabled?
duke@1 118 */
duke@1 119 boolean allowAnnotations;
duke@1 120
duke@1 121 /** Switch: -complexinference option set?
duke@1 122 */
duke@1 123 boolean complexInference;
duke@1 124
duke@1 125 /** A table mapping flat names of all compiled classes in this run to their
duke@1 126 * symbols; maintained from outside.
duke@1 127 */
duke@1 128 public Map<Name,ClassSymbol> compiled = new HashMap<Name, ClassSymbol>();
duke@1 129
duke@1 130 /** A handler for messages about deprecated usage.
duke@1 131 */
duke@1 132 private MandatoryWarningHandler deprecationHandler;
duke@1 133
duke@1 134 /** A handler for messages about unchecked or unsafe usage.
duke@1 135 */
duke@1 136 private MandatoryWarningHandler uncheckedHandler;
duke@1 137
duke@1 138
duke@1 139 /* *************************************************************************
duke@1 140 * Errors and Warnings
duke@1 141 **************************************************************************/
duke@1 142
duke@1 143 Lint setLint(Lint newLint) {
duke@1 144 Lint prev = lint;
duke@1 145 lint = newLint;
duke@1 146 return prev;
duke@1 147 }
duke@1 148
duke@1 149 /** Warn about deprecated symbol.
duke@1 150 * @param pos Position to be used for error reporting.
duke@1 151 * @param sym The deprecated symbol.
duke@1 152 */
duke@1 153 void warnDeprecated(DiagnosticPosition pos, Symbol sym) {
duke@1 154 if (!lint.isSuppressed(LintCategory.DEPRECATION))
duke@1 155 deprecationHandler.report(pos, "has.been.deprecated", sym, sym.location());
duke@1 156 }
duke@1 157
duke@1 158 /** Warn about unchecked operation.
duke@1 159 * @param pos Position to be used for error reporting.
duke@1 160 * @param msg A string describing the problem.
duke@1 161 */
duke@1 162 public void warnUnchecked(DiagnosticPosition pos, String msg, Object... args) {
duke@1 163 if (!lint.isSuppressed(LintCategory.UNCHECKED))
duke@1 164 uncheckedHandler.report(pos, msg, args);
duke@1 165 }
duke@1 166
duke@1 167 /**
duke@1 168 * Report any deferred diagnostics.
duke@1 169 */
duke@1 170 public void reportDeferredDiagnostics() {
duke@1 171 deprecationHandler.reportDeferredDiagnostic();
duke@1 172 uncheckedHandler.reportDeferredDiagnostic();
duke@1 173 }
duke@1 174
duke@1 175
duke@1 176 /** Report a failure to complete a class.
duke@1 177 * @param pos Position to be used for error reporting.
duke@1 178 * @param ex The failure to report.
duke@1 179 */
duke@1 180 public Type completionError(DiagnosticPosition pos, CompletionFailure ex) {
jjg@12 181 log.error(pos, "cant.access", ex.sym, ex.getDetailValue());
duke@1 182 if (ex instanceof ClassReader.BadClassFile) throw new Abort();
duke@1 183 else return syms.errType;
duke@1 184 }
duke@1 185
duke@1 186 /** Report a type error.
duke@1 187 * @param pos Position to be used for error reporting.
duke@1 188 * @param problem A string describing the error.
duke@1 189 * @param found The type that was found.
duke@1 190 * @param req The type that was required.
duke@1 191 */
duke@1 192 Type typeError(DiagnosticPosition pos, Object problem, Type found, Type req) {
duke@1 193 log.error(pos, "prob.found.req",
duke@1 194 problem, found, req);
jjg@110 195 return types.createErrorType(found);
duke@1 196 }
duke@1 197
duke@1 198 Type typeError(DiagnosticPosition pos, String problem, Type found, Type req, Object explanation) {
duke@1 199 log.error(pos, "prob.found.req.1", problem, found, req, explanation);
jjg@110 200 return types.createErrorType(found);
duke@1 201 }
duke@1 202
duke@1 203 /** Report an error that wrong type tag was found.
duke@1 204 * @param pos Position to be used for error reporting.
duke@1 205 * @param required An internationalized string describing the type tag
duke@1 206 * required.
duke@1 207 * @param found The type that was found.
duke@1 208 */
duke@1 209 Type typeTagError(DiagnosticPosition pos, Object required, Object found) {
jrose@267 210 // this error used to be raised by the parser,
jrose@267 211 // but has been delayed to this point:
jrose@267 212 if (found instanceof Type && ((Type)found).tag == VOID) {
jrose@267 213 log.error(pos, "illegal.start.of.type");
jrose@267 214 return syms.errType;
jrose@267 215 }
duke@1 216 log.error(pos, "type.found.req", found, required);
jjg@110 217 return types.createErrorType(found instanceof Type ? (Type)found : syms.errType);
duke@1 218 }
duke@1 219
duke@1 220 /** Report an error that symbol cannot be referenced before super
duke@1 221 * has been called.
duke@1 222 * @param pos Position to be used for error reporting.
duke@1 223 * @param sym The referenced symbol.
duke@1 224 */
duke@1 225 void earlyRefError(DiagnosticPosition pos, Symbol sym) {
duke@1 226 log.error(pos, "cant.ref.before.ctor.called", sym);
duke@1 227 }
duke@1 228
duke@1 229 /** Report duplicate declaration error.
duke@1 230 */
duke@1 231 void duplicateError(DiagnosticPosition pos, Symbol sym) {
duke@1 232 if (!sym.type.isErroneous()) {
duke@1 233 log.error(pos, "already.defined", sym, sym.location());
duke@1 234 }
duke@1 235 }
duke@1 236
duke@1 237 /** Report array/varargs duplicate declaration
duke@1 238 */
duke@1 239 void varargsDuplicateError(DiagnosticPosition pos, Symbol sym1, Symbol sym2) {
duke@1 240 if (!sym1.type.isErroneous() && !sym2.type.isErroneous()) {
duke@1 241 log.error(pos, "array.and.varargs", sym1, sym2, sym2.location());
duke@1 242 }
duke@1 243 }
duke@1 244
duke@1 245 /* ************************************************************************
duke@1 246 * duplicate declaration checking
duke@1 247 *************************************************************************/
duke@1 248
duke@1 249 /** Check that variable does not hide variable with same name in
duke@1 250 * immediately enclosing local scope.
duke@1 251 * @param pos Position for error reporting.
duke@1 252 * @param v The symbol.
duke@1 253 * @param s The scope.
duke@1 254 */
duke@1 255 void checkTransparentVar(DiagnosticPosition pos, VarSymbol v, Scope s) {
duke@1 256 if (s.next != null) {
duke@1 257 for (Scope.Entry e = s.next.lookup(v.name);
duke@1 258 e.scope != null && e.sym.owner == v.owner;
duke@1 259 e = e.next()) {
duke@1 260 if (e.sym.kind == VAR &&
duke@1 261 (e.sym.owner.kind & (VAR | MTH)) != 0 &&
duke@1 262 v.name != names.error) {
duke@1 263 duplicateError(pos, e.sym);
duke@1 264 return;
duke@1 265 }
duke@1 266 }
duke@1 267 }
duke@1 268 }
duke@1 269
duke@1 270 /** Check that a class or interface does not hide a class or
duke@1 271 * interface with same name in immediately enclosing local scope.
duke@1 272 * @param pos Position for error reporting.
duke@1 273 * @param c The symbol.
duke@1 274 * @param s The scope.
duke@1 275 */
duke@1 276 void checkTransparentClass(DiagnosticPosition pos, ClassSymbol c, Scope s) {
duke@1 277 if (s.next != null) {
duke@1 278 for (Scope.Entry e = s.next.lookup(c.name);
duke@1 279 e.scope != null && e.sym.owner == c.owner;
duke@1 280 e = e.next()) {
duke@1 281 if (e.sym.kind == TYP &&
duke@1 282 (e.sym.owner.kind & (VAR | MTH)) != 0 &&
duke@1 283 c.name != names.error) {
duke@1 284 duplicateError(pos, e.sym);
duke@1 285 return;
duke@1 286 }
duke@1 287 }
duke@1 288 }
duke@1 289 }
duke@1 290
duke@1 291 /** Check that class does not have the same name as one of
duke@1 292 * its enclosing classes, or as a class defined in its enclosing scope.
duke@1 293 * return true if class is unique in its enclosing scope.
duke@1 294 * @param pos Position for error reporting.
duke@1 295 * @param name The class name.
duke@1 296 * @param s The enclosing scope.
duke@1 297 */
duke@1 298 boolean checkUniqueClassName(DiagnosticPosition pos, Name name, Scope s) {
duke@1 299 for (Scope.Entry e = s.lookup(name); e.scope == s; e = e.next()) {
duke@1 300 if (e.sym.kind == TYP && e.sym.name != names.error) {
duke@1 301 duplicateError(pos, e.sym);
duke@1 302 return false;
duke@1 303 }
duke@1 304 }
duke@1 305 for (Symbol sym = s.owner; sym != null; sym = sym.owner) {
duke@1 306 if (sym.kind == TYP && sym.name == name && sym.name != names.error) {
duke@1 307 duplicateError(pos, sym);
duke@1 308 return true;
duke@1 309 }
duke@1 310 }
duke@1 311 return true;
duke@1 312 }
duke@1 313
duke@1 314 /* *************************************************************************
duke@1 315 * Class name generation
duke@1 316 **************************************************************************/
duke@1 317
duke@1 318 /** Return name of local class.
duke@1 319 * This is of the form <enclClass> $ n <classname>
duke@1 320 * where
duke@1 321 * enclClass is the flat name of the enclosing class,
duke@1 322 * classname is the simple name of the local class
duke@1 323 */
duke@1 324 Name localClassName(ClassSymbol c) {
duke@1 325 for (int i=1; ; i++) {
duke@1 326 Name flatname = names.
duke@1 327 fromString("" + c.owner.enclClass().flatname +
duke@1 328 target.syntheticNameChar() + i +
duke@1 329 c.name);
duke@1 330 if (compiled.get(flatname) == null) return flatname;
duke@1 331 }
duke@1 332 }
duke@1 333
duke@1 334 /* *************************************************************************
duke@1 335 * Type Checking
duke@1 336 **************************************************************************/
duke@1 337
duke@1 338 /** Check that a given type is assignable to a given proto-type.
duke@1 339 * If it is, return the type, otherwise return errType.
duke@1 340 * @param pos Position to be used for error reporting.
duke@1 341 * @param found The type that was found.
duke@1 342 * @param req The type that was required.
duke@1 343 */
duke@1 344 Type checkType(DiagnosticPosition pos, Type found, Type req) {
duke@1 345 if (req.tag == ERROR)
duke@1 346 return req;
duke@1 347 if (found.tag == FORALL)
duke@1 348 return instantiatePoly(pos, (ForAll)found, req, convertWarner(pos, found, req));
duke@1 349 if (req.tag == NONE)
duke@1 350 return found;
duke@1 351 if (types.isAssignable(found, req, convertWarner(pos, found, req)))
duke@1 352 return found;
duke@1 353 if (found.tag <= DOUBLE && req.tag <= DOUBLE)
mcimadamore@89 354 return typeError(pos, diags.fragment("possible.loss.of.precision"), found, req);
duke@1 355 if (found.isSuperBound()) {
duke@1 356 log.error(pos, "assignment.from.super-bound", found);
jjg@110 357 return types.createErrorType(found);
duke@1 358 }
duke@1 359 if (req.isExtendsBound()) {
duke@1 360 log.error(pos, "assignment.to.extends-bound", req);
jjg@110 361 return types.createErrorType(found);
duke@1 362 }
mcimadamore@89 363 return typeError(pos, diags.fragment("incompatible.types"), found, req);
duke@1 364 }
duke@1 365
duke@1 366 /** Instantiate polymorphic type to some prototype, unless
duke@1 367 * prototype is `anyPoly' in which case polymorphic type
duke@1 368 * is returned unchanged.
duke@1 369 */
duke@1 370 Type instantiatePoly(DiagnosticPosition pos, ForAll t, Type pt, Warner warn) {
duke@1 371 if (pt == Infer.anyPoly && complexInference) {
duke@1 372 return t;
duke@1 373 } else if (pt == Infer.anyPoly || pt.tag == NONE) {
duke@1 374 Type newpt = t.qtype.tag <= VOID ? t.qtype : syms.objectType;
duke@1 375 return instantiatePoly(pos, t, newpt, warn);
duke@1 376 } else if (pt.tag == ERROR) {
duke@1 377 return pt;
duke@1 378 } else {
duke@1 379 try {
duke@1 380 return infer.instantiateExpr(t, pt, warn);
duke@1 381 } catch (Infer.NoInstanceException ex) {
duke@1 382 if (ex.isAmbiguous) {
duke@1 383 JCDiagnostic d = ex.getDiagnostic();
duke@1 384 log.error(pos,
duke@1 385 "undetermined.type" + (d!=null ? ".1" : ""),
duke@1 386 t, d);
jjg@110 387 return types.createErrorType(pt);
duke@1 388 } else {
duke@1 389 JCDiagnostic d = ex.getDiagnostic();
duke@1 390 return typeError(pos,
mcimadamore@89 391 diags.fragment("incompatible.types" + (d!=null ? ".1" : ""), d),
duke@1 392 t, pt);
duke@1 393 }
mcimadamore@299 394 } catch (Infer.InvalidInstanceException ex) {
mcimadamore@299 395 JCDiagnostic d = ex.getDiagnostic();
mcimadamore@299 396 log.error(pos, "invalid.inferred.types", t.tvars, d);
mcimadamore@299 397 return types.createErrorType(pt);
duke@1 398 }
duke@1 399 }
duke@1 400 }
duke@1 401
duke@1 402 /** Check that a given type can be cast to a given target type.
duke@1 403 * Return the result of the cast.
duke@1 404 * @param pos Position to be used for error reporting.
duke@1 405 * @param found The type that is being cast.
duke@1 406 * @param req The target type of the cast.
duke@1 407 */
duke@1 408 Type checkCastable(DiagnosticPosition pos, Type found, Type req) {
duke@1 409 if (found.tag == FORALL) {
duke@1 410 instantiatePoly(pos, (ForAll) found, req, castWarner(pos, found, req));
duke@1 411 return req;
duke@1 412 } else if (types.isCastable(found, req, castWarner(pos, found, req))) {
duke@1 413 return req;
duke@1 414 } else {
duke@1 415 return typeError(pos,
mcimadamore@89 416 diags.fragment("inconvertible.types"),
duke@1 417 found, req);
duke@1 418 }
duke@1 419 }
duke@1 420 //where
duke@1 421 /** Is type a type variable, or a (possibly multi-dimensional) array of
duke@1 422 * type variables?
duke@1 423 */
duke@1 424 boolean isTypeVar(Type t) {
duke@1 425 return t.tag == TYPEVAR || t.tag == ARRAY && isTypeVar(types.elemtype(t));
duke@1 426 }
duke@1 427
duke@1 428 /** Check that a type is within some bounds.
duke@1 429 *
duke@1 430 * Used in TypeApply to verify that, e.g., X in V<X> is a valid
duke@1 431 * type argument.
duke@1 432 * @param pos Position to be used for error reporting.
duke@1 433 * @param a The type that should be bounded by bs.
duke@1 434 * @param bs The bound.
duke@1 435 */
duke@1 436 private void checkExtends(DiagnosticPosition pos, Type a, TypeVar bs) {
mcimadamore@154 437 if (a.isUnbound()) {
mcimadamore@154 438 return;
mcimadamore@154 439 } else if (a.tag != WILDCARD) {
mcimadamore@154 440 a = types.upperBound(a);
mcimadamore@154 441 for (List<Type> l = types.getBounds(bs); l.nonEmpty(); l = l.tail) {
mcimadamore@154 442 if (!types.isSubtype(a, l.head)) {
mcimadamore@154 443 log.error(pos, "not.within.bounds", a);
mcimadamore@154 444 return;
mcimadamore@154 445 }
mcimadamore@154 446 }
mcimadamore@154 447 } else if (a.isExtendsBound()) {
mcimadamore@154 448 if (!types.isCastable(bs.getUpperBound(), types.upperBound(a), Warner.noWarnings))
mcimadamore@154 449 log.error(pos, "not.within.bounds", a);
mcimadamore@154 450 } else if (a.isSuperBound()) {
mcimadamore@154 451 if (types.notSoftSubtype(types.lowerBound(a), bs.getUpperBound()))
mcimadamore@154 452 log.error(pos, "not.within.bounds", a);
mcimadamore@154 453 }
mcimadamore@154 454 }
mcimadamore@154 455
mcimadamore@154 456 /** Check that a type is within some bounds.
mcimadamore@154 457 *
mcimadamore@154 458 * Used in TypeApply to verify that, e.g., X in V<X> is a valid
mcimadamore@154 459 * type argument.
mcimadamore@154 460 * @param pos Position to be used for error reporting.
mcimadamore@154 461 * @param a The type that should be bounded by bs.
mcimadamore@154 462 * @param bs The bound.
mcimadamore@154 463 */
mcimadamore@154 464 private void checkCapture(JCTypeApply tree) {
mcimadamore@154 465 List<JCExpression> args = tree.getTypeArguments();
mcimadamore@154 466 for (Type arg : types.capture(tree.type).getTypeArguments()) {
mcimadamore@154 467 if (arg.tag == TYPEVAR && arg.getUpperBound().isErroneous()) {
mcimadamore@154 468 log.error(args.head.pos, "not.within.bounds", args.head.type);
mcimadamore@154 469 break;
mcimadamore@79 470 }
mcimadamore@154 471 args = args.tail;
mcimadamore@79 472 }
mcimadamore@154 473 }
duke@1 474
duke@1 475 /** Check that type is different from 'void'.
duke@1 476 * @param pos Position to be used for error reporting.
duke@1 477 * @param t The type to be checked.
duke@1 478 */
duke@1 479 Type checkNonVoid(DiagnosticPosition pos, Type t) {
duke@1 480 if (t.tag == VOID) {
duke@1 481 log.error(pos, "void.not.allowed.here");
jjg@110 482 return types.createErrorType(t);
duke@1 483 } else {
duke@1 484 return t;
duke@1 485 }
duke@1 486 }
duke@1 487
duke@1 488 /** Check that type is a class or interface type.
duke@1 489 * @param pos Position to be used for error reporting.
duke@1 490 * @param t The type to be checked.
duke@1 491 */
duke@1 492 Type checkClassType(DiagnosticPosition pos, Type t) {
duke@1 493 if (t.tag != CLASS && t.tag != ERROR)
duke@1 494 return typeTagError(pos,
mcimadamore@89 495 diags.fragment("type.req.class"),
duke@1 496 (t.tag == TYPEVAR)
mcimadamore@89 497 ? diags.fragment("type.parameter", t)
duke@1 498 : t);
duke@1 499 else
duke@1 500 return t;
duke@1 501 }
duke@1 502
duke@1 503 /** Check that type is a class or interface type.
duke@1 504 * @param pos Position to be used for error reporting.
duke@1 505 * @param t The type to be checked.
duke@1 506 * @param noBounds True if type bounds are illegal here.
duke@1 507 */
duke@1 508 Type checkClassType(DiagnosticPosition pos, Type t, boolean noBounds) {
duke@1 509 t = checkClassType(pos, t);
duke@1 510 if (noBounds && t.isParameterized()) {
duke@1 511 List<Type> args = t.getTypeArguments();
duke@1 512 while (args.nonEmpty()) {
duke@1 513 if (args.head.tag == WILDCARD)
duke@1 514 return typeTagError(pos,
duke@1 515 log.getLocalizedString("type.req.exact"),
duke@1 516 args.head);
duke@1 517 args = args.tail;
duke@1 518 }
duke@1 519 }
duke@1 520 return t;
duke@1 521 }
duke@1 522
duke@1 523 /** Check that type is a reifiable class, interface or array type.
duke@1 524 * @param pos Position to be used for error reporting.
duke@1 525 * @param t The type to be checked.
duke@1 526 */
duke@1 527 Type checkReifiableReferenceType(DiagnosticPosition pos, Type t) {
duke@1 528 if (t.tag != CLASS && t.tag != ARRAY && t.tag != ERROR) {
duke@1 529 return typeTagError(pos,
mcimadamore@89 530 diags.fragment("type.req.class.array"),
duke@1 531 t);
duke@1 532 } else if (!types.isReifiable(t)) {
duke@1 533 log.error(pos, "illegal.generic.type.for.instof");
jjg@110 534 return types.createErrorType(t);
duke@1 535 } else {
duke@1 536 return t;
duke@1 537 }
duke@1 538 }
duke@1 539
duke@1 540 /** Check that type is a reference type, i.e. a class, interface or array type
duke@1 541 * or a type variable.
duke@1 542 * @param pos Position to be used for error reporting.
duke@1 543 * @param t The type to be checked.
duke@1 544 */
duke@1 545 Type checkRefType(DiagnosticPosition pos, Type t) {
duke@1 546 switch (t.tag) {
duke@1 547 case CLASS:
duke@1 548 case ARRAY:
duke@1 549 case TYPEVAR:
duke@1 550 case WILDCARD:
duke@1 551 case ERROR:
duke@1 552 return t;
duke@1 553 default:
duke@1 554 return typeTagError(pos,
mcimadamore@89 555 diags.fragment("type.req.ref"),
duke@1 556 t);
duke@1 557 }
duke@1 558 }
duke@1 559
jrose@267 560 /** Check that each type is a reference type, i.e. a class, interface or array type
jrose@267 561 * or a type variable.
jrose@267 562 * @param trees Original trees, used for error reporting.
jrose@267 563 * @param types The types to be checked.
jrose@267 564 */
jrose@267 565 List<Type> checkRefTypes(List<JCExpression> trees, List<Type> types) {
jrose@267 566 List<JCExpression> tl = trees;
jrose@267 567 for (List<Type> l = types; l.nonEmpty(); l = l.tail) {
jrose@267 568 l.head = checkRefType(tl.head.pos(), l.head);
jrose@267 569 tl = tl.tail;
jrose@267 570 }
jrose@267 571 return types;
jrose@267 572 }
jrose@267 573
duke@1 574 /** Check that type is a null or reference type.
duke@1 575 * @param pos Position to be used for error reporting.
duke@1 576 * @param t The type to be checked.
duke@1 577 */
duke@1 578 Type checkNullOrRefType(DiagnosticPosition pos, Type t) {
duke@1 579 switch (t.tag) {
duke@1 580 case CLASS:
duke@1 581 case ARRAY:
duke@1 582 case TYPEVAR:
duke@1 583 case WILDCARD:
duke@1 584 case BOT:
duke@1 585 case ERROR:
duke@1 586 return t;
duke@1 587 default:
duke@1 588 return typeTagError(pos,
mcimadamore@89 589 diags.fragment("type.req.ref"),
duke@1 590 t);
duke@1 591 }
duke@1 592 }
duke@1 593
duke@1 594 /** Check that flag set does not contain elements of two conflicting sets. s
duke@1 595 * Return true if it doesn't.
duke@1 596 * @param pos Position to be used for error reporting.
duke@1 597 * @param flags The set of flags to be checked.
duke@1 598 * @param set1 Conflicting flags set #1.
duke@1 599 * @param set2 Conflicting flags set #2.
duke@1 600 */
duke@1 601 boolean checkDisjoint(DiagnosticPosition pos, long flags, long set1, long set2) {
duke@1 602 if ((flags & set1) != 0 && (flags & set2) != 0) {
duke@1 603 log.error(pos,
duke@1 604 "illegal.combination.of.modifiers",
mcimadamore@80 605 asFlagSet(TreeInfo.firstFlag(flags & set1)),
mcimadamore@80 606 asFlagSet(TreeInfo.firstFlag(flags & set2)));
duke@1 607 return false;
duke@1 608 } else
duke@1 609 return true;
duke@1 610 }
duke@1 611
duke@1 612 /** Check that given modifiers are legal for given symbol and
duke@1 613 * return modifiers together with any implicit modififiers for that symbol.
duke@1 614 * Warning: we can't use flags() here since this method
duke@1 615 * is called during class enter, when flags() would cause a premature
duke@1 616 * completion.
duke@1 617 * @param pos Position to be used for error reporting.
duke@1 618 * @param flags The set of modifiers given in a definition.
duke@1 619 * @param sym The defined symbol.
duke@1 620 */
duke@1 621 long checkFlags(DiagnosticPosition pos, long flags, Symbol sym, JCTree tree) {
duke@1 622 long mask;
duke@1 623 long implicit = 0;
duke@1 624 switch (sym.kind) {
duke@1 625 case VAR:
duke@1 626 if (sym.owner.kind != TYP)
duke@1 627 mask = LocalVarFlags;
duke@1 628 else if ((sym.owner.flags_field & INTERFACE) != 0)
duke@1 629 mask = implicit = InterfaceVarFlags;
duke@1 630 else
duke@1 631 mask = VarFlags;
duke@1 632 break;
duke@1 633 case MTH:
duke@1 634 if (sym.name == names.init) {
duke@1 635 if ((sym.owner.flags_field & ENUM) != 0) {
duke@1 636 // enum constructors cannot be declared public or
duke@1 637 // protected and must be implicitly or explicitly
duke@1 638 // private
duke@1 639 implicit = PRIVATE;
duke@1 640 mask = PRIVATE;
duke@1 641 } else
duke@1 642 mask = ConstructorFlags;
duke@1 643 } else if ((sym.owner.flags_field & INTERFACE) != 0)
duke@1 644 mask = implicit = InterfaceMethodFlags;
duke@1 645 else {
duke@1 646 mask = MethodFlags;
duke@1 647 }
duke@1 648 // Imply STRICTFP if owner has STRICTFP set.
duke@1 649 if (((flags|implicit) & Flags.ABSTRACT) == 0)
duke@1 650 implicit |= sym.owner.flags_field & STRICTFP;
duke@1 651 break;
duke@1 652 case TYP:
duke@1 653 if (sym.isLocal()) {
duke@1 654 mask = LocalClassFlags;
jjg@113 655 if (sym.name.isEmpty()) { // Anonymous class
duke@1 656 // Anonymous classes in static methods are themselves static;
duke@1 657 // that's why we admit STATIC here.
duke@1 658 mask |= STATIC;
duke@1 659 // JLS: Anonymous classes are final.
duke@1 660 implicit |= FINAL;
duke@1 661 }
duke@1 662 if ((sym.owner.flags_field & STATIC) == 0 &&
duke@1 663 (flags & ENUM) != 0)
duke@1 664 log.error(pos, "enums.must.be.static");
duke@1 665 } else if (sym.owner.kind == TYP) {
duke@1 666 mask = MemberClassFlags;
duke@1 667 if (sym.owner.owner.kind == PCK ||
duke@1 668 (sym.owner.flags_field & STATIC) != 0)
duke@1 669 mask |= STATIC;
duke@1 670 else if ((flags & ENUM) != 0)
duke@1 671 log.error(pos, "enums.must.be.static");
duke@1 672 // Nested interfaces and enums are always STATIC (Spec ???)
duke@1 673 if ((flags & (INTERFACE | ENUM)) != 0 ) implicit = STATIC;
duke@1 674 } else {
duke@1 675 mask = ClassFlags;
duke@1 676 }
duke@1 677 // Interfaces are always ABSTRACT
duke@1 678 if ((flags & INTERFACE) != 0) implicit |= ABSTRACT;
duke@1 679
duke@1 680 if ((flags & ENUM) != 0) {
duke@1 681 // enums can't be declared abstract or final
duke@1 682 mask &= ~(ABSTRACT | FINAL);
duke@1 683 implicit |= implicitEnumFinalFlag(tree);
duke@1 684 }
duke@1 685 // Imply STRICTFP if owner has STRICTFP set.
duke@1 686 implicit |= sym.owner.flags_field & STRICTFP;
duke@1 687 break;
duke@1 688 default:
duke@1 689 throw new AssertionError();
duke@1 690 }
duke@1 691 long illegal = flags & StandardFlags & ~mask;
duke@1 692 if (illegal != 0) {
duke@1 693 if ((illegal & INTERFACE) != 0) {
duke@1 694 log.error(pos, "intf.not.allowed.here");
duke@1 695 mask |= INTERFACE;
duke@1 696 }
duke@1 697 else {
duke@1 698 log.error(pos,
mcimadamore@80 699 "mod.not.allowed.here", asFlagSet(illegal));
duke@1 700 }
duke@1 701 }
duke@1 702 else if ((sym.kind == TYP ||
duke@1 703 // ISSUE: Disallowing abstract&private is no longer appropriate
duke@1 704 // in the presence of inner classes. Should it be deleted here?
duke@1 705 checkDisjoint(pos, flags,
duke@1 706 ABSTRACT,
duke@1 707 PRIVATE | STATIC))
duke@1 708 &&
duke@1 709 checkDisjoint(pos, flags,
duke@1 710 ABSTRACT | INTERFACE,
duke@1 711 FINAL | NATIVE | SYNCHRONIZED)
duke@1 712 &&
duke@1 713 checkDisjoint(pos, flags,
duke@1 714 PUBLIC,
duke@1 715 PRIVATE | PROTECTED)
duke@1 716 &&
duke@1 717 checkDisjoint(pos, flags,
duke@1 718 PRIVATE,
duke@1 719 PUBLIC | PROTECTED)
duke@1 720 &&
duke@1 721 checkDisjoint(pos, flags,
duke@1 722 FINAL,
duke@1 723 VOLATILE)
duke@1 724 &&
duke@1 725 (sym.kind == TYP ||
duke@1 726 checkDisjoint(pos, flags,
duke@1 727 ABSTRACT | NATIVE,
duke@1 728 STRICTFP))) {
duke@1 729 // skip
duke@1 730 }
duke@1 731 return flags & (mask | ~StandardFlags) | implicit;
duke@1 732 }
duke@1 733
duke@1 734
duke@1 735 /** Determine if this enum should be implicitly final.
duke@1 736 *
duke@1 737 * If the enum has no specialized enum contants, it is final.
duke@1 738 *
duke@1 739 * If the enum does have specialized enum contants, it is
duke@1 740 * <i>not</i> final.
duke@1 741 */
duke@1 742 private long implicitEnumFinalFlag(JCTree tree) {
duke@1 743 if (tree.getTag() != JCTree.CLASSDEF) return 0;
duke@1 744 class SpecialTreeVisitor extends JCTree.Visitor {
duke@1 745 boolean specialized;
duke@1 746 SpecialTreeVisitor() {
duke@1 747 this.specialized = false;
duke@1 748 };
duke@1 749
duke@1 750 public void visitTree(JCTree tree) { /* no-op */ }
duke@1 751
duke@1 752 public void visitVarDef(JCVariableDecl tree) {
duke@1 753 if ((tree.mods.flags & ENUM) != 0) {
duke@1 754 if (tree.init instanceof JCNewClass &&
duke@1 755 ((JCNewClass) tree.init).def != null) {
duke@1 756 specialized = true;
duke@1 757 }
duke@1 758 }
duke@1 759 }
duke@1 760 }
duke@1 761
duke@1 762 SpecialTreeVisitor sts = new SpecialTreeVisitor();
duke@1 763 JCClassDecl cdef = (JCClassDecl) tree;
duke@1 764 for (JCTree defs: cdef.defs) {
duke@1 765 defs.accept(sts);
duke@1 766 if (sts.specialized) return 0;
duke@1 767 }
duke@1 768 return FINAL;
duke@1 769 }
duke@1 770
duke@1 771 /* *************************************************************************
duke@1 772 * Type Validation
duke@1 773 **************************************************************************/
duke@1 774
duke@1 775 /** Validate a type expression. That is,
duke@1 776 * check that all type arguments of a parametric type are within
duke@1 777 * their bounds. This must be done in a second phase after type attributon
duke@1 778 * since a class might have a subclass as type parameter bound. E.g:
duke@1 779 *
duke@1 780 * class B<A extends C> { ... }
duke@1 781 * class C extends B<C> { ... }
duke@1 782 *
duke@1 783 * and we can't make sure that the bound is already attributed because
duke@1 784 * of possible cycles.
duke@1 785 */
duke@1 786 private Validator validator = new Validator();
duke@1 787
duke@1 788 /** Visitor method: Validate a type expression, if it is not null, catching
duke@1 789 * and reporting any completion failures.
duke@1 790 */
mcimadamore@122 791 void validate(JCTree tree, Env<AttrContext> env) {
duke@1 792 try {
mcimadamore@122 793 if (tree != null) {
mcimadamore@122 794 validator.env = env;
mcimadamore@122 795 tree.accept(validator);
mcimadamore@122 796 checkRaw(tree, env);
mcimadamore@122 797 }
duke@1 798 } catch (CompletionFailure ex) {
duke@1 799 completionError(tree.pos(), ex);
duke@1 800 }
duke@1 801 }
mcimadamore@122 802 //where
mcimadamore@122 803 void checkRaw(JCTree tree, Env<AttrContext> env) {
mcimadamore@122 804 if (lint.isEnabled(Lint.LintCategory.RAW) &&
mcimadamore@122 805 tree.type.tag == CLASS &&
mcimadamore@122 806 !env.enclClass.name.isEmpty() && //anonymous or intersection
mcimadamore@122 807 tree.type.isRaw()) {
mcimadamore@122 808 log.warning(tree.pos(), "raw.class.use", tree.type, tree.type.tsym.type);
mcimadamore@122 809 }
mcimadamore@122 810 }
duke@1 811
duke@1 812 /** Visitor method: Validate a list of type expressions.
duke@1 813 */
mcimadamore@122 814 void validate(List<? extends JCTree> trees, Env<AttrContext> env) {
duke@1 815 for (List<? extends JCTree> l = trees; l.nonEmpty(); l = l.tail)
mcimadamore@122 816 validate(l.head, env);
duke@1 817 }
duke@1 818
duke@1 819 /** A visitor class for type validation.
duke@1 820 */
duke@1 821 class Validator extends JCTree.Visitor {
duke@1 822
duke@1 823 public void visitTypeArray(JCArrayTypeTree tree) {
mcimadamore@122 824 validate(tree.elemtype, env);
duke@1 825 }
duke@1 826
duke@1 827 public void visitTypeApply(JCTypeApply tree) {
duke@1 828 if (tree.type.tag == CLASS) {
mcimadamore@158 829 List<Type> formals = tree.type.tsym.type.allparams();
mcimadamore@158 830 List<Type> actuals = tree.type.allparams();
duke@1 831 List<JCExpression> args = tree.arguments;
mcimadamore@158 832 List<Type> forms = tree.type.tsym.type.getTypeArguments();
duke@1 833 ListBuffer<TypeVar> tvars_buf = new ListBuffer<TypeVar>();
duke@1 834
duke@1 835 // For matching pairs of actual argument types `a' and
duke@1 836 // formal type parameters with declared bound `b' ...
duke@1 837 while (args.nonEmpty() && forms.nonEmpty()) {
mcimadamore@122 838 validate(args.head, env);
duke@1 839
duke@1 840 // exact type arguments needs to know their
duke@1 841 // bounds (for upper and lower bound
duke@1 842 // calculations). So we create new TypeVars with
duke@1 843 // bounds substed with actuals.
duke@1 844 tvars_buf.append(types.substBound(((TypeVar)forms.head),
duke@1 845 formals,
mcimadamore@78 846 actuals));
duke@1 847
duke@1 848 args = args.tail;
duke@1 849 forms = forms.tail;
duke@1 850 }
duke@1 851
duke@1 852 args = tree.arguments;
mcimadamore@154 853 List<Type> tvars_cap = types.substBounds(formals,
mcimadamore@154 854 formals,
mcimadamore@158 855 types.capture(tree.type).allparams());
mcimadamore@154 856 while (args.nonEmpty() && tvars_cap.nonEmpty()) {
mcimadamore@154 857 // Let the actual arguments know their bound
mcimadamore@154 858 args.head.type.withTypeVar((TypeVar)tvars_cap.head);
mcimadamore@154 859 args = args.tail;
mcimadamore@154 860 tvars_cap = tvars_cap.tail;
mcimadamore@154 861 }
mcimadamore@154 862
mcimadamore@154 863 args = tree.arguments;
duke@1 864 List<TypeVar> tvars = tvars_buf.toList();
mcimadamore@154 865
duke@1 866 while (args.nonEmpty() && tvars.nonEmpty()) {
mcimadamore@154 867 checkExtends(args.head.pos(),
mcimadamore@154 868 args.head.type,
mcimadamore@154 869 tvars.head);
duke@1 870 args = args.tail;
duke@1 871 tvars = tvars.tail;
duke@1 872 }
duke@1 873
mcimadamore@154 874 checkCapture(tree);
duke@1 875
duke@1 876 // Check that this type is either fully parameterized, or
duke@1 877 // not parameterized at all.
duke@1 878 if (tree.type.getEnclosingType().isRaw())
duke@1 879 log.error(tree.pos(), "improperly.formed.type.inner.raw.param");
duke@1 880 if (tree.clazz.getTag() == JCTree.SELECT)
duke@1 881 visitSelectInternal((JCFieldAccess)tree.clazz);
duke@1 882 }
duke@1 883 }
duke@1 884
duke@1 885 public void visitTypeParameter(JCTypeParameter tree) {
mcimadamore@122 886 validate(tree.bounds, env);
duke@1 887 checkClassBounds(tree.pos(), tree.type);
duke@1 888 }
duke@1 889
duke@1 890 @Override
duke@1 891 public void visitWildcard(JCWildcard tree) {
duke@1 892 if (tree.inner != null)
mcimadamore@122 893 validate(tree.inner, env);
duke@1 894 }
duke@1 895
duke@1 896 public void visitSelect(JCFieldAccess tree) {
duke@1 897 if (tree.type.tag == CLASS) {
duke@1 898 visitSelectInternal(tree);
duke@1 899
duke@1 900 // Check that this type is either fully parameterized, or
duke@1 901 // not parameterized at all.
duke@1 902 if (tree.selected.type.isParameterized() && tree.type.tsym.type.getTypeArguments().nonEmpty())
duke@1 903 log.error(tree.pos(), "improperly.formed.type.param.missing");
duke@1 904 }
duke@1 905 }
duke@1 906 public void visitSelectInternal(JCFieldAccess tree) {
mcimadamore@122 907 if (tree.type.tsym.isStatic() &&
duke@1 908 tree.selected.type.isParameterized()) {
duke@1 909 // The enclosing type is not a class, so we are
duke@1 910 // looking at a static member type. However, the
duke@1 911 // qualifying expression is parameterized.
duke@1 912 log.error(tree.pos(), "cant.select.static.class.from.param.type");
duke@1 913 } else {
duke@1 914 // otherwise validate the rest of the expression
mcimadamore@122 915 tree.selected.accept(this);
duke@1 916 }
duke@1 917 }
duke@1 918
jjg@308 919 public void visitAnnotatedType(JCAnnotatedType tree) {
jjg@308 920 tree.underlyingType.accept(this);
jjg@308 921 }
jjg@308 922
duke@1 923 /** Default visitor method: do nothing.
duke@1 924 */
duke@1 925 public void visitTree(JCTree tree) {
duke@1 926 }
mcimadamore@122 927
mcimadamore@122 928 Env<AttrContext> env;
duke@1 929 }
duke@1 930
duke@1 931 /* *************************************************************************
duke@1 932 * Exception checking
duke@1 933 **************************************************************************/
duke@1 934
duke@1 935 /* The following methods treat classes as sets that contain
duke@1 936 * the class itself and all their subclasses
duke@1 937 */
duke@1 938
duke@1 939 /** Is given type a subtype of some of the types in given list?
duke@1 940 */
duke@1 941 boolean subset(Type t, List<Type> ts) {
duke@1 942 for (List<Type> l = ts; l.nonEmpty(); l = l.tail)
duke@1 943 if (types.isSubtype(t, l.head)) return true;
duke@1 944 return false;
duke@1 945 }
duke@1 946
duke@1 947 /** Is given type a subtype or supertype of
duke@1 948 * some of the types in given list?
duke@1 949 */
duke@1 950 boolean intersects(Type t, List<Type> ts) {
duke@1 951 for (List<Type> l = ts; l.nonEmpty(); l = l.tail)
duke@1 952 if (types.isSubtype(t, l.head) || types.isSubtype(l.head, t)) return true;
duke@1 953 return false;
duke@1 954 }
duke@1 955
duke@1 956 /** Add type set to given type list, unless it is a subclass of some class
duke@1 957 * in the list.
duke@1 958 */
duke@1 959 List<Type> incl(Type t, List<Type> ts) {
duke@1 960 return subset(t, ts) ? ts : excl(t, ts).prepend(t);
duke@1 961 }
duke@1 962
duke@1 963 /** Remove type set from type set list.
duke@1 964 */
duke@1 965 List<Type> excl(Type t, List<Type> ts) {
duke@1 966 if (ts.isEmpty()) {
duke@1 967 return ts;
duke@1 968 } else {
duke@1 969 List<Type> ts1 = excl(t, ts.tail);
duke@1 970 if (types.isSubtype(ts.head, t)) return ts1;
duke@1 971 else if (ts1 == ts.tail) return ts;
duke@1 972 else return ts1.prepend(ts.head);
duke@1 973 }
duke@1 974 }
duke@1 975
duke@1 976 /** Form the union of two type set lists.
duke@1 977 */
duke@1 978 List<Type> union(List<Type> ts1, List<Type> ts2) {
duke@1 979 List<Type> ts = ts1;
duke@1 980 for (List<Type> l = ts2; l.nonEmpty(); l = l.tail)
duke@1 981 ts = incl(l.head, ts);
duke@1 982 return ts;
duke@1 983 }
duke@1 984
duke@1 985 /** Form the difference of two type lists.
duke@1 986 */
duke@1 987 List<Type> diff(List<Type> ts1, List<Type> ts2) {
duke@1 988 List<Type> ts = ts1;
duke@1 989 for (List<Type> l = ts2; l.nonEmpty(); l = l.tail)
duke@1 990 ts = excl(l.head, ts);
duke@1 991 return ts;
duke@1 992 }
duke@1 993
duke@1 994 /** Form the intersection of two type lists.
duke@1 995 */
duke@1 996 public List<Type> intersect(List<Type> ts1, List<Type> ts2) {
duke@1 997 List<Type> ts = List.nil();
duke@1 998 for (List<Type> l = ts1; l.nonEmpty(); l = l.tail)
duke@1 999 if (subset(l.head, ts2)) ts = incl(l.head, ts);
duke@1 1000 for (List<Type> l = ts2; l.nonEmpty(); l = l.tail)
duke@1 1001 if (subset(l.head, ts1)) ts = incl(l.head, ts);
duke@1 1002 return ts;
duke@1 1003 }
duke@1 1004
duke@1 1005 /** Is exc an exception symbol that need not be declared?
duke@1 1006 */
duke@1 1007 boolean isUnchecked(ClassSymbol exc) {
duke@1 1008 return
duke@1 1009 exc.kind == ERR ||
duke@1 1010 exc.isSubClass(syms.errorType.tsym, types) ||
duke@1 1011 exc.isSubClass(syms.runtimeExceptionType.tsym, types);
duke@1 1012 }
duke@1 1013
duke@1 1014 /** Is exc an exception type that need not be declared?
duke@1 1015 */
duke@1 1016 boolean isUnchecked(Type exc) {
duke@1 1017 return
duke@1 1018 (exc.tag == TYPEVAR) ? isUnchecked(types.supertype(exc)) :
duke@1 1019 (exc.tag == CLASS) ? isUnchecked((ClassSymbol)exc.tsym) :
duke@1 1020 exc.tag == BOT;
duke@1 1021 }
duke@1 1022
duke@1 1023 /** Same, but handling completion failures.
duke@1 1024 */
duke@1 1025 boolean isUnchecked(DiagnosticPosition pos, Type exc) {
duke@1 1026 try {
duke@1 1027 return isUnchecked(exc);
duke@1 1028 } catch (CompletionFailure ex) {
duke@1 1029 completionError(pos, ex);
duke@1 1030 return true;
duke@1 1031 }
duke@1 1032 }
duke@1 1033
duke@1 1034 /** Is exc handled by given exception list?
duke@1 1035 */
duke@1 1036 boolean isHandled(Type exc, List<Type> handled) {
duke@1 1037 return isUnchecked(exc) || subset(exc, handled);
duke@1 1038 }
duke@1 1039
duke@1 1040 /** Return all exceptions in thrown list that are not in handled list.
duke@1 1041 * @param thrown The list of thrown exceptions.
duke@1 1042 * @param handled The list of handled exceptions.
duke@1 1043 */
duke@1 1044 List<Type> unHandled(List<Type> thrown, List<Type> handled) {
duke@1 1045 List<Type> unhandled = List.nil();
duke@1 1046 for (List<Type> l = thrown; l.nonEmpty(); l = l.tail)
duke@1 1047 if (!isHandled(l.head, handled)) unhandled = unhandled.prepend(l.head);
duke@1 1048 return unhandled;
duke@1 1049 }
duke@1 1050
duke@1 1051 /* *************************************************************************
duke@1 1052 * Overriding/Implementation checking
duke@1 1053 **************************************************************************/
duke@1 1054
duke@1 1055 /** The level of access protection given by a flag set,
duke@1 1056 * where PRIVATE is highest and PUBLIC is lowest.
duke@1 1057 */
duke@1 1058 static int protection(long flags) {
duke@1 1059 switch ((short)(flags & AccessFlags)) {
duke@1 1060 case PRIVATE: return 3;
duke@1 1061 case PROTECTED: return 1;
duke@1 1062 default:
duke@1 1063 case PUBLIC: return 0;
duke@1 1064 case 0: return 2;
duke@1 1065 }
duke@1 1066 }
duke@1 1067
duke@1 1068 /** A customized "cannot override" error message.
duke@1 1069 * @param m The overriding method.
duke@1 1070 * @param other The overridden method.
duke@1 1071 * @return An internationalized string.
duke@1 1072 */
mcimadamore@89 1073 Object cannotOverride(MethodSymbol m, MethodSymbol other) {
duke@1 1074 String key;
duke@1 1075 if ((other.owner.flags() & INTERFACE) == 0)
duke@1 1076 key = "cant.override";
duke@1 1077 else if ((m.owner.flags() & INTERFACE) == 0)
duke@1 1078 key = "cant.implement";
duke@1 1079 else
duke@1 1080 key = "clashes.with";
mcimadamore@89 1081 return diags.fragment(key, m, m.location(), other, other.location());
duke@1 1082 }
duke@1 1083
duke@1 1084 /** A customized "override" warning message.
duke@1 1085 * @param m The overriding method.
duke@1 1086 * @param other The overridden method.
duke@1 1087 * @return An internationalized string.
duke@1 1088 */
mcimadamore@89 1089 Object uncheckedOverrides(MethodSymbol m, MethodSymbol other) {
duke@1 1090 String key;
duke@1 1091 if ((other.owner.flags() & INTERFACE) == 0)
duke@1 1092 key = "unchecked.override";
duke@1 1093 else if ((m.owner.flags() & INTERFACE) == 0)
duke@1 1094 key = "unchecked.implement";
duke@1 1095 else
duke@1 1096 key = "unchecked.clash.with";
mcimadamore@89 1097 return diags.fragment(key, m, m.location(), other, other.location());
duke@1 1098 }
duke@1 1099
duke@1 1100 /** A customized "override" warning message.
duke@1 1101 * @param m The overriding method.
duke@1 1102 * @param other The overridden method.
duke@1 1103 * @return An internationalized string.
duke@1 1104 */
mcimadamore@89 1105 Object varargsOverrides(MethodSymbol m, MethodSymbol other) {
duke@1 1106 String key;
duke@1 1107 if ((other.owner.flags() & INTERFACE) == 0)
duke@1 1108 key = "varargs.override";
duke@1 1109 else if ((m.owner.flags() & INTERFACE) == 0)
duke@1 1110 key = "varargs.implement";
duke@1 1111 else
duke@1 1112 key = "varargs.clash.with";
mcimadamore@89 1113 return diags.fragment(key, m, m.location(), other, other.location());
duke@1 1114 }
duke@1 1115
duke@1 1116 /** Check that this method conforms with overridden method 'other'.
duke@1 1117 * where `origin' is the class where checking started.
duke@1 1118 * Complications:
duke@1 1119 * (1) Do not check overriding of synthetic methods
duke@1 1120 * (reason: they might be final).
duke@1 1121 * todo: check whether this is still necessary.
duke@1 1122 * (2) Admit the case where an interface proxy throws fewer exceptions
duke@1 1123 * than the method it implements. Augment the proxy methods with the
duke@1 1124 * undeclared exceptions in this case.
duke@1 1125 * (3) When generics are enabled, admit the case where an interface proxy
duke@1 1126 * has a result type
duke@1 1127 * extended by the result type of the method it implements.
duke@1 1128 * Change the proxies result type to the smaller type in this case.
duke@1 1129 *
duke@1 1130 * @param tree The tree from which positions
duke@1 1131 * are extracted for errors.
duke@1 1132 * @param m The overriding method.
duke@1 1133 * @param other The overridden method.
duke@1 1134 * @param origin The class of which the overriding method
duke@1 1135 * is a member.
duke@1 1136 */
duke@1 1137 void checkOverride(JCTree tree,
duke@1 1138 MethodSymbol m,
duke@1 1139 MethodSymbol other,
duke@1 1140 ClassSymbol origin) {
duke@1 1141 // Don't check overriding of synthetic methods or by bridge methods.
duke@1 1142 if ((m.flags() & (SYNTHETIC|BRIDGE)) != 0 || (other.flags() & SYNTHETIC) != 0) {
duke@1 1143 return;
duke@1 1144 }
duke@1 1145
duke@1 1146 // Error if static method overrides instance method (JLS 8.4.6.2).
duke@1 1147 if ((m.flags() & STATIC) != 0 &&
duke@1 1148 (other.flags() & STATIC) == 0) {
duke@1 1149 log.error(TreeInfo.diagnosticPositionFor(m, tree), "override.static",
duke@1 1150 cannotOverride(m, other));
duke@1 1151 return;
duke@1 1152 }
duke@1 1153
duke@1 1154 // Error if instance method overrides static or final
duke@1 1155 // method (JLS 8.4.6.1).
duke@1 1156 if ((other.flags() & FINAL) != 0 ||
duke@1 1157 (m.flags() & STATIC) == 0 &&
duke@1 1158 (other.flags() & STATIC) != 0) {
duke@1 1159 log.error(TreeInfo.diagnosticPositionFor(m, tree), "override.meth",
duke@1 1160 cannotOverride(m, other),
mcimadamore@80 1161 asFlagSet(other.flags() & (FINAL | STATIC)));
duke@1 1162 return;
duke@1 1163 }
duke@1 1164
duke@1 1165 if ((m.owner.flags() & ANNOTATION) != 0) {
duke@1 1166 // handled in validateAnnotationMethod
duke@1 1167 return;
duke@1 1168 }
duke@1 1169
duke@1 1170 // Error if overriding method has weaker access (JLS 8.4.6.3).
duke@1 1171 if ((origin.flags() & INTERFACE) == 0 &&
duke@1 1172 protection(m.flags()) > protection(other.flags())) {
duke@1 1173 log.error(TreeInfo.diagnosticPositionFor(m, tree), "override.weaker.access",
duke@1 1174 cannotOverride(m, other),
mcimadamore@80 1175 other.flags() == 0 ?
mcimadamore@80 1176 Flag.PACKAGE :
mcimadamore@80 1177 asFlagSet(other.flags() & AccessFlags));
duke@1 1178 return;
duke@1 1179 }
duke@1 1180
duke@1 1181 Type mt = types.memberType(origin.type, m);
duke@1 1182 Type ot = types.memberType(origin.type, other);
duke@1 1183 // Error if overriding result type is different
duke@1 1184 // (or, in the case of generics mode, not a subtype) of
duke@1 1185 // overridden result type. We have to rename any type parameters
duke@1 1186 // before comparing types.
duke@1 1187 List<Type> mtvars = mt.getTypeArguments();
duke@1 1188 List<Type> otvars = ot.getTypeArguments();
duke@1 1189 Type mtres = mt.getReturnType();
duke@1 1190 Type otres = types.subst(ot.getReturnType(), otvars, mtvars);
duke@1 1191
duke@1 1192 overrideWarner.warned = false;
duke@1 1193 boolean resultTypesOK =
tbell@202 1194 types.returnTypeSubstitutable(mt, ot, otres, overrideWarner);
duke@1 1195 if (!resultTypesOK) {
duke@1 1196 if (!source.allowCovariantReturns() &&
duke@1 1197 m.owner != origin &&
duke@1 1198 m.owner.isSubClass(other.owner, types)) {
duke@1 1199 // allow limited interoperability with covariant returns
duke@1 1200 } else {
duke@1 1201 typeError(TreeInfo.diagnosticPositionFor(m, tree),
mcimadamore@89 1202 diags.fragment("override.incompatible.ret",
duke@1 1203 cannotOverride(m, other)),
duke@1 1204 mtres, otres);
duke@1 1205 return;
duke@1 1206 }
duke@1 1207 } else if (overrideWarner.warned) {
duke@1 1208 warnUnchecked(TreeInfo.diagnosticPositionFor(m, tree),
duke@1 1209 "prob.found.req",
mcimadamore@89 1210 diags.fragment("override.unchecked.ret",
duke@1 1211 uncheckedOverrides(m, other)),
duke@1 1212 mtres, otres);
duke@1 1213 }
duke@1 1214
duke@1 1215 // Error if overriding method throws an exception not reported
duke@1 1216 // by overridden method.
duke@1 1217 List<Type> otthrown = types.subst(ot.getThrownTypes(), otvars, mtvars);
duke@1 1218 List<Type> unhandled = unHandled(mt.getThrownTypes(), otthrown);
duke@1 1219 if (unhandled.nonEmpty()) {
duke@1 1220 log.error(TreeInfo.diagnosticPositionFor(m, tree),
duke@1 1221 "override.meth.doesnt.throw",
duke@1 1222 cannotOverride(m, other),
duke@1 1223 unhandled.head);
duke@1 1224 return;
duke@1 1225 }
duke@1 1226
duke@1 1227 // Optional warning if varargs don't agree
duke@1 1228 if ((((m.flags() ^ other.flags()) & Flags.VARARGS) != 0)
duke@1 1229 && lint.isEnabled(Lint.LintCategory.OVERRIDES)) {
duke@1 1230 log.warning(TreeInfo.diagnosticPositionFor(m, tree),
duke@1 1231 ((m.flags() & Flags.VARARGS) != 0)
duke@1 1232 ? "override.varargs.missing"
duke@1 1233 : "override.varargs.extra",
duke@1 1234 varargsOverrides(m, other));
duke@1 1235 }
duke@1 1236
duke@1 1237 // Warn if instance method overrides bridge method (compiler spec ??)
duke@1 1238 if ((other.flags() & BRIDGE) != 0) {
duke@1 1239 log.warning(TreeInfo.diagnosticPositionFor(m, tree), "override.bridge",
duke@1 1240 uncheckedOverrides(m, other));
duke@1 1241 }
duke@1 1242
duke@1 1243 // Warn if a deprecated method overridden by a non-deprecated one.
duke@1 1244 if ((other.flags() & DEPRECATED) != 0
duke@1 1245 && (m.flags() & DEPRECATED) == 0
duke@1 1246 && m.outermostClass() != other.outermostClass()
duke@1 1247 && !isDeprecatedOverrideIgnorable(other, origin)) {
duke@1 1248 warnDeprecated(TreeInfo.diagnosticPositionFor(m, tree), other);
duke@1 1249 }
duke@1 1250 }
duke@1 1251 // where
duke@1 1252 private boolean isDeprecatedOverrideIgnorable(MethodSymbol m, ClassSymbol origin) {
duke@1 1253 // If the method, m, is defined in an interface, then ignore the issue if the method
duke@1 1254 // is only inherited via a supertype and also implemented in the supertype,
duke@1 1255 // because in that case, we will rediscover the issue when examining the method
duke@1 1256 // in the supertype.
duke@1 1257 // If the method, m, is not defined in an interface, then the only time we need to
duke@1 1258 // address the issue is when the method is the supertype implemementation: any other
duke@1 1259 // case, we will have dealt with when examining the supertype classes
duke@1 1260 ClassSymbol mc = m.enclClass();
duke@1 1261 Type st = types.supertype(origin.type);
duke@1 1262 if (st.tag != CLASS)
duke@1 1263 return true;
duke@1 1264 MethodSymbol stimpl = m.implementation((ClassSymbol)st.tsym, types, false);
duke@1 1265
duke@1 1266 if (mc != null && ((mc.flags() & INTERFACE) != 0)) {
duke@1 1267 List<Type> intfs = types.interfaces(origin.type);
duke@1 1268 return (intfs.contains(mc.type) ? false : (stimpl != null));
duke@1 1269 }
duke@1 1270 else
duke@1 1271 return (stimpl != m);
duke@1 1272 }
duke@1 1273
duke@1 1274
duke@1 1275 // used to check if there were any unchecked conversions
duke@1 1276 Warner overrideWarner = new Warner();
duke@1 1277
duke@1 1278 /** Check that a class does not inherit two concrete methods
duke@1 1279 * with the same signature.
duke@1 1280 * @param pos Position to be used for error reporting.
duke@1 1281 * @param site The class type to be checked.
duke@1 1282 */
duke@1 1283 public void checkCompatibleConcretes(DiagnosticPosition pos, Type site) {
duke@1 1284 Type sup = types.supertype(site);
duke@1 1285 if (sup.tag != CLASS) return;
duke@1 1286
duke@1 1287 for (Type t1 = sup;
duke@1 1288 t1.tsym.type.isParameterized();
duke@1 1289 t1 = types.supertype(t1)) {
duke@1 1290 for (Scope.Entry e1 = t1.tsym.members().elems;
duke@1 1291 e1 != null;
duke@1 1292 e1 = e1.sibling) {
duke@1 1293 Symbol s1 = e1.sym;
duke@1 1294 if (s1.kind != MTH ||
duke@1 1295 (s1.flags() & (STATIC|SYNTHETIC|BRIDGE)) != 0 ||
duke@1 1296 !s1.isInheritedIn(site.tsym, types) ||
duke@1 1297 ((MethodSymbol)s1).implementation(site.tsym,
duke@1 1298 types,
duke@1 1299 true) != s1)
duke@1 1300 continue;
duke@1 1301 Type st1 = types.memberType(t1, s1);
duke@1 1302 int s1ArgsLength = st1.getParameterTypes().length();
duke@1 1303 if (st1 == s1.type) continue;
duke@1 1304
duke@1 1305 for (Type t2 = sup;
duke@1 1306 t2.tag == CLASS;
duke@1 1307 t2 = types.supertype(t2)) {
mcimadamore@24 1308 for (Scope.Entry e2 = t2.tsym.members().lookup(s1.name);
duke@1 1309 e2.scope != null;
duke@1 1310 e2 = e2.next()) {
duke@1 1311 Symbol s2 = e2.sym;
duke@1 1312 if (s2 == s1 ||
duke@1 1313 s2.kind != MTH ||
duke@1 1314 (s2.flags() & (STATIC|SYNTHETIC|BRIDGE)) != 0 ||
duke@1 1315 s2.type.getParameterTypes().length() != s1ArgsLength ||
duke@1 1316 !s2.isInheritedIn(site.tsym, types) ||
duke@1 1317 ((MethodSymbol)s2).implementation(site.tsym,
duke@1 1318 types,
duke@1 1319 true) != s2)
duke@1 1320 continue;
duke@1 1321 Type st2 = types.memberType(t2, s2);
duke@1 1322 if (types.overrideEquivalent(st1, st2))
duke@1 1323 log.error(pos, "concrete.inheritance.conflict",
duke@1 1324 s1, t1, s2, t2, sup);
duke@1 1325 }
duke@1 1326 }
duke@1 1327 }
duke@1 1328 }
duke@1 1329 }
duke@1 1330
duke@1 1331 /** Check that classes (or interfaces) do not each define an abstract
duke@1 1332 * method with same name and arguments but incompatible return types.
duke@1 1333 * @param pos Position to be used for error reporting.
duke@1 1334 * @param t1 The first argument type.
duke@1 1335 * @param t2 The second argument type.
duke@1 1336 */
duke@1 1337 public boolean checkCompatibleAbstracts(DiagnosticPosition pos,
duke@1 1338 Type t1,
duke@1 1339 Type t2) {
duke@1 1340 return checkCompatibleAbstracts(pos, t1, t2,
duke@1 1341 types.makeCompoundType(t1, t2));
duke@1 1342 }
duke@1 1343
duke@1 1344 public boolean checkCompatibleAbstracts(DiagnosticPosition pos,
duke@1 1345 Type t1,
duke@1 1346 Type t2,
duke@1 1347 Type site) {
duke@1 1348 Symbol sym = firstIncompatibility(t1, t2, site);
duke@1 1349 if (sym != null) {
duke@1 1350 log.error(pos, "types.incompatible.diff.ret",
duke@1 1351 t1, t2, sym.name +
duke@1 1352 "(" + types.memberType(t2, sym).getParameterTypes() + ")");
duke@1 1353 return false;
duke@1 1354 }
duke@1 1355 return true;
duke@1 1356 }
duke@1 1357
duke@1 1358 /** Return the first method which is defined with same args
duke@1 1359 * but different return types in two given interfaces, or null if none
duke@1 1360 * exists.
duke@1 1361 * @param t1 The first type.
duke@1 1362 * @param t2 The second type.
duke@1 1363 * @param site The most derived type.
duke@1 1364 * @returns symbol from t2 that conflicts with one in t1.
duke@1 1365 */
duke@1 1366 private Symbol firstIncompatibility(Type t1, Type t2, Type site) {
duke@1 1367 Map<TypeSymbol,Type> interfaces1 = new HashMap<TypeSymbol,Type>();
duke@1 1368 closure(t1, interfaces1);
duke@1 1369 Map<TypeSymbol,Type> interfaces2;
duke@1 1370 if (t1 == t2)
duke@1 1371 interfaces2 = interfaces1;
duke@1 1372 else
duke@1 1373 closure(t2, interfaces1, interfaces2 = new HashMap<TypeSymbol,Type>());
duke@1 1374
duke@1 1375 for (Type t3 : interfaces1.values()) {
duke@1 1376 for (Type t4 : interfaces2.values()) {
duke@1 1377 Symbol s = firstDirectIncompatibility(t3, t4, site);
duke@1 1378 if (s != null) return s;
duke@1 1379 }
duke@1 1380 }
duke@1 1381 return null;
duke@1 1382 }
duke@1 1383
duke@1 1384 /** Compute all the supertypes of t, indexed by type symbol. */
duke@1 1385 private void closure(Type t, Map<TypeSymbol,Type> typeMap) {
duke@1 1386 if (t.tag != CLASS) return;
duke@1 1387 if (typeMap.put(t.tsym, t) == null) {
duke@1 1388 closure(types.supertype(t), typeMap);
duke@1 1389 for (Type i : types.interfaces(t))
duke@1 1390 closure(i, typeMap);
duke@1 1391 }
duke@1 1392 }
duke@1 1393
duke@1 1394 /** Compute all the supertypes of t, indexed by type symbol (except thise in typesSkip). */
duke@1 1395 private void closure(Type t, Map<TypeSymbol,Type> typesSkip, Map<TypeSymbol,Type> typeMap) {
duke@1 1396 if (t.tag != CLASS) return;
duke@1 1397 if (typesSkip.get(t.tsym) != null) return;
duke@1 1398 if (typeMap.put(t.tsym, t) == null) {
duke@1 1399 closure(types.supertype(t), typesSkip, typeMap);
duke@1 1400 for (Type i : types.interfaces(t))
duke@1 1401 closure(i, typesSkip, typeMap);
duke@1 1402 }
duke@1 1403 }
duke@1 1404
duke@1 1405 /** Return the first method in t2 that conflicts with a method from t1. */
duke@1 1406 private Symbol firstDirectIncompatibility(Type t1, Type t2, Type site) {
duke@1 1407 for (Scope.Entry e1 = t1.tsym.members().elems; e1 != null; e1 = e1.sibling) {
duke@1 1408 Symbol s1 = e1.sym;
duke@1 1409 Type st1 = null;
duke@1 1410 if (s1.kind != MTH || !s1.isInheritedIn(site.tsym, types)) continue;
duke@1 1411 Symbol impl = ((MethodSymbol)s1).implementation(site.tsym, types, false);
duke@1 1412 if (impl != null && (impl.flags() & ABSTRACT) == 0) continue;
duke@1 1413 for (Scope.Entry e2 = t2.tsym.members().lookup(s1.name); e2.scope != null; e2 = e2.next()) {
duke@1 1414 Symbol s2 = e2.sym;
duke@1 1415 if (s1 == s2) continue;
duke@1 1416 if (s2.kind != MTH || !s2.isInheritedIn(site.tsym, types)) continue;
duke@1 1417 if (st1 == null) st1 = types.memberType(t1, s1);
duke@1 1418 Type st2 = types.memberType(t2, s2);
duke@1 1419 if (types.overrideEquivalent(st1, st2)) {
duke@1 1420 List<Type> tvars1 = st1.getTypeArguments();
duke@1 1421 List<Type> tvars2 = st2.getTypeArguments();
duke@1 1422 Type rt1 = st1.getReturnType();
duke@1 1423 Type rt2 = types.subst(st2.getReturnType(), tvars2, tvars1);
duke@1 1424 boolean compat =
duke@1 1425 types.isSameType(rt1, rt2) ||
duke@1 1426 rt1.tag >= CLASS && rt2.tag >= CLASS &&
duke@1 1427 (types.covariantReturnType(rt1, rt2, Warner.noWarnings) ||
mcimadamore@59 1428 types.covariantReturnType(rt2, rt1, Warner.noWarnings)) ||
mcimadamore@59 1429 checkCommonOverriderIn(s1,s2,site);
duke@1 1430 if (!compat) return s2;
duke@1 1431 }
duke@1 1432 }
duke@1 1433 }
duke@1 1434 return null;
duke@1 1435 }
mcimadamore@59 1436 //WHERE
mcimadamore@59 1437 boolean checkCommonOverriderIn(Symbol s1, Symbol s2, Type site) {
mcimadamore@59 1438 Map<TypeSymbol,Type> supertypes = new HashMap<TypeSymbol,Type>();
mcimadamore@59 1439 Type st1 = types.memberType(site, s1);
mcimadamore@59 1440 Type st2 = types.memberType(site, s2);
mcimadamore@59 1441 closure(site, supertypes);
mcimadamore@59 1442 for (Type t : supertypes.values()) {
mcimadamore@59 1443 for (Scope.Entry e = t.tsym.members().lookup(s1.name); e.scope != null; e = e.next()) {
mcimadamore@59 1444 Symbol s3 = e.sym;
mcimadamore@59 1445 if (s3 == s1 || s3 == s2 || s3.kind != MTH || (s3.flags() & (BRIDGE|SYNTHETIC)) != 0) continue;
mcimadamore@59 1446 Type st3 = types.memberType(site,s3);
mcimadamore@59 1447 if (types.overrideEquivalent(st3, st1) && types.overrideEquivalent(st3, st2)) {
mcimadamore@59 1448 if (s3.owner == site.tsym) {
mcimadamore@59 1449 return true;
mcimadamore@59 1450 }
mcimadamore@59 1451 List<Type> tvars1 = st1.getTypeArguments();
mcimadamore@59 1452 List<Type> tvars2 = st2.getTypeArguments();
mcimadamore@59 1453 List<Type> tvars3 = st3.getTypeArguments();
mcimadamore@59 1454 Type rt1 = st1.getReturnType();
mcimadamore@59 1455 Type rt2 = st2.getReturnType();
mcimadamore@59 1456 Type rt13 = types.subst(st3.getReturnType(), tvars3, tvars1);
mcimadamore@59 1457 Type rt23 = types.subst(st3.getReturnType(), tvars3, tvars2);
mcimadamore@59 1458 boolean compat =
mcimadamore@59 1459 rt13.tag >= CLASS && rt23.tag >= CLASS &&
mcimadamore@59 1460 (types.covariantReturnType(rt13, rt1, Warner.noWarnings) &&
mcimadamore@59 1461 types.covariantReturnType(rt23, rt2, Warner.noWarnings));
mcimadamore@59 1462 if (compat)
mcimadamore@59 1463 return true;
mcimadamore@59 1464 }
mcimadamore@59 1465 }
mcimadamore@59 1466 }
mcimadamore@59 1467 return false;
mcimadamore@59 1468 }
duke@1 1469
duke@1 1470 /** Check that a given method conforms with any method it overrides.
duke@1 1471 * @param tree The tree from which positions are extracted
duke@1 1472 * for errors.
duke@1 1473 * @param m The overriding method.
duke@1 1474 */
duke@1 1475 void checkOverride(JCTree tree, MethodSymbol m) {
duke@1 1476 ClassSymbol origin = (ClassSymbol)m.owner;
duke@1 1477 if ((origin.flags() & ENUM) != 0 && names.finalize.equals(m.name))
duke@1 1478 if (m.overrides(syms.enumFinalFinalize, origin, types, false)) {
duke@1 1479 log.error(tree.pos(), "enum.no.finalize");
duke@1 1480 return;
duke@1 1481 }
duke@1 1482 for (Type t = types.supertype(origin.type); t.tag == CLASS;
duke@1 1483 t = types.supertype(t)) {
duke@1 1484 TypeSymbol c = t.tsym;
duke@1 1485 Scope.Entry e = c.members().lookup(m.name);
duke@1 1486 while (e.scope != null) {
duke@1 1487 if (m.overrides(e.sym, origin, types, false))
duke@1 1488 checkOverride(tree, m, (MethodSymbol)e.sym, origin);
mcimadamore@252 1489 else if (e.sym.kind == MTH &&
mcimadamore@252 1490 e.sym.isInheritedIn(origin, types) &&
mcimadamore@252 1491 (e.sym.flags() & SYNTHETIC) == 0 &&
mcimadamore@252 1492 !m.isConstructor()) {
mcimadamore@24 1493 Type er1 = m.erasure(types);
mcimadamore@24 1494 Type er2 = e.sym.erasure(types);
mcimadamore@252 1495 if (types.isSameTypes(er1.getParameterTypes(),
mcimadamore@252 1496 er2.getParameterTypes())) {
mcimadamore@24 1497 log.error(TreeInfo.diagnosticPositionFor(m, tree),
mcimadamore@24 1498 "name.clash.same.erasure.no.override",
mcimadamore@24 1499 m, m.location(),
mcimadamore@24 1500 e.sym, e.sym.location());
mcimadamore@24 1501 }
mcimadamore@24 1502 }
duke@1 1503 e = e.next();
duke@1 1504 }
duke@1 1505 }
duke@1 1506 }
duke@1 1507
duke@1 1508 /** Check that all abstract members of given class have definitions.
duke@1 1509 * @param pos Position to be used for error reporting.
duke@1 1510 * @param c The class.
duke@1 1511 */
duke@1 1512 void checkAllDefined(DiagnosticPosition pos, ClassSymbol c) {
duke@1 1513 try {
duke@1 1514 MethodSymbol undef = firstUndef(c, c);
duke@1 1515 if (undef != null) {
duke@1 1516 if ((c.flags() & ENUM) != 0 &&
duke@1 1517 types.supertype(c.type).tsym == syms.enumSym &&
duke@1 1518 (c.flags() & FINAL) == 0) {
duke@1 1519 // add the ABSTRACT flag to an enum
duke@1 1520 c.flags_field |= ABSTRACT;
duke@1 1521 } else {
duke@1 1522 MethodSymbol undef1 =
duke@1 1523 new MethodSymbol(undef.flags(), undef.name,
duke@1 1524 types.memberType(c.type, undef), undef.owner);
duke@1 1525 log.error(pos, "does.not.override.abstract",
duke@1 1526 c, undef1, undef1.location());
duke@1 1527 }
duke@1 1528 }
duke@1 1529 } catch (CompletionFailure ex) {
duke@1 1530 completionError(pos, ex);
duke@1 1531 }
duke@1 1532 }
duke@1 1533 //where
duke@1 1534 /** Return first abstract member of class `c' that is not defined
duke@1 1535 * in `impl', null if there is none.
duke@1 1536 */
duke@1 1537 private MethodSymbol firstUndef(ClassSymbol impl, ClassSymbol c) {
duke@1 1538 MethodSymbol undef = null;
duke@1 1539 // Do not bother to search in classes that are not abstract,
duke@1 1540 // since they cannot have abstract members.
duke@1 1541 if (c == impl || (c.flags() & (ABSTRACT | INTERFACE)) != 0) {
duke@1 1542 Scope s = c.members();
duke@1 1543 for (Scope.Entry e = s.elems;
duke@1 1544 undef == null && e != null;
duke@1 1545 e = e.sibling) {
duke@1 1546 if (e.sym.kind == MTH &&
duke@1 1547 (e.sym.flags() & (ABSTRACT|IPROXY)) == ABSTRACT) {
duke@1 1548 MethodSymbol absmeth = (MethodSymbol)e.sym;
duke@1 1549 MethodSymbol implmeth = absmeth.implementation(impl, types, true);
duke@1 1550 if (implmeth == null || implmeth == absmeth)
duke@1 1551 undef = absmeth;
duke@1 1552 }
duke@1 1553 }
duke@1 1554 if (undef == null) {
duke@1 1555 Type st = types.supertype(c.type);
duke@1 1556 if (st.tag == CLASS)
duke@1 1557 undef = firstUndef(impl, (ClassSymbol)st.tsym);
duke@1 1558 }
duke@1 1559 for (List<Type> l = types.interfaces(c.type);
duke@1 1560 undef == null && l.nonEmpty();
duke@1 1561 l = l.tail) {
duke@1 1562 undef = firstUndef(impl, (ClassSymbol)l.head.tsym);
duke@1 1563 }
duke@1 1564 }
duke@1 1565 return undef;
duke@1 1566 }
duke@1 1567
duke@1 1568 /** Check for cyclic references. Issue an error if the
duke@1 1569 * symbol of the type referred to has a LOCKED flag set.
duke@1 1570 *
duke@1 1571 * @param pos Position to be used for error reporting.
duke@1 1572 * @param t The type referred to.
duke@1 1573 */
duke@1 1574 void checkNonCyclic(DiagnosticPosition pos, Type t) {
duke@1 1575 checkNonCyclicInternal(pos, t);
duke@1 1576 }
duke@1 1577
duke@1 1578
duke@1 1579 void checkNonCyclic(DiagnosticPosition pos, TypeVar t) {
mcimadamore@236 1580 checkNonCyclic1(pos, t, List.<TypeVar>nil());
duke@1 1581 }
duke@1 1582
mcimadamore@236 1583 private void checkNonCyclic1(DiagnosticPosition pos, Type t, List<TypeVar> seen) {
duke@1 1584 final TypeVar tv;
mcimadamore@42 1585 if (t.tag == TYPEVAR && (t.tsym.flags() & UNATTRIBUTED) != 0)
mcimadamore@42 1586 return;
duke@1 1587 if (seen.contains(t)) {
duke@1 1588 tv = (TypeVar)t;
jjg@110 1589 tv.bound = types.createErrorType(t);
duke@1 1590 log.error(pos, "cyclic.inheritance", t);
duke@1 1591 } else if (t.tag == TYPEVAR) {
duke@1 1592 tv = (TypeVar)t;
mcimadamore@236 1593 seen = seen.prepend(tv);
duke@1 1594 for (Type b : types.getBounds(tv))
duke@1 1595 checkNonCyclic1(pos, b, seen);
duke@1 1596 }
duke@1 1597 }
duke@1 1598
duke@1 1599 /** Check for cyclic references. Issue an error if the
duke@1 1600 * symbol of the type referred to has a LOCKED flag set.
duke@1 1601 *
duke@1 1602 * @param pos Position to be used for error reporting.
duke@1 1603 * @param t The type referred to.
duke@1 1604 * @returns True if the check completed on all attributed classes
duke@1 1605 */
duke@1 1606 private boolean checkNonCyclicInternal(DiagnosticPosition pos, Type t) {
duke@1 1607 boolean complete = true; // was the check complete?
duke@1 1608 //- System.err.println("checkNonCyclicInternal("+t+");");//DEBUG
duke@1 1609 Symbol c = t.tsym;
duke@1 1610 if ((c.flags_field & ACYCLIC) != 0) return true;
duke@1 1611
duke@1 1612 if ((c.flags_field & LOCKED) != 0) {
duke@1 1613 noteCyclic(pos, (ClassSymbol)c);
duke@1 1614 } else if (!c.type.isErroneous()) {
duke@1 1615 try {
duke@1 1616 c.flags_field |= LOCKED;
duke@1 1617 if (c.type.tag == CLASS) {
duke@1 1618 ClassType clazz = (ClassType)c.type;
duke@1 1619 if (clazz.interfaces_field != null)
duke@1 1620 for (List<Type> l=clazz.interfaces_field; l.nonEmpty(); l=l.tail)
duke@1 1621 complete &= checkNonCyclicInternal(pos, l.head);
duke@1 1622 if (clazz.supertype_field != null) {
duke@1 1623 Type st = clazz.supertype_field;
duke@1 1624 if (st != null && st.tag == CLASS)
duke@1 1625 complete &= checkNonCyclicInternal(pos, st);
duke@1 1626 }
duke@1 1627 if (c.owner.kind == TYP)
duke@1 1628 complete &= checkNonCyclicInternal(pos, c.owner.type);
duke@1 1629 }
duke@1 1630 } finally {
duke@1 1631 c.flags_field &= ~LOCKED;
duke@1 1632 }
duke@1 1633 }
duke@1 1634 if (complete)
duke@1 1635 complete = ((c.flags_field & UNATTRIBUTED) == 0) && c.completer == null;
duke@1 1636 if (complete) c.flags_field |= ACYCLIC;
duke@1 1637 return complete;
duke@1 1638 }
duke@1 1639
duke@1 1640 /** Note that we found an inheritance cycle. */
duke@1 1641 private void noteCyclic(DiagnosticPosition pos, ClassSymbol c) {
duke@1 1642 log.error(pos, "cyclic.inheritance", c);
duke@1 1643 for (List<Type> l=types.interfaces(c.type); l.nonEmpty(); l=l.tail)
jjg@110 1644 l.head = types.createErrorType((ClassSymbol)l.head.tsym, Type.noType);
duke@1 1645 Type st = types.supertype(c.type);
duke@1 1646 if (st.tag == CLASS)
jjg@110 1647 ((ClassType)c.type).supertype_field = types.createErrorType((ClassSymbol)st.tsym, Type.noType);
jjg@110 1648 c.type = types.createErrorType(c, c.type);
duke@1 1649 c.flags_field |= ACYCLIC;
duke@1 1650 }
duke@1 1651
duke@1 1652 /** Check that all methods which implement some
duke@1 1653 * method conform to the method they implement.
duke@1 1654 * @param tree The class definition whose members are checked.
duke@1 1655 */
duke@1 1656 void checkImplementations(JCClassDecl tree) {
duke@1 1657 checkImplementations(tree, tree.sym);
duke@1 1658 }
duke@1 1659 //where
duke@1 1660 /** Check that all methods which implement some
duke@1 1661 * method in `ic' conform to the method they implement.
duke@1 1662 */
duke@1 1663 void checkImplementations(JCClassDecl tree, ClassSymbol ic) {
duke@1 1664 ClassSymbol origin = tree.sym;
duke@1 1665 for (List<Type> l = types.closure(ic.type); l.nonEmpty(); l = l.tail) {
duke@1 1666 ClassSymbol lc = (ClassSymbol)l.head.tsym;
duke@1 1667 if ((allowGenerics || origin != lc) && (lc.flags() & ABSTRACT) != 0) {
duke@1 1668 for (Scope.Entry e=lc.members().elems; e != null; e=e.sibling) {
duke@1 1669 if (e.sym.kind == MTH &&
duke@1 1670 (e.sym.flags() & (STATIC|ABSTRACT)) == ABSTRACT) {
duke@1 1671 MethodSymbol absmeth = (MethodSymbol)e.sym;
duke@1 1672 MethodSymbol implmeth = absmeth.implementation(origin, types, false);
duke@1 1673 if (implmeth != null && implmeth != absmeth &&
duke@1 1674 (implmeth.owner.flags() & INTERFACE) ==
duke@1 1675 (origin.flags() & INTERFACE)) {
duke@1 1676 // don't check if implmeth is in a class, yet
duke@1 1677 // origin is an interface. This case arises only
duke@1 1678 // if implmeth is declared in Object. The reason is
duke@1 1679 // that interfaces really don't inherit from
duke@1 1680 // Object it's just that the compiler represents
duke@1 1681 // things that way.
duke@1 1682 checkOverride(tree, implmeth, absmeth, origin);
duke@1 1683 }
duke@1 1684 }
duke@1 1685 }
duke@1 1686 }
duke@1 1687 }
duke@1 1688 }
duke@1 1689
duke@1 1690 /** Check that all abstract methods implemented by a class are
duke@1 1691 * mutually compatible.
duke@1 1692 * @param pos Position to be used for error reporting.
duke@1 1693 * @param c The class whose interfaces are checked.
duke@1 1694 */
duke@1 1695 void checkCompatibleSupertypes(DiagnosticPosition pos, Type c) {
duke@1 1696 List<Type> supertypes = types.interfaces(c);
duke@1 1697 Type supertype = types.supertype(c);
duke@1 1698 if (supertype.tag == CLASS &&
duke@1 1699 (supertype.tsym.flags() & ABSTRACT) != 0)
duke@1 1700 supertypes = supertypes.prepend(supertype);
duke@1 1701 for (List<Type> l = supertypes; l.nonEmpty(); l = l.tail) {
duke@1 1702 if (allowGenerics && !l.head.getTypeArguments().isEmpty() &&
duke@1 1703 !checkCompatibleAbstracts(pos, l.head, l.head, c))
duke@1 1704 return;
duke@1 1705 for (List<Type> m = supertypes; m != l; m = m.tail)
duke@1 1706 if (!checkCompatibleAbstracts(pos, l.head, m.head, c))
duke@1 1707 return;
duke@1 1708 }
duke@1 1709 checkCompatibleConcretes(pos, c);
duke@1 1710 }
duke@1 1711
duke@1 1712 /** Check that class c does not implement directly or indirectly
duke@1 1713 * the same parameterized interface with two different argument lists.
duke@1 1714 * @param pos Position to be used for error reporting.
duke@1 1715 * @param type The type whose interfaces are checked.
duke@1 1716 */
duke@1 1717 void checkClassBounds(DiagnosticPosition pos, Type type) {
duke@1 1718 checkClassBounds(pos, new HashMap<TypeSymbol,Type>(), type);
duke@1 1719 }
duke@1 1720 //where
duke@1 1721 /** Enter all interfaces of type `type' into the hash table `seensofar'
duke@1 1722 * with their class symbol as key and their type as value. Make
duke@1 1723 * sure no class is entered with two different types.
duke@1 1724 */
duke@1 1725 void checkClassBounds(DiagnosticPosition pos,
duke@1 1726 Map<TypeSymbol,Type> seensofar,
duke@1 1727 Type type) {
duke@1 1728 if (type.isErroneous()) return;
duke@1 1729 for (List<Type> l = types.interfaces(type); l.nonEmpty(); l = l.tail) {
duke@1 1730 Type it = l.head;
duke@1 1731 Type oldit = seensofar.put(it.tsym, it);
duke@1 1732 if (oldit != null) {
duke@1 1733 List<Type> oldparams = oldit.allparams();
duke@1 1734 List<Type> newparams = it.allparams();
duke@1 1735 if (!types.containsTypeEquivalent(oldparams, newparams))
duke@1 1736 log.error(pos, "cant.inherit.diff.arg",
duke@1 1737 it.tsym, Type.toString(oldparams),
duke@1 1738 Type.toString(newparams));
duke@1 1739 }
duke@1 1740 checkClassBounds(pos, seensofar, it);
duke@1 1741 }
duke@1 1742 Type st = types.supertype(type);
duke@1 1743 if (st != null) checkClassBounds(pos, seensofar, st);
duke@1 1744 }
duke@1 1745
duke@1 1746 /** Enter interface into into set.
duke@1 1747 * If it existed already, issue a "repeated interface" error.
duke@1 1748 */
duke@1 1749 void checkNotRepeated(DiagnosticPosition pos, Type it, Set<Type> its) {
duke@1 1750 if (its.contains(it))
duke@1 1751 log.error(pos, "repeated.interface");
duke@1 1752 else {
duke@1 1753 its.add(it);
duke@1 1754 }
duke@1 1755 }
duke@1 1756
duke@1 1757 /* *************************************************************************
duke@1 1758 * Check annotations
duke@1 1759 **************************************************************************/
duke@1 1760
duke@1 1761 /** Annotation types are restricted to primitives, String, an
duke@1 1762 * enum, an annotation, Class, Class<?>, Class<? extends
duke@1 1763 * Anything>, arrays of the preceding.
duke@1 1764 */
duke@1 1765 void validateAnnotationType(JCTree restype) {
duke@1 1766 // restype may be null if an error occurred, so don't bother validating it
duke@1 1767 if (restype != null) {
duke@1 1768 validateAnnotationType(restype.pos(), restype.type);
duke@1 1769 }
duke@1 1770 }
duke@1 1771
duke@1 1772 void validateAnnotationType(DiagnosticPosition pos, Type type) {
duke@1 1773 if (type.isPrimitive()) return;
duke@1 1774 if (types.isSameType(type, syms.stringType)) return;
duke@1 1775 if ((type.tsym.flags() & Flags.ENUM) != 0) return;
duke@1 1776 if ((type.tsym.flags() & Flags.ANNOTATION) != 0) return;
duke@1 1777 if (types.lowerBound(type).tsym == syms.classType.tsym) return;
duke@1 1778 if (types.isArray(type) && !types.isArray(types.elemtype(type))) {
duke@1 1779 validateAnnotationType(pos, types.elemtype(type));
duke@1 1780 return;
duke@1 1781 }
duke@1 1782 log.error(pos, "invalid.annotation.member.type");
duke@1 1783 }
duke@1 1784
duke@1 1785 /**
duke@1 1786 * "It is also a compile-time error if any method declared in an
duke@1 1787 * annotation type has a signature that is override-equivalent to
duke@1 1788 * that of any public or protected method declared in class Object
duke@1 1789 * or in the interface annotation.Annotation."
duke@1 1790 *
duke@1 1791 * @jls3 9.6 Annotation Types
duke@1 1792 */
duke@1 1793 void validateAnnotationMethod(DiagnosticPosition pos, MethodSymbol m) {
duke@1 1794 for (Type sup = syms.annotationType; sup.tag == CLASS; sup = types.supertype(sup)) {
duke@1 1795 Scope s = sup.tsym.members();
duke@1 1796 for (Scope.Entry e = s.lookup(m.name); e.scope != null; e = e.next()) {
duke@1 1797 if (e.sym.kind == MTH &&
duke@1 1798 (e.sym.flags() & (PUBLIC | PROTECTED)) != 0 &&
duke@1 1799 types.overrideEquivalent(m.type, e.sym.type))
duke@1 1800 log.error(pos, "intf.annotation.member.clash", e.sym, sup);
duke@1 1801 }
duke@1 1802 }
duke@1 1803 }
duke@1 1804
duke@1 1805 /** Check the annotations of a symbol.
duke@1 1806 */
duke@1 1807 public void validateAnnotations(List<JCAnnotation> annotations, Symbol s) {
duke@1 1808 if (skipAnnotations) return;
duke@1 1809 for (JCAnnotation a : annotations)
duke@1 1810 validateAnnotation(a, s);
duke@1 1811 }
duke@1 1812
jjg@308 1813 /** Check the type annotations
jjg@308 1814 */
jjg@308 1815 public void validateTypeAnnotations(List<JCTypeAnnotation> annotations, boolean isTypeParameter) {
jjg@308 1816 if (skipAnnotations) return;
jjg@308 1817 for (JCTypeAnnotation a : annotations)
jjg@308 1818 validateTypeAnnotation(a, isTypeParameter);
jjg@308 1819 }
jjg@308 1820
duke@1 1821 /** Check an annotation of a symbol.
duke@1 1822 */
duke@1 1823 public void validateAnnotation(JCAnnotation a, Symbol s) {
duke@1 1824 validateAnnotation(a);
duke@1 1825
duke@1 1826 if (!annotationApplicable(a, s))
duke@1 1827 log.error(a.pos(), "annotation.type.not.applicable");
duke@1 1828
duke@1 1829 if (a.annotationType.type.tsym == syms.overrideType.tsym) {
duke@1 1830 if (!isOverrider(s))
duke@1 1831 log.error(a.pos(), "method.does.not.override.superclass");
duke@1 1832 }
duke@1 1833 }
duke@1 1834
jjg@308 1835 public void validateTypeAnnotation(JCTypeAnnotation a, boolean isTypeParameter) {
jjg@308 1836 if (a.type == null)
jjg@308 1837 throw new AssertionError("annotation tree hasn't been attributed yet: " + a);
jjg@308 1838 validateAnnotation(a);
jjg@308 1839
jjg@308 1840 if (!isTypeAnnotation(a, isTypeParameter))
jjg@308 1841 log.error(a.pos(), "annotation.type.not.applicable");
jjg@308 1842 }
jjg@308 1843
duke@1 1844 /** Is s a method symbol that overrides a method in a superclass? */
duke@1 1845 boolean isOverrider(Symbol s) {
duke@1 1846 if (s.kind != MTH || s.isStatic())
duke@1 1847 return false;
duke@1 1848 MethodSymbol m = (MethodSymbol)s;
duke@1 1849 TypeSymbol owner = (TypeSymbol)m.owner;
duke@1 1850 for (Type sup : types.closure(owner.type)) {
duke@1 1851 if (sup == owner.type)
duke@1 1852 continue; // skip "this"
duke@1 1853 Scope scope = sup.tsym.members();
duke@1 1854 for (Scope.Entry e = scope.lookup(m.name); e.scope != null; e = e.next()) {
duke@1 1855 if (!e.sym.isStatic() && m.overrides(e.sym, owner, types, true))
duke@1 1856 return true;
duke@1 1857 }
duke@1 1858 }
duke@1 1859 return false;
duke@1 1860 }
duke@1 1861
jjg@308 1862 /** Is the annotation applicable to type annotations */
jjg@308 1863 boolean isTypeAnnotation(JCTypeAnnotation a, boolean isTypeParameter) {
jjg@308 1864 Attribute.Compound atTarget =
jjg@308 1865 a.annotationType.type.tsym.attribute(syms.annotationTargetType.tsym);
jjg@308 1866 if (atTarget == null) return true;
jjg@308 1867 Attribute atValue = atTarget.member(names.value);
jjg@308 1868 if (!(atValue instanceof Attribute.Array)) return true; // error recovery
jjg@308 1869 Attribute.Array arr = (Attribute.Array) atValue;
jjg@308 1870 for (Attribute app : arr.values) {
jjg@308 1871 if (!(app instanceof Attribute.Enum)) return true; // recovery
jjg@308 1872 Attribute.Enum e = (Attribute.Enum) app;
jjg@308 1873 if (!isTypeParameter && e.value.name == names.TYPE_USE)
jjg@308 1874 return true;
jjg@308 1875 else if (isTypeParameter && e.value.name == names.TYPE_PARAMETER)
jjg@308 1876 return true;
jjg@308 1877 }
jjg@308 1878 return false;
jjg@308 1879 }
jjg@308 1880
duke@1 1881 /** Is the annotation applicable to the symbol? */
duke@1 1882 boolean annotationApplicable(JCAnnotation a, Symbol s) {
duke@1 1883 Attribute.Compound atTarget =
duke@1 1884 a.annotationType.type.tsym.attribute(syms.annotationTargetType.tsym);
duke@1 1885 if (atTarget == null) return true;
duke@1 1886 Attribute atValue = atTarget.member(names.value);
duke@1 1887 if (!(atValue instanceof Attribute.Array)) return true; // error recovery
duke@1 1888 Attribute.Array arr = (Attribute.Array) atValue;
duke@1 1889 for (Attribute app : arr.values) {
duke@1 1890 if (!(app instanceof Attribute.Enum)) return true; // recovery
duke@1 1891 Attribute.Enum e = (Attribute.Enum) app;
duke@1 1892 if (e.value.name == names.TYPE)
duke@1 1893 { if (s.kind == TYP) return true; }
duke@1 1894 else if (e.value.name == names.FIELD)
duke@1 1895 { if (s.kind == VAR && s.owner.kind != MTH) return true; }
duke@1 1896 else if (e.value.name == names.METHOD)
duke@1 1897 { if (s.kind == MTH && !s.isConstructor()) return true; }
duke@1 1898 else if (e.value.name == names.PARAMETER)
duke@1 1899 { if (s.kind == VAR &&
duke@1 1900 s.owner.kind == MTH &&
duke@1 1901 (s.flags() & PARAMETER) != 0)
duke@1 1902 return true;
duke@1 1903 }
duke@1 1904 else if (e.value.name == names.CONSTRUCTOR)
duke@1 1905 { if (s.kind == MTH && s.isConstructor()) return true; }
duke@1 1906 else if (e.value.name == names.LOCAL_VARIABLE)
duke@1 1907 { if (s.kind == VAR && s.owner.kind == MTH &&
duke@1 1908 (s.flags() & PARAMETER) == 0)
duke@1 1909 return true;
duke@1 1910 }
duke@1 1911 else if (e.value.name == names.ANNOTATION_TYPE)
duke@1 1912 { if (s.kind == TYP && (s.flags() & ANNOTATION) != 0)
duke@1 1913 return true;
duke@1 1914 }
duke@1 1915 else if (e.value.name == names.PACKAGE)
duke@1 1916 { if (s.kind == PCK) return true; }
jjg@308 1917 else if (e.value.name == names.TYPE_USE)
jjg@308 1918 { if (s.kind == TYP ||
jjg@308 1919 s.kind == VAR ||
jjg@308 1920 (s.kind == MTH && !s.isConstructor() &&
jjg@308 1921 s.type.getReturnType().tag != VOID))
jjg@308 1922 return true;
jjg@308 1923 }
duke@1 1924 else
duke@1 1925 return true; // recovery
duke@1 1926 }
duke@1 1927 return false;
duke@1 1928 }
duke@1 1929
duke@1 1930 /** Check an annotation value.
duke@1 1931 */
duke@1 1932 public void validateAnnotation(JCAnnotation a) {
duke@1 1933 if (a.type.isErroneous()) return;
duke@1 1934
duke@1 1935 // collect an inventory of the members
duke@1 1936 Set<MethodSymbol> members = new HashSet<MethodSymbol>();
duke@1 1937 for (Scope.Entry e = a.annotationType.type.tsym.members().elems;
duke@1 1938 e != null;
duke@1 1939 e = e.sibling)
duke@1 1940 if (e.sym.kind == MTH)
duke@1 1941 members.add((MethodSymbol) e.sym);
duke@1 1942
duke@1 1943 // count them off as they're annotated
duke@1 1944 for (JCTree arg : a.args) {
duke@1 1945 if (arg.getTag() != JCTree.ASSIGN) continue; // recovery
duke@1 1946 JCAssign assign = (JCAssign) arg;
duke@1 1947 Symbol m = TreeInfo.symbol(assign.lhs);
duke@1 1948 if (m == null || m.type.isErroneous()) continue;
duke@1 1949 if (!members.remove(m))
duke@1 1950 log.error(arg.pos(), "duplicate.annotation.member.value",
duke@1 1951 m.name, a.type);
duke@1 1952 if (assign.rhs.getTag() == ANNOTATION)
duke@1 1953 validateAnnotation((JCAnnotation)assign.rhs);
duke@1 1954 }
duke@1 1955
duke@1 1956 // all the remaining ones better have default values
duke@1 1957 for (MethodSymbol m : members)
duke@1 1958 if (m.defaultValue == null && !m.type.isErroneous())
duke@1 1959 log.error(a.pos(), "annotation.missing.default.value",
duke@1 1960 a.type, m.name);
duke@1 1961
duke@1 1962 // special case: java.lang.annotation.Target must not have
duke@1 1963 // repeated values in its value member
duke@1 1964 if (a.annotationType.type.tsym != syms.annotationTargetType.tsym ||
duke@1 1965 a.args.tail == null)
duke@1 1966 return;
duke@1 1967
duke@1 1968 if (a.args.head.getTag() != JCTree.ASSIGN) return; // error recovery
duke@1 1969 JCAssign assign = (JCAssign) a.args.head;
duke@1 1970 Symbol m = TreeInfo.symbol(assign.lhs);
duke@1 1971 if (m.name != names.value) return;
duke@1 1972 JCTree rhs = assign.rhs;
duke@1 1973 if (rhs.getTag() != JCTree.NEWARRAY) return;
duke@1 1974 JCNewArray na = (JCNewArray) rhs;
duke@1 1975 Set<Symbol> targets = new HashSet<Symbol>();
duke@1 1976 for (JCTree elem : na.elems) {
duke@1 1977 if (!targets.add(TreeInfo.symbol(elem))) {
duke@1 1978 log.error(elem.pos(), "repeated.annotation.target");
duke@1 1979 }
duke@1 1980 }
duke@1 1981 }
duke@1 1982
duke@1 1983 void checkDeprecatedAnnotation(DiagnosticPosition pos, Symbol s) {
duke@1 1984 if (allowAnnotations &&
duke@1 1985 lint.isEnabled(Lint.LintCategory.DEP_ANN) &&
duke@1 1986 (s.flags() & DEPRECATED) != 0 &&
duke@1 1987 !syms.deprecatedType.isErroneous() &&
duke@1 1988 s.attribute(syms.deprecatedType.tsym) == null) {
duke@1 1989 log.warning(pos, "missing.deprecated.annotation");
duke@1 1990 }
duke@1 1991 }
duke@1 1992
duke@1 1993 /* *************************************************************************
duke@1 1994 * Check for recursive annotation elements.
duke@1 1995 **************************************************************************/
duke@1 1996
duke@1 1997 /** Check for cycles in the graph of annotation elements.
duke@1 1998 */
duke@1 1999 void checkNonCyclicElements(JCClassDecl tree) {
duke@1 2000 if ((tree.sym.flags_field & ANNOTATION) == 0) return;
duke@1 2001 assert (tree.sym.flags_field & LOCKED) == 0;
duke@1 2002 try {
duke@1 2003 tree.sym.flags_field |= LOCKED;
duke@1 2004 for (JCTree def : tree.defs) {
duke@1 2005 if (def.getTag() != JCTree.METHODDEF) continue;
duke@1 2006 JCMethodDecl meth = (JCMethodDecl)def;
duke@1 2007 checkAnnotationResType(meth.pos(), meth.restype.type);
duke@1 2008 }
duke@1 2009 } finally {
duke@1 2010 tree.sym.flags_field &= ~LOCKED;
duke@1 2011 tree.sym.flags_field |= ACYCLIC_ANN;
duke@1 2012 }
duke@1 2013 }
duke@1 2014
duke@1 2015 void checkNonCyclicElementsInternal(DiagnosticPosition pos, TypeSymbol tsym) {
duke@1 2016 if ((tsym.flags_field & ACYCLIC_ANN) != 0)
duke@1 2017 return;
duke@1 2018 if ((tsym.flags_field & LOCKED) != 0) {
duke@1 2019 log.error(pos, "cyclic.annotation.element");
duke@1 2020 return;
duke@1 2021 }
duke@1 2022 try {
duke@1 2023 tsym.flags_field |= LOCKED;
duke@1 2024 for (Scope.Entry e = tsym.members().elems; e != null; e = e.sibling) {
duke@1 2025 Symbol s = e.sym;
duke@1 2026 if (s.kind != Kinds.MTH)
duke@1 2027 continue;
duke@1 2028 checkAnnotationResType(pos, ((MethodSymbol)s).type.getReturnType());
duke@1 2029 }
duke@1 2030 } finally {
duke@1 2031 tsym.flags_field &= ~LOCKED;
duke@1 2032 tsym.flags_field |= ACYCLIC_ANN;
duke@1 2033 }
duke@1 2034 }
duke@1 2035
duke@1 2036 void checkAnnotationResType(DiagnosticPosition pos, Type type) {
duke@1 2037 switch (type.tag) {
duke@1 2038 case TypeTags.CLASS:
duke@1 2039 if ((type.tsym.flags() & ANNOTATION) != 0)
duke@1 2040 checkNonCyclicElementsInternal(pos, type.tsym);
duke@1 2041 break;
duke@1 2042 case TypeTags.ARRAY:
duke@1 2043 checkAnnotationResType(pos, types.elemtype(type));
duke@1 2044 break;
duke@1 2045 default:
duke@1 2046 break; // int etc
duke@1 2047 }
duke@1 2048 }
duke@1 2049
duke@1 2050 /* *************************************************************************
duke@1 2051 * Check for cycles in the constructor call graph.
duke@1 2052 **************************************************************************/
duke@1 2053
duke@1 2054 /** Check for cycles in the graph of constructors calling other
duke@1 2055 * constructors.
duke@1 2056 */
duke@1 2057 void checkCyclicConstructors(JCClassDecl tree) {
duke@1 2058 Map<Symbol,Symbol> callMap = new HashMap<Symbol, Symbol>();
duke@1 2059
duke@1 2060 // enter each constructor this-call into the map
duke@1 2061 for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
duke@1 2062 JCMethodInvocation app = TreeInfo.firstConstructorCall(l.head);
duke@1 2063 if (app == null) continue;
duke@1 2064 JCMethodDecl meth = (JCMethodDecl) l.head;
duke@1 2065 if (TreeInfo.name(app.meth) == names._this) {
duke@1 2066 callMap.put(meth.sym, TreeInfo.symbol(app.meth));
duke@1 2067 } else {
duke@1 2068 meth.sym.flags_field |= ACYCLIC;
duke@1 2069 }
duke@1 2070 }
duke@1 2071
duke@1 2072 // Check for cycles in the map
duke@1 2073 Symbol[] ctors = new Symbol[0];
duke@1 2074 ctors = callMap.keySet().toArray(ctors);
duke@1 2075 for (Symbol caller : ctors) {
duke@1 2076 checkCyclicConstructor(tree, caller, callMap);
duke@1 2077 }
duke@1 2078 }
duke@1 2079
duke@1 2080 /** Look in the map to see if the given constructor is part of a
duke@1 2081 * call cycle.
duke@1 2082 */
duke@1 2083 private void checkCyclicConstructor(JCClassDecl tree, Symbol ctor,
duke@1 2084 Map<Symbol,Symbol> callMap) {
duke@1 2085 if (ctor != null && (ctor.flags_field & ACYCLIC) == 0) {
duke@1 2086 if ((ctor.flags_field & LOCKED) != 0) {
duke@1 2087 log.error(TreeInfo.diagnosticPositionFor(ctor, tree),
duke@1 2088 "recursive.ctor.invocation");
duke@1 2089 } else {
duke@1 2090 ctor.flags_field |= LOCKED;
duke@1 2091 checkCyclicConstructor(tree, callMap.remove(ctor), callMap);
duke@1 2092 ctor.flags_field &= ~LOCKED;
duke@1 2093 }
duke@1 2094 ctor.flags_field |= ACYCLIC;
duke@1 2095 }
duke@1 2096 }
duke@1 2097
duke@1 2098 /* *************************************************************************
duke@1 2099 * Miscellaneous
duke@1 2100 **************************************************************************/
duke@1 2101
duke@1 2102 /**
duke@1 2103 * Return the opcode of the operator but emit an error if it is an
duke@1 2104 * error.
duke@1 2105 * @param pos position for error reporting.
duke@1 2106 * @param operator an operator
duke@1 2107 * @param tag a tree tag
duke@1 2108 * @param left type of left hand side
duke@1 2109 * @param right type of right hand side
duke@1 2110 */
duke@1 2111 int checkOperator(DiagnosticPosition pos,
duke@1 2112 OperatorSymbol operator,
duke@1 2113 int tag,
duke@1 2114 Type left,
duke@1 2115 Type right) {
duke@1 2116 if (operator.opcode == ByteCodes.error) {
duke@1 2117 log.error(pos,
duke@1 2118 "operator.cant.be.applied",
duke@1 2119 treeinfo.operatorName(tag),
mcimadamore@80 2120 List.of(left, right));
duke@1 2121 }
duke@1 2122 return operator.opcode;
duke@1 2123 }
duke@1 2124
duke@1 2125
duke@1 2126 /**
duke@1 2127 * Check for division by integer constant zero
duke@1 2128 * @param pos Position for error reporting.
duke@1 2129 * @param operator The operator for the expression
duke@1 2130 * @param operand The right hand operand for the expression
duke@1 2131 */
duke@1 2132 void checkDivZero(DiagnosticPosition pos, Symbol operator, Type operand) {
duke@1 2133 if (operand.constValue() != null
duke@1 2134 && lint.isEnabled(Lint.LintCategory.DIVZERO)
duke@1 2135 && operand.tag <= LONG
duke@1 2136 && ((Number) (operand.constValue())).longValue() == 0) {
duke@1 2137 int opc = ((OperatorSymbol)operator).opcode;
duke@1 2138 if (opc == ByteCodes.idiv || opc == ByteCodes.imod
duke@1 2139 || opc == ByteCodes.ldiv || opc == ByteCodes.lmod) {
duke@1 2140 log.warning(pos, "div.zero");
duke@1 2141 }
duke@1 2142 }
duke@1 2143 }
duke@1 2144
duke@1 2145 /**
duke@1 2146 * Check for empty statements after if
duke@1 2147 */
duke@1 2148 void checkEmptyIf(JCIf tree) {
duke@1 2149 if (tree.thenpart.getTag() == JCTree.SKIP && tree.elsepart == null && lint.isEnabled(Lint.LintCategory.EMPTY))
duke@1 2150 log.warning(tree.thenpart.pos(), "empty.if");
duke@1 2151 }
duke@1 2152
duke@1 2153 /** Check that symbol is unique in given scope.
duke@1 2154 * @param pos Position for error reporting.
duke@1 2155 * @param sym The symbol.
duke@1 2156 * @param s The scope.
duke@1 2157 */
duke@1 2158 boolean checkUnique(DiagnosticPosition pos, Symbol sym, Scope s) {
duke@1 2159 if (sym.type.isErroneous())
duke@1 2160 return true;
duke@1 2161 if (sym.owner.name == names.any) return false;
duke@1 2162 for (Scope.Entry e = s.lookup(sym.name); e.scope == s; e = e.next()) {
duke@1 2163 if (sym != e.sym &&
duke@1 2164 sym.kind == e.sym.kind &&
duke@1 2165 sym.name != names.error &&
mcimadamore@252 2166 (sym.kind != MTH || types.hasSameArgs(types.erasure(sym.type), types.erasure(e.sym.type)))) {
duke@1 2167 if ((sym.flags() & VARARGS) != (e.sym.flags() & VARARGS))
duke@1 2168 varargsDuplicateError(pos, sym, e.sym);
mcimadamore@252 2169 else if (sym.kind == MTH && !types.overrideEquivalent(sym.type, e.sym.type))
mcimadamore@252 2170 duplicateErasureError(pos, sym, e.sym);
duke@1 2171 else
duke@1 2172 duplicateError(pos, e.sym);
duke@1 2173 return false;
duke@1 2174 }
duke@1 2175 }
duke@1 2176 return true;
duke@1 2177 }
mcimadamore@252 2178 //where
mcimadamore@252 2179 /** Report duplicate declaration error.
mcimadamore@252 2180 */
mcimadamore@252 2181 void duplicateErasureError(DiagnosticPosition pos, Symbol sym1, Symbol sym2) {
mcimadamore@252 2182 if (!sym1.type.isErroneous() && !sym2.type.isErroneous()) {
mcimadamore@252 2183 log.error(pos, "name.clash.same.erasure", sym1, sym2);
mcimadamore@252 2184 }
mcimadamore@252 2185 }
duke@1 2186
duke@1 2187 /** Check that single-type import is not already imported or top-level defined,
duke@1 2188 * but make an exception for two single-type imports which denote the same type.
duke@1 2189 * @param pos Position for error reporting.
duke@1 2190 * @param sym The symbol.
duke@1 2191 * @param s The scope
duke@1 2192 */
duke@1 2193 boolean checkUniqueImport(DiagnosticPosition pos, Symbol sym, Scope s) {
duke@1 2194 return checkUniqueImport(pos, sym, s, false);
duke@1 2195 }
duke@1 2196
duke@1 2197 /** Check that static single-type import is not already imported or top-level defined,
duke@1 2198 * but make an exception for two single-type imports which denote the same type.
duke@1 2199 * @param pos Position for error reporting.
duke@1 2200 * @param sym The symbol.
duke@1 2201 * @param s The scope
duke@1 2202 * @param staticImport Whether or not this was a static import
duke@1 2203 */
duke@1 2204 boolean checkUniqueStaticImport(DiagnosticPosition pos, Symbol sym, Scope s) {
duke@1 2205 return checkUniqueImport(pos, sym, s, true);
duke@1 2206 }
duke@1 2207
duke@1 2208 /** Check that single-type import is not already imported or top-level defined,
duke@1 2209 * but make an exception for two single-type imports which denote the same type.
duke@1 2210 * @param pos Position for error reporting.
duke@1 2211 * @param sym The symbol.
duke@1 2212 * @param s The scope.
duke@1 2213 * @param staticImport Whether or not this was a static import
duke@1 2214 */
duke@1 2215 private boolean checkUniqueImport(DiagnosticPosition pos, Symbol sym, Scope s, boolean staticImport) {
duke@1 2216 for (Scope.Entry e = s.lookup(sym.name); e.scope != null; e = e.next()) {
duke@1 2217 // is encountered class entered via a class declaration?
duke@1 2218 boolean isClassDecl = e.scope == s;
duke@1 2219 if ((isClassDecl || sym != e.sym) &&
duke@1 2220 sym.kind == e.sym.kind &&
duke@1 2221 sym.name != names.error) {
duke@1 2222 if (!e.sym.type.isErroneous()) {
duke@1 2223 String what = e.sym.toString();
duke@1 2224 if (!isClassDecl) {
duke@1 2225 if (staticImport)
duke@1 2226 log.error(pos, "already.defined.static.single.import", what);
duke@1 2227 else
duke@1 2228 log.error(pos, "already.defined.single.import", what);
duke@1 2229 }
duke@1 2230 else if (sym != e.sym)
duke@1 2231 log.error(pos, "already.defined.this.unit", what);
duke@1 2232 }
duke@1 2233 return false;
duke@1 2234 }
duke@1 2235 }
duke@1 2236 return true;
duke@1 2237 }
duke@1 2238
duke@1 2239 /** Check that a qualified name is in canonical form (for import decls).
duke@1 2240 */
duke@1 2241 public void checkCanonical(JCTree tree) {
duke@1 2242 if (!isCanonical(tree))
duke@1 2243 log.error(tree.pos(), "import.requires.canonical",
duke@1 2244 TreeInfo.symbol(tree));
duke@1 2245 }
duke@1 2246 // where
duke@1 2247 private boolean isCanonical(JCTree tree) {
duke@1 2248 while (tree.getTag() == JCTree.SELECT) {
duke@1 2249 JCFieldAccess s = (JCFieldAccess) tree;
duke@1 2250 if (s.sym.owner != TreeInfo.symbol(s.selected))
duke@1 2251 return false;
duke@1 2252 tree = s.selected;
duke@1 2253 }
duke@1 2254 return true;
duke@1 2255 }
duke@1 2256
duke@1 2257 private class ConversionWarner extends Warner {
duke@1 2258 final String key;
duke@1 2259 final Type found;
duke@1 2260 final Type expected;
duke@1 2261 public ConversionWarner(DiagnosticPosition pos, String key, Type found, Type expected) {
duke@1 2262 super(pos);
duke@1 2263 this.key = key;
duke@1 2264 this.found = found;
duke@1 2265 this.expected = expected;
duke@1 2266 }
duke@1 2267
duke@1 2268 public void warnUnchecked() {
duke@1 2269 boolean warned = this.warned;
duke@1 2270 super.warnUnchecked();
duke@1 2271 if (warned) return; // suppress redundant diagnostics
mcimadamore@89 2272 Object problem = diags.fragment(key);
duke@1 2273 Check.this.warnUnchecked(pos(), "prob.found.req", problem, found, expected);
duke@1 2274 }
duke@1 2275 }
duke@1 2276
duke@1 2277 public Warner castWarner(DiagnosticPosition pos, Type found, Type expected) {
duke@1 2278 return new ConversionWarner(pos, "unchecked.cast.to.type", found, expected);
duke@1 2279 }
duke@1 2280
duke@1 2281 public Warner convertWarner(DiagnosticPosition pos, Type found, Type expected) {
duke@1 2282 return new ConversionWarner(pos, "unchecked.assign", found, expected);
duke@1 2283 }
duke@1 2284 }

mercurial