use of org.eclipse.ceylon.model.typechecker.model.Interface in project ceylon by eclipse.
the class AnnotationVisitor method visit.
@Override
public void visit(Tree.DocLink that) {
super.visit(that);
String text = that.getText();
int pipeIndex = text.indexOf("|");
if (pipeIndex != -1) {
text = text.substring(pipeIndex + 1);
}
String kind = null;
if (text.startsWith(DOC_LINK_MODULE)) {
kind = DOC_LINK_MODULE;
text = text.substring(DOC_LINK_MODULE.length());
} else if (text.startsWith(DOC_LINK_PACKAGE)) {
kind = DOC_LINK_PACKAGE;
text = text.substring(DOC_LINK_PACKAGE.length());
} else if (text.startsWith(DOC_LINK_CLASS)) {
kind = DOC_LINK_CLASS;
text = text.substring(DOC_LINK_CLASS.length());
} else if (text.startsWith(DOC_LINK_INTERFACE)) {
kind = DOC_LINK_INTERFACE;
text = text.substring(DOC_LINK_INTERFACE.length());
} else if (text.startsWith(DOC_LINK_FUNCTION)) {
kind = DOC_LINK_FUNCTION;
text = text.substring(DOC_LINK_FUNCTION.length());
} else if (text.startsWith(DOC_LINK_VALUE)) {
kind = DOC_LINK_VALUE;
text = text.substring(DOC_LINK_VALUE.length());
} else if (text.startsWith(DOC_LINK_ALIAS)) {
kind = DOC_LINK_ALIAS;
text = text.substring(DOC_LINK_ALIAS.length());
}
boolean parentheses = false;
if (text.endsWith("()")) {
parentheses = true;
text = text.substring(0, text.length() - 2);
}
int scopeIndex = text.indexOf("::");
String packageName;
if (DOC_LINK_MODULE.equals(kind) || DOC_LINK_PACKAGE.equals(kind)) {
packageName = text;
} else {
packageName = scopeIndex < 0 ? null : text.substring(0, scopeIndex);
}
String path = scopeIndex < 0 ? text : text.substring(scopeIndex + 2);
String[] names = path.isEmpty() ? new String[0] : path.split("\\.");
Declaration base = null;
if (packageName == null) {
if (names.length > 0) {
base = that.getScope().getMemberOrParameter(that.getUnit(), names[0], null, false);
}
} else {
Package pack = that.getUnit().getPackage().getModule().getPackage(packageName);
if (pack == null) {
if (DOC_LINK_MODULE.equals(kind)) {
that.addUsageWarning(Warning.doclink, "module is missing: '" + packageName + "'");
} else {
that.addUsageWarning(Warning.doclink, "package is missing: '" + packageName + "'");
}
} else {
that.setPkg(pack);
if (DOC_LINK_MODULE.equals(kind)) {
Package rootPack = pack.getModule().getRootPackage();
if (pack.equals(rootPack)) {
that.setModule(pack.getModule());
} else {
that.addUsageWarning(Warning.doclink, "module is missing: '" + packageName + "'");
}
}
if (names.length > 0) {
base = pack.getDirectMember(names[0], null, false);
}
}
if (DOC_LINK_MODULE.equals(kind) || DOC_LINK_PACKAGE.equals(kind)) {
return;
}
}
if (base == null) {
that.addUsageWarning(Warning.doclink, "declaration is missing: '" + (names.length > 0 ? names[0] : text) + "'");
} else {
that.setBase(base);
if (names.length > 1) {
that.setQualified(new ArrayList<Declaration>(names.length - 1));
}
for (int i = 1; i < names.length; i++) {
if (base instanceof Value) {
Value value = (Value) base;
if (!value.isParameter() && !value.isTransient() && value.getTypeDeclaration() != null && value.getTypeDeclaration().isAnonymous()) {
base = value.getTypeDeclaration();
}
}
if (base instanceof TypeDeclaration || base instanceof Functional) {
Declaration qualified = base.getMember(names[i], null, false);
if (qualified == null) {
that.addUsageWarning(Warning.doclink, "member declaration or parameter is missing: '" + names[i] + "'");
break;
} else {
that.getQualified().add(qualified);
base = qualified;
}
} else {
that.addUsageWarning(Warning.doclink, "not a type or functional declaration: '" + base.getName() + "'");
break;
}
}
}
if (base != null) {
if (kind != null && (names.length == 1 || names.length == that.getQualified().size() + 1)) {
if (DOC_LINK_CLASS.equals(kind) && !(base instanceof Class)) {
that.addUsageWarning(Warning.doclink, "linked declaration is not a class: '" + base.getName() + "'");
} else if (DOC_LINK_INTERFACE.equals(kind) && !(base instanceof Interface)) {
that.addUsageWarning(Warning.doclink, "linked declaration is not an interface: '" + base.getName() + "'");
} else if (DOC_LINK_ALIAS.equals(kind) && !(base instanceof TypeAlias)) {
that.addUsageWarning(Warning.doclink, "linked declaration is not a type alias: '" + base.getName() + "'");
} else if (DOC_LINK_FUNCTION.equals(kind) && !(base instanceof Function)) {
that.addUsageWarning(Warning.doclink, "linked declaration is not a function: '" + base.getName() + "'");
} else if (DOC_LINK_VALUE.equals(kind) && !(base instanceof Value)) {
that.addUsageWarning(Warning.doclink, "linked declaration is not a value: '" + base.getName() + "'");
}
}
if (parentheses && !(base instanceof Functional)) {
that.addUsageWarning(Warning.doclink, "linked declaration is not a function: '" + base.getName() + "'");
}
}
}
use of org.eclipse.ceylon.model.typechecker.model.Interface in project ceylon by eclipse.
the class ExpressionVisitor method eraseDefaultedParameters.
private Type eraseDefaultedParameters(Type type) {
Interface cd = unit.getCallableDeclaration();
Type callableType = type.getSupertype(cd);
if (callableType != null) {
List<Type> typeArgs = callableType.getTypeArgumentList();
if (typeArgs.size() >= 2) {
Type rt = typeArgs.get(0);
Type pts = typeArgs.get(1);
List<Type> argTypes = unit.getTupleElementTypes(pts);
boolean variadic = unit.isTupleLengthUnbounded(pts);
boolean atLeastOne = unit.isTupleVariantAtLeastOne(pts);
if (variadic) {
argTypes = new ArrayList<Type>(argTypes);
Type spt = argTypes.get(argTypes.size() - 1);
argTypes.set(argTypes.size() - 1, unit.getIteratedType(spt));
}
Type tt = unit.getTupleType(argTypes, variadic, atLeastOne, -1);
return appliedType(cd, rt, tt);
}
}
return type;
}
use of org.eclipse.ceylon.model.typechecker.model.Interface in project ceylon by eclipse.
the class ExpressionVisitor method visit.
@Override
public void visit(Tree.TypeLiteral that) {
if (that instanceof Tree.InterfaceLiteral || that instanceof Tree.ClassLiteral || that instanceof Tree.NewLiteral || that instanceof Tree.AliasLiteral || that instanceof Tree.TypeParameterLiteral) {
declarationLiteral = true;
} else {
modelLiteral = true;
}
try {
super.visit(that);
} finally {
declarationLiteral = false;
modelLiteral = false;
}
Type t;
TypeDeclaration d;
Tree.StaticType type = that.getType();
Node errorNode;
if (type != null) {
t = type.getTypeModel();
d = t.getDeclaration();
errorNode = type;
} else {
errorNode = that;
ClassOrInterface classOrInterface = getContainingClassOrInterface(that.getScope());
if (that instanceof Tree.ClassLiteral || that instanceof Tree.InterfaceLiteral) {
d = classOrInterface;
if (d == null) {
errorNode.addError("no containing type");
// EARLY EXIT!!
return;
} else {
t = classOrInterface.getType();
}
} else {
errorNode.addError("missing type reference");
// EARLY EXIT!!
return;
}
}
if (t != null) {
that.setDeclaration(d);
that.setWantsDeclaration(true);
if (that instanceof Tree.ClassLiteral) {
if (!(d instanceof Class)) {
if (d != null) {
errorNode.addError("referenced declaration is not a class" + getDeclarationReferenceSuggestion(d));
}
that.setTypeModel(unit.getClassDeclarationType());
} else {
that.setTypeModel(unit.getClassDeclarationType((Class) d));
}
} else if (that instanceof Tree.NewLiteral) {
if (d instanceof Class) {
Class c = (Class) d;
Constructor defaultConstructor = c.getDefaultConstructor();
if (defaultConstructor != null) {
d = defaultConstructor;
}
}
if (d instanceof Constructor) {
Constructor c = (Constructor) d;
if (c.getParameterList() == null) {
that.setTypeModel(unit.getValueConstructorDeclarationType());
} else {
that.setTypeModel(unit.getCallableConstructorDeclarationType());
}
} else if (d != null) {
errorNode.addError("referenced declaration is not a constructor" + getDeclarationReferenceSuggestion(d));
}
} else if (that instanceof Tree.InterfaceLiteral) {
if (!(d instanceof Interface)) {
if (d != null) {
errorNode.addError("referenced declaration is not an interface" + getDeclarationReferenceSuggestion(d));
}
}
that.setTypeModel(unit.getInterfaceDeclarationType());
} else if (that instanceof Tree.AliasLiteral) {
if (!(d instanceof TypeAlias)) {
errorNode.addError("referenced declaration is not a type alias" + getDeclarationReferenceSuggestion(d));
}
that.setTypeModel(unit.getAliasDeclarationType());
} else if (that instanceof Tree.TypeParameterLiteral) {
if (!(d instanceof TypeParameter)) {
errorNode.addError("referenced declaration is not a type parameter" + getDeclarationReferenceSuggestion(d));
}
that.setTypeModel(unit.getTypeParameterDeclarationType());
} else if (d != null) {
that.setWantsDeclaration(false);
t = t.resolveAliases();
if (t == null || t.isUnknown()) {
return;
}
// checkNonlocalType(that.getType(), t.getDeclaration());
if (d instanceof Constructor) {
if (((Constructor) d).isAbstraction()) {
errorNode.addError("constructor is overloaded");
} else {
that.setTypeModel(unit.getConstructorMetatype(t));
}
} else if (d instanceof Class) {
// checkNonlocal(that, t.getDeclaration());
that.setTypeModel(unit.getClassMetatype(t));
} else if (d instanceof Interface) {
that.setTypeModel(unit.getInterfaceMetatype(t));
} else {
that.setTypeModel(unit.getTypeMetaType(t));
}
}
}
}
use of org.eclipse.ceylon.model.typechecker.model.Interface in project ceylon by eclipse.
the class TypeParserTests method testPossiblyEmptyIterableAbbrev.
@Test
public void testPossiblyEmptyIterableAbbrev() {
Type type = new TypeParser(MockLoader.instance).decodeType("{a*}", null, mockDefaultModule, mockPkgUnit);
Assert.assertNotNull(type);
TypeDeclaration declaration = type.getDeclaration();
Assert.assertNotNull(declaration);
Assert.assertTrue(declaration instanceof Interface);
Assert.assertEquals("ceylon.language::Iterable", declaration.getQualifiedNameString());
Assert.assertEquals("{a*}", type.asString());
Assert.assertNull(type.getQualifyingType());
}
use of org.eclipse.ceylon.model.typechecker.model.Interface in project ceylon by eclipse.
the class InvocationGenerator method positionalInvocation.
private void positionalInvocation(final Tree.InvocationExpression that) {
final Tree.Primary typeArgSource = that.getPrimary();
final Tree.PositionalArgumentList argList = that.getPositionalArgumentList();
final Map<TypeParameter, Type> targs = getTypeArguments(typeArgSource);
if (gen.isInDynamicBlock() && typeArgSource instanceof Tree.BaseTypeExpression && ((Tree.BaseTypeExpression) typeArgSource).getDeclaration() == null) {
gen.out("(");
// Could be a dynamic object, or a Ceylon one
// We might need to call "new" so we need to get all the args to pass directly later
final List<String> argnames = generatePositionalArguments(typeArgSource, argList, argList.getPositionalArguments(), false, true);
if (!argnames.isEmpty()) {
gen.out(",");
}
final String fname = names.createTempVariable();
gen.out(fname, "=");
typeArgSource.visit(gen);
String theargs = "";
if (!argnames.isEmpty()) {
theargs = argnames.toString().substring(1);
theargs = theargs.substring(0, theargs.length() - 1);
}
gen.out(",", fname, ".$$===undefined?new ", fname, "(", theargs, "):", fname, "(", theargs, "))");
// TODO we lose type args for now
return;
} else {
final Tree.PositionalArgument lastArg = argList.getPositionalArguments().isEmpty() ? null : argList.getPositionalArguments().get(argList.getPositionalArguments().size() - 1);
boolean hasSpread = lastArg instanceof Tree.SpreadArgument && that.getUnit().isUnknownArgumentsCallable(that.getPrimary().getTypeModel()) && !typeArgSource.getTypeModel().isUnknown();
if (hasSpread) {
gen.out(gen.getClAlias(), "spread$2(");
}
if (typeArgSource instanceof Tree.BaseMemberExpression) {
final Tree.BaseMemberExpression _bme = (Tree.BaseMemberExpression) typeArgSource;
if (gen.isInDynamicBlock()) {
if (_bme.getDeclaration() == null || _bme.getDeclaration().isDynamic() || (_bme.getDeclaration() instanceof TypedDeclaration && ((TypedDeclaration) _bme.getDeclaration()).isDynamicallyTyped())) {
if (lastArg instanceof Tree.SpreadArgument && (lastArg.getTypeModel() == null || lastArg.getTypeModel().isUnknown())) {
BmeGenerator.generateBme(_bme, gen);
gen.out(".apply(0,");
if (argList.getPositionalArguments().size() == 1) {
generatePositionalArguments(typeArgSource, argList, argList.getPositionalArguments(), false, true);
} else {
gen.out("[");
ArrayList<Tree.PositionalArgument> subargs = new ArrayList<>(argList.getPositionalArguments().size());
subargs.addAll(argList.getPositionalArguments());
subargs.remove(subargs.size() - 1);
generatePositionalArguments(typeArgSource, argList, subargs, false, true);
gen.out("].concat(");
lastArg.visit(gen);
gen.out(")");
}
gen.out(")");
return;
}
} else if ("ceylon.language::print".equals(_bme.getDeclaration().getQualifiedNameString())) {
Tree.PositionalArgument printArg = that.getPositionalArgumentList().getPositionalArguments().get(0);
if (ModelUtil.isTypeUnknown(printArg.getTypeModel())) {
// #397
gen.out(gen.getClAlias(), "pndo$(");
printArg.visit(gen);
gen.out(")");
return;
}
}
}
BmeGenerator.generateBme(_bme, gen);
} else if (typeArgSource instanceof Tree.QualifiedTypeExpression) {
BmeGenerator.generateQte((Tree.QualifiedTypeExpression) typeArgSource, gen);
} else {
typeArgSource.visit(gen);
}
if (gen.opts.isOptimize() && (gen.getSuperMemberScope(typeArgSource) != null)) {
gen.out(".call(", names.self(ModelUtil.getContainingClassOrInterface(typeArgSource.getScope())));
if (!argList.getPositionalArguments().isEmpty()) {
gen.out(",");
}
} else if (hasSpread) {
gen.out(",");
} else {
gen.out("(");
}
// Check if args have params
boolean fillInParams = !argList.getPositionalArguments().isEmpty();
for (Tree.PositionalArgument arg : argList.getPositionalArguments()) {
fillInParams &= arg.getParameter() == null;
}
if (fillInParams) {
// Get the callable and try to assign params from there
Interface cd = that.getUnit().getCallableDeclaration();
final Type ed = that.getUnit().getEmptyType();
Class td = that.getUnit().getTupleDeclaration();
Type callable = typeArgSource.getTypeModel() == null ? null : typeArgSource.getTypeModel().getSupertype(cd);
if (callable != null) {
// This is a tuple with the arguments to the callable
// (can be union with empty if first param is defaulted)
Type callableArgs = callable.getTypeArgumentList().get(1);
boolean isUnion = false;
if (callableArgs.isUnion()) {
if (callableArgs.getCaseTypes().size() == 2) {
callableArgs = callableArgs.minus(ed);
}
isUnion = callableArgs.isUnion();
}
// This is the type of the first argument
boolean isSequenced = !(isUnion || td.equals(callableArgs.getDeclaration()));
Type argtype = isUnion ? callableArgs : callableArgs.isTypeParameter() || callableArgs.isEmpty() ? callableArgs : callableArgs.isSequence() || callableArgs.isSequential() ? callableArgs.getTypeArgumentList().get(0) : callableArgs.getTypeArgumentList().get(isSequenced ? 0 : 1);
Parameter p = null;
int c = 0;
for (Tree.PositionalArgument arg : argList.getPositionalArguments()) {
if (p == null) {
p = new Parameter();
p.setName("arg" + c);
p.setDeclaration(typeArgSource.getTypeModel().getDeclaration());
Value v = new Value();
Scope scope = that.getPositionalArgumentList().getScope();
v.setContainer(scope);
v.setScope(scope);
v.setType(argtype);
p.setModel(v);
if (callableArgs == null || isSequenced) {
p.setSequenced(true);
} else if (!isSequenced) {
Type next = isUnion ? null : callableArgs.getTypeArgumentList().get(2);
if (next != null && next.getSupertype(td) == null) {
// sequential if sequenced param
if (next.isUnion()) {
// empty|tuple
callableArgs = next.minus(ed);
isSequenced = !td.equals(callableArgs.getDeclaration());
argtype = callableArgs.getTypeArgumentList().get(isSequenced ? 0 : 1);
} else {
// we'll bet on sequential (if it's empty we don't care anyway)
argtype = next;
callableArgs = null;
}
} else {
// If it's a tuple then there are more params
callableArgs = next;
argtype = callableArgs == null ? null : callableArgs.getTypeArgumentList().get(1);
}
}
}
arg.setParameter(p);
c++;
if (!p.isSequenced()) {
p = null;
}
}
}
}
generatePositionalArguments(typeArgSource, argList, argList.getPositionalArguments(), false, false);
}
if (targs != null && !targs.isEmpty() && typeArgSource instanceof Tree.MemberOrTypeExpression && ((Tree.MemberOrTypeExpression) typeArgSource).getDeclaration() instanceof Functional) {
if (argList.getPositionalArguments().size() > 0) {
gen.out(",");
}
Functional bmed = (Functional) ((Tree.MemberOrTypeExpression) typeArgSource).getDeclaration();
// If there are fewer arguments than there are parameters...
final int argsSize = argList.getPositionalArguments().size();
int paramArgDiff = bmed.getFirstParameterList().getParameters().size() - argsSize;
if (paramArgDiff > 0) {
final Tree.PositionalArgument parg = argsSize > 0 ? argList.getPositionalArguments().get(argsSize - 1) : null;
if (parg instanceof Tree.Comprehension || parg instanceof Tree.SpreadArgument) {
paramArgDiff--;
}
for (int i = 0; i < paramArgDiff; i++) {
gen.out("undefined,");
}
}
if (targs != null && !targs.isEmpty()) {
TypeUtils.printTypeArguments(typeArgSource, targs, gen, false, null);
}
}
gen.out(")");
}
Aggregations