use of org.eclipse.xsemantics.runtime.RuleEnvironment in project n4js by eclipse.
the class ComposedMemberInfo method handleFParameters.
private void handleFParameters(TMember member, RuleEnvironment G) {
EList<TFormalParameter> fpars = null;
if (member instanceof TMethod) {
TMethod method = (TMethod) member;
fpars = method.getFpars();
}
if (member instanceof TSetter) {
TSetter setter = (TSetter) member;
fpars = new BasicEList<>();
fpars.add(setter.getFpar());
}
if (fpars != null) {
for (int i = 0; i < fpars.size(); i++) {
TFormalParameter fpar = fpars.get(i);
if (fParameters.size() <= i) {
fParameters.add(new ComposedFParInfo());
}
ComposedFParInfo fpAggr = fParameters.get(i);
Pair<TFormalParameter, RuleEnvironment> fpPair = new Pair<>(fpar, G);
fpAggr.fpSiblings.add(fpPair);
}
}
}
use of org.eclipse.xsemantics.runtime.RuleEnvironment in project n4js by eclipse.
the class TypeXpectMethod method getTypeString.
private String getTypeString(IEObjectCoveringRegion offset, boolean expectedType) {
final String calculatedString;
EObject eobject = offset.getEObject();
if (eobject instanceof LiteralOrComputedPropertyName) {
eobject = eobject.eContainer();
}
RuleEnvironment G = newRuleEnvironment(eobject);
Result<org.eclipse.n4js.ts.typeRefs.TypeRef> result;
if (expectedType) {
if (!(eobject instanceof Expression && eobject.eContainer() != null))
return "Not an Expression at given region (required to obtain expected type); got instead: " + eobject.eClass().getName();
result = ts.expectedTypeIn(G, eobject.eContainer(), (Expression) eobject);
} else {
if (eobject instanceof BindingProperty) {
/*-
* Small tweak to allow testing the inferred type of variable declarations within binding patterns. For
* example, without this tweak, the following test would fail with a "Not a TypableElement at given
* region" exception:
*
* // Xpect type of 'len' --> number
* var {length:len} = "hello";
*/
if (((BindingProperty) eobject).getValue() != null && ((BindingProperty) eobject).getValue().getVarDecl() != null) {
eobject = ((BindingProperty) eobject).getValue().getVarDecl();
}
}
if (!(eobject instanceof TypableElement))
return "Not a TypableElement at given region; got instead: " + eobject.eClass().getName();
result = ts.type(G, (TypableElement) eobject);
}
if (result.getRuleFailedException() != null) {
calculatedString = result.getRuleFailedException().getMessage();
} else {
calculatedString = result.getValue().getTypeRefAsString();
}
return calculatedString;
}
use of org.eclipse.xsemantics.runtime.RuleEnvironment in project n4js by eclipse.
the class TypeXpectMethod method getTypeArgumentsString.
private String getTypeArgumentsString(IEObjectCoveringRegion offset) {
final EObject eobject = offset != null ? offset.getEObject() : null;
final EObject container = eobject != null ? eobject.eContainer() : null;
if (eobject == null || !(container instanceof ParameterizedCallExpression && ((ParameterizedCallExpression) container).getTarget() == eobject)) {
// missing or invalid offset
return "xpect method error: offset not given or does not point to target of a call expression";
}
if (!(eobject.eResource() instanceof N4JSResource)) {
return "xpect method error: offset does not point to an EObject contained in a N4JSResource";
}
// offset points to the target of a call expression
final ParameterizedCallExpression callExpr = (ParameterizedCallExpression) container;
final RuleEnvironment G = RuleEnvironmentExtensions.newRuleEnvironment(eobject);
final Result<TypeRef> targetTypeRef = ts.type(G, callExpr.getTarget());
if (targetTypeRef.failed() || !(targetTypeRef.getValue() instanceof FunctionTypeExprOrRef)) {
return "xpect method error: cannot infer type of call expression target OR it's not a FunctionTypeExprOrRef";
}
final List<TypeVariable> typeParams = ((FunctionTypeExprOrRef) targetTypeRef.getValue()).getTypeVars();
// not interested in the actual typeParams, just the size
final int expectedNumOfTypeArgs = typeParams.size();
final List<TypeRef> typeArgs;
if (callExpr.getTypeArgs().isEmpty()) {
// no type arguments given in call expression -> use inferred type arguments
// (should be the standard case when testing)
final List<TypeRef> inferredTypeArgs = ASTMetaInfoUtils.getInferredTypeArgs(callExpr);
if (inferredTypeArgs != null) {
typeArgs = inferredTypeArgs;
} else {
typeArgs = Collections.emptyList();
}
} else {
// call expression is parameterized -> use the explicitly given type arguments
// (just provided for completeness)
typeArgs = callExpr.getTypeArgs();
}
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < expectedNumOfTypeArgs; i++) {
final TypeRef inferredTypeArg = i < typeArgs.size() ? typeArgs.get(i) : null;
if (sb.length() > 0)
sb.append(", ");
if (inferredTypeArg != null)
sb.append(inferredTypeArg.getTypeRefAsString());
else
sb.append("*missing*");
}
return sb.toString();
}
use of org.eclipse.xsemantics.runtime.RuleEnvironment in project n4js by eclipse.
the class MemberVisibilityChecker method getActualDeclaredReceiverType.
/**
* Returns the actual receiver type, which usually simply is the declared type of the receiver type. However, in
* case of classifier references, enums, or structural type references, the actual receiver may be differently
* computed.
*/
private Type getActualDeclaredReceiverType(EObject context, TypeRef receiverType, ResourceSet resourceSet) {
if (receiverType instanceof TypeTypeRef) {
final RuleEnvironment G = RuleEnvironmentExtensions.newRuleEnvironment(context);
return tsh.getStaticType(G, (TypeTypeRef) receiverType);
}
if (receiverType instanceof ThisTypeRef) {
ThisTypeRef thisTypeRef = (ThisTypeRef) receiverType;
if (thisTypeRef.isUseSiteStructuralTyping()) {
FunctionOrFieldAccessor foa = N4JSASTUtils.getContainingFunctionOrAccessor(thisTypeRef);
N4ClassifierDefinition classifier = EcoreUtil2.getContainerOfType(foa, N4ClassifierDefinition.class);
return classifier.getDefinedType();
}
}
if (receiverType instanceof FunctionTypeExpression) {
if (resourceSet == null)
return null;
// Change receiverType to implicit super class Function.
BuiltInTypeScope builtInTypeScope = BuiltInTypeScope.get(resourceSet);
TObjectPrototype functionType = builtInTypeScope.getFunctionType();
return functionType;
}
return receiverType.getDeclaredType();
}
use of org.eclipse.xsemantics.runtime.RuleEnvironment in project n4js by eclipse.
the class ComposedMemberInfo method initMemberAggregate.
// ///////////////////////// Init Methods ///////////////////////////
private synchronized void initMemberAggregate() {
if (isInitialized)
return;
this.isSiblingMissing = siblings.contains(null);
MemberType lastMType = null;
for (Pair<TMember, RuleEnvironment> pair : siblings) {
if (pair == null)
continue;
this.isEmpty = false;
TMember member = pair.getKey();
RuleEnvironment G = pair.getValue();
lastMType = handleMemberTypes(lastMType, member);
handleReadOnlyField(member);
handleAccessibility(member);
handleTypeRefLists(member, G);
handleFParameters(member, G);
}
// init: fParameters
List<TypeRef> currVariadicAccumulated = new LinkedList<>();
for (ComposedFParInfo fpAggr : fParameters) {
initFParAggregate(fpAggr);
// handle: typeRefsVariadicAccumulated
currVariadicAccumulated.addAll(fpAggr.typeRefsVariadic);
fpAggr.typeRefsVariadicAccumulated.addAll(currVariadicAccumulated);
}
handleIsVariadicButLastFParIsDifferent();
handleValidationProblems();
this.isInitialized = true;
}
Aggregations