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 7 commits
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
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
19 changes: 11 additions & 8 deletions src/main/java/com/eprosima/idl/context/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,14 @@ public Context(
}

// Add here builtin annotations? (IDL 4.2 - 8.3.1 section)
AnnotationDeclaration idann = createAnnotationDeclaration("id", null);
AnnotationDeclaration idann = createAnnotationDeclaration(Annotation.id_str, null);
idann.addMember(new AnnotationMember("value", 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("value", autoidannenum, Annotation.autoid_hash_str));
JLBuenoLopez marked this conversation as resolved.
Show resolved Hide resolved

AnnotationDeclaration optionalann = createAnnotationDeclaration("optional", null);
optionalann.addMember(new AnnotationMember("value", new PrimitiveTypeCode(Kind.KIND_BOOLEAN), "true"));
Expand All @@ -192,7 +192,7 @@ public Context(
valueann.addMember(new AnnotationMember("value", 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));
Expand All @@ -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 Expand Up @@ -262,6 +262,9 @@ public Context(
AnnotationDeclaration amiann = createAnnotationDeclaration("ami", null);
amiann.addMember(new AnnotationMember("value", new PrimitiveTypeCode(Kind.KIND_BOOLEAN), "true"));

AnnotationDeclaration hashid_annotation = createAnnotationDeclaration(Annotation.hashid_str, null);
hashid_annotation.addMember(new AnnotationMember("value", 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"));
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/com/eprosima/idl/parser/tree/Annotation.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,27 @@

public class Annotation
{
public static final String autoid_str = "autoid";
public static final String autoid_enum_str = "AutoidKind";
public static final String autoid_sequential_str = "SEQUENTIAL";
public static final String autoid_sequential_value_str = "0";
public static final String autoid_hash_str = "HASH";

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 extensibility_str = "extensibility";
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
Loading