next up previous
Next: Class and interface declarations Up: Scope of a simple Previous: Scope of a simple


Imported types

FIXME: This is actually an incorrect rendition of the import policy. A SingleTypeImportDeclaration should act precisely like the declaration of a class or interface in this compilation unit. In order to implement that, however, the compilation unit would have to be a range sequence, with the range elements being the SingleTypeImportDeclarations and the TypeDeclarationsOpt. I'm not certain whether it is possible for a single symbol to inherit both RangeSequence and RangeMulInh, and I don't want to get distracted by that issue (which is irrelevant for type analysis).

The scope of a type imported by a single-type-import declaration or type-import-on-demand declaration is all of the class and interface type declarations in the compilation unit in which the import declaration appears.

Scopes[11]:

TREE SYMBOL CompilationUnit INHERITS TypExportRange COMPUTE
  SYNT.TypScopeKey=NoKey;
END;

RULE: CompilationUnit ::=
        PackageDeclarationOpt
        ImportJavaLang ImportDeclarationsOpt TypeDeclarationsOpt
COMPUTE
  CompilationUnit.TypGotLocKeys=
    ImportJavaLang.TypGotLocKeys
    <- ImportDeclarationsOpt CONSTITUENTS (
      TypeImportOnDemandDeclaration.TypGotLocKeys,
      SingleTypeImportDeclaration.TypGotLocKeys);
END;

TREE SYMBOL ImportJavaLang INHERITS PkgIdUseEnv END;

RULE: ImportJavaLang ::= COMPUTE
  ImportJavaLang.Sym=MakeName("java.lang");
  ImportJavaLang.TypGotLocKeys=
    AddPackage(
      GetPtyScope(ImportJavaLang.PkgKey,NoEnv),
      INCLUDING CompilationUnit.TypEnv);
END;

TREE SYMBOL QualInhName INHERITS PkgIdUseEnv END;

RULE: TypeImportOnDemandDeclaration ::= 'import' QualInhName '.' '*' COMPUTE
  TypeImportOnDemandDeclaration.TypGotLocKeys=
    AddPackage(
      GetPtyScope(QualInhName.PkgKey,NoEnv),
      INCLUDING CompilationUnit.TypEnv);
END;
This macro is defined in definitions 11, 12, 14, 15, 16, 17, 18, 19, and 20.
This macro is invoked in definition 2.

Each of the donating environments must be created from information about the specified packages and types. We must ensure that all of the members of all packages have been defined. Those computations are specific to the context of the donating environment:

Scopes[12]:

TREE SYMBOL Goal COMPUTE
  SYNT.TypEnv = TypRootEnv <- CONSTITUENTS PtyAnyScope.PtyGotLocKeys;
END;

TREE SYMBOL TypeDeclarationsOpt INHERITS TypExportRange COMPUTE
  INH.TypEnv=AddPackage(THIS.PtyEnv,NewScope(INCLUDING TypAnyScope.TypEnv));
END;

RULE: CompilationUnit ::=
        PackageDeclarationOpt
        ImportJavaLang ImportDeclarationsOpt TypeDeclarationsOpt
COMPUTE
  TypeDeclarationsOpt.TypScopeKey=PackageDeclarationOpt.PkgKey;
END;

RULE: SingleTypeImportDeclaration ::= 'import' QualInhName COMPUTE
  SingleTypeImportDeclaration.TypGotLocKeys=
    BindKeyInScope(
      INCLUDING CompilationUnit.TypEnv,
      HeadintList(QualInhName.Ids),
      KeyInScope(TypRootEnv, QualInhName.Sym));
END;
This macro is defined in definitions 11, 12, 14, 15, 16, 17, 18, 19, and 20.
This macro is invoked in definition 2.

FIXME: If the identifier is already bound in the environment, BindKeyInScope returns NoBinding and does not re-bind the identifier. Depending on the particular environment used, that condition might indicate a multiple definition error that should be reported.

FIXME: Depending on the situation, we might want to check modifiers of the type being bound.

Environment AddPackage(Environment pkg, Environment env)[13]:
/* Add the types in a package to an environment
 *   On entry-
 *     pkg=environment of the package
 *     env=environment to which types are to be added
 *   On exit-
 *     All visible types of package sym have been added to env
 ***/
{ Binding bind;

  for (bind = DefinitionsOf(pkg);
       bind != NoBinding;
       bind = NextDefinition(bind)) {
    BindKeyInScope(env, IdnOf(bind), KeyOf(bind));
  }

  return env;
}
This macro is invoked in definition 39.


next up previous
Next: Class and interface declarations Up: Scope of a simple Previous: Scope of a simple
2008-09-11