use of org.eclipse.ceylon.model.typechecker.model.TypeDeclaration in project ceylon by eclipse.
the class DeclarationVisitor method visit.
@Override
public void visit(Tree.ExtendedType that) {
inExtends = true;
super.visit(that);
inExtends = false;
TypeDeclaration td = (TypeDeclaration) that.getScope();
if (!td.isAlias()) {
Tree.SimpleType type = that.getType();
if (type == null) {
that.addError("missing extended type");
} else {
Type et = type.getTypeModel();
if (et != null) {
// we can't check here that it's a
// sensible supertype, because this is
// just a lazy reference that will be
// resolvable later
td.setExtendedType(et);
}
}
}
}
use of org.eclipse.ceylon.model.typechecker.model.TypeDeclaration in project ceylon by eclipse.
the class DeclarationVisitor method visit.
@Override
public void visit(Tree.BaseType that) {
super.visit(that);
final Scope scope = that.getScope();
final String name = name(that.getIdentifier());
if (inExtends) {
final Tree.TypeArgumentList tal = that.getTypeArgumentList();
final boolean packageQualified = that.getPackageQualified();
Type t = new LazyType(unit) {
@Override
public TypeDeclaration initDeclaration() {
return packageQualified ? getPackageTypeDeclaration(name, null, false, unit) : getTypeDeclaration(scope, name, null, false, unit);
}
@Override
public Map<TypeParameter, Type> initTypeArguments() {
TypeDeclaration dec = getDeclaration();
List<TypeParameter> tps = dec.getTypeParameters();
return getTypeArgumentMap(dec, null, AnalyzerUtil.getTypeArguments(tal, null, tps));
}
@Override
public Map<TypeParameter, SiteVariance> initVarianceOverrides() {
TypeDeclaration dec = getDeclaration();
List<TypeParameter> tps = dec.getTypeParameters();
return getVarianceMap(dec, null, AnalyzerUtil.getVariances(tal, tps));
}
};
that.setTypeModel(t);
}
}
use of org.eclipse.ceylon.model.typechecker.model.TypeDeclaration in project ceylon by eclipse.
the class DeclarationVisitor method visit.
@Override
public void visit(Tree.QualifiedType that) {
super.visit(that);
final String name = name(that.getIdentifier());
final Tree.StaticType outerType = that.getOuterType();
if (inExtends) {
final Tree.TypeArgumentList tal = that.getTypeArgumentList();
Type t = new LazyType(unit) {
@Override
public TypeDeclaration initDeclaration() {
if (outerType == null) {
return null;
} else {
TypeDeclaration dec = outerType.getTypeModel().getDeclaration();
return AnalyzerUtil.getTypeMember(dec, name, null, false, unit, scope);
}
}
@Override
public Map<TypeParameter, Type> initTypeArguments() {
if (outerType == null) {
return emptyMap();
} else {
TypeDeclaration dec = getDeclaration();
List<TypeParameter> tps = dec.getTypeParameters();
Type ot = outerType.getTypeModel();
return getTypeArgumentMap(dec, ot, AnalyzerUtil.getTypeArguments(tal, ot, tps));
}
}
@Override
public Map<TypeParameter, SiteVariance> initVarianceOverrides() {
TypeDeclaration dec = getDeclaration();
List<TypeParameter> tps = dec.getTypeParameters();
Type ot = outerType.getTypeModel();
return getVarianceMap(dec, ot, AnalyzerUtil.getVariances(tal, tps));
}
};
that.setTypeModel(t);
}
}
use of org.eclipse.ceylon.model.typechecker.model.TypeDeclaration in project ceylon by eclipse.
the class DeclarationVisitor method visit.
public void visit(Tree.SequenceType that) {
super.visit(that);
if (inExtends) {
final Tree.StaticType elementType = that.getElementType();
final Tree.NaturalLiteral length = that.getLength();
Type t;
if (length == null) {
t = new LazyType(unit) {
@Override
public boolean isUnknown() {
return false;
}
@Override
public TypeDeclaration initDeclaration() {
return unit.getSequentialDeclaration();
}
@Override
public Map<TypeParameter, Type> initTypeArguments() {
List<TypeParameter> stps = unit.getSequentialDeclaration().getTypeParameters();
return singletonMap(stps.get(0), elementType.getTypeModel());
}
};
} else {
final int len;
try {
len = parseInt(length.getText());
} catch (NumberFormatException nfe) {
return;
}
if (len < 1 || len > 1000) {
return;
} else {
t = new StaticLengthSequenceType(elementType, len);
}
}
that.setTypeModel(t);
}
}
use of org.eclipse.ceylon.model.typechecker.model.TypeDeclaration in project ceylon by eclipse.
the class ClassTransformer method transform.
public void transform(Tree.AttributeDeclaration decl, ClassDefinitionBuilder classBuilder) {
final Value model = decl.getDeclarationModel();
boolean withinInterface = model.isInterfaceMember();
Tree.SpecifierOrInitializerExpression initializer = decl.getSpecifierOrInitializerExpression();
final boolean lazy = initializer instanceof Tree.LazySpecifierExpression;
String attrName = decl.getIdentifier().getText();
boolean memoized = Decl.isMemoized(decl);
boolean isStatic = model.isStatic();
// Only a non-formal or a concrete-non-lazy attribute has a corresponding field
// and if a captured class parameter exists with the same name we skip this part as well
Parameter parameter = CodegenUtil.findParamForDecl(decl);
boolean useField = !lazy && Strategy.useField(model);
boolean createField = !lazy && !model.isFormal() && Strategy.createField(parameter, model) && !model.isJavaNative();
boolean createCompanionField = !lazy && withinInterface && initializer != null;
JCThrow err = null;
JCExpression memoizedInitialValue = null;
if (createCompanionField || createField) {
TypedReference typedRef = getTypedReference(model);
TypedReference nonWideningTypedRef = nonWideningTypeDecl(typedRef);
Type nonWideningType = nonWideningType(typedRef, nonWideningTypedRef);
if (Decl.isIndirect(decl)) {
attrName = Naming.getAttrClassName(model, 0);
nonWideningType = getGetterInterfaceType(model);
}
JCExpression initialValue = null;
BoxingStrategy boxingStrategy = null;
if (initializer != null) {
Tree.Expression expression = initializer.getExpression();
HasErrorException error = errors().getFirstExpressionErrorAndMarkBrokenness(expression.getTerm());
int flags = CodegenUtil.downcastForSmall(expression, model) ? ExpressionTransformer.EXPR_UNSAFE_PRIMITIVE_TYPECAST_OK : 0;
flags |= model.hasUncheckedNullType() ? ExpressionTransformer.EXPR_TARGET_ACCEPTS_NULL : 0;
if (error != null) {
initialValue = null;
err = makeThrowUnresolvedCompilationError(error.getErrorMessage().getMessage());
} else {
boxingStrategy = useJavaBox(model, nonWideningType) && javaBoxExpression(expression.getTypeModel(), nonWideningType) ? BoxingStrategy.JAVA : CodegenUtil.getBoxingStrategy(model);
initialValue = expressionGen().transformExpression(expression, boxingStrategy, isStatic && nonWideningType.isTypeParameter() ? typeFact().getAnythingType() : nonWideningType, flags);
}
}
if (memoized) {
memoizedInitialValue = initialValue;
initialValue = makeDefaultExprForType(nonWideningType);
}
int flags = 0;
if (!CodegenUtil.isUnBoxed(nonWideningTypedRef.getDeclaration())) {
flags |= JT_NO_PRIMITIVES;
}
long modifiers = useField ? modifierTransformation().field(decl) : modifierTransformation().localVar(decl);
// does it in those cases)
if (parameter == null || parameter.isHidden()) {
JCExpression type;
if (isStatic && nonWideningType.isTypeParameter()) {
type = make().Type(syms().objectType);
} else {
type = makeJavaType(nonWideningType, flags);
}
if (createCompanionField) {
classBuilder.getCompanionBuilder((TypeDeclaration) model.getContainer()).field(modifiers, attrName, type, initialValue, !useField);
} else {
List<JCAnnotation> annos = makeAtIgnore().prependList(expressionGen().transformAnnotations(OutputElement.FIELD, decl));
if (classBuilder.hasDelegatingConstructors()) {
annos = annos.prependList(makeAtNoInitCheck());
}
// fields should be ignored, they are accessed by the getters
if (err == null) {
// TODO This should really be using AttributeDefinitionBuilder somehow
if (useField) {
AttributeDefinitionBuilder adb = AttributeDefinitionBuilder.field(this, null, attrName, model, Decl.isIndirect(decl)).fieldAnnotations(annos).fieldNullability(makeNullabilityAnnotations(model)).initialValue(initialValue, boxingStrategy).fieldVisibilityModifiers(modifiers).modifiers(modifiers);
classBuilder.defs(adb.buildFields());
List<JCStatement> buildInit = adb.buildInit(false);
if (!buildInit.isEmpty()) {
if (isStatic) {
classBuilder.defs(make().Block(STATIC, buildInit));
} else {
classBuilder.getInitBuilder().init(buildInit);
}
}
} else if (!memoized) {
classBuilder.field(modifiers, attrName, type, initialValue, !useField, annos);
if (!isEe(model) && model.isLate() && CodegenUtil.needsLateInitField(model, typeFact())) {
classBuilder.field(PRIVATE | Flags.VOLATILE | Flags.TRANSIENT, Naming.getInitializationFieldName(attrName), make().Type(syms().booleanType), make().Literal(false), false, makeAtIgnore());
}
}
}
}
}
// A shared attribute might be initialized in a for statement, so
// we might need a def-assignment subst for it
JCStatement outerSubs = statementGen().openOuterSubstitutionIfNeeded(model, model.getType(), 0);
if (outerSubs != null) {
classBuilder.getInitBuilder().init(outerSubs);
}
}
if (useField || withinInterface || lazy) {
boolean generateInClassOrInterface = !withinInterface || model.isShared() || isStatic;
boolean generateInCompanionClass = withinInterface && lazy && !isStatic;
if (generateInClassOrInterface) {
// Generate getter in main class or interface (when shared)
at(decl.getType());
AttributeDefinitionBuilder getter = makeGetter(decl, false, memoizedInitialValue);
if (err != null) {
getter.getterBlock(make().Block(0, List.<JCStatement>of(err)));
}
classBuilder.attribute(getter);
}
if (generateInCompanionClass) {
Interface container = (Interface) model.getContainer();
// Generate getter in companion class
classBuilder.getCompanionBuilder(container).attribute(makeGetter(decl, true, null));
}
if (Decl.isVariable(model) || model.isLate()) {
if (generateInClassOrInterface) {
// Generate setter in main class or interface (when shared)
classBuilder.attribute(makeSetter(decl, false, memoizedInitialValue));
}
if (generateInCompanionClass) {
Interface container = (Interface) model.getContainer();
// Generate setter in companion class
classBuilder.getCompanionBuilder(container).attribute(makeSetter(decl, true, null));
}
}
}
}
Aggregations