Symtab.java (3294:9adfb22ff08f) | Symtab.java (3323:680712ce0386) |
---|---|
1/* | 1/* |
2 * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved. | 2 * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * --- 9 unchanged lines hidden (view full) --- 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26package com.sun.tools.javac.code; 27 | 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * --- 9 unchanged lines hidden (view full) --- 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26package com.sun.tools.javac.code; 27 |
28import java.util.Collection; 29import java.util.Collections; | |
30import java.util.HashMap; | 28import java.util.HashMap; |
31import java.util.LinkedHashMap; | |
32import java.util.Map; 33 34import javax.lang.model.element.ElementVisitor; | 29import java.util.Map; 30 31import javax.lang.model.element.ElementVisitor; |
32import javax.tools.JavaFileObject; |
|
35 | 33 |
34 |
|
36import com.sun.tools.javac.code.Scope.WriteableScope; 37import com.sun.tools.javac.code.Symbol.ClassSymbol; 38import com.sun.tools.javac.code.Symbol.Completer; 39import com.sun.tools.javac.code.Symbol.CompletionFailure; 40import com.sun.tools.javac.code.Symbol.MethodSymbol; | 35import com.sun.tools.javac.code.Scope.WriteableScope; 36import com.sun.tools.javac.code.Symbol.ClassSymbol; 37import com.sun.tools.javac.code.Symbol.Completer; 38import com.sun.tools.javac.code.Symbol.CompletionFailure; 39import com.sun.tools.javac.code.Symbol.MethodSymbol; |
41import com.sun.tools.javac.code.Symbol.ModuleSymbol; | |
42import com.sun.tools.javac.code.Symbol.PackageSymbol; 43import com.sun.tools.javac.code.Symbol.TypeSymbol; 44import com.sun.tools.javac.code.Symbol.VarSymbol; 45import com.sun.tools.javac.code.Type.BottomType; 46import com.sun.tools.javac.code.Type.ClassType; 47import com.sun.tools.javac.code.Type.ErrorType; 48import com.sun.tools.javac.code.Type.JCPrimitiveType; 49import com.sun.tools.javac.code.Type.JCVoidType; 50import com.sun.tools.javac.code.Type.MethodType; 51import com.sun.tools.javac.code.Type.UnknownType; | 40import com.sun.tools.javac.code.Symbol.PackageSymbol; 41import com.sun.tools.javac.code.Symbol.TypeSymbol; 42import com.sun.tools.javac.code.Symbol.VarSymbol; 43import com.sun.tools.javac.code.Type.BottomType; 44import com.sun.tools.javac.code.Type.ClassType; 45import com.sun.tools.javac.code.Type.ErrorType; 46import com.sun.tools.javac.code.Type.JCPrimitiveType; 47import com.sun.tools.javac.code.Type.JCVoidType; 48import com.sun.tools.javac.code.Type.MethodType; 49import com.sun.tools.javac.code.Type.UnknownType; |
52import com.sun.tools.javac.comp.Modules; | 50import com.sun.tools.javac.jvm.Target; |
53import com.sun.tools.javac.util.Assert; 54import com.sun.tools.javac.util.Context; 55import com.sun.tools.javac.util.Convert; 56import com.sun.tools.javac.util.DefinedBy; 57import com.sun.tools.javac.util.DefinedBy.Api; | 51import com.sun.tools.javac.util.Assert; 52import com.sun.tools.javac.util.Context; 53import com.sun.tools.javac.util.Convert; 54import com.sun.tools.javac.util.DefinedBy; 55import com.sun.tools.javac.util.DefinedBy.Api; |
58import com.sun.tools.javac.util.Iterators; | |
59import com.sun.tools.javac.util.JavacMessages; 60import com.sun.tools.javac.util.List; | 56import com.sun.tools.javac.util.JavacMessages; 57import com.sun.tools.javac.util.List; |
58import com.sun.tools.javac.util.Log; |
|
61import com.sun.tools.javac.util.Name; 62import com.sun.tools.javac.util.Names; | 59import com.sun.tools.javac.util.Name; 60import com.sun.tools.javac.util.Names; |
63import com.sun.tools.javac.util.Options; | |
64 65import static com.sun.tools.javac.code.Flags.*; 66import static com.sun.tools.javac.code.Kinds.Kind.*; 67import static com.sun.tools.javac.code.TypeTag.*; 68 69/** A class that defines all predefined constants and operators 70 * as well as special classes such as java.lang.Object, which need 71 * to be known to the compiler. All symbols are held in instance --- 26 unchanged lines hidden (view full) --- 98 public final JCPrimitiveType longType = new JCPrimitiveType(LONG, null); 99 public final JCPrimitiveType floatType = new JCPrimitiveType(FLOAT, null); 100 public final JCPrimitiveType doubleType = new JCPrimitiveType(DOUBLE, null); 101 public final JCPrimitiveType booleanType = new JCPrimitiveType(BOOLEAN, null); 102 public final Type botType = new BottomType(); 103 public final JCVoidType voidType = new JCVoidType(); 104 105 private final Names names; | 61 62import static com.sun.tools.javac.code.Flags.*; 63import static com.sun.tools.javac.code.Kinds.Kind.*; 64import static com.sun.tools.javac.code.TypeTag.*; 65 66/** A class that defines all predefined constants and operators 67 * as well as special classes such as java.lang.Object, which need 68 * to be known to the compiler. All symbols are held in instance --- 26 unchanged lines hidden (view full) --- 95 public final JCPrimitiveType longType = new JCPrimitiveType(LONG, null); 96 public final JCPrimitiveType floatType = new JCPrimitiveType(FLOAT, null); 97 public final JCPrimitiveType doubleType = new JCPrimitiveType(DOUBLE, null); 98 public final JCPrimitiveType booleanType = new JCPrimitiveType(BOOLEAN, null); 99 public final Type botType = new BottomType(); 100 public final JCVoidType voidType = new JCVoidType(); 101 102 private final Names names; |
106 private final JavacMessages messages; | |
107 private final Completer initialCompleter; | 103 private final Completer initialCompleter; |
108 private final Completer moduleCompleter; | 104 private final Target target; |
109 | 105 |
110 /** A symbol for the unnamed module. 111 */ 112 public final ModuleSymbol unnamedModule; 113 114 /** The error module. 115 */ 116 public final ModuleSymbol errModule; 117 118 /** A symbol for no module, for use with -source 8 or less 119 */ 120 public final ModuleSymbol noModule; 121 | |
122 /** A symbol for the root package. 123 */ 124 public final PackageSymbol rootPackage; 125 | 106 /** A symbol for the root package. 107 */ 108 public final PackageSymbol rootPackage; 109 |
110 /** A symbol for the unnamed package. 111 */ 112 public final PackageSymbol unnamedPackage; 113 |
|
126 /** A symbol that stands for a missing symbol. 127 */ 128 public final TypeSymbol noSymbol; 129 130 /** The error symbol. 131 */ 132 public final ClassSymbol errSymbol; 133 --- 12 unchanged lines hidden (view full) --- 146 public final MethodSymbol arrayCloneMethod; 147 148 /** VGJ: The (singleton) type of all bound types. */ 149 public final ClassSymbol boundClass; 150 151 /** The builtin type of all methods. */ 152 public final ClassSymbol methodClass; 153 | 114 /** A symbol that stands for a missing symbol. 115 */ 116 public final TypeSymbol noSymbol; 117 118 /** The error symbol. 119 */ 120 public final ClassSymbol errSymbol; 121 --- 12 unchanged lines hidden (view full) --- 134 public final MethodSymbol arrayCloneMethod; 135 136 /** VGJ: The (singleton) type of all bound types. */ 137 public final ClassSymbol boundClass; 138 139 /** The builtin type of all methods. */ 140 public final ClassSymbol methodClass; 141 |
154 /** A symbol for the java.base module. 155 */ 156 public final ModuleSymbol java_base; 157 | |
158 /** Predefined types. 159 */ 160 public final Type objectType; 161 public final Type objectsType; 162 public final Type classType; 163 public final Type classLoaderType; 164 public final Type stringType; 165 public final Type stringBufferType; 166 public final Type stringBuilderType; 167 public final Type cloneableType; 168 public final Type serializableType; 169 public final Type serializedLambdaType; | 142 /** Predefined types. 143 */ 144 public final Type objectType; 145 public final Type objectsType; 146 public final Type classType; 147 public final Type classLoaderType; 148 public final Type stringType; 149 public final Type stringBufferType; 150 public final Type stringBuilderType; 151 public final Type cloneableType; 152 public final Type serializableType; 153 public final Type serializedLambdaType; |
154 public final Type varHandleType; |
|
170 public final Type methodHandleType; 171 public final Type methodHandleLookupType; 172 public final Type methodTypeType; 173 public final Type nativeHeaderType; 174 public final Type throwableType; 175 public final Type errorType; 176 public final Type interruptedExceptionType; 177 public final Type illegalArgumentExceptionType; --- 41 unchanged lines hidden (view full) --- 219 220 /** The symbol representing the close method on TWR AutoCloseable type */ 221 public final MethodSymbol autoCloseableClose; 222 223 /** The predefined type that belongs to a tag. 224 */ 225 public final Type[] typeOfTag = new Type[TypeTag.getTypeTagCount()]; 226 | 155 public final Type methodHandleType; 156 public final Type methodHandleLookupType; 157 public final Type methodTypeType; 158 public final Type nativeHeaderType; 159 public final Type throwableType; 160 public final Type errorType; 161 public final Type interruptedExceptionType; 162 public final Type illegalArgumentExceptionType; --- 41 unchanged lines hidden (view full) --- 204 205 /** The symbol representing the close method on TWR AutoCloseable type */ 206 public final MethodSymbol autoCloseableClose; 207 208 /** The predefined type that belongs to a tag. 209 */ 210 public final Type[] typeOfTag = new Type[TypeTag.getTypeTagCount()]; 211 |
227 /** The name of the class that belongs to a basic type tag. | 212 /** The name of the class that belongs to a basix type tag. |
228 */ 229 public final Name[] boxedName = new Name[TypeTag.getTypeTagCount()]; 230 231 /** A hashtable containing the encountered top-level and member classes, 232 * indexed by flat names. The table does not contain local classes. 233 * It should be updated from the outside to reflect classes defined 234 * by compiled source files. 235 */ | 213 */ 214 public final Name[] boxedName = new Name[TypeTag.getTypeTagCount()]; 215 216 /** A hashtable containing the encountered top-level and member classes, 217 * indexed by flat names. The table does not contain local classes. 218 * It should be updated from the outside to reflect classes defined 219 * by compiled source files. 220 */ |
236 private final Map<Name, Map<ModuleSymbol,ClassSymbol>> classes = new HashMap<>(); | 221 public final Map<Name, ClassSymbol> classes = new HashMap<>(); |
237 238 /** A hashtable containing the encountered packages. 239 * the table should be updated from outside to reflect packages defined 240 * by compiled source files. 241 */ | 222 223 /** A hashtable containing the encountered packages. 224 * the table should be updated from outside to reflect packages defined 225 * by compiled source files. 226 */ |
242 private final Map<Name, Map<ModuleSymbol,PackageSymbol>> packages = new HashMap<>(); | 227 public final Map<Name, PackageSymbol> packages = new HashMap<>(); |
243 | 228 |
244 /** A hashtable giving the encountered modules. 245 */ 246 private final Map<Name, ModuleSymbol> modules = new LinkedHashMap<>(); 247 | |
248 public void initType(Type type, ClassSymbol c) { 249 type.tsym = c; 250 typeOfTag[type.getTag().ordinal()] = type; 251 } 252 253 public void initType(Type type, String name) { 254 initType( 255 type, 256 new ClassSymbol( 257 PUBLIC, names.fromString(name), type, rootPackage)); 258 } 259 260 public void initType(Type type, String name, String bname) { 261 initType(type, name); | 229 public void initType(Type type, ClassSymbol c) { 230 type.tsym = c; 231 typeOfTag[type.getTag().ordinal()] = type; 232 } 233 234 public void initType(Type type, String name) { 235 initType( 236 type, 237 new ClassSymbol( 238 PUBLIC, names.fromString(name), type, rootPackage)); 239 } 240 241 public void initType(Type type, String name, String bname) { 242 initType(type, name); |
262 boxedName[type.getTag().ordinal()] = names.fromString("java.lang." + bname); | 243 boxedName[type.getTag().ordinal()] = names.fromString("java.lang." + bname); |
263 } 264 265 /** The class symbol that owns all predefined symbols. 266 */ 267 public final ClassSymbol predefClass; 268 269 /** Enter a class into symbol table. 270 * @param s The name of the class. 271 */ 272 private Type enterClass(String s) { | 244 } 245 246 /** The class symbol that owns all predefined symbols. 247 */ 248 public final ClassSymbol predefClass; 249 250 /** Enter a class into symbol table. 251 * @param s The name of the class. 252 */ 253 private Type enterClass(String s) { |
273 return enterClass(java_base, names.fromString(s)).type; | 254 return enterClass(names.fromString(s)).type; |
274 } 275 276 public void synthesizeEmptyInterfaceIfMissing(final Type type) { 277 final Completer completer = type.tsym.completer; 278 type.tsym.completer = new Completer() { | 255 } 256 257 public void synthesizeEmptyInterfaceIfMissing(final Type type) { 258 final Completer completer = type.tsym.completer; 259 type.tsym.completer = new Completer() { |
279 @Override | |
280 public void complete(Symbol sym) throws CompletionFailure { 281 try { 282 completer.complete(sym); 283 } catch (CompletionFailure e) { 284 sym.flags_field |= (PUBLIC | INTERFACE); 285 ((ClassType) sym.type).supertype_field = objectType; 286 } 287 } 288 289 @Override 290 public boolean isTerminal() { 291 return completer.isTerminal(); 292 } 293 }; 294 } 295 296 public void synthesizeBoxTypeIfMissing(final Type type) { | 260 public void complete(Symbol sym) throws CompletionFailure { 261 try { 262 completer.complete(sym); 263 } catch (CompletionFailure e) { 264 sym.flags_field |= (PUBLIC | INTERFACE); 265 ((ClassType) sym.type).supertype_field = objectType; 266 } 267 } 268 269 @Override 270 public boolean isTerminal() { 271 return completer.isTerminal(); 272 } 273 }; 274 } 275 276 public void synthesizeBoxTypeIfMissing(final Type type) { |
297 ClassSymbol sym = enterClass(java_base, boxedName[type.getTag().ordinal()]); | 277 ClassSymbol sym = enterClass(boxedName[type.getTag().ordinal()]); |
298 final Completer completer = sym.completer; 299 sym.completer = new Completer() { | 278 final Completer completer = sym.completer; 279 sym.completer = new Completer() { |
300 @Override | |
301 public void complete(Symbol sym) throws CompletionFailure { 302 try { 303 completer.complete(sym); 304 } catch (CompletionFailure e) { 305 sym.flags_field |= PUBLIC; 306 ((ClassType) sym.type).supertype_field = objectType; 307 MethodSymbol boxMethod = 308 new MethodSymbol(PUBLIC | STATIC, names.valueOf, --- 16 unchanged lines hidden (view full) --- 325 return completer.isTerminal(); 326 } 327 }; 328 } 329 330 // Enter a synthetic class that is used to mark classes in ct.sym. 331 // This class does not have a class file. 332 private Type enterSyntheticAnnotation(String name) { | 280 public void complete(Symbol sym) throws CompletionFailure { 281 try { 282 completer.complete(sym); 283 } catch (CompletionFailure e) { 284 sym.flags_field |= PUBLIC; 285 ((ClassType) sym.type).supertype_field = objectType; 286 MethodSymbol boxMethod = 287 new MethodSymbol(PUBLIC | STATIC, names.valueOf, --- 16 unchanged lines hidden (view full) --- 304 return completer.isTerminal(); 305 } 306 }; 307 } 308 309 // Enter a synthetic class that is used to mark classes in ct.sym. 310 // This class does not have a class file. 311 private Type enterSyntheticAnnotation(String name) { |
333 // for now, leave the module null, to prevent problems from synthesizing the 334 // existence of a class in any specific module, including noModule 335 ClassType type = (ClassType)enterClass(java_base, names.fromString(name)).type; | 312 ClassType type = (ClassType)enterClass(name); |
336 ClassSymbol sym = (ClassSymbol)type.tsym; 337 sym.completer = Completer.NULL_COMPLETER; 338 sym.flags_field = PUBLIC|ACYCLIC|ANNOTATION|INTERFACE; 339 sym.erasure_field = type; 340 sym.members_field = WriteableScope.create(sym); 341 type.typarams_field = List.nil(); 342 type.allparams_field = List.nil(); 343 type.supertype_field = annotationType; 344 type.interfaces_field = List.nil(); 345 return type; 346 } 347 348 /** Constructor; enters all predefined identifiers and operators 349 * into symbol table. 350 */ 351 protected Symtab(Context context) throws CompletionFailure { 352 context.put(symtabKey, this); 353 354 names = Names.instance(context); | 313 ClassSymbol sym = (ClassSymbol)type.tsym; 314 sym.completer = Completer.NULL_COMPLETER; 315 sym.flags_field = PUBLIC|ACYCLIC|ANNOTATION|INTERFACE; 316 sym.erasure_field = type; 317 sym.members_field = WriteableScope.create(sym); 318 type.typarams_field = List.nil(); 319 type.allparams_field = List.nil(); 320 type.supertype_field = annotationType; 321 type.interfaces_field = List.nil(); 322 return type; 323 } 324 325 /** Constructor; enters all predefined identifiers and operators 326 * into symbol table. 327 */ 328 protected Symtab(Context context) throws CompletionFailure { 329 context.put(symtabKey, this); 330 331 names = Names.instance(context); |
332 target = Target.instance(context); |
|
355 356 // Create the unknown type 357 unknownType = new UnknownType(); 358 | 333 334 // Create the unknown type 335 unknownType = new UnknownType(); 336 |
359 messages = JavacMessages.instance(context); 360 361 rootPackage = new PackageSymbol(names.empty, null); 362 | |
363 // create the basic builtin symbols | 337 // create the basic builtin symbols |
364 unnamedModule = new ModuleSymbol(names.empty, null) { 365 @Override | 338 rootPackage = new PackageSymbol(names.empty, null); 339 packages.put(names.empty, rootPackage); 340 final JavacMessages messages = JavacMessages.instance(context); 341 unnamedPackage = new PackageSymbol(names.empty, rootPackage) { |
366 public String toString() { | 342 public String toString() { |
367 return messages.getLocalizedString("compiler.misc.unnamed.module"); | 343 return messages.getLocalizedString("compiler.misc.unnamed.package"); |
368 } 369 }; | 344 } 345 }; |
370 addRootPackageFor(unnamedModule); 371 372 errModule = new ModuleSymbol(names.empty, null) { }; 373 addRootPackageFor(errModule); 374 375 noModule = new ModuleSymbol(names.empty, null) { 376 @Override public boolean isNoModule() { 377 return true; 378 } 379 }; 380 addRootPackageFor(noModule); 381 | |
382 noSymbol = new TypeSymbol(NIL, 0, names.empty, Type.noType, rootPackage) { | 346 noSymbol = new TypeSymbol(NIL, 0, names.empty, Type.noType, rootPackage) { |
383 @Override @DefinedBy(Api.LANGUAGE_MODEL) | 347 @DefinedBy(Api.LANGUAGE_MODEL) |
384 public <R, P> R accept(ElementVisitor<R, P> v, P p) { 385 return v.visitUnknown(this, p); 386 } 387 }; 388 389 // create the error symbols 390 errSymbol = new ClassSymbol(PUBLIC|STATIC|ACYCLIC, names.any, null, rootPackage); 391 errType = new ErrorType(errSymbol, Type.noType); --- 29 unchanged lines hidden (view full) --- 421 422 // Create class to hold all predefined constants and operations. 423 predefClass = new ClassSymbol(PUBLIC|ACYCLIC, names.empty, rootPackage); 424 WriteableScope scope = WriteableScope.create(predefClass); 425 predefClass.members_field = scope; 426 427 // Get the initial completer for Symbols from the ClassFinder 428 initialCompleter = ClassFinder.instance(context).getCompleter(); | 348 public <R, P> R accept(ElementVisitor<R, P> v, P p) { 349 return v.visitUnknown(this, p); 350 } 351 }; 352 353 // create the error symbols 354 errSymbol = new ClassSymbol(PUBLIC|STATIC|ACYCLIC, names.any, null, rootPackage); 355 errType = new ErrorType(errSymbol, Type.noType); --- 29 unchanged lines hidden (view full) --- 385 386 // Create class to hold all predefined constants and operations. 387 predefClass = new ClassSymbol(PUBLIC|ACYCLIC, names.empty, rootPackage); 388 WriteableScope scope = WriteableScope.create(predefClass); 389 predefClass.members_field = scope; 390 391 // Get the initial completer for Symbols from the ClassFinder 392 initialCompleter = ClassFinder.instance(context).getCompleter(); |
429 rootPackage.members_field = WriteableScope.create(rootPackage); | 393 rootPackage.completer = initialCompleter; 394 unnamedPackage.completer = initialCompleter; |
430 431 // Enter symbols for basic types. 432 scope.enter(byteType.tsym); 433 scope.enter(shortType.tsym); 434 scope.enter(charType.tsym); 435 scope.enter(intType.tsym); 436 scope.enter(longType.tsym); 437 scope.enter(floatType.tsym); 438 scope.enter(doubleType.tsym); 439 scope.enter(booleanType.tsym); 440 scope.enter(errType.tsym); 441 442 // Enter symbol for the errSymbol 443 scope.enter(errSymbol); 444 | 395 396 // Enter symbols for basic types. 397 scope.enter(byteType.tsym); 398 scope.enter(shortType.tsym); 399 scope.enter(charType.tsym); 400 scope.enter(intType.tsym); 401 scope.enter(longType.tsym); 402 scope.enter(floatType.tsym); 403 scope.enter(doubleType.tsym); 404 scope.enter(booleanType.tsym); 405 scope.enter(errType.tsym); 406 407 // Enter symbol for the errSymbol 408 scope.enter(errSymbol); 409 |
445 Source source = Source.instance(context); 446 Options options = Options.instance(context); 447 boolean noModules = options.isSet("noModules"); 448 if (source.allowModules() && !noModules) { 449 java_base = enterModule(names.java_base); 450 //avoid completing java.base during the Symtab initialization 451 java_base.completer = Completer.NULL_COMPLETER; 452 java_base.visiblePackages = Collections.emptyMap(); 453 } else { 454 java_base = noModule; 455 } | 410 classes.put(predefClass.fullname, predefClass); |
456 | 411 |
457 // Get the initial completer for ModuleSymbols from Modules 458 moduleCompleter = Modules.instance(context).getCompleter(); 459 460 // Enter predefined classes. All are assumed to be in the java.base module. | 412 // Enter predefined classes. |
461 objectType = enterClass("java.lang.Object"); 462 objectsType = enterClass("java.util.Objects"); 463 classType = enterClass("java.lang.Class"); 464 stringType = enterClass("java.lang.String"); 465 stringBufferType = enterClass("java.lang.StringBuffer"); 466 stringBuilderType = enterClass("java.lang.StringBuilder"); 467 cloneableType = enterClass("java.lang.Cloneable"); 468 throwableType = enterClass("java.lang.Throwable"); 469 serializableType = enterClass("java.io.Serializable"); 470 serializedLambdaType = enterClass("java.lang.invoke.SerializedLambda"); | 413 objectType = enterClass("java.lang.Object"); 414 objectsType = enterClass("java.util.Objects"); 415 classType = enterClass("java.lang.Class"); 416 stringType = enterClass("java.lang.String"); 417 stringBufferType = enterClass("java.lang.StringBuffer"); 418 stringBuilderType = enterClass("java.lang.StringBuilder"); 419 cloneableType = enterClass("java.lang.Cloneable"); 420 throwableType = enterClass("java.lang.Throwable"); 421 serializableType = enterClass("java.io.Serializable"); 422 serializedLambdaType = enterClass("java.lang.invoke.SerializedLambda"); |
423 varHandleType = enterClass("java.lang.invoke.VarHandle"); |
|
471 methodHandleType = enterClass("java.lang.invoke.MethodHandle"); 472 methodHandleLookupType = enterClass("java.lang.invoke.MethodHandles$Lookup"); 473 methodTypeType = enterClass("java.lang.invoke.MethodType"); 474 errorType = enterClass("java.lang.Error"); 475 illegalArgumentExceptionType = enterClass("java.lang.IllegalArgumentException"); 476 interruptedExceptionType = enterClass("java.lang.InterruptedException"); 477 exceptionType = enterClass("java.lang.Exception"); 478 runtimeExceptionType = enterClass("java.lang.RuntimeException"); 479 classNotFoundExceptionType = enterClass("java.lang.ClassNotFoundException"); 480 noClassDefFoundErrorType = enterClass("java.lang.NoClassDefFoundError"); 481 noSuchFieldErrorType = enterClass("java.lang.NoSuchFieldError"); 482 assertionErrorType = enterClass("java.lang.AssertionError"); 483 cloneNotSupportedExceptionType = enterClass("java.lang.CloneNotSupportedException"); 484 annotationType = enterClass("java.lang.annotation.Annotation"); 485 classLoaderType = enterClass("java.lang.ClassLoader"); | 424 methodHandleType = enterClass("java.lang.invoke.MethodHandle"); 425 methodHandleLookupType = enterClass("java.lang.invoke.MethodHandles$Lookup"); 426 methodTypeType = enterClass("java.lang.invoke.MethodType"); 427 errorType = enterClass("java.lang.Error"); 428 illegalArgumentExceptionType = enterClass("java.lang.IllegalArgumentException"); 429 interruptedExceptionType = enterClass("java.lang.InterruptedException"); 430 exceptionType = enterClass("java.lang.Exception"); 431 runtimeExceptionType = enterClass("java.lang.RuntimeException"); 432 classNotFoundExceptionType = enterClass("java.lang.ClassNotFoundException"); 433 noClassDefFoundErrorType = enterClass("java.lang.NoClassDefFoundError"); 434 noSuchFieldErrorType = enterClass("java.lang.NoSuchFieldError"); 435 assertionErrorType = enterClass("java.lang.AssertionError"); 436 cloneNotSupportedExceptionType = enterClass("java.lang.CloneNotSupportedException"); 437 annotationType = enterClass("java.lang.annotation.Annotation"); 438 classLoaderType = enterClass("java.lang.ClassLoader"); |
486 enumSym = enterClass(java_base, names.java_lang_Enum); | 439 enumSym = enterClass(names.java_lang_Enum); |
487 enumFinalFinalize = 488 new MethodSymbol(PROTECTED|FINAL|HYPOTHETICAL, 489 names.finalize, 490 new MethodType(List.<Type>nil(), voidType, 491 List.<Type>nil(), methodClass), 492 enumSym); 493 listType = enterClass("java.util.List"); 494 collectionsType = enterClass("java.util.Collections"); --- 61 unchanged lines hidden (view full) --- 556 arrayClass.members().enter(lengthVar); 557 arrayCloneMethod = new MethodSymbol( 558 PUBLIC, 559 names.clone, 560 new MethodType(List.<Type>nil(), objectType, 561 List.<Type>nil(), methodClass), 562 arrayClass); 563 arrayClass.members().enter(arrayCloneMethod); | 440 enumFinalFinalize = 441 new MethodSymbol(PROTECTED|FINAL|HYPOTHETICAL, 442 names.finalize, 443 new MethodType(List.<Type>nil(), voidType, 444 List.<Type>nil(), methodClass), 445 enumSym); 446 listType = enterClass("java.util.List"); 447 collectionsType = enterClass("java.util.Collections"); --- 61 unchanged lines hidden (view full) --- 509 arrayClass.members().enter(lengthVar); 510 arrayCloneMethod = new MethodSymbol( 511 PUBLIC, 512 names.clone, 513 new MethodType(List.<Type>nil(), objectType, 514 List.<Type>nil(), methodClass), 515 arrayClass); 516 arrayClass.members().enter(arrayCloneMethod); |
564 565 if (java_base != noModule) 566 java_base.completer = sym -> moduleCompleter.complete(sym); //bootstrap issues 567 | |
568 } 569 570 /** Define a new class given its name and owner. 571 */ 572 public ClassSymbol defineClass(Name name, Symbol owner) { 573 ClassSymbol c = new ClassSymbol(0, name, owner); | 517 } 518 519 /** Define a new class given its name and owner. 520 */ 521 public ClassSymbol defineClass(Name name, Symbol owner) { 522 ClassSymbol c = new ClassSymbol(0, name, owner); |
523 if (owner.kind == PCK) 524 Assert.checkNull(classes.get(c.flatname), c); |
|
574 c.completer = initialCompleter; 575 return c; 576 } 577 578 /** Create a new toplevel or member class symbol with given name 579 * and owner and enter in `classes' unless already there. 580 */ | 525 c.completer = initialCompleter; 526 return c; 527 } 528 529 /** Create a new toplevel or member class symbol with given name 530 * and owner and enter in `classes' unless already there. 531 */ |
581 public ClassSymbol enterClass(ModuleSymbol msym, Name name, TypeSymbol owner) { 582 Assert.checkNonNull(msym); | 532 public ClassSymbol enterClass(Name name, TypeSymbol owner) { |
583 Name flatname = TypeSymbol.formFlatName(name, owner); | 533 Name flatname = TypeSymbol.formFlatName(name, owner); |
584 ClassSymbol c = getClass(msym, flatname); | 534 ClassSymbol c = classes.get(flatname); |
585 if (c == null) { 586 c = defineClass(name, owner); | 535 if (c == null) { 536 c = defineClass(name, owner); |
587 doEnterClass(msym, c); | 537 classes.put(flatname, c); |
588 } else if ((c.name != name || c.owner != owner) && owner.kind == TYP && c.owner.kind == PCK) { 589 // reassign fields of classes that might have been loaded with 590 // their flat names. 591 c.owner.members().remove(c); 592 c.name = name; 593 c.owner = owner; 594 c.fullname = ClassSymbol.formFullName(name, owner); 595 } 596 return c; 597 } 598 | 538 } else if ((c.name != name || c.owner != owner) && owner.kind == TYP && c.owner.kind == PCK) { 539 // reassign fields of classes that might have been loaded with 540 // their flat names. 541 c.owner.members().remove(c); 542 c.name = name; 543 c.owner = owner; 544 c.fullname = ClassSymbol.formFullName(name, owner); 545 } 546 return c; 547 } 548 |
599 public ClassSymbol getClass(ModuleSymbol msym, Name flatName) { 600 Assert.checkNonNull(msym, () -> flatName.toString()); 601 return classes.getOrDefault(flatName, Collections.emptyMap()).get(msym); 602 } 603 604 public PackageSymbol lookupPackage(ModuleSymbol msym, Name flatName) { 605 Assert.checkNonNull(msym); 606 607 if (flatName.isEmpty()) { 608 //unnamed packages only from the current module - visiblePackages contains *root* package, not unnamed package! 609 return msym.unnamedPackage; | 549 /** 550 * Creates a new toplevel class symbol with given flat name and 551 * given class (or source) file. 552 * 553 * @param flatName a fully qualified binary class name 554 * @param classFile the class file or compilation unit defining 555 * the class (may be {@code null}) 556 * @return a newly created class symbol 557 * @throws AssertionError if the class symbol already exists 558 */ 559 public ClassSymbol enterClass(Name flatName, JavaFileObject classFile) { 560 ClassSymbol cs = classes.get(flatName); 561 if (cs != null) { 562 String msg = Log.format("%s: completer = %s; class file = %s; source file = %s", 563 cs.fullname, 564 cs.completer, 565 cs.classfile, 566 cs.sourcefile); 567 throw new AssertionError(msg); |
610 } | 568 } |
611 612 if (msym == noModule) { 613 return enterPackage(msym, flatName); 614 } 615 616 msym.complete(); 617 618 PackageSymbol pack; 619 620 pack = msym.visiblePackages.get(flatName); 621 622 if (pack != null) 623 return pack; 624 625 pack = getPackage(msym, flatName); 626 627 if (pack != null && pack.exists()) 628 return pack; 629 630 boolean dependsOnUnnamed = msym.requires != null && 631 msym.requires.stream() 632 .map(rd -> rd.module) 633 .anyMatch(mod -> mod == unnamedModule); 634 635 if (dependsOnUnnamed) { 636 //msyms depends on the unnamed module, for which we generally don't know 637 //the list of packages it "exports" ahead of time. So try to lookup the package in the 638 //current module, and in the unnamed module and see if it exists in one of them 639 PackageSymbol unnamedPack = getPackage(unnamedModule, flatName); 640 641 if (unnamedPack != null && unnamedPack.exists()) { 642 msym.visiblePackages.put(unnamedPack.fullname, unnamedPack); 643 return unnamedPack; 644 } 645 646 pack = enterPackage(msym, flatName); 647 pack.complete(); 648 if (pack.exists()) 649 return pack; 650 651 unnamedPack = enterPackage(unnamedModule, flatName); 652 unnamedPack.complete(); 653 if (unnamedPack.exists()) { 654 msym.visiblePackages.put(unnamedPack.fullname, unnamedPack); 655 return unnamedPack; 656 } 657 658 return pack; 659 } 660 661 return enterPackage(msym, flatName); | 569 Name packageName = Convert.packagePart(flatName); 570 PackageSymbol owner = packageName.isEmpty() 571 ? unnamedPackage 572 : enterPackage(packageName); 573 cs = defineClass(Convert.shortName(flatName), owner); 574 cs.classfile = classFile; 575 classes.put(flatName, cs); 576 return cs; |
662 } 663 | 577 } 578 |
664 private static final Map<ModuleSymbol, ClassSymbol> EMPTY = new HashMap<>(); 665 666 public void removeClass(ModuleSymbol msym, Name flatName) { 667 classes.getOrDefault(flatName, EMPTY).remove(msym); 668 } 669 670 public Iterable<ClassSymbol> getAllClasses() { 671 return () -> Iterators.createCompoundIterator(classes.values(), v -> v.values().iterator()); 672 } 673 674 private void doEnterClass(ModuleSymbol msym, ClassSymbol cs) { 675 classes.computeIfAbsent(cs.flatname, n -> new HashMap<>()).put(msym, cs); 676 } 677 | |
678 /** Create a new member or toplevel class symbol with given flat name 679 * and enter in `classes' unless already there. 680 */ | 579 /** Create a new member or toplevel class symbol with given flat name 580 * and enter in `classes' unless already there. 581 */ |
681 public ClassSymbol enterClass(ModuleSymbol msym, Name flatname) { 682 Assert.checkNonNull(msym); 683 PackageSymbol ps = lookupPackage(msym, Convert.packagePart(flatname)); 684 Assert.checkNonNull(ps); 685 Assert.checkNonNull(ps.modle); 686 ClassSymbol c = getClass(ps.modle, flatname); 687 if (c == null) { 688 c = defineClass(Convert.shortName(flatname), ps); 689 doEnterClass(ps.modle, c); | 582 public ClassSymbol enterClass(Name flatname) { 583 ClassSymbol c = classes.get(flatname); 584 if (c == null) 585 return enterClass(flatname, (JavaFileObject)null); 586 else |
690 return c; | 587 return c; |
691 } else 692 return c; | |
693 } 694 695 /** Check to see if a package exists, given its fully qualified name. 696 */ | 588 } 589 590 /** Check to see if a package exists, given its fully qualified name. 591 */ |
697 public boolean packageExists(ModuleSymbol msym, Name fullname) { 698 Assert.checkNonNull(msym); 699 return enterPackage(msym, fullname).exists(); | 592 public boolean packageExists(Name fullname) { 593 return enterPackage(fullname).exists(); |
700 } 701 702 /** Make a package, given its fully qualified name. 703 */ | 594 } 595 596 /** Make a package, given its fully qualified name. 597 */ |
704 public PackageSymbol enterPackage(ModuleSymbol currModule, Name fullname) { 705 Assert.checkNonNull(currModule); 706 PackageSymbol p = getPackage(currModule, fullname); | 598 public PackageSymbol enterPackage(Name fullname) { 599 PackageSymbol p = packages.get(fullname); |
707 if (p == null) { | 600 if (p == null) { |
708 Assert.check(!fullname.isEmpty(), () -> "rootPackage missing!; currModule: " + currModule); | 601 Assert.check(!fullname.isEmpty(), "rootPackage missing!"); |
709 p = new PackageSymbol( | 602 p = new PackageSymbol( |
710 Convert.shortName(fullname), 711 enterPackage(currModule, Convert.packagePart(fullname))); | 603 Convert.shortName(fullname), 604 enterPackage(Convert.packagePart(fullname))); |
712 p.completer = initialCompleter; | 605 p.completer = initialCompleter; |
713 p.modle = currModule; 714 doEnterPackage(currModule, p); | 606 packages.put(fullname, p); |
715 } 716 return p; 717 } 718 | 607 } 608 return p; 609 } 610 |
719 private void doEnterPackage(ModuleSymbol msym, PackageSymbol pack) { 720 packages.computeIfAbsent(pack.fullname, n -> new HashMap<>()).put(msym, pack); 721 msym.enclosedPackages = msym.enclosedPackages.prepend(pack); | 611 /** Make a package, given its unqualified name and enclosing package. 612 */ 613 public PackageSymbol enterPackage(Name name, PackageSymbol owner) { 614 return enterPackage(TypeSymbol.formFullName(name, owner)); |
722 } | 615 } |
723 724 private void addRootPackageFor(ModuleSymbol module) { 725 doEnterPackage(module, rootPackage); 726 PackageSymbol unnamedPackage = new PackageSymbol(names.empty, rootPackage) { 727 @Override 728 public String toString() { 729 return messages.getLocalizedString("compiler.misc.unnamed.package"); 730 } 731 }; 732 unnamedPackage.modle = module; 733 unnamedPackage.completer = sym -> initialCompleter.complete(sym); 734 module.unnamedPackage = unnamedPackage; 735 } 736 737 public PackageSymbol getPackage(ModuleSymbol module, Name fullname) { 738 return packages.getOrDefault(fullname, Collections.emptyMap()).get(module); 739 } 740 741 public ModuleSymbol enterModule(Name name) { 742 ModuleSymbol msym = modules.get(name); 743 if (msym == null) { 744 msym = ModuleSymbol.create(name, names.module_info); 745 addRootPackageFor(msym); 746 msym.completer = sym -> moduleCompleter.complete(sym); //bootstrap issues 747 modules.put(name, msym); 748 } 749 return msym; 750 } 751 752 public void enterModule(ModuleSymbol msym, Name name) { 753 Assert.checkNull(modules.get(name)); 754 Assert.checkNull(msym.name); 755 msym.name = name; 756 addRootPackageFor(msym); 757 ClassSymbol info = msym.module_info; 758 info.fullname = msym.name.append('.', names.module_info); 759 info.flatname = info.fullname; 760 modules.put(name, msym); 761 } 762 763 public ModuleSymbol getModule(Name name) { 764 return modules.get(name); 765 } 766 767 //temporary: 768 public ModuleSymbol inferModule(Name packageName) { 769 if (packageName.isEmpty()) 770 return java_base == noModule ? noModule : unnamedModule;//! 771 772 ModuleSymbol msym = null; 773 Map<ModuleSymbol,PackageSymbol> map = packages.get(packageName); 774 if (map == null) 775 return null; 776 for (Map.Entry<ModuleSymbol,PackageSymbol> e: map.entrySet()) { 777 if (!e.getValue().members().isEmpty()) { 778 if (msym == null) { 779 msym = e.getKey(); 780 } else { 781 return null; 782 } 783 } 784 } 785 return msym; 786 } 787 788 public List<ModuleSymbol> listPackageModules(Name packageName) { 789 if (packageName.isEmpty()) 790 return List.nil(); 791 792 List<ModuleSymbol> result = List.nil(); 793 Map<ModuleSymbol,PackageSymbol> map = packages.get(packageName); 794 if (map != null) { 795 for (Map.Entry<ModuleSymbol, PackageSymbol> e: map.entrySet()) { 796 if (!e.getValue().members().isEmpty()) { 797 result = result.prepend(e.getKey()); 798 } 799 } 800 } 801 return result; 802 } 803 804 public Collection<ModuleSymbol> getAllModules() { 805 return modules.values(); 806 } | |
807} | 616} |