Skip to content

Commit

Permalink
Support for annotations to set MemberId (#115)
Browse files Browse the repository at this point in the history
* Refs #20084. Add index to Members

Signed-off-by: Ricardo González Moreno <ricardo@richiware.dev>

* Refs #20084. Add @id

Signed-off-by: Ricardo González Moreno <ricardo@richiware.dev>

* Refs #20084. Good use of StructTypeCode super_type

Signed-off-by: Ricardo González Moreno <ricardo@richiware.dev>

* Refs #20084. Returning calculated id.

Signed-off-by: Ricardo González Moreno <ricardo@richiware.dev>

* Refs #20084. Using autoid(SEQUENTIAL).

Signed-off-by: Ricardo González Moreno <ricardo@richiware.dev>

* Refs #20084. Using autoid(HASH) and hashid.

Signed-off-by: Ricardo González Moreno <ricardo@richiware.dev>

* Refs #20084. Fix applying annotations to struct

Signed-off-by: Ricardo González Moreno <ricardo@richiware.dev>

* Refs #20084. Applying suggestions.

Signed-off-by: Ricardo González Moreno <ricardo@richiware.dev>

---------

Signed-off-by: Ricardo González Moreno <ricardo@richiware.dev>
  • Loading branch information
richiware authored Dec 11, 2023
1 parent 18d5941 commit 003aa5c
Show file tree
Hide file tree
Showing 9 changed files with 428 additions and 121 deletions.
130 changes: 94 additions & 36 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; }
| struct_type { ttg=$struct_type.returnPair; fw_name = $struct_type.fw_name; }
| union_type[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; }
: ( 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[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 @@ -950,11 +952,11 @@ template_type_spec returns [Pair<TypeCode, TemplateGroup> returnPair = null]
;

constr_type_spec returns [Pair<Vector<TypeCode>, TemplateGroup> returnPair = null]
: struct_type
| union_type[null]
| enum_type
| bitset_type
| bitmask_type
: struct_type[null]
| union_type[null, null]
| 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 @@ -1458,7 +1484,7 @@ bit_values [BitmaskTypeCode owner]
)*
;

struct_type returns [Pair<Vector<TypeCode>, TemplateGroup> returnPair = null, String fw_name = null]
struct_type[Vector<Annotation> annotations] returns [Pair<Vector<TypeCode>, TemplateGroup> returnPair = null, String fw_name = null]
@init{
String name = null;
Vector<TypeCode> vector = null;
Expand Down Expand Up @@ -1508,13 +1534,25 @@ struct_type returns [Pair<Vector<TypeCode>, TemplateGroup> returnPair = null, St
structTP = ctx.createStructTypeCode(name);
}
structTP.setDefined();

// Apply annotations to the TypeCode
if (null != annotations)
{
for(Annotation annotation : annotations)
{
if (annotation != null) // Some annotations may be ignored
{
structTP.addAnnotation(ctx, annotation);
}
}
}
}
(COLON scoped_name
{
TypeCode scopedType = ctx.getTypeCode($scoped_name.pair.first());
if (scopedType instanceof StructTypeCode)
{
parentStruct = (StructTypeCode)scopedType;
structTP.addInheritance(ctx, scopedType);
}
else
{
Expand All @@ -1540,10 +1578,6 @@ struct_type returns [Pair<Vector<TypeCode>, TemplateGroup> returnPair = null, St
vector = new Vector<TypeCode>();
structTP.setForwarded(false);
vector.add(structTP);
if (parentStruct != null)
{
structTP.addInheritance(ctx, parentStruct);
}
$returnPair = new Pair<Vector<TypeCode>, TemplateGroup>(vector, structTemplates);
$fw_name = (fw_declaration) ? name : null;
}
Expand Down Expand Up @@ -1587,7 +1621,7 @@ member_list [StructTypeCode structTP] returns [TemplateGroup tg = null]
if(pair.first().second() != null)
{
$tg = pair.first().second();
}
}
}
}
}
Expand Down Expand Up @@ -1667,7 +1701,7 @@ member returns [Vector<Pair<Pair<Pair<String, Token>, TemplateGroup>, Member>> r
}
;

