Commit 01bf6283 authored by Miklos Magyari's avatar Miklos Magyari
Browse files

OOP: semantic check - a class can only extend one non-trait class (issue #427)


Signed-off-by: Miklos Magyari's avatarMiklos Magyari <miklos.magyari@sigmatechnology.se>
parent c245face
......@@ -29,6 +29,15 @@ type class BadInheritance extends MyUnion {
}
type class MinimalClass { }
type class @abstract MinimalAbstractClass { }
type class @final MinimalFinalClass { }
type class @trait MinimalTraitClass { }
type class BadExtends extends MinimalClass, MinimalAbstractClass, MinimalFinalClass, MinimalTraitClass {
// dummy
}
type class SuperClass {
var integer vl_a;
template MyUnion t_union := { a := 10 }
......@@ -69,7 +78,7 @@ type class OuterClass {
public class Core {
public function f_inside(in integer pl_int) return charstring {
return this.chr;
}
}
}
}
}
......
......@@ -16,12 +16,15 @@ import java.util.concurrent.CopyOnWriteArrayList;
import org.eclipse.titan.designer.AST.ASTNode;
import org.eclipse.titan.designer.AST.ASTVisitor;
import org.eclipse.titan.designer.AST.Assignment;
import org.eclipse.titan.designer.AST.ILocateableNode;
import org.eclipse.titan.designer.AST.Identifier;
import org.eclipse.titan.designer.AST.Location;
import org.eclipse.titan.designer.AST.Reference;
import org.eclipse.titan.designer.AST.Scope;
import org.eclipse.titan.designer.AST.Type;
import org.eclipse.titan.designer.AST.TTCN3.IIncrementallyUpdateable;
import org.eclipse.titan.designer.AST.TTCN3.definitions.Def_Type;
import org.eclipse.titan.designer.parsers.CompilationTimeStamp;
import org.eclipse.titan.designer.parsers.ttcn3parser.ReParseException;
import org.eclipse.titan.designer.parsers.ttcn3parser.TTCN3ReparseUpdater;
......@@ -30,6 +33,8 @@ import org.eclipse.titan.designer.parsers.ttcn3parser.TTCN3ReparseUpdater;
* @author Miklos Magyari
* */
public final class ClassTypeReferenceList extends ASTNode implements ILocateableNode, IIncrementallyUpdateable {
private final String ONLYONNONTRAIT = "A class can only extend one non-trait class";
private final List<Reference> classReferences;
private Location location;
private Map<ClassTypeBody, Reference> classTypeBodies;
......@@ -72,6 +77,24 @@ public final class ClassTypeReferenceList extends ASTNode implements ILocateable
return;
}
int nrNonTraitExtends = 0;
for (final Reference classRef : classReferences) {
final Assignment refdAss = classRef.getRefdAssignment(timestamp, false);
if (refdAss instanceof Def_Type) {
final Def_Type deftype = (Def_Type)refdAss;
final Type type = deftype.getType(timestamp);
if (type instanceof Class_Type) {
final Class_Type extClass = (Class_Type)type;
if (! extClass.isTrait()) {
nrNonTraitExtends++;
if (nrNonTraitExtends > 1) {
classRef.getLocation().reportSemanticError(ONLYONNONTRAIT);
}
}
}
}
}
checkUniqueness(timestamp);
lastCompilationTimeStamp = timestamp;
......
......@@ -219,6 +219,14 @@ public final class Class_Type extends Type implements ITypeWithComponents {
return classBody.hasExtendsReferences();
}
/**
* Checks if the class is declared using the '@trait' modifier
* @return
*/
public boolean isTrait() {
return isTrait;
}
@Override
public Identifier getComponentIdentifierByName(Identifier identifier) {
// TODO Auto-generated method stub
......
......@@ -45,9 +45,14 @@ public class OOP_Semantic_tests {
private ArrayList<MarkerToCheck> oopNegative_ttcn_initializer() {
//oopNegativeSemanticTest.ttcn
ArrayList<MarkerToCheck> markersToCheck = new ArrayList<MarkerToCheck>(19);
ArrayList<MarkerToCheck> markersToCheck = new ArrayList<MarkerToCheck>(21);
int lineNum = 28;
markersToCheck.add(new MarkerToCheck("class type expected", lineNum, IMarker.SEVERITY_ERROR));
lineNum += 9;
int i = 0;
for (i = 0; i < 2; i++) {
markersToCheck.add(new MarkerToCheck("A class can only extend one non-trait class", lineNum, IMarker.SEVERITY_ERROR));
}
lineNum += 15;
markersToCheck.add(new MarkerToCheck("Reference to non-existent field `nonexist' in union template for type `@classesNegativeSemantic.MyUnion'", lineNum, IMarker.SEVERITY_ERROR));
lineNum += 4;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment