Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for annotations to set MemberId [20112] #115

Merged
merged 8 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 61 additions & 24 deletions src/main/antlr/com/eprosima/idl/parser/grammar/IDL.g4
Original file line number Diff line number Diff line change
Expand Up @@ -768,12 +768,12 @@ type_decl [Vector<Annotation> annotations, ArrayList<Definition> defs] returns [
Token tk = null;
String fw_name = null;
}
: ( KW_TYPEDEF {tk = _input.LT(1);} type_declarator[null] { ttg=$type_declarator.returnPair; }
: ( KW_TYPEDEF {tk = _input.LT(1);} type_declarator[null, annotations] { ttg=$type_declarator.returnPair; }
| struct_type[annotations] { ttg=$struct_type.returnPair; fw_name = $struct_type.fw_name; }
| union_type[annotations, defs] { ttg=$union_type.returnPair; fw_name = $union_type.fw_name; }
| enum_type { ttg=$enum_type.returnPair; }
| bitset_type { ttg=$bitset_type.returnPair; }
| bitmask_type { ttg=$bitmask_type.returnPair; }
| enum_type[annotations] { ttg=$enum_type.returnPair; }
| bitset_type[annotations] { ttg=$bitset_type.returnPair; }
| bitmask_type[annotations] { ttg=$bitmask_type.returnPair; }
| KW_NATIVE { System.out.println("WARNING (File " + ctx.getFilename() + ", Line " + (_input.LT(1) != null ? _input.LT(1).getLine() - ctx.getCurrentIncludeLine() : "1") + "): Native declarations are not supported. Ignoring..."); } simple_declarator
| constr_forward_decl )
{
Expand All @@ -798,16 +798,6 @@ type_decl [Vector<Annotation> annotations, ArrayList<Definition> defs] returns [
}

TypeDeclaration typedeclaration = (fw_name == null) ? new TypeDeclaration(ctx.getScopeFile(), ctx.isInScopedFile(), ctx.getScope(), name, ttg.first().get(count), tk) : ctx.getTypeDeclaration(fw_name);
//System.out.println("Type ttg not null: " + name);

// Add annotations
for(Annotation annotation : annotations)
{
if (annotation != null) // Some annotations may be ignored
{
typedeclaration.addAnnotation(ctx, annotation);
}
}

// Add type declaration to the map with all typedeclarations.
if (fw_name == null)
Expand All @@ -823,7 +813,7 @@ type_decl [Vector<Annotation> annotations, ArrayList<Definition> defs] returns [
}
;

type_declarator [AnnotationDeclaration annotation] returns [Pair<Vector<TypeCode>, TemplateGroup> returnPair = null]
type_declarator [AnnotationDeclaration annotation, Vector<Annotation> annotations] returns [Pair<Vector<TypeCode>, TemplateGroup> returnPair = null]
@init {
Vector<TypeCode> vector = null;
AliasTypeCode typedefTypecode = null;
Expand Down Expand Up @@ -856,6 +846,18 @@ type_declarator [AnnotationDeclaration annotation] returns [Pair<Vector<TypeCode
typedefTypecode.setContentTypeCode($type_spec.returnPair.first());
}

// Apply annotations to the TypeCode
if (null != annotations)
{
for(Annotation ann : annotations)
{
if (ann != null) // Some annotations may be ignored
{
typedefTypecode.addAnnotation(ctx, ann);
}
}
}

if(typedefTemplates != null) {
typedefTemplates.setAttribute("typedefs", typedefTypecode);
if($declarators.ret.get(count).second() != null)
Expand Down Expand Up @@ -952,9 +954,9 @@ template_type_spec returns [Pair<TypeCode, TemplateGroup> returnPair = null]
constr_type_spec returns [Pair<Vector<TypeCode>, TemplateGroup> returnPair = null]
: struct_type[null]
| union_type[null, null]
| enum_type
| bitset_type
| bitmask_type
| enum_type[null]
| bitset_type[null]
| bitmask_type[null]
;

declarators returns [Vector<Pair<Pair<Pair<String, Token>, ContainerTypeCode>, TemplateGroup>> ret = new Vector<Pair<Pair<Pair<String, Token>, ContainerTypeCode>, TemplateGroup>>()]
Expand Down Expand Up @@ -1245,7 +1247,7 @@ annotation_body [AnnotationDeclaration annotation]
:
(
annotation_member[annotation]
| enum_type SEMICOLON
| enum_type[null] SEMICOLON
{
pairtype = $enum_type.returnPair;
$annotation.addEnums(pairtype.first());
Expand All @@ -1255,7 +1257,7 @@ annotation_body [AnnotationDeclaration annotation]
pairconst = $const_decl.returnPair;
$annotation.addConstDecl(pairconst.first());
}
| KW_TYPEDEF type_declarator[annotation] SEMICOLON
| KW_TYPEDEF type_declarator[annotation, null] SEMICOLON
{
pairtype = $type_declarator.returnPair;
$annotation.addTypeDefs(pairtype.first());
Expand Down Expand Up @@ -1283,7 +1285,7 @@ annotation_forward_dcl
KW_AT_ANNOTATION scoped_name
;

bitset_type returns [Pair<Vector<TypeCode>, TemplateGroup> returnPair = null]
bitset_type[Vector<Annotation> annotations] returns [Pair<Vector<TypeCode>, TemplateGroup> returnPair = null]
@init {
String name = null;
Vector<TypeCode> vector = null;
Expand All @@ -1300,6 +1302,18 @@ bitset_type returns [Pair<Vector<TypeCode>, TemplateGroup> returnPair = null]
}
name = ctx.removeEscapeCharacter($identifier.id);
typecode = ctx.createBitsetTypeCode(ctx.getScope(), name);

// Apply annotations to the TypeCode
if (null != annotations)
{
for(Annotation annotation : annotations)
{
if (annotation != null) // Some annotations may be ignored
{
typecode.addAnnotation(ctx, annotation);
}
}
}
}
( COLON scoped_name
{
Expand Down Expand Up @@ -1394,7 +1408,7 @@ bitfield_spec returns [BitfieldSpec bitfieldType = null]
}
;

bitmask_type returns [Pair<Vector<TypeCode>, TemplateGroup> returnPair = null]
bitmask_type[Vector<Annotation> annotations] returns [Pair<Vector<TypeCode>, TemplateGroup> returnPair = null]
@init {
String name = null;
Vector<TypeCode> vector = null;
Expand All @@ -1410,6 +1424,18 @@ bitmask_type returns [Pair<Vector<TypeCode>, TemplateGroup> returnPair = null]
}
name = ctx.removeEscapeCharacter($identifier.id);
typecode = ctx.createBitmaskTypeCode(ctx.getScope(), name);

// Apply annotations to the TypeCode
if (null != annotations)
{
for(Annotation annotation : annotations)
{
if (annotation != null) // Some annotations may be ignored
{
typecode.addAnnotation(ctx, annotation);
}
}
}
}
LEFT_BRACE bit_values[typecode] RIGHT_BRACE
{
Expand Down Expand Up @@ -1810,7 +1836,6 @@ switch_type_spec returns [TypeCode typecode = null]
| wide_char_type { $typecode = $wide_char_type.typecode; }
| octet_type { $typecode=$octet_type.typecode; }
| boolean_type { $typecode=$boolean_type.typecode; }
| enum_type
| scoped_name
{
pair=$scoped_name.pair;
Expand Down Expand Up @@ -1926,7 +1951,7 @@ element_spec [List<String> labels, boolean isDefault] returns [Pair<Pair<Pair<St
}
;

enum_type returns [Pair<Vector<TypeCode>, TemplateGroup> returnPair = null]
enum_type[Vector<Annotation> annotations] returns [Pair<Vector<TypeCode>, TemplateGroup> returnPair = null]
@init{
String name = null;
Vector<TypeCode> vector = null;
Expand All @@ -1943,6 +1968,18 @@ enum_type returns [Pair<Vector<TypeCode>, TemplateGroup> returnPair = null]
}
name = ctx.removeEscapeCharacter($identifier.id);
enumTP = ctx.createEnumTypeCode(ctx.getScope(), name);

// Apply annotations to the TypeCode
if (null != annotations)
{
for(Annotation annotation : annotations)
{
if (annotation != null) // Some annotations may be ignored
{
enumTP.addAnnotation(ctx, annotation);
}
}
}
}
LEFT_BRACE enumerator_list[enumTP] RIGHT_BRACE
{
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/eprosima/idl/context/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public Context(
createAnnotationDeclaration(Annotation.mutable_str, null);

// Create default @Key annotation.
AnnotationDeclaration keyann = createAnnotationDeclaration("key", null);
AnnotationDeclaration keyann = createAnnotationDeclaration("" + Annotation.key_str + "", null);
JLBuenoLopez marked this conversation as resolved.
Show resolved Hide resolved
keyann.addMember(new AnnotationMember("value", new PrimitiveTypeCode(Kind.KIND_BOOLEAN), "true"));

AnnotationDeclaration mustundann = createAnnotationDeclaration("must_understand", null);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/eprosima/idl/parser/tree/Annotation.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,19 @@ public class Annotation

public static final String hashid_str = "hashid";
public static final String id_str = "id";
public static final String key_str = "key";

public static final String extensibility_str = "extensibility";
public static final String extensibility_enum_str = "ExtensibilityKind";
public static final String final_str = "final";
public static final String appendable_str = "appendable";
public static final String mutable_str = "mutable";
public static final String ex_final_str = "FINAL";
public static final String ex_final_val = "0";
public static final String ex_appendable_str = "APPENDABLE";
public static final String ex_appendable_val = "1";
public static final String ex_mutable_str = "MUTABLE";
public static final String ex_mutable_val = "2";

public Annotation(AnnotationDeclaration declaration)
{
Expand Down
11 changes: 4 additions & 7 deletions src/main/java/com/eprosima/idl/parser/tree/TypeDeclaration.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,10 @@ public TypeDeclaration(String scopeFile, boolean isInScope, String scope, String
m_typecode = typecode;
// Set as parent to the Typecode.
m_typecode.setParent(this);
}

@Override
public void addAnnotation(Context ctx, Annotation annotation)
{
super.addAnnotation(ctx, annotation);
m_typecode.addAnnotation(ctx, annotation); // The TypeObject may interpret the annotation directly
for(Annotation annotation : typecode.getAnnotationList())
{
addAnnotation(null, annotation);
}
}

public TypeCode getTypeCode()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public boolean addMember(
if (member.isAnnotationKey() && Kind.KIND_STRUCT != getKind())
{
throw new ParseException(null, "Error in member " + member.getName() +
": @key annotations only supported for structure's members.");
": @" + Annotation.key_str + " annotations only supported for structure's members.");
JLBuenoLopez marked this conversation as resolved.
Show resolved Hide resolved
}
if (null != member.getAnnotationBitBound() && (
Kind.KIND_ENUM != getKind() && Kind.KIND_BITMASK != getKind()))
Expand All @@ -148,12 +148,12 @@ public boolean addMember(
if(member.isAnnotationKey() && member.isAnnotationNonSerialized())
{
throw new ParseException(null, "Error in member " + member.getName() +
": @key and @non_serialized annotations are incompatible.");
": @" + Annotation.key_str + " and @non_serialized annotations are incompatible.");
}
if(member.isAnnotationKey() && member.isAnnotationOptional())
{
throw new ParseException(null, "Error in member " + member.getName() +
": @key and @optional annotations are incompatible.");
": @" + Annotation.key_str + " and @optional annotations are incompatible.");
}
if (member.isAnnotationId() && (
Kind.KIND_STRUCT != getKind() && Kind.KIND_UNION != getKind()))
Expand Down Expand Up @@ -366,7 +366,7 @@ public String getAnnotationAutoidValue() throws RuntimeGenerationException

private LinkedHashMap<String, Member> m_members = null;

private int last_index_ = 0;
protected int last_index_ = 0;

protected int last_id_ = 0;
}
12 changes: 12 additions & 0 deletions src/main/java/com/eprosima/idl/parser/typecode/StructTypeCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,14 @@ else if (super_type_ != null)
throw new ParseException(null, "Inheritance must correspond to the name of a previously defined structure");
}


last_id_ = super_type_.last_id_;
last_index_ = super_type_.last_index_;
if (get_extensibility(super_type_.get_extensibility()) != super_type_.get_extensibility())
{
throw new ParseException(null, "Base structure and derived structure must have same " +
Annotation.extensibility_enum_str);
}
}

@Override
Expand Down Expand Up @@ -214,6 +221,11 @@ public void addAnnotation(
public boolean addMember(
Member member) throws ParseException
{
if (member.isAnnotationKey() && null != super_type_)
{
throw new ParseException(null, "Error in member " + member.getName() +
": @" + Annotation.key_str + " cannot be used in a derived structure.");
}
calculate_member_id_(member);
return super.addMember(member);
}
Expand Down
44 changes: 29 additions & 15 deletions src/main/java/com/eprosima/idl/parser/typecode/TypeCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -360,56 +360,70 @@ public Collection<Annotation> getAnnotationList()
return m_annotations.values();
}

void calculate_extensibility()
void calculate_extensibility(
ExtensibilityKind base_ext)
{
if (ExtensibilityKind.NOT_APPLIED == extensibility_)
{
if (null != m_annotations.get(Annotation.final_str) ||
(null != m_annotations.get(Annotation.extensibility_str) &&
m_annotations.get(Annotation.extensibility_str).getValue().equals(Annotation.ex_final_str)))
if (m_annotations.containsKey(Annotation.final_str) ||
(m_annotations.containsKey(Annotation.extensibility_str) &&
m_annotations.get(Annotation.extensibility_str).getValue().equals(Annotation.ex_final_val)))
{
extensibility_ = ExtensibilityKind.FINAL;
}
else if (null != m_annotations.get(Annotation.appendable_str) ||
(null != m_annotations.get(Annotation.extensibility_str) &&
m_annotations.get(Annotation.extensibility_str).getValue().equals(Annotation.ex_appendable_str)))
else if (m_annotations.containsKey(Annotation.appendable_str) ||
(m_annotations.containsKey(Annotation.extensibility_str) &&
m_annotations.get(Annotation.extensibility_str).getValue().equals(Annotation.ex_appendable_val)))
{
extensibility_ = ExtensibilityKind.APPENDABLE;
}
else if (null != m_annotations.get(Annotation.mutable_str) ||
(null != m_annotations.get(Annotation.extensibility_str) &&
m_annotations.get(Annotation.extensibility_str).getValue().equals(Annotation.ex_mutable_str)))
else if (m_annotations.containsKey(Annotation.mutable_str) ||
(m_annotations.containsKey(Annotation.extensibility_str) &&
m_annotations.get(Annotation.extensibility_str).getValue().equals(Annotation.ex_mutable_val)))
{
extensibility_ = ExtensibilityKind.MUTABLE;
}
else
{
extensibility_ = default_extensibility;
if (ExtensibilityKind.NOT_APPLIED != base_ext)
{
extensibility_ = base_ext;
}
else
{
extensibility_ = default_extensibility;
}
JLBuenoLopez marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

public ExtensibilityKind get_extensibility()
{
calculate_extensibility();
return get_extensibility(ExtensibilityKind.NOT_APPLIED);
}

public ExtensibilityKind get_extensibility(
ExtensibilityKind base_ext)
{
calculate_extensibility(base_ext);
return extensibility_;
}

public boolean isAnnotationFinal()
{
calculate_extensibility();
calculate_extensibility(ExtensibilityKind.NOT_APPLIED);
return ExtensibilityKind.FINAL == extensibility_;
}

public boolean isAnnotationAppendable()
{
calculate_extensibility();
calculate_extensibility(ExtensibilityKind.NOT_APPLIED);
return ExtensibilityKind.APPENDABLE == extensibility_;
}

public boolean isAnnotationMutable()
{
calculate_extensibility();
calculate_extensibility(ExtensibilityKind.NOT_APPLIED);
return ExtensibilityKind.MUTABLE == extensibility_;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ public UnionTypeCode(
{
super(Kind.KIND_UNION, scope, name);
m_discriminatorTypeCode = discriminatorTypeCode;
++last_index_;
}

public void setDiscriminatorType(
TypeCode discriminatorTypeCode)
{
m_discriminatorTypeCode = discriminatorTypeCode;
++last_index_;
}

@Override
Expand Down