union_type [ArrayList<Definition> defs] returns [Pair<Vector<TypeCode>, TemplateGroup> returnPair = null, String fw_name = null]
union_type [Vector<Annotation> annotations, ArrayList<Definition> defs] returns [Pair<Vector<TypeCode>, TemplateGroup> returnPair = null, String fw_name = null]
@init {
String name = null;
int line = 0;
Expand Down Expand Up @@ -1726,6 +1760,19 @@ union_type [ArrayList<Definition> defs] returns [Pair<Vector<TypeCode>, Template
unionTP = ctx.createUnionTypeCode(ctx.getScope(), name, dist_type);
}
unionTP.setDefined();

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

line= _input.LT(1) != null ? _input.LT(1).getLine() - ctx.getCurrentIncludeLine() : 1;
}
LEFT_BRACE switch_body[unionTP] RIGHT_BRACE
Expand Down Expand Up @@ -1789,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 @@ -1905,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 @@ -1922,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
55 changes: 29 additions & 26 deletions src/main/java/com/eprosima/idl/context/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,42 +173,42 @@ public Context(
}

// Add here builtin annotations? (IDL 4.2 - 8.3.1 section)
AnnotationDeclaration idann = createAnnotationDeclaration("id", null);
idann.addMember(new AnnotationMember("value", new PrimitiveTypeCode(Kind.KIND_LONG), "-1"));
AnnotationDeclaration idann = createAnnotationDeclaration(Annotation.id_str, null);
idann.addMember(new AnnotationMember(Annotation.value_str, new PrimitiveTypeCode(Kind.KIND_LONG), "-1"));

AnnotationDeclaration autoidann = createAnnotationDeclaration("autoid", null);
EnumTypeCode autoidannenum = new EnumTypeCode(autoidann.getScopedname(), "autoidannenum");
autoidannenum.addMember(new EnumMember("SEQUENTIAL"));
autoidannenum.addMember(new EnumMember("HASH"));
autoidann.addMember(new AnnotationMember("value", autoidannenum, autoidannenum.getInitialValue()));
AnnotationDeclaration autoidann = createAnnotationDeclaration(Annotation.autoid_str, null);
EnumTypeCode autoidannenum = new EnumTypeCode(autoidann.getScopedname(), Annotation.autoid_enum_str);
autoidannenum.addMember(new EnumMember(Annotation.autoid_sequential_str));
autoidannenum.addMember(new EnumMember(Annotation.autoid_hash_str));
autoidann.addMember(new AnnotationMember(Annotation.value_str, autoidannenum, Annotation.autoid_hash_str));

AnnotationDeclaration optionalann = createAnnotationDeclaration("optional", null);
optionalann.addMember(new AnnotationMember("value", new PrimitiveTypeCode(Kind.KIND_BOOLEAN), "true"));
optionalann.addMember(new AnnotationMember(Annotation.value_str, new PrimitiveTypeCode(Kind.KIND_BOOLEAN), "true"));

AnnotationDeclaration positionann = createAnnotationDeclaration("position", null);
positionann.addMember(new AnnotationMember("value", new PrimitiveTypeCode(Kind.KIND_USHORT), "-1"));
positionann.addMember(new AnnotationMember(Annotation.value_str, new PrimitiveTypeCode(Kind.KIND_USHORT), "-1"));

AnnotationDeclaration valueann = createAnnotationDeclaration("value", null);
valueann.addMember(new AnnotationMember("value", new AnyTypeCode(), null));
AnnotationDeclaration valueann = createAnnotationDeclaration(Annotation.value_str, null);
valueann.addMember(new AnnotationMember(Annotation.value_str, new AnyTypeCode(), null));

