use of org.eclipse.n4js.ts.types.TClass in project n4js by eclipse.
the class N4JSMemberRedefinitionValidator method checkUnpairedAccessorFilling.
private void checkUnpairedAccessorFilling(MemberMatrix mm, N4ClassifierDefinition definition) {
if (definition.getDefinedType().isStaticPolyfill() && mm.hasMixedAccessorPair()) {
FieldAccessor ownedAccessor = (FieldAccessor) Iterables.getFirst(mm.owned(), null);
if (null == ownedAccessor) {
// Should not happen, a mixed accessor pair implies at least one owned member
return;
}
if (!(definition instanceof N4ClassDefinition)) {
// Non-class static polyfills aren't allowed. Validated somewhere else.
return;
}
TClass filledClass = MemberRedefinitionUtils.getFilledClass((N4ClassDefinition) definition);
if (null == filledClass) {
// Invalid static polyfill class. Validated somewhere else.
return;
}
// Iterate over all inherited members
SourceAwareIterator memberIterator = mm.actuallyInheritedAndMixedMembers();
while (memberIterator.hasNext()) {
TMember next = memberIterator.next();
ContainerType<?> containingType = next.getContainingType();
// Issue an error if the member isn't owned by the filled class
if (containingType != filledClass) {
messageMissingOwnedAccessor(ownedAccessor);
}
}
}
}
use of org.eclipse.n4js.ts.types.TClass in project n4js by eclipse.
the class N4JSMemberRedefinitionValidator method constraints_41_AbstractClass.
/**
* Constraints 41 (Abstract Class)
*/
private boolean constraints_41_AbstractClass(TClassifier classifier, MemberCube memberCube) {
List<TMember> abstractMembers = null;
if (!classifier.isAbstract() && classifier instanceof TClass) {
for (Entry<NameStaticPair, MemberMatrix> entry : memberCube.entrySet()) {
MemberMatrix mm = entry.getValue();
MemberList<TMember> l = new MemberList<>();
Iterators.addAll(l, mm.actuallyInheritedAndMixedMembers());
for (SourceAwareIterator iter = mm.actuallyInheritedAndMixedMembers(); iter.hasNext(); ) {
TMember m = iter.next();
if (m.isAbstract()) {
if (abstractMembers == null) {
abstractMembers = new ArrayList<>();
}
abstractMembers.add(m);
}
}
}
}
if (abstractMembers != null) {
messageMissingImplementations(abstractMembers);
return false;
}
return true;
}
use of org.eclipse.n4js.ts.types.TClass in project n4js by eclipse.
the class N4JSSyntaxValidator method checkClassDefinition.
/**
* Checks that no "with" is used and that list of implemented interfaces is separated with commas and not with
* keywords. These checks (with some warnings created instead of errors) should help the transition from roles to
* interfaces. However, they may be useful later on as well, e.g., if an interface is manually refactored into a
* class or vice versa.
* <p>
* Note that "with" is used in Dart for roles, so maybe it is useful to have a user-friendly message instead of a
* parser error.
*/
@Check
public void checkClassDefinition(N4ClassDefinition n4ClassDefinition) {
holdsCorrectOrderOfExtendsImplements(n4ClassDefinition);
ICompositeNode node = NodeModelUtils.findActualNodeFor(n4ClassDefinition);
ILeafNode keywordNode = findSecondLeafWithKeyword(n4ClassDefinition, "{", node, EXTENDS_KEYWORD, false);
if (keywordNode != null) {
TClass tclass = n4ClassDefinition.getDefinedTypeAsClass();
if (tclass == null) {
// avoid consequential errors
return;
}
if (StreamSupport.stream(tclass.getImplementedInterfaceRefs().spliterator(), false).allMatch(superTypeRef -> superTypeRef.getDeclaredType() instanceof TInterface)) {
List<? extends IdentifiableElement> interfaces = StreamSupport.stream(tclass.getImplementedInterfaceRefs().spliterator(), false).map(ref -> (TInterface) (ref.getDeclaredType())).collect(Collectors.toList());
String message = getMessageForSYN_KW_EXTENDS_IMPLEMENTS_MIXED_UP(validatorMessageHelper.description(tclass), "extend", "interface" + (interfaces.size() > 1 ? "s " : " ") + validatorMessageHelper.names(interfaces), IMPLEMENTS_KEYWORD);
addIssue(message, n4ClassDefinition, keywordNode.getTotalOffset(), keywordNode.getLength(), SYN_KW_EXTENDS_IMPLEMENTS_MIXED_UP);
}
}
}
use of org.eclipse.n4js.ts.types.TClass in project n4js by eclipse.
the class PolyfillValidatorFragment method holdsPolyfill.
/**
* Checks polyfill constraints on given class declaration using validator to issue errors. Constraints (Polyfill
* Class) 156: Polyfill
*/
public boolean holdsPolyfill(N4JSClassValidator validator, N4ClassDeclaration n4Class) {
boolean isStaticPolyFill = isStaticPolyfill(n4Class);
if (isStaticPolyFill || isPolyfill(n4Class)) {
PolyfillValidationState state = new PolyfillValidationState();
state.host = validator;
state.n4Class = n4Class;
state.name = n4Class.getName();
if (state.name == null || !(n4Class.getDefinedType() instanceof TClass)) {
// consequential error, AST corrupt
return true;
}
state.polyType = (TClass) n4Class.getDefinedType();
if (state.polyType == null || state.name == null) {
// consequential error
return true;
}
if (!holdsExpliciteExtends(state)) {
return false;
}
final Type superType = n4Class.getSuperClassRef().getDeclaredType();
if (!(superType instanceof TClassifier)) {
// consequential error
return true;
}
state.filledType = (TClassifier) superType;
// Different rules for static/non-static polyfills:
if (!isStaticPolyFill) {
if (!(//
holdPolyfillName(state) && //
holdsProvidedByRuntime(state) && //
holdsNoImplementsOrConsumes(state) && //
holdsEqualModifiers(state) && //
holdsEqualTypeVariables(state) && //
holdsSinglePolyfillSource(state))) {
return false;
}
} else {
// static polyfill case, IDE-1735
if (!(//
holdPolyfillName(state) && // && holdsNoImplementsOrConsumes(state) //
holdsFilledClassIsStaticPolyfillAware(//
state) && //
holdsSameJavascriptVariant(state) && //
holdsEqualModifiers(state) && //
holdsEqualTypeVariables(state) && //
holdsSinglePolyfillSource(state))) {
return false;
}
}
}
// ยง 140.1 only polyfills are allowed in StaticPolyfillModule.
if (!isStaticPolyFill && isContainedInStaticPolyfillModule(n4Class)) {
// n4Class is toplevel by default
validator.addIssue(getMessageForPOLY_STATIC_POLYFILL_MODULE_ONLY_FILLING_CLASSES(), n4Class, N4_TYPE_DECLARATION__NAME, POLY_STATIC_POLYFILL_MODULE_ONLY_FILLING_CLASSES);
return false;
}
return true;
}
use of org.eclipse.n4js.ts.types.TClass in project n4js by eclipse.
the class InternalTypeSystem method applyRuleTypeSuperLiteral.
protected Result<TypeRef> applyRuleTypeSuperLiteral(final RuleEnvironment G, final RuleApplicationTrace _trace_, final SuperLiteral superLiteral) throws RuleFailedException {
// output parameter
TypeRef T = null;
final N4MemberDeclaration containingMemberDecl = EcoreUtil2.<N4MemberDeclaration>getContainerOfType(superLiteral.eContainer(), N4MemberDeclaration.class);
/* if (containingMemberDecl === null) { T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef } else { val containingClass = (containingMemberDecl.eContainer as N4ClassDeclaration).definedType as TClass; val superClass = G.getDeclaredOrImplicitSuperType(containingClass) var effectiveSuperClass = superClass if( containingClass.isStaticPolyfill ) { effectiveSuperClass = G.getDeclaredOrImplicitSuperType( superClass as TClass ) } { superLiteral.eContainer instanceof ParameterizedPropertyAccessExpression || superLiteral.eContainer instanceof IndexedAccessExpression if(containingMemberDecl.static) T = effectiveSuperClass?.createConstructorTypeRef else T = effectiveSuperClass?.createTypeRef if (T !== null) T = TypeUtils.enforceNominalTyping(T) } or { superLiteral.eContainer instanceof ParameterizedCallExpression if(containingMemberDecl instanceof N4MethodDeclaration && containingMemberDecl.name == 'constructor') { val ctor = containerTypesHelper.fromContext(superLiteral.eResource).findConstructor(effectiveSuperClass); T = ctor?.createTypeRef } else { T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef } } or { superLiteral.eContainer instanceof NewExpression } } or { T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef } */
{
RuleFailedException previousFailure = null;
try {
if ((containingMemberDecl == null)) {
T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef();
} else {
EObject _eContainer = containingMemberDecl.eContainer();
Type _definedType = ((N4ClassDeclaration) _eContainer).getDefinedType();
final TClass containingClass = ((TClass) _definedType);
final TClassifier superClass = RuleEnvironmentExtensions.getDeclaredOrImplicitSuperType(G, containingClass);
TClassifier effectiveSuperClass = superClass;
boolean _isStaticPolyfill = containingClass.isStaticPolyfill();
if (_isStaticPolyfill) {
effectiveSuperClass = RuleEnvironmentExtensions.getDeclaredOrImplicitSuperType(G, ((TClass) superClass));
}
/* { superLiteral.eContainer instanceof ParameterizedPropertyAccessExpression || superLiteral.eContainer instanceof IndexedAccessExpression if(containingMemberDecl.static) T = effectiveSuperClass?.createConstructorTypeRef else T = effectiveSuperClass?.createTypeRef if (T !== null) T = TypeUtils.enforceNominalTyping(T) } or { superLiteral.eContainer instanceof ParameterizedCallExpression if(containingMemberDecl instanceof N4MethodDeclaration && containingMemberDecl.name == 'constructor') { val ctor = containerTypesHelper.fromContext(superLiteral.eResource).findConstructor(effectiveSuperClass); T = ctor?.createTypeRef } else { T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef } } or { superLiteral.eContainer instanceof NewExpression } */
{
try {
/* superLiteral.eContainer instanceof ParameterizedPropertyAccessExpression || superLiteral.eContainer instanceof IndexedAccessExpression */
if (!((superLiteral.eContainer() instanceof ParameterizedPropertyAccessExpression) || (superLiteral.eContainer() instanceof IndexedAccessExpression))) {
sneakyThrowRuleFailedException("superLiteral.eContainer instanceof ParameterizedPropertyAccessExpression || superLiteral.eContainer instanceof IndexedAccessExpression");
}
boolean _isStatic = containingMemberDecl.isStatic();
if (_isStatic) {
TypeRef _createConstructorTypeRef = null;
if (effectiveSuperClass != null) {
_createConstructorTypeRef = TypeUtils.createConstructorTypeRef(effectiveSuperClass);
}
T = _createConstructorTypeRef;
} else {
ParameterizedTypeRef _createTypeRef = null;
if (effectiveSuperClass != null) {
_createTypeRef = TypeUtils.createTypeRef(effectiveSuperClass);
}
T = _createTypeRef;
}
if ((T != null)) {
T = TypeUtils.enforceNominalTyping(T);
}
} catch (Exception e) {
previousFailure = extractRuleFailedException(e);
/* { superLiteral.eContainer instanceof ParameterizedCallExpression if(containingMemberDecl instanceof N4MethodDeclaration && containingMemberDecl.name == 'constructor') { val ctor = containerTypesHelper.fromContext(superLiteral.eResource).findConstructor(effectiveSuperClass); T = ctor?.createTypeRef } else { T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef } } or { superLiteral.eContainer instanceof NewExpression } */
{
try {
EObject _eContainer_1 = superLiteral.eContainer();
/* superLiteral.eContainer instanceof ParameterizedCallExpression */
if (!(_eContainer_1 instanceof ParameterizedCallExpression)) {
sneakyThrowRuleFailedException("superLiteral.eContainer instanceof ParameterizedCallExpression");
}
if (((containingMemberDecl instanceof N4MethodDeclaration) && Objects.equal(containingMemberDecl.getName(), "constructor"))) {
final TMethod ctor = this.containerTypesHelper.fromContext(superLiteral.eResource()).findConstructor(effectiveSuperClass);
ParameterizedTypeRef _createTypeRef_1 = null;
if (ctor != null) {
_createTypeRef_1 = TypeUtils.createTypeRef(ctor);
}
T = _createTypeRef_1;
} else {
T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef();
}
} catch (Exception e_1) {
previousFailure = extractRuleFailedException(e_1);
EObject _eContainer_2 = superLiteral.eContainer();
/* superLiteral.eContainer instanceof NewExpression */
if (!(_eContainer_2 instanceof NewExpression)) {
sneakyThrowRuleFailedException("superLiteral.eContainer instanceof NewExpression");
}
}
}
}
}
}
} catch (Exception e_2) {
previousFailure = extractRuleFailedException(e_2);
T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef();
}
}
return new Result<TypeRef>(T);
}
Aggregations