use of org.eclipse.xsemantics.runtime.RuleEnvironment in project n4js by eclipse.
the class InternalTypeSystem method applyRuleExpectedTypeOfArgument.
protected Result<TypeRef> applyRuleExpectedTypeOfArgument(final RuleEnvironment G, final RuleApplicationTrace _trace_, final Argument argument, final Expression argumentExpression) throws RuleFailedException {
// output parameter
TypeRef T = null;
final EObject expr = argument.eContainer();
if ((expr instanceof NewExpression)) {
boolean _contains = ((NewExpression) expr).getArguments().contains(argument);
boolean _not = (!_contains);
if (_not) {
} else {
/* G |- expr.callee : var TypeTypeRef ctorTypeRef */
Expression _callee = ((NewExpression) expr).getCallee();
TypeTypeRef ctorTypeRef = null;
Result<TypeRef> result = typeInternal(G, _trace_, _callee);
checkAssignableTo(result.getFirst(), TypeTypeRef.class);
ctorTypeRef = (TypeTypeRef) result.getFirst();
TypeRef typeRefOfInstanceToCreate = this.typeSystemHelper.createTypeRefFromStaticType(G, ctorTypeRef, ((TypeArgument[]) Conversions.unwrapArray(((NewExpression) expr).getTypeArgs(), TypeArgument.class)));
Type _declaredType = typeRefOfInstanceToCreate.getDeclaredType();
ContainerType<?> typeOfInstanceToCreate = ((ContainerType<?>) _declaredType);
final RuleEnvironment G2 = RuleEnvironmentExtensions.wrap(G);
this.typeSystemHelper.addSubstitutions(G2, typeRefOfInstanceToCreate);
RuleEnvironmentExtensions.addThisType(G2, typeRefOfInstanceToCreate);
TMethod ctor = this.containerTypesHelper.fromContext(((NewExpression) expr).eResource()).findConstructor(typeOfInstanceToCreate);
TFormalParameter _fparForArgIdx = null;
if (ctor != null) {
_fparForArgIdx = ctor.getFparForArgIdx(ECollections.indexOf(((NewExpression) expr).getArguments(), argument, 0));
}
final TFormalParameter fpar = _fparForArgIdx;
if ((fpar == null)) {
T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef();
} else {
final TypeRef paramType = fpar.getTypeRef();
if ((paramType == null)) {
T = RuleEnvironmentExtensions.anyTypeRef(G2);
} else {
/* G2 |- paramType ~> T */
Result<TypeArgument> result_1 = substTypeVariablesInternal(G2, _trace_, paramType);
checkAssignableTo(result_1.getFirst(), TypeRef.class);
T = (TypeRef) result_1.getFirst();
}
}
}
} else {
if ((expr instanceof ParameterizedCallExpression)) {
boolean _contains_1 = ((ParameterizedCallExpression) expr).getArguments().contains(argument);
/* expr.arguments.contains(argument) */
if (!_contains_1) {
sneakyThrowRuleFailedException("expr.arguments.contains(argument)");
}
/* G |- expr.target : var TypeRef targetTypeRef */
Expression _target = ((ParameterizedCallExpression) expr).getTarget();
TypeRef targetTypeRef = null;
Result<TypeRef> result_2 = typeInternal(G, _trace_, _target);
checkAssignableTo(result_2.getFirst(), TypeRef.class);
targetTypeRef = (TypeRef) result_2.getFirst();
if ((targetTypeRef instanceof FunctionTypeExprOrRef)) {
final FunctionTypeExprOrRef F = ((FunctionTypeExprOrRef) targetTypeRef);
final int argIndex = ECollections.indexOf(((ParameterizedCallExpression) expr).getArguments(), argument, 0);
final TFormalParameter fpar_1 = F.getFparForArgIdx(argIndex);
if ((fpar_1 == null)) {
T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef();
} else {
final TypeRef paramType_1 = fpar_1.getTypeRef();
if ((paramType_1 == null)) {
T = RuleEnvironmentExtensions.anyTypeRef(G);
} else {
final RuleEnvironment G2_1 = RuleEnvironmentExtensions.wrap(G);
this.typeSystemHelper.addSubstitutions(G2_1, ((ParameterizedCallExpression) expr), F);
Expression _target_1 = ((ParameterizedCallExpression) expr).getTarget();
if ((_target_1 instanceof SuperLiteral)) {
N4ClassDeclaration _containerOfType = EcoreUtil2.<N4ClassDeclaration>getContainerOfType(expr, N4ClassDeclaration.class);
Type _definedType = null;
if (_containerOfType != null) {
_definedType = _containerOfType.getDefinedType();
}
final Type containingClass = _definedType;
if ((containingClass instanceof TClass)) {
RuleEnvironmentExtensions.addThisType(G2_1, TypeExtensions.ref(containingClass));
ParameterizedTypeRef _superClassRef = ((TClass) containingClass).getSuperClassRef();
boolean _tripleNotEquals = (_superClassRef != null);
if (_tripleNotEquals) {
this.typeSystemHelper.addSubstitutions(G2_1, ((TClass) containingClass).getSuperClassRef());
}
if ((paramType_1 instanceof ThisTypeRefStructural)) {
RuleEnvironmentExtensions.addThisType(G2_1, ((TClass) containingClass).getSuperClassRef());
}
}
}
/* G2 |- paramType ~> T */
Result<TypeArgument> result_3 = substTypeVariablesInternal(G2_1, _trace_, paramType_1);
checkAssignableTo(result_3.getFirst(), TypeRef.class);
T = (TypeRef) result_3.getFirst();
}
}
} else {
T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef();
}
}
}
return new Result<TypeRef>(T);
}
use of org.eclipse.xsemantics.runtime.RuleEnvironment in project n4js by eclipse.
the class InternalTypeSystem method applyRuleTypeIndexedAccessExpression.
protected Result<TypeRef> applyRuleTypeIndexedAccessExpression(final RuleEnvironment G, final RuleApplicationTrace _trace_, final IndexedAccessExpression expr) throws RuleFailedException {
// output parameter
TypeRef T = null;
/* { expr.target === null || expr.index === null; T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef } or { expr.target instanceof SuperLiteral T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef } or { G |- expr.target : var TypeRef targetTypeRef targetTypeRef = typeSystemHelper.resolveType(G, targetTypeRef); G |- expr.index : var TypeRef indexTypeRef; val targetDeclType = targetTypeRef.declaredType; val targetIsLiteralOfStringBasedEnum = targetDeclType instanceof TEnum && AnnotationDefinition.STRING_BASED.hasAnnotation(targetDeclType); val indexIsNumeric = { G |- indexTypeRef <: G.numberTypeRef }; val indexValue = ASTMetaInfoUtils.getCompileTimeValue(expr.index); val memberName = N4JSLanguageUtils.derivePropertyNameFromCompileTimeValue(indexValue); if (indexIsNumeric && (targetTypeRef.isArrayLike || targetIsLiteralOfStringBasedEnum)) { if (targetDeclType.generic && targetTypeRef.typeArgs.isEmpty) { T = G.anyTypeRef } else { val G2 = G.wrap typeSystemHelper.addSubstitutions(G2, targetTypeRef) G2.addThisType(targetTypeRef) val elementTypeRef = if(targetIsLiteralOfStringBasedEnum) { G.stringType.elementType } else { targetDeclType.elementType }; G2 |- elementTypeRef ~> T } } else if (memberName!==null) { val staticAccess = (targetTypeRef instanceof TypeTypeRef) val checkVisibility = false val scope = memberScopingHelper.createMemberScope(targetTypeRef, expr, checkVisibility, staticAccess) val memberDesc = if(memberName!==null && !memberName.isEmpty()) { scope.getSingleElement(qualifiedNameConverter.toQualifiedName(memberName)) }; val member = if(memberDesc!==null && !IEObjectDescriptionWithError.isErrorDescription(memberDesc)) { memberDesc.getEObjectOrProxy() }; if(member instanceof TMember && !member.eIsProxy) { G |- (member as TMember) : var TypeRef memberTypeRef val G2 = G.wrap typeSystemHelper.addSubstitutions(G2,targetTypeRef) G2.addThisType(targetTypeRef) G2 |- memberTypeRef ~> T } else if (targetTypeRef.dynamic) { T = G.anyTypeRefDynamic } else { T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef } } else if (targetTypeRef.dynamic) { T = G.anyTypeRefDynamic } else { T = G.anyTypeRef } } */
{
RuleFailedException previousFailure = null;
try {
/* expr.target === null || expr.index === null */
if (!((expr.getTarget() == null) || (expr.getIndex() == null))) {
sneakyThrowRuleFailedException("expr.target === null || expr.index === null");
}
T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef();
} catch (Exception e) {
previousFailure = extractRuleFailedException(e);
/* { expr.target instanceof SuperLiteral T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef } or { G |- expr.target : var TypeRef targetTypeRef targetTypeRef = typeSystemHelper.resolveType(G, targetTypeRef); G |- expr.index : var TypeRef indexTypeRef; val targetDeclType = targetTypeRef.declaredType; val targetIsLiteralOfStringBasedEnum = targetDeclType instanceof TEnum && AnnotationDefinition.STRING_BASED.hasAnnotation(targetDeclType); val indexIsNumeric = { G |- indexTypeRef <: G.numberTypeRef }; val indexValue = ASTMetaInfoUtils.getCompileTimeValue(expr.index); val memberName = N4JSLanguageUtils.derivePropertyNameFromCompileTimeValue(indexValue); if (indexIsNumeric && (targetTypeRef.isArrayLike || targetIsLiteralOfStringBasedEnum)) { if (targetDeclType.generic && targetTypeRef.typeArgs.isEmpty) { T = G.anyTypeRef } else { val G2 = G.wrap typeSystemHelper.addSubstitutions(G2, targetTypeRef) G2.addThisType(targetTypeRef) val elementTypeRef = if(targetIsLiteralOfStringBasedEnum) { G.stringType.elementType } else { targetDeclType.elementType }; G2 |- elementTypeRef ~> T } } else if (memberName!==null) { val staticAccess = (targetTypeRef instanceof TypeTypeRef) val checkVisibility = false val scope = memberScopingHelper.createMemberScope(targetTypeRef, expr, checkVisibility, staticAccess) val memberDesc = if(memberName!==null && !memberName.isEmpty()) { scope.getSingleElement(qualifiedNameConverter.toQualifiedName(memberName)) }; val member = if(memberDesc!==null && !IEObjectDescriptionWithError.isErrorDescription(memberDesc)) { memberDesc.getEObjectOrProxy() }; if(member instanceof TMember && !member.eIsProxy) { G |- (member as TMember) : var TypeRef memberTypeRef val G2 = G.wrap typeSystemHelper.addSubstitutions(G2,targetTypeRef) G2.addThisType(targetTypeRef) G2 |- memberTypeRef ~> T } else if (targetTypeRef.dynamic) { T = G.anyTypeRefDynamic } else { T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef } } else if (targetTypeRef.dynamic) { T = G.anyTypeRefDynamic } else { T = G.anyTypeRef } } */
{
try {
Expression _target = expr.getTarget();
/* expr.target instanceof SuperLiteral */
if (!(_target instanceof SuperLiteral)) {
sneakyThrowRuleFailedException("expr.target instanceof SuperLiteral");
}
T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef();
} catch (Exception e_1) {
previousFailure = extractRuleFailedException(e_1);
/* G |- expr.target : var TypeRef targetTypeRef */
Expression _target_1 = expr.getTarget();
TypeRef targetTypeRef = null;
Result<TypeRef> result = typeInternal(G, _trace_, _target_1);
checkAssignableTo(result.getFirst(), TypeRef.class);
targetTypeRef = (TypeRef) result.getFirst();
targetTypeRef = this.typeSystemHelper.resolveType(G, targetTypeRef);
/* G |- expr.index : var TypeRef indexTypeRef */
Expression _index = expr.getIndex();
TypeRef indexTypeRef = null;
Result<TypeRef> result_1 = typeInternal(G, _trace_, _index);
checkAssignableTo(result_1.getFirst(), TypeRef.class);
indexTypeRef = (TypeRef) result_1.getFirst();
final Type targetDeclType = targetTypeRef.getDeclaredType();
final boolean targetIsLiteralOfStringBasedEnum = ((targetDeclType instanceof TEnum) && AnnotationDefinition.STRING_BASED.hasAnnotation(targetDeclType));
/* G |- indexTypeRef <: G.numberTypeRef */
ParameterizedTypeRef _numberTypeRef = RuleEnvironmentExtensions.numberTypeRef(G);
boolean _ruleinvocation = subtypeSucceeded(G, _trace_, indexTypeRef, _numberTypeRef);
final boolean indexIsNumeric = _ruleinvocation;
final CompileTimeValue indexValue = ASTMetaInfoUtils.getCompileTimeValue(expr.getIndex());
final String memberName = N4JSLanguageUtils.derivePropertyNameFromCompileTimeValue(indexValue);
if ((indexIsNumeric && (targetTypeRef.isArrayLike() || targetIsLiteralOfStringBasedEnum))) {
if ((targetDeclType.isGeneric() && targetTypeRef.getTypeArgs().isEmpty())) {
T = RuleEnvironmentExtensions.anyTypeRef(G);
} else {
final RuleEnvironment G2 = RuleEnvironmentExtensions.wrap(G);
this.typeSystemHelper.addSubstitutions(G2, targetTypeRef);
RuleEnvironmentExtensions.addThisType(G2, targetTypeRef);
TypeRef _xifexpression = null;
if (targetIsLiteralOfStringBasedEnum) {
_xifexpression = RuleEnvironmentExtensions.stringType(G).getElementType();
} else {
_xifexpression = targetDeclType.getElementType();
}
final TypeRef elementTypeRef = _xifexpression;
/* G2 |- elementTypeRef ~> T */
Result<TypeArgument> result_2 = substTypeVariablesInternal(G2, _trace_, elementTypeRef);
checkAssignableTo(result_2.getFirst(), TypeRef.class);
T = (TypeRef) result_2.getFirst();
}
} else {
if ((memberName != null)) {
final boolean staticAccess = (targetTypeRef instanceof TypeTypeRef);
final boolean checkVisibility = false;
final IScope scope = this.memberScopingHelper.createMemberScope(targetTypeRef, expr, checkVisibility, staticAccess);
IEObjectDescription _xifexpression_1 = null;
if (((memberName != null) && (!memberName.isEmpty()))) {
_xifexpression_1 = scope.getSingleElement(this.qualifiedNameConverter.toQualifiedName(memberName));
}
final IEObjectDescription memberDesc = _xifexpression_1;
EObject _xifexpression_2 = null;
if (((memberDesc != null) && (!IEObjectDescriptionWithError.isErrorDescription(memberDesc)))) {
_xifexpression_2 = memberDesc.getEObjectOrProxy();
}
final EObject member = _xifexpression_2;
if (((member instanceof TMember) && (!member.eIsProxy()))) {
/* G |- (member as TMember) : var TypeRef memberTypeRef */
TypeRef memberTypeRef = null;
Result<TypeRef> result_3 = typeInternal(G, _trace_, ((TMember) member));
checkAssignableTo(result_3.getFirst(), TypeRef.class);
memberTypeRef = (TypeRef) result_3.getFirst();
final RuleEnvironment G2_1 = RuleEnvironmentExtensions.wrap(G);
this.typeSystemHelper.addSubstitutions(G2_1, targetTypeRef);
RuleEnvironmentExtensions.addThisType(G2_1, targetTypeRef);
/* G2 |- memberTypeRef ~> T */
Result<TypeArgument> result_4 = substTypeVariablesInternal(G2_1, _trace_, memberTypeRef);
checkAssignableTo(result_4.getFirst(), TypeRef.class);
T = (TypeRef) result_4.getFirst();
} else {
boolean _isDynamic = targetTypeRef.isDynamic();
if (_isDynamic) {
T = RuleEnvironmentExtensions.anyTypeRefDynamic(G);
} else {
T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef();
}
}
} else {
boolean _isDynamic_1 = targetTypeRef.isDynamic();
if (_isDynamic_1) {
T = RuleEnvironmentExtensions.anyTypeRefDynamic(G);
} else {
T = RuleEnvironmentExtensions.anyTypeRef(G);
}
}
}
}
}
}
}
return new Result<TypeRef>(T);
}
use of org.eclipse.xsemantics.runtime.RuleEnvironment in project n4js by eclipse.
the class BoundSet method substituteInferenceVariable.
/**
* Returns a copy of {@code typeRef} in which {@code infVar} is substituted by {@code typeArg} or {@code typeRef}
* itself if no change has occurred.
*
* @return typeRef[infVar:=typeArg]
*/
private TypeRef substituteInferenceVariable(TypeRef typeRef, InferenceVariable infVar, TypeArgument typeArg) {
final RuleEnvironment Gtemp = RuleEnvironmentExtensions.wrap(this.G);
RuleEnvironmentExtensions.addTypeMapping(Gtemp, infVar, typeArg);
final TypeRef result = (TypeRef) this.ts.substTypeVariables(Gtemp, typeRef).getValue();
// assert !(TypeVarUtils.occursIn(infVar, result));
return result;
}
use of org.eclipse.xsemantics.runtime.RuleEnvironment in project n4js by eclipse.
the class InferenceContext method newInferenceVariablesFor.
/**
* Introduces newly generated inference variables for each type parameter of the given function type (if generic)
* and returns a non-generic function type in which all type variables owned by the given function type are replaced
* by those inference variables; simply returns the given function type unchanged if it already is non-generic.
* Returns given function type unchanged in case of error (so returned function type may actually be generic, but
* only in error cases).
* <p>
* Example: given a function type such as
*
* <pre>
* {function<T,S>(G<T,string>):S}
* </pre>
*
* this method will add two new type variables α', β' and return
*
* <pre>
* {function(G<α',string>):β'}
* </pre>
*
* Note that the returned function type is non-generic.
*/
public FunctionTypeExprOrRef newInferenceVariablesFor(FunctionTypeExprOrRef funTypeRef) {
if (!funTypeRef.isGeneric())
return funTypeRef;
// NOTE: typeParam may contain null entries!
final List<TypeVariable> typeParams = funTypeRef.getTypeVars();
final InferenceVariable[] newInfVars = newInferenceVariables(typeParams.size(), true);
final List<? extends TypeRef> newInfVarsRefs = Stream.of(newInfVars).map(TypeUtils::createTypeRef).collect(Collectors.toList());
// new, empty RE
final RuleEnvironment G_params2infVars = RuleEnvironmentExtensions.newRuleEnvironment(G);
RuleEnvironmentExtensions.addTypeMappings(G_params2infVars, typeParams, newInfVarsRefs);
final TypeArgument left_withInfVars = ts.substTypeVariables(G_params2infVars, funTypeRef).getValue();
if (left_withInfVars instanceof FunctionTypeExprOrRef)
return (FunctionTypeExprOrRef) left_withInfVars;
// in case of substitution error: return original funTypeRef
return funTypeRef;
}
use of org.eclipse.xsemantics.runtime.RuleEnvironment in project n4js by eclipse.
the class Reducer method mightBeSubtypeOf.
private boolean mightBeSubtypeOf(FunctionTypeExprOrRef left, FunctionTypeExprOrRef right) {
// step 1: replace all inference variables by UnknownTypeRef
final TypeRef unknown = TypeRefsFactory.eINSTANCE.createUnknownTypeRef();
final RuleEnvironment G_temp = RuleEnvironmentExtensions.newRuleEnvironment(G);
for (InferenceVariable iv : ic.getInferenceVariables()) {
RuleEnvironmentExtensions.addTypeMapping(G_temp, iv, unknown);
}
final TypeArgument leftSubst = ts.substTypeVariables(G_temp, left).getValue();
final TypeArgument rightSubst = ts.substTypeVariables(G_temp, right).getValue();
// step 2: now, perform subtype check reusing existing logic
return ts.subtypeSucceeded(G, leftSubst, rightSubst);
}
Aggregations