AnnotationDeclaration extensibilityann = createAnnotationDeclaration(Annotation.extensibility_str, null);
EnumTypeCode extensibilityannenum = new EnumTypeCode(extensibilityann.getScopedname(), "extensibilityannenum");
EnumTypeCode extensibilityannenum = new EnumTypeCode(extensibilityann.getScopedname(), Annotation.extensibility_enum_str);
extensibilityannenum.addMember(new EnumMember(Annotation.ex_final_str));
extensibilityannenum.addMember(new EnumMember(Annotation.ex_appendable_str));
extensibilityannenum.addMember(new EnumMember(Annotation.ex_mutable_str));
extensibilityann.addMember(new AnnotationMember("value", extensibilityannenum,
extensibilityann.addMember(new AnnotationMember(Annotation.value_str, extensibilityannenum,
extensibilityannenum.getInitialValue()));

createAnnotationDeclaration(Annotation.final_str, null);
createAnnotationDeclaration(Annotation.appendable_str, null);
createAnnotationDeclaration(Annotation.mutable_str, null);

// Create default @Key annotation.
AnnotationDeclaration keyann = createAnnotationDeclaration("key", null);
keyann.addMember(new AnnotationMember("value", new PrimitiveTypeCode(Kind.KIND_BOOLEAN), "true"));
AnnotationDeclaration keyann = createAnnotationDeclaration(Annotation.key_str, null);
keyann.addMember(new AnnotationMember(Annotation.value_str, new PrimitiveTypeCode(Kind.KIND_BOOLEAN), "true"));

AnnotationDeclaration mustundann = createAnnotationDeclaration("must_understand", null);
mustundann.addMember(new AnnotationMember("value", new PrimitiveTypeCode(Kind.KIND_BOOLEAN), "true"));
mustundann.addMember(new AnnotationMember(Annotation.value_str, new PrimitiveTypeCode(Kind.KIND_BOOLEAN), "true"));

createAnnotationDeclaration("default_literal", null);

Expand All @@ -219,25 +219,25 @@ public Context(
//String.valueOf(Integer.MAX_VALUE)));

AnnotationDeclaration unitsann = createAnnotationDeclaration("units", null);
unitsann.addMember(new AnnotationMember("value", new PrimitiveTypeCode(Kind.KIND_STRING), ""));
unitsann.addMember(new AnnotationMember(Annotation.value_str, new PrimitiveTypeCode(Kind.KIND_STRING), ""));

AnnotationDeclaration defaultann = createAnnotationDeclaration("default", null);
defaultann.addMember(new AnnotationMember("value", new AnyTypeCode(), null));
defaultann.addMember(new AnnotationMember(Annotation.value_str, new AnyTypeCode(), null));

AnnotationDeclaration minann = createAnnotationDeclaration("min", null);
minann.addMember(new AnnotationMember("value", new AnyTypeCode(), null));
minann.addMember(new AnnotationMember(Annotation.value_str, new AnyTypeCode(), null));

AnnotationDeclaration maxann = createAnnotationDeclaration("max", null);
maxann.addMember(new AnnotationMember("value", new AnyTypeCode(), null));
maxann.addMember(new AnnotationMember(Annotation.value_str, new AnyTypeCode(), null));

AnnotationDeclaration bit_boundann = createAnnotationDeclaration("bit_bound", null);
bit_boundann.addMember(new AnnotationMember("value", new PrimitiveTypeCode(Kind.KIND_USHORT), "-1"));
bit_boundann.addMember(new AnnotationMember(Annotation.value_str, new PrimitiveTypeCode(Kind.KIND_USHORT), "-1"));

AnnotationDeclaration externalann = createAnnotationDeclaration("external", null);
externalann.addMember(new AnnotationMember("value", new PrimitiveTypeCode(Kind.KIND_BOOLEAN), "true"));
externalann.addMember(new AnnotationMember(Annotation.value_str, new PrimitiveTypeCode(Kind.KIND_BOOLEAN), "true"));

AnnotationDeclaration nestedann = createAnnotationDeclaration("nested", null);
nestedann.addMember(new AnnotationMember("value", new PrimitiveTypeCode(Kind.KIND_BOOLEAN), "true"));
nestedann.addMember(new AnnotationMember(Annotation.value_str, new PrimitiveTypeCode(Kind.KIND_BOOLEAN), "true"));

AnnotationDeclaration verbatimann = createAnnotationDeclaration("verbatim", null);
EnumTypeCode verbatimannenum = new EnumTypeCode(verbatimann.getScopedname(), "verbatimannenum");
Expand All @@ -257,14 +257,17 @@ public Context(
// CORBA, DDS, * (any), or custom value

AnnotationDeclaration onewayann = createAnnotationDeclaration("oneway", null);
onewayann.addMember(new AnnotationMember("value", new PrimitiveTypeCode(Kind.KIND_BOOLEAN), "true"));
onewayann.addMember(new AnnotationMember(Annotation.value_str, new PrimitiveTypeCode(Kind.KIND_BOOLEAN), "true"));

AnnotationDeclaration amiann = createAnnotationDeclaration("ami", null);
amiann.addMember(new AnnotationMember("value", new PrimitiveTypeCode(Kind.KIND_BOOLEAN), "true"));
amiann.addMember(new AnnotationMember(Annotation.value_str, new PrimitiveTypeCode(Kind.KIND_BOOLEAN), "true"));

AnnotationDeclaration hashid_annotation = createAnnotationDeclaration(Annotation.hashid_str, null);
hashid_annotation.addMember(new AnnotationMember(Annotation.value_str, new StringTypeCode(Kind.KIND_STRING, null, null), ""));

// Create default @non_serialized annotation.
AnnotationDeclaration non_serializedann = createAnnotationDeclaration("non_serialized", null);
non_serializedann.addMember(new AnnotationMember("value", new PrimitiveTypeCode(Kind.KIND_BOOLEAN), "true"));
non_serializedann.addMember(new AnnotationMember(Annotation.value_str, new PrimitiveTypeCode(Kind.KIND_BOOLEAN), "true"));
}

public String getFilename()
Expand Down
Loading

0 comments on commit 003aa5c

Please sign in to comment.