use of org.eclipse.ceylon.model.typechecker.model.Parameter in project ceylon by eclipse.
the class MethodDefinitionBuilder method implementsRawParameter.
private boolean implementsRawParameter(FunctionOrValue decl) {
if (ModelUtil.containsRawType(decl.getType()))
return true;
// Taken pretty much straight from JvmBackendUtil.getTopmostRefinement
Functional func = (Functional) JvmBackendUtil.getParameterized((FunctionOrValue) decl);
if (func == null || func instanceof TypedDeclaration == false)
return false;
Declaration kk = getFirstRefinedDeclaration((TypedDeclaration) func);
// error recovery
if (kk instanceof Functional == false)
return false;
Functional refinedFunc = (Functional) kk;
// shortcut if the functional doesn't override anything
if (ModelUtil.equal((Declaration) refinedFunc, (Declaration) func)) {
return false;
}
if (func.getParameterLists().size() != refinedFunc.getParameterLists().size()) {
// invalid input
return false;
}
for (int ii = 0; ii < func.getParameterLists().size(); ii++) {
if (func.getParameterLists().get(ii).getParameters().size() != refinedFunc.getParameterLists().get(ii).getParameters().size()) {
// invalid input
return false;
}
// find the index of the parameter in the declaration
int index = 0;
for (Parameter px : func.getParameterLists().get(ii).getParameters()) {
if (px.getModel() == null || px.getModel().equals(decl)) {
// And return the corresponding parameter from the refined declaration
FunctionOrValue refinedDecl = refinedFunc.getParameterLists().get(ii).getParameters().get(index).getModel();
return implementsRawParameter(refinedDecl);
}
index++;
}
continue;
}
// invalid input
return false;
}
use of org.eclipse.ceylon.model.typechecker.model.Parameter in project ceylon by eclipse.
the class NamedArgumentInvocation method appendVarsForSequencedArguments.
private void appendVarsForSequencedArguments(Tree.SequencedArgument sequencedArgument, java.util.List<Parameter> declaredParams) {
// FIXME: this is suspisciously similar to AbstractTransformer.makeIterable(java.util.List<Tree.PositionalArgument> list, Type seqElemType)
// and possibly needs to be merged
Parameter parameter = sequencedArgument.getParameter();
Type parameterType = parameterType(parameter, parameter.getType(), AbstractTransformer.TP_TO_BOUND);
// find out the individual type, we use the argument type for the value, and the param type for the temp variable
gen.at(sequencedArgument);
Type tupleType = AnalyzerUtil.getTupleType(sequencedArgument.getPositionalArguments(), gen.typeFact(), false);
// we can't just generate types like Foo<?> if the target type param is not raw because the bounds will
// not match, so we go raw, we also ignore primitives naturally
int flags = JT_RAW | JT_NO_PRIMITIVES;
Type argumentsType;
Type iteratedType;
Type absentType;
if (gen.typeFact().isIterableType(tupleType)) {
argumentsType = tupleType.getSupertype(gen.typeFact().getIterableDeclaration());
iteratedType = gen.typeFact().getIteratedType(argumentsType);
absentType = gen.typeFact().getIteratedAbsentType(argumentsType);
} else if (gen.typeFact().isJavaIterableType(tupleType)) {
argumentsType = tupleType.getSupertype(gen.typeFact().getJavaIterableDeclaration());
iteratedType = gen.typeFact().getJavaIteratedType(argumentsType);
absentType = gen.typeFact().getNullType();
} else if (gen.typeFact().isJavaObjectArrayType(tupleType)) {
argumentsType = tupleType.getSupertype(gen.typeFact().getJavaObjectArrayDeclaration());
iteratedType = gen.typeFact().getJavaArrayElementType(argumentsType);
absentType = gen.typeFact().getNullType();
} else if (gen.typeFact().isJavaPrimitiveArrayType(tupleType)) {
argumentsType = tupleType.getSupertype(gen.typeFact().getJavaObjectArrayDeclaration());
iteratedType = gen.typeFact().getJavaArrayElementType(tupleType);
absentType = gen.typeFact().getNullType();
} else {
throw BugException.unhandledTypeCase(tupleType);
}
JCTree.JCExpression sequenceValue;
if (Strategy.useConstantIterable(sequencedArgument)) {
sequenceValue = gen.makeConstantIterable(sequencedArgument, iteratedType, absentType, flags);
} else {
sequenceValue = gen.makeLazyIterable(sequencedArgument, iteratedType, absentType, flags);
}
JCTree.JCExpression sequenceType = gen.makeJavaType(parameterType, flags);
Naming.SyntheticName argName = argName(parameter);
JCTree.JCVariableDecl varDecl = gen.makeVar(argName, sequenceType, sequenceValue);
gen.at(getPrimary());
bind(parameter, argName, gen.makeJavaType(parameterType, flags), List.<JCTree.JCStatement>of(varDecl));
}
use of org.eclipse.ceylon.model.typechecker.model.Parameter in project ceylon by eclipse.
the class NamedArgumentInvocation method appendVarsForDefaulted.
private boolean appendVarsForDefaulted(java.util.List<Parameter> declaredParams) {
boolean hasDefaulted = false;
if (!Decl.isOverloaded(getPrimaryDeclaration())) {
// append any arguments for defaulted parameters
for (Parameter param : declaredParams) {
if (bound.contains(param)) {
continue;
}
final JCExpression argExpr;
if (Strategy.hasDefaultParameterValueMethod(param)) {
// special handling for "element" optional param of java array constructors
if (getPrimaryDeclaration() instanceof Class && gen.isJavaArray(((Class) getPrimaryDeclaration()).getType())) {
// default values are hard-coded to Java default values, and are actually ignored
continue;
} else if (gen.typeFact().getExceptionDeclaration().equals(getPrimaryDeclaration())) {
// erasure means there's no default value method, but we know it's null
argExpr = gen.makeNull();
} else if (getQmePrimary() != null && gen.isJavaArray(getQmePrimary().getTypeModel())) {
// we support array methods with optional parameters
if (getPrimaryDeclaration() instanceof Function && getPrimaryDeclaration().getName().equals("copyTo")) {
if (param.getName().equals("sourcePosition") || param.getName().equals("destinationPosition")) {
argExpr = gen.makeInteger(0);
hasDefaulted |= true;
} else if (param.getName().equals("length")) {
argExpr = gen.makeSelect(varBaseName.suffixedBy(Suffix.$argthis$).makeIdent(), "length");
hasDefaulted |= true;
} else {
argExpr = gen.makeErroneous(this.getNode(), "compiler bug: argument to copyTo method of java array type not supported: " + param.getName());
}
} else {
argExpr = gen.makeErroneous(this.getNode(), "compiler bug: virtual method of java array type not supported: " + getPrimaryDeclaration());
}
} else {
argExpr = makeDefaultedArgumentMethodCall(param);
hasDefaulted |= true;
}
} else if (Strategy.hasEmptyDefaultArgument(param)) {
argExpr = gen.makeEmptyAsSequential(true);
} else if (gen.typeFact().isIterableType(param.getType())) {
// must be an iterable we need to fill with empty
// FIXME: deal with this erasure bug later
argExpr = gen.make().TypeCast(gen.makeJavaType(gen.typeFact().getIterableDeclaration().getType(), AbstractTransformer.JT_RAW), gen.makeEmpty());
} else {
// more expensive but worth a try
Type appliedType = gen.getTypeForParameter(param, producedReference, AbstractTransformer.TP_TO_BOUND);
if (gen.typeFact().isIterableType(appliedType)) {
argExpr = gen.make().TypeCast(gen.makeJavaType(gen.typeFact().getIterableDeclaration().getType(), AbstractTransformer.JT_RAW), gen.makeEmpty());
} else {
argExpr = gen.makeErroneous(this.getNode(), "compiler bug: missing argument, and parameter is not defaulted");
}
}
appendDefaulted(param, argExpr);
}
}
return hasDefaulted;
}
use of org.eclipse.ceylon.model.typechecker.model.Parameter in project ceylon by eclipse.
the class ParameterAnnotationTerm method encode.
@Override
public int encode(AbstractTransformer gen, ListBuffer<JCExpression> instantiations) {
Parameter parameter = getSourceParameter();
int index = ((Functional) parameter.getDeclaration()).getFirstParameterList().getParameters().indexOf(parameter);
if (isSpread()) {
index += 256;
}
return index;
}
use of org.eclipse.ceylon.model.typechecker.model.Parameter in project ceylon by eclipse.
the class ParameterDefinitionBuilder method functionalParameters.
static void functionalParameters(StringBuilder sb, ParameterList pl) {
sb.append('(');
Iterator<Parameter> parameters = pl.getParameters().iterator();
while (parameters.hasNext()) {
Parameter p = parameters.next();
FunctionOrValue pm = p.getModel();
sb.append(pm.getName());
if (p.isSequenced()) {
if (p.isAtLeastOne()) {
sb.append('+');
} else {
sb.append('*');
}
}
if (pm instanceof Function) {
functionalName(sb, (Function) pm);
}
if (parameters.hasNext()) {
sb.append(',');
}
}
sb.append(')');
}
Aggregations