use of com.redhat.ceylon.model.typechecker.model.Declaration in project ceylon-compiler by ceylon.
the class ClassDoc method loadMembers.
private void loadMembers() {
constructors = new TreeMap<String, Declaration>();
methods = new TreeMap<String, Function>();
attributes = new TreeMap<String, TypedDeclaration>();
innerInterfaces = new TreeMap<String, Interface>();
innerClasses = new TreeMap<String, Class>();
innerExceptions = new TreeMap<String, Class>();
innerAliases = new TreeMap<String, TypeAlias>();
superClasses = getAncestors(klass);
superInterfaces = getSuperInterfaces(klass);
for (Declaration m : klass.getMembers()) {
if (tool.shouldInclude(m)) {
if (ModelUtil.isConstructor(m)) {
addTo(constructors, m);
} else if (m instanceof Value) {
addTo(attributes, (Value) m);
} else if (m instanceof Function) {
addTo(methods, (Function) m);
} else if (m instanceof Interface) {
addTo(innerInterfaces, (Interface) m);
} else if (m instanceof Class) {
Class c = (Class) m;
if (Util.isThrowable(c)) {
addTo(innerExceptions, c);
} else {
addTo(innerClasses, c);
}
} else if (m instanceof TypeAlias) {
addTo(innerAliases, (TypeAlias) m);
}
}
}
Collections.sort(superInterfaces, ReferenceableComparatorByName.INSTANCE);
loadInheritedMembers(attributeSpecification, superClasses, superclassInheritedMembers);
loadInheritedMembers(methodSpecification, superClasses, superclassInheritedMembers);
loadInheritedMembers(attributeSpecification, superInterfaces, interfaceInheritedMembers);
loadInheritedMembers(methodSpecification, superInterfaces, interfaceInheritedMembers);
}
use of com.redhat.ceylon.model.typechecker.model.Declaration in project ceylon-compiler by ceylon.
the class AbstractTransformer method canOptimiseReifiedTypeTest.
/**
* Determine whether we can use a plain {@code instanceof} instead of
* a full {@code Util.isReified()} for a {@code is} test
*/
private boolean canOptimiseReifiedTypeTest(Type type) {
if (isJavaArray(type)) {
if (isJavaObjectArray(type)) {
MultidimensionalArray multiArray = getMultiDimensionalArrayInfo(type);
// we can test, even if not fully reified in Java
return multiArray.type.getDeclaration() instanceof ClassOrInterface;
} else {
// primitive array we can test
return true;
}
}
// we can optimise it if we've got a ClassOrInterface with only Anything type parameters
if (type.getDeclaration() instanceof ClassOrInterface == false)
return false;
for (Entry<TypeParameter, Type> entry : type.getTypeArguments().entrySet()) {
TypeParameter tp = entry.getKey();
if (!type.isCovariant(tp)) {
return false;
}
java.util.List<Type> bounds = tp.getSatisfiedTypes();
Type ta = entry.getValue();
if ((bounds == null || bounds.isEmpty()) && !isAnything(ta)) {
return false;
}
for (Type bound : bounds) {
if (!ta.isSupertypeOf(bound)) {
return false;
}
}
}
// they're all Anything (or supertypes of their upper bound) we can optimise, unless we have a container with type arguments
Type qualifyingType = type.getQualifyingType();
if (qualifyingType == null && // ignore qualifying types of static java declarations
(Decl.isCeylon(type.getDeclaration()) || !type.getDeclaration().isStaticallyImportable())) {
Declaration declaration = type.getDeclaration();
do {
// it may be contained in a function or value, and we want its type
Declaration enclosingDeclaration = getDeclarationContainer(declaration);
if (enclosingDeclaration instanceof TypedDeclaration) {
// must be in scope
if (enclosingDeclaration instanceof Generic && !((Generic) enclosingDeclaration).getTypeParameters().isEmpty())
return false;
// look up the containers
declaration = enclosingDeclaration;
} else if (enclosingDeclaration instanceof TypeDeclaration) {
// we can't optimise if that container has type arguments as they are not provided
if (enclosingDeclaration instanceof Generic && !((Generic) enclosingDeclaration).getTypeParameters().isEmpty())
return false;
// look up the containers
declaration = enclosingDeclaration;
} else {
// that's fucked up
break;
}
// go up every containing typed declaration
} while (declaration != null);
// we can optimise!
return true;
} else if (qualifyingType != null) {
// we can only optimise if the qualifying type can also be optimised
return canOptimiseReifiedTypeTest(qualifyingType);
} else {
// we can optimise!
return true;
}
}
use of com.redhat.ceylon.model.typechecker.model.Declaration in project ceylon-compiler by ceylon.
the class AnnotationModelVisitor method visit.
@Override
public void visit(Tree.BaseMemberExpression bme) {
if (annotationConstructor != null) {
Declaration declaration = bme.getDeclaration();
if (checkingInvocationPrimary && isAnnotationConstructor(bme.getDeclaration())) {
Function ctor = (Function) bme.getDeclaration();
instantiation.setPrimary(ctor);
if (ctor.getAnnotationConstructor() != null) {
instantiation.getConstructorParameters().addAll(((AnnotationInvocation) ctor.getAnnotationConstructor()).getConstructorParameters());
}
} else if (checkingArguments || checkingDefaults) {
if (declaration instanceof Value && ((Value) declaration).isParameter()) {
Value constructorParameter = (Value) declaration;
ParameterAnnotationTerm a = new ParameterAnnotationTerm();
a.setSpread(spread);
// XXX Is this right?
a.setSourceParameter(constructorParameter.getInitializerParameter());
this.term = a;
} else if (isBooleanTrue(declaration)) {
LiteralAnnotationTerm argument = new BooleanLiteralAnnotationTerm(true);
appendLiteralArgument(bme, argument);
} else if (isBooleanFalse(declaration)) {
LiteralAnnotationTerm argument = new BooleanLiteralAnnotationTerm(false);
appendLiteralArgument(bme, argument);
} else if (bme.getUnit().isEmptyType(bme.getTypeModel()) && bme.getUnit().isIterableType(bme.getTypeModel()) && elements == null) {
// If we're dealing with an iterable, empty means empty collection, not object
endCollection(startCollection(bme), bme);
} else if (Decl.isAnonCaseOfEnumeratedType(bme)) {
LiteralAnnotationTerm argument = new ObjectLiteralAnnotationTerm(bme.getTypeModel());
appendLiteralArgument(bme, argument);
} else {
bme.addError("compiler bug: unsupported base member expression in annotation constructor", Backend.Java);
}
} else {
bme.addError("compiler bug: unsupported base member expression in annotation constructor", Backend.Java);
}
}
}
use of com.redhat.ceylon.model.typechecker.model.Declaration in project ceylon-compiler by ceylon.
the class AnnotationModelVisitor method defaultedParameter.
public void defaultedParameter(Tree.SpecifierOrInitializerExpression d) {
if (annotationConstructor != null) {
AnnotationConstructorParameter annotationConstructorParameter = instantiation.getConstructorParameters().get(instantiation.getConstructorParameters().size() - 1);
Declaration t = d.getUnit().getTrueValueDeclaration();
Declaration f = d.getUnit().getFalseValueDeclaration();
Term term = d.getExpression().getTerm();
if (term instanceof Tree.InvocationExpression) {
Tree.Primary primary = ((Tree.InvocationExpression) term).getPrimary();
if (primary instanceof Tree.BaseMemberOrTypeExpression && (isAnnotationConstructor(((Tree.BaseMemberOrTypeExpression) primary).getDeclaration()) || isAnnotationClass(((Tree.BaseMemberOrTypeExpression) primary).getDeclaration()))) {
final AnnotationInvocation prevInstantiation = this.instantiation;
this.instantiation = new AnnotationInvocation();
if (isAnnotationConstructor(((Tree.BaseMemberOrTypeExpression) primary).getDeclaration())) {
Function constructor = (Function) ((Tree.BaseMemberOrTypeExpression) primary).getDeclaration();
instantiation.setConstructorDeclaration(constructor);
instantiation.getConstructorParameters().addAll(((AnnotationInvocation) constructor.getAnnotationConstructor()).getConstructorParameters());
}
checkingDefaults = true;
super.visit(d);
annotationConstructorParameter.setDefaultArgument(this.term);
this.term = null;
checkingDefaults = false;
this.instantiation = prevInstantiation;
} else {
errorDefaultedParameter(d);
}
} else if (term instanceof Tree.Literal || (term instanceof Tree.NegativeOp && ((Tree.NegativeOp) term).getTerm() instanceof Tree.Literal) || (term instanceof Tree.BaseMemberExpression && (((Tree.BaseMemberExpression) term).getDeclaration().equals(t) || ((Tree.BaseMemberExpression) term).getDeclaration().equals(f) || ((Tree.BaseMemberExpression) term).getDeclaration().isParameter() || Decl.isAnonCaseOfEnumeratedType((Tree.BaseMemberExpression) term)))) {
checkingDefaults = true;
super.visit(d);
annotationConstructorParameter.setDefaultArgument(this.term);
this.term = null;
checkingDefaults = false;
} else if (term instanceof Tree.Tuple || term instanceof Tree.SequenceEnumeration) {
// TODO Tuples and SequenceEnumerations of the above cases should also be allowed
checkingDefaults = true;
super.visit(d);
annotationConstructorParameter.setDefaultArgument(this.term);
this.term = null;
checkingDefaults = false;
} else {
errorDefaultedParameter(d);
}
}
}
use of com.redhat.ceylon.model.typechecker.model.Declaration in project ceylon-compiler by ceylon.
the class ExpressionTransformer method appendDeclarationLiteralForAnnotation.
/**
* Appends into the given builder a String representation of the given
* declaration, suitable for parsing my the DeclarationParser.
*/
private static void appendDeclarationLiteralForAnnotation(Declaration decl, StringBuilder sb) {
Scope container = decl.getContainer();
while (true) {
if (container instanceof Declaration) {
appendDeclarationLiteralForAnnotation((Declaration) container, sb);
sb.append(".");
break;
} else if (container instanceof Package) {
appendDeclarationLiteralForAnnotation((Package) container, sb);
sb.append(":");
break;
}
container = container.getContainer();
}
if (decl instanceof Class) {
sb.append("C").append(decl.getName());
} else if (decl instanceof Interface) {
sb.append("I").append(decl.getName());
} else if (decl instanceof TypeAlias) {
sb.append("A").append(decl.getName());
} else if (decl instanceof Value) {
sb.append("V").append(decl.getName());
} else if (decl instanceof Function) {
sb.append("F").append(decl.getName());
} else if (decl instanceof TypeParameter) {
sb.append("P").append(decl.getName());
} else if (decl instanceof Constructor) {
sb.append("c").append(decl.getName());
} else {
throw BugException.unhandledDeclarationCase(decl);
}
}
Aggregations