use of org.eclipse.ceylon.compiler.typechecker.tree.Tree.Term in project ceylon by eclipse.
the class AnnotationModelVisitor method defaultedParameter.
public void defaultedParameter(Tree.SpecifierOrInitializerExpression d, AnnotationConstructorParameter annotationConstructorParameter) {
if (annotationConstructor != null) {
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);
for (AnnotationConstructorParameter x : ((AnnotationInvocation) constructor.getAnnotationConstructor()).getConstructorParameters()) {
instantiation.addConstructorParameter(x);
}
}
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 org.eclipse.ceylon.compiler.typechecker.tree.Tree.Term in project ceylon by eclipse.
the class GenerateJsVisitor method assignOp.
private void assignOp(final Tree.AssignmentOp that, final String functionName, final Map<TypeParameter, Type> targs) {
if (errVisitor.hasErrors(that))
return;
Term lhs = that.getLeftTerm();
final boolean isNative = "||".equals(functionName) || "&&".equals(functionName);
if (lhs instanceof BaseMemberExpression) {
BaseMemberExpression lhsBME = (BaseMemberExpression) lhs;
Declaration lhsDecl = lhsBME.getDeclaration();
final String getLHS = memberAccess(lhsBME, null);
out("(");
BmeGenerator.generateMemberAccess(lhsBME, new GenerateCallback() {
@Override
public void generateValue() {
if (isNative) {
out(getLHS, functionName);
} else {
out(getLHS, ".", functionName, "(");
}
if (!isNaturalLiteral(that.getRightTerm())) {
that.getRightTerm().visit(GenerateJsVisitor.this);
}
if (!isNative) {
if (targs != null) {
out(",");
TypeUtils.printTypeArguments(that, targs, GenerateJsVisitor.this, false, null);
}
out(")");
}
}
}, null, this);
if (!hasSimpleGetterSetter(lhsDecl)) {
out(",", getLHS);
}
out(")");
} else if (lhs instanceof QualifiedMemberExpression) {
QualifiedMemberExpression lhsQME = (QualifiedMemberExpression) lhs;
if (TypeUtils.isNativeJs(lhsQME)) {
// ($1.foo = Box($1.foo).operator($2))
final String tmp = names.createTempVariable();
final String dec = isInDynamicBlock() && lhsQME.getDeclaration() == null ? lhsQME.getIdentifier().getText() : lhsQME.getDeclaration().getName();
out("(", tmp, "=");
lhsQME.getPrimary().visit(this);
out(",", tmp, ".", dec, "=");
int boxType = boxStart(lhsQME);
out(tmp, ".", dec);
if (boxType == 4)
out("/*TODO: callable targs 8*/");
boxUnboxEnd(boxType);
out(".", functionName, "(");
if (!isNaturalLiteral(that.getRightTerm())) {
that.getRightTerm().visit(this);
}
out("))");
} else {
final String lhsPrimaryVar = createRetainedTempVar();
final String getLHS = memberAccess(lhsQME, lhsPrimaryVar);
out("(", lhsPrimaryVar, "=");
lhsQME.getPrimary().visit(this);
out(",");
BmeGenerator.generateMemberAccess(lhsQME, new GenerateCallback() {
@Override
public void generateValue() {
out(getLHS, ".", functionName, "(");
Tree.Term term = that.getRightTerm();
if (!isNaturalLiteral(term)) {
term.visit(GenerateJsVisitor.this);
}
out(")");
}
}, lhsPrimaryVar, this);
if (!hasSimpleGetterSetter(lhsQME.getDeclaration())) {
out(",", getLHS);
}
out(")");
}
} else if (lhs instanceof Tree.IndexExpression) {
lhs.addUnsupportedError("Index expressions are not supported in this kind of assignment.");
}
}
use of org.eclipse.ceylon.compiler.typechecker.tree.Tree.Term in project ceylon by eclipse.
the class GenerateJsVisitor method boxUnboxStart.
int boxUnboxStart(final Tree.Term fromTerm, boolean toNative) {
// Box the value
final Type fromType = fromTerm.getTypeModel();
if (fromType == null) {
return 0;
}
final boolean fromNative = TypeUtils.isNativeJs(fromTerm);
if (fromNative != toNative || fromType.isCallable()) {
if (fromNative) {
// conversion from native value to Ceylon value
if (fromType.isInteger() || fromType.isFloat()) {
out("(");
} else if (fromType.isBoolean()) {
out("(");
} else if (fromType.isCharacter()) {
out(getClAlias(), "Character(");
} else if (fromType.isCallable()) {
out(getClAlias(), "jsc$2(");
return 4;
} else {
return 0;
}
return 1;
} else if (fromType.isFloat()) {
// conversion from Ceylon Float to native value
return toNative ? 2 : 1;
} else if (fromType.isCallable()) {
Term _t = fromTerm;
if (_t instanceof Tree.InvocationExpression) {
_t = ((Tree.InvocationExpression) _t).getPrimary();
}
// Don't box callables if they're not members or anonymous
if (_t instanceof Tree.MemberOrTypeExpression) {
final Declaration d = ((Tree.MemberOrTypeExpression) _t).getDeclaration();
if (d != null && !(d.isClassOrInterfaceMember() || d.isAnonymous())) {
return 0;
}
}
out(getClAlias(), "jsc$2(");
return 4;
} else {
return 3;
}
}
return 0;
}
use of org.eclipse.ceylon.compiler.typechecker.tree.Tree.Term in project ceylon by eclipse.
the class GenerateJsVisitor method generateIsOfType.
/**
* Generates js code to check if a term is of a certain type. We solve this in JS by
* checking against all types that Type satisfies (in the case of union types, matching any
* type will do, and in case of intersection types, all types must be matched).
* @param term The term that is to be checked against a type
* @param termString (optional) a string to be used as the term to be checked
* @param type The type to check against
* @param tmpvar (optional) a variable to which the term is assigned
* @param negate If true, negates the generated condition
*/
void generateIsOfType(Node term, String termString, final Type type, String tmpvar, final boolean negate, final boolean coerceDynamic) {
if (negate) {
out("!");
}
out(getClAlias(), "is$(");
if (isInDynamicBlock() && coerceDynamic) {
out(getClAlias(), "dre$$(");
}
if (term instanceof Term) {
conds.specialConditionRHS((Term) term, tmpvar);
} else {
conds.specialConditionRHS(termString, tmpvar);
}
if (isInDynamicBlock() && coerceDynamic) {
out(",");
TypeUtils.typeNameOrList(term, type, this, false);
out(",false)");
}
out(",");
TypeUtils.typeNameOrList(term, type, this, false);
if (type.getQualifyingType() != null) {
Type outer = type.getQualifyingType();
boolean first = true;
while (outer != null) {
if (first) {
out(",[");
first = false;
} else {
out(",");
}
TypeUtils.typeNameOrList(term, outer, this, false);
outer = outer.getQualifyingType();
}
if (!first) {
out("]");
}
} else if (type.getDeclaration() != null) {
Scope container = type.getDeclaration().getContainer();
if (container != null) {
Declaration d = ModelUtil.getContainingDeclarationOfScope(container);
if (d != null && d instanceof Function && !((Function) d).getTypeParameters().isEmpty()) {
out(",", names.typeArgsParamName((Function) d));
}
}
}
out(")");
}
Aggregations