Skip to content

Commit 90cde53

Browse files
author
Quang Tran
committed
IDE-2391: Merge branch 'master' into IDE-2391
2 parents c4a1648 + 3c28d02 commit 90cde53

File tree

18 files changed

+1447
-1201
lines changed

18 files changed

+1447
-1201
lines changed

plugins/eu.numberfour.n4js.ts.model/emf-gen/eu/numberfour/n4js/ts/types/TEnumLiteral.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,13 @@ public interface TEnumLiteral extends SyntaxRelatedTElement, IdentifiableElement
5555
*/
5656
void setValue(String value);
5757

58+
/**
59+
* <!-- begin-user-doc -->
60+
* <!-- end-user-doc -->
61+
* @model kind="operation" unique="false"
62+
* annotation="http://www.eclipse.org/emf/2002/GenModel body='<%java.lang.String%> _elvis = null;\n<%java.lang.String%> _value = this.getValue();\nif (_value != null)\n{\n\t_elvis = _value;\n} else\n{\n\t<%java.lang.String%> _name = this.getName();\n\t_elvis = _name;\n}\nreturn _elvis;'"
63+
* @generated
64+
*/
65+
String getValueOrName();
66+
5867
} // TEnumLiteral

plugins/eu.numberfour.n4js.ts.model/emf-gen/eu/numberfour/n4js/ts/types/TypesPackage.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10789,14 +10789,23 @@ public interface TypesPackage extends EPackage {
1078910789
*/
1079010790
int TENUM_LITERAL___GET_CONTAINING_MODULE = SYNTAX_RELATED_TELEMENT_OPERATION_COUNT + 0;
1079110791

10792+
/**
10793+
* The operation id for the '<em>Get Value Or Name</em>' operation.
10794+
* <!-- begin-user-doc -->
10795+
* <!-- end-user-doc -->
10796+
* @generated
10797+
* @ordered
10798+
*/
10799+
int TENUM_LITERAL___GET_VALUE_OR_NAME = SYNTAX_RELATED_TELEMENT_OPERATION_COUNT + 1;
10800+
1079210801
/**
1079310802
* The number of operations of the '<em>TEnum Literal</em>' class.
1079410803
* <!-- begin-user-doc -->
1079510804
* <!-- end-user-doc -->
1079610805
* @generated
1079710806
* @ordered
1079810807
*/
10799-
int TENUM_LITERAL_OPERATION_COUNT = SYNTAX_RELATED_TELEMENT_OPERATION_COUNT + 1;
10808+
int TENUM_LITERAL_OPERATION_COUNT = SYNTAX_RELATED_TELEMENT_OPERATION_COUNT + 2;
1080010809

1080110810
/**
1080210811
* The meta object id for the '{@link eu.numberfour.n4js.ts.types.impl.TVariableImpl <em>TVariable</em>}' class.
@@ -13494,6 +13503,16 @@ public interface TypesPackage extends EPackage {
1349413503
*/
1349513504
EAttribute getTEnumLiteral_Value();
1349613505

13506+
/**
13507+
* Returns the meta object for the '{@link eu.numberfour.n4js.ts.types.TEnumLiteral#getValueOrName() <em>Get Value Or Name</em>}' operation.
13508+
* <!-- begin-user-doc -->
13509+
* <!-- end-user-doc -->
13510+
* @return the meta object for the '<em>Get Value Or Name</em>' operation.
13511+
* @see eu.numberfour.n4js.ts.types.TEnumLiteral#getValueOrName()
13512+
* @generated
13513+
*/
13514+
EOperation getTEnumLiteral__GetValueOrName();
13515+
1349713516
/**
1349813517
* Returns the meta object for class '{@link eu.numberfour.n4js.ts.types.SyntaxRelatedTElement <em>Syntax Related TElement</em>}'.
1349913518
* <!-- begin-user-doc -->
@@ -15682,6 +15701,14 @@ interface Literals {
1568215701
*/
1568315702
EAttribute TENUM_LITERAL__VALUE = eINSTANCE.getTEnumLiteral_Value();
1568415703

15704+
/**
15705+
* The meta object literal for the '<em><b>Get Value Or Name</b></em>' operation.
15706+
* <!-- begin-user-doc -->
15707+
* <!-- end-user-doc -->
15708+
* @generated
15709+
*/
15710+
EOperation TENUM_LITERAL___GET_VALUE_OR_NAME = eINSTANCE.getTEnumLiteral__GetValueOrName();
15711+
1568515712
/**
1568615713
* The meta object literal for the '{@link eu.numberfour.n4js.ts.types.impl.SyntaxRelatedTElementImpl <em>Syntax Related TElement</em>}' class.
1568715714
* <!-- begin-user-doc -->

plugins/eu.numberfour.n4js.ts.model/emf-gen/eu/numberfour/n4js/ts/types/impl/TEnumLiteralImpl.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,23 @@ public void setValue(String newValue) {
141141
eNotify(new ENotificationImpl(this, Notification.SET, TypesPackage.TENUM_LITERAL__VALUE, oldValue, value));
142142
}
143143

144+
/**
145+
* <!-- begin-user-doc -->
146+
* <!-- end-user-doc -->
147+
* @generated
148+
*/
149+
public String getValueOrName() {
150+
String _elvis = null;
151+
String _value = this.getValue();
152+
if (_value != null) {
153+
_elvis = _value;
154+
} else {
155+
String _name = this.getName();
156+
_elvis = _name;
157+
}
158+
return _elvis;
159+
}
160+
144161
/**
145162
* <!-- begin-user-doc -->
146163
* <!-- end-user-doc -->
@@ -289,6 +306,8 @@ public int eDerivedOperationID(int baseOperationID, Class<?> baseClass) {
289306
@Override
290307
public Object eInvoke(int operationID, EList<?> arguments) throws InvocationTargetException {
291308
switch (operationID) {
309+
case TypesPackage.TENUM_LITERAL___GET_VALUE_OR_NAME:
310+
return getValueOrName();
292311
case TypesPackage.TENUM_LITERAL___GET_CONTAINING_MODULE:
293312
return getContainingModule();
294313
}

plugins/eu.numberfour.n4js.ts.model/emf-gen/eu/numberfour/n4js/ts/types/impl/TypesPackageImpl.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2705,6 +2705,15 @@ public EAttribute getTEnumLiteral_Value() {
27052705
return (EAttribute)tEnumLiteralEClass.getEStructuralFeatures().get(0);
27062706
}
27072707

2708+
/**
2709+
* <!-- begin-user-doc -->
2710+
* <!-- end-user-doc -->
2711+
* @generated
2712+
*/
2713+
public EOperation getTEnumLiteral__GetValueOrName() {
2714+
return tEnumLiteralEClass.getEOperations().get(0);
2715+
}
2716+
27082717
/**
27092718
* <!-- begin-user-doc -->
27102719
* <!-- end-user-doc -->
@@ -3177,6 +3186,7 @@ public void createPackageContents() {
31773186

31783187
tEnumLiteralEClass = createEClass(TENUM_LITERAL);
31793188
createEAttribute(tEnumLiteralEClass, TENUM_LITERAL__VALUE);
3189+
createEOperation(tEnumLiteralEClass, TENUM_LITERAL___GET_VALUE_OR_NAME);
31803190

31813191
syntaxRelatedTElementEClass = createEClass(SYNTAX_RELATED_TELEMENT);
31823192
createEReference(syntaxRelatedTElementEClass, SYNTAX_RELATED_TELEMENT__AST_ELEMENT);
@@ -3742,6 +3752,8 @@ public void initializePackageContents() {
37423752
initEClass(tEnumLiteralEClass, TEnumLiteral.class, "TEnumLiteral", !IS_ABSTRACT, !IS_INTERFACE, IS_GENERATED_INSTANCE_CLASS);
37433753
initEAttribute(getTEnumLiteral_Value(), theEcorePackage.getEString(), "value", null, 0, 1, TEnumLiteral.class, !IS_TRANSIENT, !IS_VOLATILE, IS_CHANGEABLE, !IS_UNSETTABLE, !IS_ID, !IS_UNIQUE, !IS_DERIVED, IS_ORDERED);
37443754

3755+
initEOperation(getTEnumLiteral__GetValueOrName(), theEcorePackage.getEString(), "getValueOrName", 0, 1, !IS_UNIQUE, IS_ORDERED);
3756+
37453757
initEClass(syntaxRelatedTElementEClass, SyntaxRelatedTElement.class, "SyntaxRelatedTElement", IS_ABSTRACT, !IS_INTERFACE, IS_GENERATED_INSTANCE_CLASS);
37463758
initEReference(getSyntaxRelatedTElement_AstElement(), theEcorePackage.getEObject(), null, "astElement", null, 0, 1, SyntaxRelatedTElement.class, !IS_TRANSIENT, !IS_VOLATILE, IS_CHANGEABLE, !IS_COMPOSITE, IS_RESOLVE_PROXIES, !IS_UNSETTABLE, IS_UNIQUE, !IS_DERIVED, IS_ORDERED);
37473759

plugins/eu.numberfour.n4js.ts.model/model/Types.xcore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,6 +1468,10 @@ class TEnum extends DeclaredTypeWithAccessModifier, SyntaxRelatedTElement {
14681468
*/
14691469
class TEnumLiteral extends SyntaxRelatedTElement, IdentifiableElement {
14701470
String value
1471+
1472+
op String getValueOrName() {
1473+
return value ?: name;
1474+
}
14711475
}
14721476

14731477
/*

plugins/eu.numberfour.n4js/src/eu/numberfour/n4js/resource/N4JSPreProcessor.xtend

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ package final class N4JSPreProcessor {
6565
if (target instanceof IdentifierRef) {
6666
if (target.idAsText == 'Symbol') {
6767
return ComputedPropertyNameValueConverter.SYMBOL_IDENTIFIER_PREFIX + expr.propertyAsText;
68+
} else {
69+
// this case implements support for literals of @StringBased enums
70+
// (but here we cannot ensure that 'target' is actually a @StringBased enum, because that would require
71+
// resolution of proxies which we are not allowed to do)
72+
return expr.propertyAsText;
6873
}
6974
}
7075
}

plugins/eu.numberfour.n4js/src/eu/numberfour/n4js/typesystem/n4js.xsemantics

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -621,13 +621,13 @@ from {
621621
G |- expr.target : var TypeRef targetTypeRef
622622
targetTypeRef = typeSystemHelper.resolveType(G, targetTypeRef);
623623

624-
val accessedBuiltInSymbol = G.getAccessedBuiltInSymbol(expr.index);
624+
val memberName = N4JSLanguageUtils.getMemberNameForIndexExpression(G, expr.index);
625625
val elementType = targetTypeRef.declaredType?.elementType
626626

627-
if (accessedBuiltInSymbol!==null) {
627+
if (memberName!==null && memberName.startsWith(N4JSLanguageUtils.SYMBOL_IDENTIFIER_PREFIX)) {
628+
// the index (inside the []) is a symbol
628629
val declType = targetTypeRef.declaredType;
629630
if(declType instanceof ContainerType<?>) {
630-
val memberName = '#' + accessedBuiltInSymbol.name;
631631
val member = containerTypesHelper.fromContext(expr).findMember(declType,memberName,false,false); // TODO support for static access?
632632
if(member!==null) {
633633
G |- member : var TypeRef memberTypeRef
@@ -644,22 +644,11 @@ from {
644644
else {
645645
T = G.anyTypeRef
646646
}
647-
} else if (elementType !== null) {
648-
val declaredType = targetTypeRef.declaredType
649-
if (declaredType.generic && targetTypeRef.typeArgs.isEmpty) {
650-
T = G.anyTypeRef // later: evaluate name if possible, we may even want to return smth like intersect(allProperties)
651-
} else {
652-
val G2 = G.wrap
653-
typeSystemHelper.addSubstitutions(G2, targetTypeRef)
654-
G2.addThisType(targetTypeRef)
655-
G2 |- elementType ~> T
656-
}
657-
} else if (expr.index instanceof StringLiteral) {
647+
} else if (memberName!==null) {
658648
// indexing via constant computed-name, sub-cases: static or instance member, for the latter nominally-typed or structurally-typed receiver
659649
val staticAccess = (targetTypeRef instanceof TypeTypeRef)
660650
val checkVisibility = false // access modifiers checked in validation
661651
val scope = memberScopingHelper.createMemberScopeFor(targetTypeRef, expr, checkVisibility, staticAccess)
662-
val memberName = (expr.index as StringLiteral).value;
663652
val member = memberScopingHelper.findUniqueMemberForName(scope, memberName, staticAccess)
664653
if(member != null) {
665654
G |- member : var TypeRef memberTypeRef
@@ -670,6 +659,16 @@ from {
670659
} else {
671660
T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef
672661
}
662+
} else if (elementType !== null) {
663+
val declaredType = targetTypeRef.declaredType
664+
if (declaredType.generic && targetTypeRef.typeArgs.isEmpty) {
665+
T = G.anyTypeRef // later: evaluate name if possible, we may even want to return smth like intersect(allProperties)
666+
} else {
667+
val G2 = G.wrap
668+
typeSystemHelper.addSubstitutions(G2, targetTypeRef)
669+
G2.addThisType(targetTypeRef)
670+
G2 |- elementType ~> T
671+
}
673672
} else if (targetTypeRef.dynamic) {
674673
T = G.anyTypeRefDynamic
675674
} else {

plugins/eu.numberfour.n4js/src/eu/numberfour/n4js/utils/N4JSLanguageUtils.xtend

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ import eu.numberfour.n4js.conversion.IdentifierValueConverter
1616
import eu.numberfour.n4js.n4JS.AbstractAnnotationList
1717
import eu.numberfour.n4js.n4JS.AnnotableElement
1818
import eu.numberfour.n4js.n4JS.ExportedVariableDeclaration
19+
import eu.numberfour.n4js.n4JS.Expression
1920
import eu.numberfour.n4js.n4JS.FormalParameter
2021
import eu.numberfour.n4js.n4JS.FunctionDeclaration
2122
import eu.numberfour.n4js.n4JS.FunctionDefinition
23+
import eu.numberfour.n4js.n4JS.IndexedAccessExpression
2224
import eu.numberfour.n4js.n4JS.N4ClassDeclaration
2325
import eu.numberfour.n4js.n4JS.N4ClassifierDeclaration
2426
import eu.numberfour.n4js.n4JS.N4EnumLiteral
@@ -29,10 +31,12 @@ import eu.numberfour.n4js.n4JS.N4MemberAnnotationList
2931
import eu.numberfour.n4js.n4JS.N4MemberDeclaration
3032
import eu.numberfour.n4js.n4JS.N4MethodDeclaration
3133
import eu.numberfour.n4js.n4JS.NumericLiteral
34+
import eu.numberfour.n4js.n4JS.ParameterizedPropertyAccessExpression
3235
import eu.numberfour.n4js.n4JS.PropertyAssignment
3336
import eu.numberfour.n4js.n4JS.PropertyAssignmentAnnotationList
3437
import eu.numberfour.n4js.n4JS.PropertyMethodDeclaration
3538
import eu.numberfour.n4js.n4JS.Script
39+
import eu.numberfour.n4js.n4JS.StringLiteral
3640
import eu.numberfour.n4js.n4JS.TypeDefiningElement
3741
import eu.numberfour.n4js.n4JS.UnaryExpression
3842
import eu.numberfour.n4js.n4JS.UnaryOperator
@@ -52,6 +56,7 @@ import eu.numberfour.n4js.ts.types.MemberAccessModifier
5256
import eu.numberfour.n4js.ts.types.TAnnotableElement
5357
import eu.numberfour.n4js.ts.types.TClass
5458
import eu.numberfour.n4js.ts.types.TClassifier
59+
import eu.numberfour.n4js.ts.types.TEnumLiteral
5560
import eu.numberfour.n4js.ts.types.TField
5661
import eu.numberfour.n4js.ts.types.TFunction
5762
import eu.numberfour.n4js.ts.types.TMember
@@ -599,4 +604,40 @@ class N4JSLanguageUtils {
599604
def static TypeRef getTypeVariableImplicitUpperBound(RuleEnvironment G) {
600605
return G.anyTypeRef;
601606
}
607+
608+
/**
609+
* Tells if the given expression is valid as an index within an {@link IndexedAccessExpression}.
610+
*/
611+
def static boolean isValidIndexExpression(RuleEnvironment G, Expression indexExpr) {
612+
if(indexExpr instanceof NumericLiteral || indexExpr instanceof StringLiteral) {
613+
return true;
614+
} else if(G.getAccessedBuiltInSymbol(indexExpr)!==null) {
615+
return true;
616+
} else if(indexExpr instanceof ParameterizedPropertyAccessExpression) {
617+
return indexExpr.property instanceof TEnumLiteral;
618+
}
619+
return false;
620+
}
621+
/**
622+
* If the given expression is a {@link #isValidIndexExpression(RuleEnvironment, Expression) valid index expression}
623+
* but is *not* numerical, then this method will return the name of the member the index access expression is
624+
* referring to. Returns <code>null</code> if the expression is invalid or numerical.
625+
*/
626+
def static String getMemberNameForIndexExpression(RuleEnvironment G, Expression indexExpr) {
627+
val accessedBuiltInSymbol = G.getAccessedBuiltInSymbol(indexExpr);
628+
if(accessedBuiltInSymbol!==null) {
629+
return SYMBOL_IDENTIFIER_PREFIX + accessedBuiltInSymbol.name;
630+
} else {
631+
return switch(indexExpr) {
632+
StringLiteral:
633+
indexExpr.value
634+
ParameterizedPropertyAccessExpression: {
635+
val prop = indexExpr.property;
636+
if(prop instanceof TEnumLiteral) {
637+
prop.valueOrName
638+
}
639+
}
640+
};
641+
}
642+
}
602643
}

0 commit comments

Comments
 (0)