From c7887ba6d3aa0f8e35fbe8011032b91d7fd9a21a Mon Sep 17 00:00:00 2001 From: mmews Date: Tue, 5 Mar 2024 10:55:44 +0100 Subject: [PATCH] migrate --- .../N4JSClassDeclarationTypesBuilder.java | 135 ++++++++++++++ .../N4JSClassDeclarationTypesBuilder.xtend | 122 ------------- ...N4JSClassifierDeclarationTypesBuilder.java | 167 ++++++++++++++++++ ...4JSClassifierDeclarationTypesBuilder.xtend | 136 -------------- .../N4JSInterfaceDeclarationTypesBuilder.java | 88 +++++++++ ...N4JSInterfaceDeclarationTypesBuilder.xtend | 80 --------- .../N4JSNamespaceDeclarationTypesBuilder.java | 63 +++++++ ...N4JSNamespaceDeclarationTypesBuilder.xtend | 60 ------- 8 files changed, 453 insertions(+), 398 deletions(-) create mode 100644 plugins/org.eclipse.n4js/src/org/eclipse/n4js/typesbuilder/N4JSClassDeclarationTypesBuilder.java delete mode 100644 plugins/org.eclipse.n4js/src/org/eclipse/n4js/typesbuilder/N4JSClassDeclarationTypesBuilder.xtend create mode 100644 plugins/org.eclipse.n4js/src/org/eclipse/n4js/typesbuilder/N4JSClassifierDeclarationTypesBuilder.java delete mode 100644 plugins/org.eclipse.n4js/src/org/eclipse/n4js/typesbuilder/N4JSClassifierDeclarationTypesBuilder.xtend create mode 100644 plugins/org.eclipse.n4js/src/org/eclipse/n4js/typesbuilder/N4JSInterfaceDeclarationTypesBuilder.java delete mode 100644 plugins/org.eclipse.n4js/src/org/eclipse/n4js/typesbuilder/N4JSInterfaceDeclarationTypesBuilder.xtend create mode 100644 plugins/org.eclipse.n4js/src/org/eclipse/n4js/typesbuilder/N4JSNamespaceDeclarationTypesBuilder.java delete mode 100644 plugins/org.eclipse.n4js/src/org/eclipse/n4js/typesbuilder/N4JSNamespaceDeclarationTypesBuilder.xtend diff --git a/plugins/org.eclipse.n4js/src/org/eclipse/n4js/typesbuilder/N4JSClassDeclarationTypesBuilder.java b/plugins/org.eclipse.n4js/src/org/eclipse/n4js/typesbuilder/N4JSClassDeclarationTypesBuilder.java new file mode 100644 index 0000000000..596d72f5c6 --- /dev/null +++ b/plugins/org.eclipse.n4js/src/org/eclipse/n4js/typesbuilder/N4JSClassDeclarationTypesBuilder.java @@ -0,0 +1,135 @@ +/** + * Copyright (c) 2016 NumberFour AG. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * NumberFour AG - Initial API and implementation + */ +package org.eclipse.n4js.typesbuilder; + +import static org.eclipse.xtext.xbase.lib.IterableExtensions.map; +import static org.eclipse.xtext.xbase.lib.IterableExtensions.toList; + +import org.eclipse.n4js.AnnotationDefinition; +import org.eclipse.n4js.n4JS.N4ClassDeclaration; +import org.eclipse.n4js.n4JS.N4ClassDefinition; +import org.eclipse.n4js.n4JS.N4ClassExpression; +import org.eclipse.n4js.n4JS.TypeReferenceNode; +import org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef; +import org.eclipse.n4js.ts.types.AbstractNamespace; +import org.eclipse.n4js.ts.types.TClass; +import org.eclipse.n4js.ts.types.TypesFactory; +import org.eclipse.n4js.types.utils.TypeUtils; +import org.eclipse.n4js.utils.N4JSLanguageUtils; + +/***/ +public class N4JSClassDeclarationTypesBuilder extends N4JSClassifierDeclarationTypesBuilder { + + /***/ + protected boolean relinkTClass(N4ClassDeclaration n4Class, AbstractNamespace target, boolean preLinkingPhase, + int idx) { + if (n4Class.getName() == null) { // may be null due to syntax errors + return false; + } + + TClass tclass = (TClass) target.getTypes().get(idx); + + relinkClassifierAndMembers(tclass, n4Class, preLinkingPhase); + return true; + } + + /***/ + protected TClass createTClass(N4ClassDeclaration n4Class, AbstractNamespace target, boolean preLinkingPhase) { + if (n4Class.getName() == null) { // may be null due to syntax errors + return null; + } + + TClass tclass = createTClass(n4Class); + // modifiers + _n4JSTypesBuilderHelper.setTypeAccessModifier(tclass, n4Class); + _n4JSTypesBuilderHelper.setProvidedByRuntime(tclass, n4Class, preLinkingPhase); + tclass.setDeclaredNonStaticPolyfill(N4JSLanguageUtils.isNonStaticPolyfill(n4Class)); + tclass.setDeclaredStaticPolyfill(N4JSLanguageUtils.isStaticPolyfill(n4Class)); + tclass.setDeclaredCovariantConstructor(_n4JSTypesBuilderHelper.isDeclaredCovariantConstructor(n4Class)); + _n4JSTypeVariableTypesBuilder.addTypeParameters(tclass, n4Class, preLinkingPhase); + + // super types etc + setSuperType(tclass, n4Class, preLinkingPhase); + addImplementedInterfaces(tclass, n4Class, preLinkingPhase); + + // members + addFields(tclass, n4Class, preLinkingPhase); + addMethods(tclass, n4Class, target, preLinkingPhase); + + addGetters(tclass, n4Class, target, preLinkingPhase); + addSetters(tclass, n4Class, target, preLinkingPhase); + + _n4JSTypesBuilderHelper.copyAnnotations(tclass, n4Class, preLinkingPhase); + + // + set "bindings" (derived refs from ast to types and vice versa) + tclass.setAstElement(n4Class); + n4Class.setDefinedType(tclass); + + target.getTypes().add(tclass); + + return tclass; + } + + void createTClass(N4ClassExpression n4Class, AbstractNamespace target, boolean preLinkingPhase) { + TClass tclass = createTClass(n4Class); + + // super types etc + setSuperType(tclass, n4Class, preLinkingPhase); + addImplementedInterfaces(tclass, n4Class, preLinkingPhase); + + // members + addFields(tclass, n4Class, preLinkingPhase); + addMethods(tclass, n4Class, target, preLinkingPhase); + + addGetters(tclass, n4Class, target, preLinkingPhase); + addSetters(tclass, n4Class, target, preLinkingPhase); + + _n4JSTypesBuilderHelper.copyAnnotations(tclass, n4Class, preLinkingPhase); + + // + set "bindings" (derived refs from ast to types and vice versa) + tclass.setAstElement(n4Class); + n4Class.setDefinedType(tclass); + + target.getContainingModule().getInternalTypes().add(tclass); + } + + private TClass createTClass(N4ClassDeclaration classDecl) { + TClass tclass = TypesFactory.eINSTANCE.createTClass(); + tclass.setName(classDecl.getName()); + tclass.setExternal(classDecl.isExternal()); + tclass.setDeclaredAbstract(classDecl.isAbstract()); + tclass.setDeclaredFinal(AnnotationDefinition.FINAL.hasAnnotation(classDecl)); + tclass.setObservable(AnnotationDefinition.OBSERVABLE.hasAnnotation(classDecl)); + tclass.setDeclaredEcmaScript(AnnotationDefinition.ECMASCRIPT.hasAnnotation(classDecl)); + + return tclass; + } + + private TClass createTClass(N4ClassExpression classExpr) { + TClass tclass = TypesFactory.eINSTANCE.createTClass(); + tclass.setName(classExpr.getName()); + return tclass; + } + + private void setSuperType(TClass tclass, N4ClassDefinition classDecl, boolean preLinkingPhase) { + if (!preLinkingPhase) { + TypeReferenceNode scr = classDecl.getSuperClassRef(); + tclass.setSuperClassRef(TypeUtils.copyWithProxies(scr == null ? null : scr.getTypeRefInAST())); + } + } + + private void addImplementedInterfaces(TClass tclass, N4ClassDefinition classDecl, boolean preLinkingPhase) { + if (!preLinkingPhase) { + _n4JSTypesBuilderHelper.addCopyOfReferences(tclass.getImplementedInterfaceRefs(), + toList(map(classDecl.getImplementedInterfaceRefs(), ir -> ir.getTypeRefInAST()))); + } + } +} diff --git a/plugins/org.eclipse.n4js/src/org/eclipse/n4js/typesbuilder/N4JSClassDeclarationTypesBuilder.xtend b/plugins/org.eclipse.n4js/src/org/eclipse/n4js/typesbuilder/N4JSClassDeclarationTypesBuilder.xtend deleted file mode 100644 index f99f316f83..0000000000 --- a/plugins/org.eclipse.n4js/src/org/eclipse/n4js/typesbuilder/N4JSClassDeclarationTypesBuilder.xtend +++ /dev/null @@ -1,122 +0,0 @@ -/** - * Copyright (c) 2016 NumberFour AG. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * NumberFour AG - Initial API and implementation - */ -package org.eclipse.n4js.typesbuilder - -import org.eclipse.n4js.AnnotationDefinition -import org.eclipse.n4js.n4JS.N4ClassDeclaration -import org.eclipse.n4js.n4JS.N4ClassDefinition -import org.eclipse.n4js.n4JS.N4ClassExpression -import org.eclipse.n4js.ts.types.AbstractNamespace -import org.eclipse.n4js.ts.types.TClass -import org.eclipse.n4js.ts.types.TypesFactory -import org.eclipse.n4js.types.utils.TypeUtils -import org.eclipse.n4js.utils.N4JSLanguageUtils - -public class N4JSClassDeclarationTypesBuilder extends N4JSClassifierDeclarationTypesBuilder { - - def protected boolean relinkTClass(N4ClassDeclaration n4Class, AbstractNamespace target, boolean preLinkingPhase, int idx) { - if (n4Class.name === null) { // may be null due to syntax errors - return false; - } - - val TClass tclass = target.types.get(idx) as TClass - - tclass.relinkClassifierAndMembers(n4Class, preLinkingPhase); - return true; - } - - def protected TClass createTClass(N4ClassDeclaration n4Class, AbstractNamespace target, boolean preLinkingPhase) { - if (n4Class.name === null) { // may be null due to syntax errors - return null; - } - - val TClass tclass = n4Class.createTClass; - // modifiers - tclass.setTypeAccessModifier(n4Class); - tclass.setProvidedByRuntime(n4Class, preLinkingPhase); - tclass.declaredNonStaticPolyfill = N4JSLanguageUtils.isNonStaticPolyfill(n4Class); - tclass.declaredStaticPolyfill = N4JSLanguageUtils.isStaticPolyfill(n4Class); - tclass.declaredCovariantConstructor = n4Class.isDeclaredCovariantConstructor; - tclass.addTypeParameters(n4Class, preLinkingPhase); - - // super types etc - tclass.setSuperType(n4Class, preLinkingPhase); - tclass.addImplementedInterfaces(n4Class, preLinkingPhase); - - // members - tclass.addFields(n4Class, preLinkingPhase); - tclass.addMethods(n4Class, target, preLinkingPhase); - - tclass.addGetters(n4Class, target, preLinkingPhase); - tclass.addSetters(n4Class, target, preLinkingPhase); - - tclass.copyAnnotations(n4Class, preLinkingPhase); - - // + set "bindings" (derived refs from ast to types and vice versa) - tclass.astElement = n4Class; - n4Class.definedType = tclass; - - target.types += tclass; - - return tclass; - } - - def package createTClass(N4ClassExpression n4Class, AbstractNamespace target, boolean preLinkingPhase) { - val tclass = n4Class.createTClass; - - // super types etc - tclass.setSuperType(n4Class, preLinkingPhase); - tclass.addImplementedInterfaces(n4Class, preLinkingPhase); - - // members - tclass.addFields(n4Class, preLinkingPhase); - tclass.addMethods(n4Class, target, preLinkingPhase); - - tclass.addGetters(n4Class, target, preLinkingPhase); - tclass.addSetters(n4Class, target, preLinkingPhase); - - tclass.copyAnnotations(n4Class, preLinkingPhase); - - // + set "bindings" (derived refs from ast to types and vice versa) - tclass.astElement = n4Class; - n4Class.definedType = tclass; - - target.containingModule.internalTypes += tclass; - } - - def private createTClass(N4ClassDeclaration classDecl) { - val tclass = TypesFactory::eINSTANCE.createTClass(); - tclass.name = classDecl.name; - tclass.external = classDecl.external; - tclass.declaredAbstract = classDecl.abstract; - tclass.declaredFinal = AnnotationDefinition.FINAL.hasAnnotation(classDecl); - tclass.observable = AnnotationDefinition.OBSERVABLE.hasAnnotation(classDecl); - tclass.declaredEcmaScript = AnnotationDefinition.ECMASCRIPT.hasAnnotation(classDecl); - - return tclass; - } - - def private createTClass(N4ClassExpression classExpr) { - val tclass = TypesFactory::eINSTANCE.createTClass(); - tclass.name = classExpr.name; - return tclass; - } - - def private setSuperType(TClass tclass, N4ClassDefinition classDecl, boolean preLinkingPhase) { - if (!preLinkingPhase) - tclass.superClassRef = TypeUtils.copyWithProxies(classDecl.superClassRef?.typeRefInAST); - } - - def private addImplementedInterfaces(TClass tclass, N4ClassDefinition classDecl, boolean preLinkingPhase) { - if (!preLinkingPhase) - addCopyOfReferences(tclass.implementedInterfaceRefs, classDecl.implementedInterfaceRefs.map[typeRefInAST]); - } -} diff --git a/plugins/org.eclipse.n4js/src/org/eclipse/n4js/typesbuilder/N4JSClassifierDeclarationTypesBuilder.java b/plugins/org.eclipse.n4js/src/org/eclipse/n4js/typesbuilder/N4JSClassifierDeclarationTypesBuilder.java new file mode 100644 index 0000000000..66d6e38c4a --- /dev/null +++ b/plugins/org.eclipse.n4js/src/org/eclipse/n4js/typesbuilder/N4JSClassifierDeclarationTypesBuilder.java @@ -0,0 +1,167 @@ +/** + * Copyright (c) 2017 NumberFour AG. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * NumberFour AG - Initial API and implementation + */ +package org.eclipse.n4js.typesbuilder; + +import static org.eclipse.xtext.xbase.lib.IterableExtensions.filter; +import static org.eclipse.xtext.xbase.lib.IterableExtensions.filterNull; +import static org.eclipse.xtext.xbase.lib.IterableExtensions.map; +import static org.eclipse.xtext.xbase.lib.IterableExtensions.toList; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.eclipse.n4js.compileTime.CompileTimeEvaluator; +import org.eclipse.n4js.compileTime.CompileTimeValue; +import org.eclipse.n4js.compileTime.CompileTimeValue.ValueValid; +import org.eclipse.n4js.n4JS.Expression; +import org.eclipse.n4js.n4JS.N4ClassifierDeclaration; +import org.eclipse.n4js.n4JS.N4ClassifierDefinition; +import org.eclipse.n4js.n4JS.N4FieldDeclaration; +import org.eclipse.n4js.n4JS.N4GetterDeclaration; +import org.eclipse.n4js.n4JS.N4MemberDeclaration; +import org.eclipse.n4js.n4JS.N4MethodDeclaration; +import org.eclipse.n4js.n4JS.N4SetterDeclaration; +import org.eclipse.n4js.n4JS.PropertyNameOwner; +import org.eclipse.n4js.ts.types.AbstractNamespace; +import org.eclipse.n4js.ts.types.TClassifier; +import org.eclipse.n4js.ts.types.TField; +import org.eclipse.n4js.ts.types.TGetter; +import org.eclipse.n4js.ts.types.TMember; +import org.eclipse.n4js.ts.types.TMethod; +import org.eclipse.n4js.ts.types.TSetter; +import org.eclipse.n4js.typesystem.utils.RuleEnvironment; +import org.eclipse.n4js.typesystem.utils.RuleEnvironmentExtensions; +import org.eclipse.n4js.utils.N4JSLanguageUtils; + +import com.google.inject.Inject; + +/** + * Abstract base class for N4JSClassDeclarationTypesBuilder and N4JSInterfaceDeclarationTypesBuilder to provide reusable + * bits and pieces. + */ +abstract class N4JSClassifierDeclarationTypesBuilder { + + @Inject + protected N4JSTypesBuilderHelper _n4JSTypesBuilderHelper; + @Inject + protected N4JSTypeVariableTypesBuilder _n4JSTypeVariableTypesBuilder; + @Inject + protected N4JSFieldTypesBuilder _n4JSFieldTypesBuilder; + @Inject + protected N4JSMethodTypesBuilder _n4JSMethodTypesBuilder; + @Inject + protected N4JSGetterTypesBuilder _n4JSGetterTypesBuilder; + @Inject + protected N4JSSetterTypesBuilder _n4JSSetterTypesBuilder; + @Inject + protected CompileTimeEvaluator compileTimeEvaluator; + + protected void addFields(TClassifier classifier, N4ClassifierDefinition definition, boolean preLinkingPhase) { + Iterable n4Fields = filter(definition.getOwnedMembers(), N4FieldDeclaration.class); + List fields = toList( + filterNull(map(n4Fields, f -> _n4JSFieldTypesBuilder.createField(f, classifier, preLinkingPhase)))); + classifier.getOwnedMembers().addAll(fields); + } + + protected void addMethods(TClassifier classifier, N4ClassifierDefinition definition, AbstractNamespace target, + boolean preLinkingPhase) { + // note: won't include call/construct signatures + Iterable n4Methods = filter(definition.getOwnedMembers(), N4MethodDeclaration.class); + List methods = toList( + filterNull(map(n4Methods, m -> _n4JSMethodTypesBuilder.createMethod(m, target, preLinkingPhase)))); + classifier.getOwnedMembers().addAll(methods); + + N4MethodDeclaration cs = definition.getOwnedCallSignature(); + N4MethodDeclaration css = definition.getOwnedConstructSignature(); + classifier.setCallSignature( + cs == null ? null : _n4JSMethodTypesBuilder.createMethod(cs, target, preLinkingPhase)); + classifier.setConstructSignature( + css == null ? null : _n4JSMethodTypesBuilder.createMethod(css, target, preLinkingPhase)); + } + + protected void addGetters(TClassifier classifier, N4ClassifierDefinition definition, AbstractNamespace target, + boolean preLinkingPhase) { + // create also getters for all non private fields without explicit getter + Iterable n4Getters = filter(definition.getOwnedMembers(), N4GetterDeclaration.class); + List getters = toList(filterNull( + map(n4Getters, g -> _n4JSGetterTypesBuilder.createGetter(g, classifier, target, preLinkingPhase)))); + classifier.getOwnedMembers().addAll(getters); + } + + protected void addSetters(TClassifier classifier, N4ClassifierDefinition definition, AbstractNamespace target, + boolean preLinkingPhase) { + // create also getters for all non private fields without explicit getter + Iterable n4Setters = filter(definition.getOwnedMembers(), N4SetterDeclaration.class); + List setters = toList(filterNull( + map(n4Setters, s -> _n4JSSetterTypesBuilder.createSetter(s, classifier, target, preLinkingPhase)))); + classifier.getOwnedMembers().addAll(setters); + } + + void relinkClassifierAndMembers(TClassifier classifier, N4ClassifierDeclaration declaration, + boolean preLinkingPhase) { + _n4JSTypesBuilderHelper.ensureEqualName(declaration, classifier); + + // members + N4MethodDeclaration astCallSig = declaration.getOwnedCallSignature(); + if (astCallSig != null) { + _n4JSMethodTypesBuilder.relinkMethod(astCallSig, classifier.getCallSignature(), preLinkingPhase); + } + N4MethodDeclaration astConstructSig = declaration.getOwnedConstructSignature(); + if (astConstructSig != null) { + _n4JSMethodTypesBuilder.relinkMethod(astConstructSig, classifier.getConstructSignature(), preLinkingPhase); + } + + // OWNED members + Map memberByName = new HashMap<>(); + for (N4MemberDeclaration member : declaration.getOwnedMembersRaw()) { + PropertyNameOwner pno = (PropertyNameOwner) member; + if (member.getName() != null) { + memberByName.put(member.getName(), member); + } else if (pno.hasComputedPropertyName()) { + Expression expr = pno.getDeclaredName().getExpression(); + if (N4JSLanguageUtils.isProcessedAsCompileTimeExpression(expr)) { + RuleEnvironment G = RuleEnvironmentExtensions.newRuleEnvironment(pno); + CompileTimeValue ctv = compileTimeEvaluator.evaluateCompileTimeExpression(G, expr); + if (ctv.isValid()) { + String ctvName = ((ValueValid) ctv).getValue().toString(); + memberByName.put(ctvName, member); + } + } + } + } + + for (TMember tMember : classifier.getOwnedMembers()) { + N4MemberDeclaration member = memberByName.get(tMember.getName()); + if (tMember instanceof TField && member instanceof N4FieldDeclaration) { + _n4JSFieldTypesBuilder.relinkField((N4FieldDeclaration) member, (TField) tMember, preLinkingPhase); + } + if (tMember instanceof TMethod && member instanceof N4MethodDeclaration) { + N4MethodDeclaration method = (N4MethodDeclaration) member; + if (!method.isConstructSignature() && !method.isCallSignature()) { + _n4JSMethodTypesBuilder.relinkMethod(method, (TMethod) tMember, preLinkingPhase); + } + } + if (tMember instanceof TGetter && member instanceof N4GetterDeclaration) { + _n4JSGetterTypesBuilder.relinkGetter((N4GetterDeclaration) member, (TGetter) tMember, preLinkingPhase); + } + if (tMember instanceof TSetter && member instanceof N4SetterDeclaration) { + _n4JSSetterTypesBuilder.relinkSetter((N4SetterDeclaration) member, (TSetter) tMember, preLinkingPhase); + } + } + + // TODO proxy resolve vs setter invocation? + classifier.setAstElement(declaration); + // setter is ok here + declaration.setDefinedType(classifier); + } + +} diff --git a/plugins/org.eclipse.n4js/src/org/eclipse/n4js/typesbuilder/N4JSClassifierDeclarationTypesBuilder.xtend b/plugins/org.eclipse.n4js/src/org/eclipse/n4js/typesbuilder/N4JSClassifierDeclarationTypesBuilder.xtend deleted file mode 100644 index 1deeadd0a1..0000000000 --- a/plugins/org.eclipse.n4js/src/org/eclipse/n4js/typesbuilder/N4JSClassifierDeclarationTypesBuilder.xtend +++ /dev/null @@ -1,136 +0,0 @@ -/** - * Copyright (c) 2017 NumberFour AG. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * NumberFour AG - Initial API and implementation - */ -package org.eclipse.n4js.typesbuilder - -import com.google.inject.Inject -import java.util.HashMap -import java.util.Map -import org.eclipse.n4js.compileTime.CompileTimeEvaluator -import org.eclipse.n4js.compileTime.CompileTimeValue.ValueValid -import org.eclipse.n4js.n4JS.N4ClassifierDeclaration -import org.eclipse.n4js.n4JS.N4ClassifierDefinition -import org.eclipse.n4js.n4JS.N4FieldDeclaration -import org.eclipse.n4js.n4JS.N4GetterDeclaration -import org.eclipse.n4js.n4JS.N4MemberDeclaration -import org.eclipse.n4js.n4JS.N4MethodDeclaration -import org.eclipse.n4js.n4JS.N4SetterDeclaration -import org.eclipse.n4js.n4JS.PropertyNameOwner -import org.eclipse.n4js.ts.types.AbstractNamespace -import org.eclipse.n4js.ts.types.TClassifier -import org.eclipse.n4js.ts.types.TField -import org.eclipse.n4js.ts.types.TGetter -import org.eclipse.n4js.ts.types.TMethod -import org.eclipse.n4js.ts.types.TSetter -import org.eclipse.n4js.typesystem.utils.RuleEnvironmentExtensions -import org.eclipse.n4js.utils.N4JSLanguageUtils - -/** - * Abstract base class for N4JSClassDeclarationTypesBuilder and N4JSInterfaceDeclarationTypesBuilder - * to provide reusable bits and pieces. - */ -package abstract class N4JSClassifierDeclarationTypesBuilder { - - @Inject protected extension N4JSTypesBuilderHelper - @Inject protected extension N4JSTypeVariableTypesBuilder - @Inject protected extension N4JSFieldTypesBuilder - @Inject protected extension N4JSMethodTypesBuilder - @Inject protected extension N4JSGetterTypesBuilder - @Inject protected extension N4JSSetterTypesBuilder - @Inject protected CompileTimeEvaluator compileTimeEvaluator; - - def protected void addFields(TClassifier classifier, N4ClassifierDefinition definition, boolean preLinkingPhase) { - val n4Fields = definition.ownedMembers.filter(N4FieldDeclaration); - val fields = n4Fields.map[createField(classifier, preLinkingPhase)].filterNull - classifier.ownedMembers.addAll(fields); - } - - def protected void addMethods(TClassifier classifier, N4ClassifierDefinition definition, AbstractNamespace target, boolean preLinkingPhase) { - val n4Methods = definition.ownedMembers.filter(N4MethodDeclaration); // note: won't include call/construct signatures - val methods = n4Methods.map[createMethod(target, preLinkingPhase)].filterNull; - classifier.ownedMembers.addAll(methods); - classifier.callSignature = definition.ownedCallSignature?.createMethod(target, preLinkingPhase); - classifier.constructSignature = definition.ownedConstructSignature?.createMethod(target, preLinkingPhase); - } - - def protected void addGetters(TClassifier classifier, N4ClassifierDefinition definition, AbstractNamespace target, boolean preLinkingPhase) { - // create also getters for all non private fields without explicit getter - val n4Getters = definition.ownedMembers.filter(N4GetterDeclaration) - val getters = n4Getters.map[createGetter(classifier, target, preLinkingPhase)].filterNull - classifier.ownedMembers.addAll(getters); - } - - def protected void addSetters(TClassifier classifier, N4ClassifierDefinition definition, AbstractNamespace target, boolean preLinkingPhase) { - // create also getters for all non private fields without explicit getter - val n4Setters = definition.ownedMembers.filter(N4SetterDeclaration) - val setters = n4Setters.map[createSetter(classifier, target, preLinkingPhase)].filterNull - classifier.ownedMembers.addAll(setters); - } - - def package void relinkClassifierAndMembers(TClassifier classifier, N4ClassifierDeclaration declaration, boolean preLinkingPhase) { - ensureEqualName(declaration, classifier); - - // members - val astCallSig = declaration.ownedCallSignature; - if (astCallSig !== null ) { - relinkMethod(astCallSig, classifier.callSignature, preLinkingPhase) - } - val astConstructSig = declaration.ownedConstructSignature; - if (astConstructSig !== null) { - relinkMethod(astConstructSig, classifier.constructSignature, preLinkingPhase) - } - - // OWNED members - val Map memberByName = new HashMap(); - for (member : declaration.ownedMembersRaw) { - val pno = member as PropertyNameOwner; - if (member.name !== null) { - memberByName.put(member.name, member); - } else if (pno.hasComputedPropertyName) { - val expr = pno.declaredName.expression; - if (N4JSLanguageUtils.isProcessedAsCompileTimeExpression(expr)) { - val G = RuleEnvironmentExtensions.newRuleEnvironment(pno); - val ctv = compileTimeEvaluator.evaluateCompileTimeExpression(G, expr); - if (ctv.valid) { - val ctvName = (ctv as ValueValid).value.toString; - memberByName.put(ctvName, member); - } - } - } - } - - var idx = 0; - for (tMember : classifier.ownedMembers) { - val member = memberByName.get(tMember.name); - if (tMember instanceof TField && member instanceof N4FieldDeclaration) { - relinkField(member as N4FieldDeclaration, tMember as TField, preLinkingPhase); - } - if (tMember instanceof TMethod && member instanceof N4MethodDeclaration) { - val method = member as N4MethodDeclaration; - if (!method.isConstructSignature && !method.isCallSignature) { - relinkMethod(method, tMember as TMethod, preLinkingPhase); - } - } - if (tMember instanceof TGetter && member instanceof N4GetterDeclaration) { - relinkGetter(member as N4GetterDeclaration, tMember as TGetter, preLinkingPhase); - } - if (tMember instanceof TSetter && member instanceof N4SetterDeclaration) { - relinkSetter(member as N4SetterDeclaration, tMember as TSetter, preLinkingPhase); - } - idx++; - } - - // TODO proxy resolve vs setter invocation? - classifier.astElement = declaration; - // setter is ok here - declaration.definedType = classifier; - } - -} diff --git a/plugins/org.eclipse.n4js/src/org/eclipse/n4js/typesbuilder/N4JSInterfaceDeclarationTypesBuilder.java b/plugins/org.eclipse.n4js/src/org/eclipse/n4js/typesbuilder/N4JSInterfaceDeclarationTypesBuilder.java new file mode 100644 index 0000000000..0c5b5e55ac --- /dev/null +++ b/plugins/org.eclipse.n4js/src/org/eclipse/n4js/typesbuilder/N4JSInterfaceDeclarationTypesBuilder.java @@ -0,0 +1,88 @@ +/** + * Copyright (c) 2016 NumberFour AG. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * NumberFour AG - Initial API and implementation + */ +package org.eclipse.n4js.typesbuilder; + +import static org.eclipse.xtext.xbase.lib.IterableExtensions.map; +import static org.eclipse.xtext.xbase.lib.IterableExtensions.toList; + +import org.eclipse.n4js.n4JS.N4InterfaceDeclaration; +import org.eclipse.n4js.ts.types.AbstractNamespace; +import org.eclipse.n4js.ts.types.TInterface; +import org.eclipse.n4js.ts.types.TypesFactory; +import org.eclipse.n4js.ts.types.TypingStrategy; +import org.eclipse.n4js.utils.N4JSLanguageUtils; + +/***/ +public class N4JSInterfaceDeclarationTypesBuilder extends N4JSClassifierDeclarationTypesBuilder { + + boolean relinkTInterface(N4InterfaceDeclaration n4Interface, AbstractNamespace target, boolean preLinkingPhase, + int idx) { + if (n4Interface.getName() == null) { // may be null due to syntax errors + return false; + } + + TInterface interfaceType = (TInterface) target.getTypes().get(idx); + relinkClassifierAndMembers(interfaceType, n4Interface, preLinkingPhase); + return true; + } + + /***/ + protected TInterface createTInterface(N4InterfaceDeclaration n4Interface, AbstractNamespace target, + boolean preLinkingPhase) { + if (n4Interface.getName() == null) { + return null; + } + + TInterface interfaceType = createTInterface(n4Interface); + _n4JSTypesBuilderHelper.setTypeAccessModifier(interfaceType, n4Interface); + + interfaceType.setTypingStrategy( + (n4Interface.getTypingStrategy() == TypingStrategy.DEFAULT) ? TypingStrategy.DEFAULT + : // STRUCTURAL_FIELD is not allowed on def site, but maybe we got a wrong input + TypingStrategy.STRUCTURAL); + + _n4JSTypesBuilderHelper.setProvidedByRuntime(interfaceType, n4Interface, preLinkingPhase); + interfaceType.setDeclaredNonStaticPolyfill(N4JSLanguageUtils.isNonStaticPolyfill(n4Interface)); + interfaceType + .setDeclaredCovariantConstructor(_n4JSTypesBuilderHelper.isDeclaredCovariantConstructor(n4Interface)); + _n4JSTypeVariableTypesBuilder.addTypeParameters(interfaceType, n4Interface, preLinkingPhase); + addExtendedInterfaces(interfaceType, n4Interface, preLinkingPhase); + + addFields(interfaceType, n4Interface, preLinkingPhase); + addMethods(interfaceType, n4Interface, target, preLinkingPhase); + + addGetters(interfaceType, n4Interface, target, preLinkingPhase); + addSetters(interfaceType, n4Interface, target, preLinkingPhase); + + _n4JSTypesBuilderHelper.copyAnnotations(interfaceType, n4Interface, preLinkingPhase); + + interfaceType.setAstElement(n4Interface); + n4Interface.setDefinedType(interfaceType); + + target.getTypes().add(interfaceType); + return interfaceType; + } + + private TInterface createTInterface(N4InterfaceDeclaration n4Interface) { + TInterface interfaceType = TypesFactory.eINSTANCE.createTInterface(); + interfaceType.setName(n4Interface.getName()); + interfaceType.setExternal(n4Interface.isExternal()); + + return interfaceType; + } + + private void addExtendedInterfaces(TInterface interfaceType, N4InterfaceDeclaration c, boolean preLinkingPhase) { + if (!preLinkingPhase) { + _n4JSTypesBuilderHelper.addCopyOfReferences(interfaceType.getSuperInterfaceRefs(), + toList(map(c.getSuperInterfaceRefs(), sir -> sir.getTypeRefInAST()))); + } + } +} diff --git a/plugins/org.eclipse.n4js/src/org/eclipse/n4js/typesbuilder/N4JSInterfaceDeclarationTypesBuilder.xtend b/plugins/org.eclipse.n4js/src/org/eclipse/n4js/typesbuilder/N4JSInterfaceDeclarationTypesBuilder.xtend deleted file mode 100644 index d44dc65229..0000000000 --- a/plugins/org.eclipse.n4js/src/org/eclipse/n4js/typesbuilder/N4JSInterfaceDeclarationTypesBuilder.xtend +++ /dev/null @@ -1,80 +0,0 @@ -/** - * Copyright (c) 2016 NumberFour AG. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * NumberFour AG - Initial API and implementation - */ -package org.eclipse.n4js.typesbuilder - -import org.eclipse.n4js.n4JS.N4InterfaceDeclaration -import org.eclipse.n4js.ts.types.AbstractNamespace -import org.eclipse.n4js.ts.types.TInterface -import org.eclipse.n4js.ts.types.TypesFactory -import org.eclipse.n4js.ts.types.TypingStrategy -import org.eclipse.n4js.utils.N4JSLanguageUtils - -public class N4JSInterfaceDeclarationTypesBuilder extends N4JSClassifierDeclarationTypesBuilder { - - def package boolean relinkTInterface(N4InterfaceDeclaration n4Interface, AbstractNamespace target, boolean preLinkingPhase, int idx) { - if (n4Interface.name === null) { // may be null due to syntax errors - return false; - } - - val TInterface interfaceType = target.types.get(idx) as TInterface - interfaceType.relinkClassifierAndMembers(n4Interface, preLinkingPhase); - return true; - } - - def protected TInterface createTInterface(N4InterfaceDeclaration n4Interface, AbstractNamespace target, boolean preLinkingPhase) { - if (n4Interface.name === null) { - return null; - } - - val interfaceType = createTInterface(n4Interface); - interfaceType.setTypeAccessModifier(n4Interface) - - interfaceType.setTypingStrategy( - if (n4Interface.typingStrategy === TypingStrategy.DEFAULT) { - TypingStrategy.DEFAULT - } else { // STRUCTURAL_FIELD is not allowed on def site, but maybe we got a wrong input - TypingStrategy.STRUCTURAL - }) - - interfaceType.setProvidedByRuntime(n4Interface, preLinkingPhase) - interfaceType.declaredNonStaticPolyfill = N4JSLanguageUtils.isNonStaticPolyfill(n4Interface); - interfaceType.declaredCovariantConstructor = n4Interface.isDeclaredCovariantConstructor; - interfaceType.addTypeParameters(n4Interface, preLinkingPhase) - interfaceType.addExtendedInterfaces(n4Interface, preLinkingPhase) - - interfaceType.addFields(n4Interface, preLinkingPhase) - interfaceType.addMethods(n4Interface, target, preLinkingPhase) - - interfaceType.addGetters(n4Interface, target, preLinkingPhase) - interfaceType.addSetters(n4Interface, target, preLinkingPhase) - - interfaceType.copyAnnotations(n4Interface, preLinkingPhase) - - interfaceType.astElement = n4Interface - n4Interface.definedType = interfaceType - - target.types += interfaceType - return interfaceType; - } - - def private TInterface createTInterface(N4InterfaceDeclaration n4Interface) { - val interfaceType = TypesFactory::eINSTANCE.createTInterface(); - interfaceType.name = n4Interface.name; - interfaceType.external = n4Interface.external; - - return interfaceType - } - - def private void addExtendedInterfaces(TInterface interfaceType, N4InterfaceDeclaration c, boolean preLinkingPhase) { - if (!preLinkingPhase) - addCopyOfReferences(interfaceType.superInterfaceRefs, c.superInterfaceRefs.map[typeRefInAST]) - } -} diff --git a/plugins/org.eclipse.n4js/src/org/eclipse/n4js/typesbuilder/N4JSNamespaceDeclarationTypesBuilder.java b/plugins/org.eclipse.n4js/src/org/eclipse/n4js/typesbuilder/N4JSNamespaceDeclarationTypesBuilder.java new file mode 100644 index 0000000000..9564275b42 --- /dev/null +++ b/plugins/org.eclipse.n4js/src/org/eclipse/n4js/typesbuilder/N4JSNamespaceDeclarationTypesBuilder.java @@ -0,0 +1,63 @@ +/** + * Copyright (c) 2016 NumberFour AG. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * NumberFour AG - Initial API and implementation + */ +package org.eclipse.n4js.typesbuilder; + +import org.eclipse.n4js.n4JS.N4NamespaceDeclaration; +import org.eclipse.n4js.ts.types.AbstractNamespace; +import org.eclipse.n4js.ts.types.TNamespace; +import org.eclipse.n4js.ts.types.TypesFactory; + +@SuppressWarnings("javadoc") +public class N4JSNamespaceDeclarationTypesBuilder extends N4JSClassifierDeclarationTypesBuilder { + + boolean relinkTNamespace(N4NamespaceDeclaration n4Namespace, AbstractNamespace target, + @SuppressWarnings("unused") boolean preLinkingPhase, int idx) { + + if (n4Namespace.getName() == null) { // may be null due to syntax errors + return false; + } + + TNamespace namespaceType = target.getNamespaces().get(idx); + _n4JSTypesBuilderHelper.ensureEqualName(n4Namespace, namespaceType); + + namespaceType.setAstElement(n4Namespace); + n4Namespace.setDefinedType(namespaceType); + + return true; + } + + protected TNamespace createTNamespace(N4NamespaceDeclaration n4Namespace, AbstractNamespace target, + boolean preLinkingPhase) { + if (n4Namespace.getName() == null) { + return null; + } + + TNamespace namespaceType = createTNamespace(n4Namespace); + _n4JSTypesBuilderHelper.setTypeAccessModifier(namespaceType, n4Namespace); + + _n4JSTypesBuilderHelper.setProvidedByRuntime(namespaceType, n4Namespace, preLinkingPhase); + + namespaceType.setAstElement(n4Namespace); + n4Namespace.setDefinedType(namespaceType); + + target.getNamespaces().add(namespaceType); + return namespaceType; + } + + private TNamespace createTNamespace(N4NamespaceDeclaration n4Namespace) { + TNamespace namespaceType = TypesFactory.eINSTANCE.createTNamespace(); + namespaceType.setName(n4Namespace.getName()); + namespaceType.setExternal(n4Namespace.isExternal()); + + return namespaceType; + } + +} diff --git a/plugins/org.eclipse.n4js/src/org/eclipse/n4js/typesbuilder/N4JSNamespaceDeclarationTypesBuilder.xtend b/plugins/org.eclipse.n4js/src/org/eclipse/n4js/typesbuilder/N4JSNamespaceDeclarationTypesBuilder.xtend deleted file mode 100644 index dde09b61a0..0000000000 --- a/plugins/org.eclipse.n4js/src/org/eclipse/n4js/typesbuilder/N4JSNamespaceDeclarationTypesBuilder.xtend +++ /dev/null @@ -1,60 +0,0 @@ -/** - * Copyright (c) 2016 NumberFour AG. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * NumberFour AG - Initial API and implementation - */ -package org.eclipse.n4js.typesbuilder - -import org.eclipse.n4js.n4JS.N4NamespaceDeclaration -import org.eclipse.n4js.ts.types.AbstractNamespace -import org.eclipse.n4js.ts.types.TNamespace -import org.eclipse.n4js.ts.types.TypesFactory - -public class N4JSNamespaceDeclarationTypesBuilder extends N4JSClassifierDeclarationTypesBuilder { - - def package boolean relinkTNamespace(N4NamespaceDeclaration n4Namespace, AbstractNamespace target, boolean preLinkingPhase, int idx) { - if (n4Namespace.name === null) { // may be null due to syntax errors - return false; - } - - val TNamespace namespaceType = target.namespaces.get(idx) - ensureEqualName(n4Namespace, namespaceType); - - namespaceType.astElement = n4Namespace - n4Namespace.definedType = namespaceType - - return true; - } - - def protected TNamespace createTNamespace(N4NamespaceDeclaration n4Namespace, AbstractNamespace target, boolean preLinkingPhase) { - if (n4Namespace.name === null) { - return null; - } - - val namespaceType = createTNamespace(n4Namespace); - namespaceType.setTypeAccessModifier(n4Namespace) - - - namespaceType.setProvidedByRuntime(n4Namespace, preLinkingPhase) - - namespaceType.astElement = n4Namespace - n4Namespace.definedType = namespaceType - - target.namespaces += namespaceType - return namespaceType; - } - - def private TNamespace createTNamespace(N4NamespaceDeclaration n4Namespace) { - val namespaceType = TypesFactory::eINSTANCE.createTNamespace(); - namespaceType.name = n4Namespace.name; - namespaceType.external = n4Namespace.external; - - return namespaceType - } - -}