use of org.eclipse.ceylon.model.typechecker.model.TypeAlias in project ceylon by eclipse.
the class CeylonVisitor method visit.
/*
* Compilation Unit
*/
public void visit(Tree.TypeAliasDeclaration decl) {
TransformationPlan plan = gen.errors().hasDeclarationAndMarkBrokenness(decl);
if (plan instanceof Drop) {
return;
}
int annots = gen.checkCompilerAnnotations(decl, defs);
TypeAlias dec = decl.getDeclarationModel();
if (dec.isClassOrInterfaceMember()) {
if (dec.isInterfaceMember()) {
classBuilder.getCompanionBuilder((Interface) dec.getContainer()).defs(gen.classGen().transform(decl));
} else {
classBuilder.defs(gen.classGen().transform(decl));
}
} else {
appendList(gen.classGen().transform(decl));
}
gen.resetCompilerAnnotations(annots);
}
use of org.eclipse.ceylon.model.typechecker.model.TypeAlias in project ceylon by eclipse.
the class Naming method appendTypeDeclaration.
private void appendTypeDeclaration(final TypeDeclaration decl, EnumSet<DeclNameFlag> flags, TypeDeclarationBuilder<?> typeDeclarationBuilder, Scope scope, final boolean last) {
if (scope instanceof Class || scope instanceof TypeAlias || (scope instanceof Constructor && (scope.equals(decl) || !ModelUtil.isLocalNotInitializerScope(scope)))) {
TypeDeclaration klass = (TypeDeclaration) scope;
if (klass.isAnonymous() && !klass.isNamed())
typeDeclarationBuilder.clear();
String className = "";
if (klass.getName() != null) {
if (ModelUtil.isCeylonDeclaration(klass))
className = escapeClassName(klass.getName());
else
className = getRealName(klass, NA_WRAPPER_UNQUOTED);
}
typeDeclarationBuilder.append(className);
if (ModelUtil.isCeylonDeclaration(klass)) {
if (flags.contains(DeclNameFlag.COMPANION) && ModelUtil.isLocalNotInitializer(klass) && last) {
typeDeclarationBuilder.append(IMPL_POSTFIX);
} else if (flags.contains(DeclNameFlag.ANNOTATION) && last) {
typeDeclarationBuilder.append(ANNO_POSTFIX);
} else if (flags.contains(DeclNameFlag.ANNOTATIONS) && last) {
typeDeclarationBuilder.append(ANNOS_POSTFIX);
} else if (flags.contains(DeclNameFlag.DELEGATION) && last) {
typeDeclarationBuilder.append(DELEGATION_POSTFIX);
}
}
} else if (scope instanceof Interface) {
Interface iface = (Interface) scope;
String className = "";
if (iface.getName() != null) {
if (ModelUtil.isCeylonDeclaration(iface))
className = iface.getName();
else
className = getRealName(iface, NA_WRAPPER_UNQUOTED);
}
typeDeclarationBuilder.append(className);
if (ModelUtil.isCeylonDeclaration(iface) && ((decl instanceof Class || decl instanceof Constructor || decl instanceof TypeAlias || scope instanceof Constructor) || flags.contains(DeclNameFlag.COMPANION))) {
typeDeclarationBuilder.append(IMPL_POSTFIX);
}
} else if (ModelUtil.isLocalNotInitializerScope(scope)) {
if (flags.contains(DeclNameFlag.COMPANION) || !(decl instanceof Interface)) {
typeDeclarationBuilder.clear();
} else if (flags.contains(DeclNameFlag.QUALIFIED) || (decl instanceof Interface)) {
Scope nonLocal = scope;
while (!(nonLocal instanceof Declaration)) {
nonLocal = nonLocal.getContainer();
}
typeDeclarationBuilder.append(((Declaration) nonLocal).getPrefixedName());
if (!Decl.equalScopes(scope, nonLocal)) {
typeDeclarationBuilder.append('$');
typeDeclarationBuilder.append(getLocalId(scope));
}
if (decl instanceof Interface) {
typeDeclarationBuilder.append('$');
} else {
if (flags.contains(DeclNameFlag.QUALIFIED)) {
typeDeclarationBuilder.selectAppended();
} else {
typeDeclarationBuilder.clear();
}
}
}
return;
} else if (scope instanceof TypedDeclaration && ((Declaration) scope).isToplevel()) {
// nothing? that's just weird
}
if (!last) {
if (decl instanceof Interface && ModelUtil.isCeylonDeclaration((TypeDeclaration) decl) && !flags.contains(DeclNameFlag.COMPANION)) {
typeDeclarationBuilder.append('$');
} else if (decl instanceof Constructor && ((Class) decl.getContainer()).isMember() && decl.getContainer().equals(scope)) {
typeDeclarationBuilder.append('$');
} else {
if (flags.contains(DeclNameFlag.QUALIFIED)) {
typeDeclarationBuilder.selectAppended();
} else {
typeDeclarationBuilder.clear();
}
}
} else {
typeDeclarationBuilder.selectAppended();
}
return;
}
use of org.eclipse.ceylon.model.typechecker.model.TypeAlias in project ceylon by eclipse.
the class JsonPackage method loadTypeAlias.
/**
* Load a type alias, creating it if necessary.
* @return The TypeAlias declaration.
*/
@SuppressWarnings("unchecked")
private TypeAlias loadTypeAlias(String name, Map<String, Object> m, Scope parent, List<TypeParameter> existing) {
TypeAlias alias;
if (m.get(KEY_METATYPE) instanceof TypeAlias) {
// It's been loaded already
alias = (TypeAlias) m.get(KEY_METATYPE);
if (m.size() == 1) {
return alias;
}
} else {
Declaration maybe = parent.getDirectMember(name, null, false);
if (maybe == null) {
alias = new TypeAlias();
alias.setContainer(parent);
alias.setScope(parent);
alias.setName(name);
alias.setUnit(u2);
setAnnotations(alias, (Integer) m.remove(KEY_PACKED_ANNS), m.remove(KEY_ANNOTATIONS));
m.put(KEY_METATYPE, alias);
} else if (maybe instanceof TypeAlias) {
alias = (TypeAlias) maybe;
} else {
throw new IllegalStateException(maybe + " should be an TypeAlias");
}
}
// Gather available type parameters
List<Map<String, Object>> listOfMaps = (List<Map<String, Object>>) m.get(KEY_TYPE_PARAMS);
final List<TypeParameter> tparms;
if (listOfMaps != null && alias.getTypeParameters().size() < listOfMaps.size()) {
tparms = parseTypeParameters(listOfMaps, alias, existing);
alias.setTypeParameters(tparms);
} else {
tparms = alias.getTypeParameters();
}
final List<TypeParameter> allparms = JsonPackage.merge(tparms, existing);
// All interfaces extend Object, except aliases
if (alias.getExtendedType() == null) {
alias.setExtendedType(getTypeFromJson((Map<String, Object>) m.get("$alias"), parent instanceof Declaration ? (Declaration) parent : null, allparms));
}
if (m.containsKey(KEY_SELF_TYPE)) {
for (TypeParameter _tp : tparms) {
if (_tp.getName().equals(m.get(KEY_SELF_TYPE))) {
alias.setSelfType(_tp.getType());
}
}
}
if (m.containsKey("of")) {
alias.setCaseTypes(parseTypeList((List<Map<String, Object>>) m.remove("of"), allparms));
}
if (m.containsKey(KEY_SATISFIES)) {
List<Map<String, Object>> stypes = (List<Map<String, Object>>) m.remove(KEY_SATISFIES);
alias.setSatisfiedTypes(parseTypeList(stypes, allparms));
}
m.clear();
m.put(KEY_METATYPE, alias);
if (parent == this) {
u2.addDeclaration(alias);
}
parent.addMember(alias);
return alias;
}
use of org.eclipse.ceylon.model.typechecker.model.TypeAlias in project ceylon by eclipse.
the class TypeUtils method generateModelPath.
/**
* Returns the list of keys to get from the package to the declaration, in the model.
*/
public static List<String> generateModelPath(final Declaration d) {
final ArrayList<String> sb = new ArrayList<>();
final Package pkg = d.getUnit().getPackage();
sb.add(pkg.isLanguagePackage() ? "$" : pkg.getNameAsString());
if (d.isToplevel()) {
sb.add(d.getName());
if (d instanceof Setter) {
sb.add("$set");
}
} else {
Declaration p = d;
final int i = sb.size();
while (p instanceof Declaration) {
if (p instanceof Setter) {
sb.add(i, "$set");
}
final String mname = TypeUtils.modelName(p);
if (!(mname.startsWith("anon$") || mname.startsWith("anonymous#"))) {
sb.add(i, mname);
// Build the path in reverse
if (!p.isToplevel()) {
if (p instanceof Class) {
sb.add(i, p.isAnonymous() ? MetamodelGenerator.KEY_OBJECTS : MetamodelGenerator.KEY_CLASSES);
} else if (p instanceof org.eclipse.ceylon.model.typechecker.model.Interface) {
sb.add(i, MetamodelGenerator.KEY_INTERFACES);
} else if (p instanceof Function) {
if (!p.isAnonymous()) {
sb.add(i, MetamodelGenerator.KEY_METHODS);
}
} else if (p instanceof TypeAlias || p instanceof Setter) {
sb.add(i, MetamodelGenerator.KEY_ATTRIBUTES);
} else if (p instanceof Constructor || ModelUtil.isConstructor(p)) {
sb.add(i, MetamodelGenerator.KEY_CONSTRUCTORS);
} else {
// It's a value
TypeDeclaration td = ((TypedDeclaration) p).getTypeDeclaration();
sb.add(i, (td != null && td.isAnonymous()) ? MetamodelGenerator.KEY_OBJECTS : MetamodelGenerator.KEY_ATTRIBUTES);
}
}
}
p = ModelUtil.getContainingDeclaration(p);
while (p != null && p instanceof ClassOrInterface == false && !(p.isToplevel() || p.isAnonymous() || p.isClassOrInterfaceMember() || p.isJsCaptured())) {
p = ModelUtil.getContainingDeclaration(p);
}
}
}
return sb;
}
use of org.eclipse.ceylon.model.typechecker.model.TypeAlias in project ceylon by eclipse.
the class JsIdentifierNames method getName.
private String getName(Declaration decl, boolean forGetterSetter, boolean priv) {
if (decl == null) {
return null;
}
String name = decl.getName();
if (name == null && ModelUtil.isConstructor(decl)) {
return "$c$";
}
if (name.startsWith("anonymous#")) {
name = "anon$" + name.substring(10);
}
if (decl.isDynamic()) {
return JsUtils.escapeStringLiteral(decl.getName());
}
boolean nonLocal = !priv;
if (nonLocal) {
// check if it's a shared member or a toplevel function
nonLocal = decl.isMember() ? decl.isShared() || decl instanceof TypeDeclaration : decl.isToplevel() && (forGetterSetter || decl instanceof Function || decl instanceof ClassOrInterface || decl instanceof TypeAlias);
}
if (nonLocal && decl instanceof Class && ((Class) decl).isAnonymous() && !forGetterSetter) {
// A lower-case class name belongs to an object and is not public.
nonLocal = false;
}
if (nonLocal) {
// The identifier might be accessed from other .js files, so it must
// be reliably reproducible. In most cases simply using the original
// name is ok because otherwise it would result in a name collision in
// Ceylon too. We just have to take care of a few exceptions:
String suffix = nestingSuffix(decl, false);
if (suffix.length() > 0) {
// nested type
name += suffix;
} else if ((!forGetterSetter && !ModelUtil.isConstructor(decl) && reservedWords.contains(name)) || isJsGlobal(decl)) {
// JavaScript keyword or global declaration
name = "$_" + name;
}
} else {
// The identifier will not be used outside the generated .js file,
// so we can simply disambiguate it with a numeric ID.
name = uniquePrivateName(decl, priv);
}
// Fix #204 - same top-level declarations in different packages
final Package declPkg = decl.getUnit().getPackage();
if (decl.isToplevel() && !declPkg.equals(declPkg.getModule().getRootPackage())) {
final Package raiz = declPkg.getModule().getRootPackage();
// rootPackage can be null when compiling from IDE
String rootName = raiz == null ? (declPkg.getModule().isDefaultModule() ? "" : declPkg.getModule().getNameAsString()) : raiz.getNameAsString();
String pkgName = declPkg.getNameAsString();
rootName = pkgName.substring(rootName.length()).replaceAll("\\.", "\\$");
if (rootName.length() > 0 && rootName.charAt(0) != '$') {
rootName = '$' + rootName;
}
name += rootName;
}
if (decl instanceof TypeAlias) {
name += "()";
}
return JsUtils.escapeStringLiteral(name);
}
Aggregations