use of com.redhat.ceylon.model.typechecker.model.Package in project ceylon-compiler by ceylon.
the class ExpressionTransformer method makeTopLevelValueOrFunctionDeclarationLiteral.
private JCExpression makeTopLevelValueOrFunctionDeclarationLiteral(Declaration declaration) {
// toplevel method or attribute: we need to fetch them from their module/package
Package pkg = Decl.getPackageContainer(declaration.getContainer());
// get the package
JCExpression packageCall = makePackageLiteralCall(pkg);
// now get the toplevel
String getter = Decl.isMethod(declaration) ? "getFunction" : "getValue";
JCExpression toplevelCall = make().Apply(null, makeSelect(packageCall, getter), List.<JCExpression>of(ceylonLiteral(declaration.getName())));
return toplevelCall;
}
use of com.redhat.ceylon.model.typechecker.model.Package in project ceylon-compiler by ceylon.
the class CodegenUtil method getJavaNameOfDeclaration.
/**
* <p>Returns the fully qualified name java name of the given declaration,
* for example
* {@code ceylon.language.sum_.sum} or {@code my.package.Outer.Inner}.
* for any toplevel or externally visible Ceylon declaration.</p>
*
* <p>Used by the IDE to support finding/renaming Ceylon declarations
* called from Java.</p>
*/
public static String getJavaNameOfDeclaration(Declaration decl) {
Scope s = decl.getScope();
while (!(s instanceof Package)) {
if (!(s instanceof TypeDeclaration)) {
throw new IllegalArgumentException();
}
s = s.getContainer();
}
String result;
Naming n = new Naming(null, null);
if (decl instanceof TypeDeclaration) {
result = n.makeTypeDeclarationName((TypeDeclaration) decl, DeclNameFlag.QUALIFIED);
// remove initial .
result = result.substring(1);
if (decl.isAnonymous()) {
result += "." + Unfix.get_.toString();
}
} else if (decl instanceof TypedDeclaration) {
if (decl.isToplevel()) {
result = n.getName((TypedDeclaration) decl, Naming.NA_FQ | Naming.NA_WRAPPER | Naming.NA_MEMBER);
// remove initial .
result = result.substring(1);
} else {
Scope container = decl.getContainer();
if (container instanceof TypeDeclaration) {
String qualifier = getJavaNameOfDeclaration((TypeDeclaration) container);
result = qualifier + n.getName((TypedDeclaration) decl, Naming.NA_MEMBER);
} else {
throw new IllegalArgumentException();
}
}
} else {
throw new RuntimeException();
}
return result;
}
use of com.redhat.ceylon.model.typechecker.model.Package in project ceylon-compiler by ceylon.
the class AbstractTransformer method collectQualifyingTypeArguments.
/**
* Collects all the type parameters and arguments required for an interface that's been pulled up to the
* toplevel, including its containing type and method type parameters.
*/
private void collectQualifyingTypeArguments(java.util.List<TypeParameter> qualifyingTypeParameters, Map<TypeParameter, Type> qualifyingTypeArguments, java.util.List<Reference> qualifyingTypes) {
// make sure we only add type parameters with the same name once, as duplicates are erased from the target interface
// since they cannot be accessed
Set<String> names = new HashSet<String>();
// walk the qualifying types backwards to make sure we only add a TP with the same name once and the outer one wins
for (int i = qualifyingTypes.size() - 1; i >= 0; i--) {
Reference qualifiedType = qualifyingTypes.get(i);
Map<TypeParameter, Type> tas = qualifiedType.getTypeArguments();
java.util.List<TypeParameter> tps = ((Generic) qualifiedType.getDeclaration()).getTypeParameters();
// add any type params for this type
if (tps != null) {
int index = 0;
for (TypeParameter tp : tps) {
// add it only once
if (names.add(tp.getName())) {
// start putting all these type parameters at 0 and then in order
// so that outer type params end up before inner type params but
// order is preserved within each type
qualifyingTypeParameters.add(index++, tp);
qualifyingTypeArguments.put(tp, tas.get(tp));
}
}
}
// add any container method TP
Declaration declaration = qualifiedType.getDeclaration();
if (Decl.isLocal(declaration)) {
Scope scope = declaration.getContainer();
// collect every container method until the next type or package
java.util.List<Function> methods = new LinkedList<Function>();
while (scope != null && scope instanceof ClassOrInterface == false && scope instanceof Package == false) {
if (scope instanceof Function) {
methods.add((Function) scope);
}
scope = scope.getContainer();
}
// methods are sorted inner to outer, which is the order we're following here for types
for (Function method : methods) {
java.util.List<TypeParameter> methodTypeParameters = method.getTypeParameters();
if (methodTypeParameters != null) {
int index = 0;
for (TypeParameter tp : methodTypeParameters) {
// add it only once
if (names.add(tp.getName())) {
// start putting all these type parameters at 0 and then in order
// so that outer type params end up before inner type params but
// order is preserved within each type
qualifyingTypeParameters.add(index++, tp);
qualifyingTypeArguments.put(tp, tp.getType());
}
}
}
}
}
}
}
use of com.redhat.ceylon.model.typechecker.model.Package in project ceylon-compiler by ceylon.
the class CeylonClassWriter method writeClass.
@Override
public JavaFileObject writeClass(ClassSymbol c) throws IOException, PoolOverflow, StringOverflow {
String packageName = c.packge().getQualifiedName().toString();
Package pkg = modelLoader.findPackage(packageName);
if (pkg == null)
throw new RuntimeException("Failed to find package: " + packageName);
Module module = pkg.getModule();
fileManager.setModule(module);
return super.writeClass(c);
}
use of com.redhat.ceylon.model.typechecker.model.Package in project ceylon-compiler by ceylon.
the class LanguageCompiler method genCodeUnlessError.
private JavaFileObject genCodeUnlessError(Env<AttrContext> env, JCClassDecl cdef) throws IOException {
CeylonFileObject sourcefile = (CeylonFileObject) env.toplevel.sourcefile;
try {
// do not look at the global number of errors but only those for this file
if (super.gen.genClass(env, cdef)) {
String packageName = cdef.sym.packge().getQualifiedName().toString();
Package pkg = modelLoader.findPackage(packageName);
if (pkg == null)
throw new RuntimeException("Failed to find package: " + packageName);
Module module = pkg.getModule();
if (!module.isDefault()) {
String moduleName = module.getNameAsString();
CeylonFileObject moduleFileObject = moduleNamesToFileObjects.get(moduleName);
// if there's no module source file object it means the module descriptor had parse errors
if (moduleFileObject == null || moduleFileObject.hasError()) {
// we do not produce any class files for modules with errors
if (options.get(OptionName.VERBOSE) != null) {
Log.printLines(log.noticeWriter, "[Not writing class " + cdef.sym.className() + " because its module has errors: " + moduleName + "]");
}
return null;
}
}
return writer.writeClass(cdef.sym);
}
} catch (ClassWriter.PoolOverflow ex) {
log.error(cdef.pos(), "limit.pool");
} catch (ClassWriter.StringOverflow ex) {
log.error(cdef.pos(), "limit.string.overflow", ex.value.substring(0, 20));
} catch (CompletionFailure ex) {
chk.completionError(cdef.pos(), ex);
} catch (AssertionError e) {
throw new RuntimeException("Error generating bytecode for " + sourcefile.getName(), e);
}
return null;
}
Aggregations