Skip to content

Commit

Permalink
Refs #19451. Apply suggestions
Browse files Browse the repository at this point in the history
Signed-off-by: Ricardo González Moreno <ricardo@richiware.dev>
  • Loading branch information
richiware committed Mar 14, 2024
1 parent 22bc8c7 commit 7c2d5a1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/main/antlr/com/eprosima/idl/parser/grammar/IDL.g4
Original file line number Diff line number Diff line change
Expand Up @@ -1773,7 +1773,7 @@ union_type [Vector<Annotation> annotations, ArrayList<Definition> defs] returns
LEFT_BRACE switch_body[unionTP] RIGHT_BRACE
{
// Calculate default label.
TemplateUtil.find_and_set_default_discriminator_value(defs, unionTP, ctx.getScopeFile(), line);
TemplateUtil.find_and_set_default_discriminator_value(defs, unionTP);
if(ctx.isInScopedFile() || ctx.isScopeLimitToAll())
{
Expand Down Expand Up @@ -1862,7 +1862,7 @@ case_stmt [UnionTypeCode unionTP] returns [TemplateGroup tg = null]
}
: ( KW_CASE const_exp
{
labels.add(TemplateUtil.checkUnionLabel(unionTP.getDiscriminator().getTypecode(), $const_exp.literalStr, ctx.getScopeFile(), _input.LT(1) != null ? _input.LT(1).getLine() - ctx.getCurrentIncludeLine() : 1));
labels.add(TemplateUtil.checkUnionLabel(unionTP.getDiscriminator().getTypecode(), $const_exp.literalStr));
} COLON
| KW_DEFAULT { defaul = true; } COLON
)+
Expand Down
71 changes: 41 additions & 30 deletions src/main/java/com/eprosima/idl/generator/manager/TemplateUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,36 +36,37 @@ else if(type.startsWith("std::vector"))
}

/*!
* This function tries to find a default discriminator value for the union following next guidelines extracted from
* standard.
* This function tries to find a default discriminator value for the union following the guidelines extracted from
* the standard.
*
* \b 6.14.2 Mapping for Union Types \b
* "If there is a default case specified, the union is initialized to this default case.
* In case the union has an implicit default member it is initialized to that case.
* In all other cases it is initialized to the first discriminant value specified in IDL"
*/
public static void find_and_set_default_discriminator_value(ArrayList<Definition> defs, UnionTypeCode union_type, String scopeFile, int line)
public static void find_and_set_default_discriminator_value(ArrayList<Definition> defs, UnionTypeCode union_type)
{
List<Member> members = union_type.getMembers();
TypeCode dist_type = union_type.getDiscriminator().getTypecode();
TypeCode disc_type = union_type.getDiscriminator().getTypecode();

if (Kind.KIND_ALIAS == dist_type.getKind())
if (Kind.KIND_ALIAS == disc_type.getKind())
{
dist_type = ((AliasTypeCode)dist_type).getContentTypeCode();
disc_type = ((AliasTypeCode)disc_type).getContentTypeCode();
}

if(dist_type != null)
if(disc_type != null)
{
if(dist_type.getKind() == Kind.KIND_OCTET ||
dist_type.getKind() == Kind.KIND_INT8 ||
dist_type.getKind() == Kind.KIND_SHORT ||
dist_type.getKind() == Kind.KIND_LONG ||
dist_type.getKind() == Kind.KIND_LONGLONG ||
dist_type.getKind() == Kind.KIND_UINT8 ||
dist_type.getKind() == Kind.KIND_USHORT ||
dist_type.getKind() == Kind.KIND_ULONG ||
dist_type.getKind() == Kind.KIND_ULONGLONG ||
dist_type.getKind() == Kind.KIND_CHAR ||
dist_type.getKind() == Kind.KIND_WCHAR)
if(disc_type.getKind() == Kind.KIND_OCTET ||
disc_type.getKind() == Kind.KIND_INT8 ||
disc_type.getKind() == Kind.KIND_SHORT ||
disc_type.getKind() == Kind.KIND_LONG ||
disc_type.getKind() == Kind.KIND_LONGLONG ||
disc_type.getKind() == Kind.KIND_UINT8 ||
disc_type.getKind() == Kind.KIND_USHORT ||
disc_type.getKind() == Kind.KIND_ULONG ||
disc_type.getKind() == Kind.KIND_ULONGLONG ||
disc_type.getKind() == Kind.KIND_CHAR ||
disc_type.getKind() == Kind.KIND_WCHAR)
{
long dvalue = -1;
boolean found = true;
Expand Down Expand Up @@ -124,7 +125,7 @@ public static void find_and_set_default_discriminator_value(ArrayList<Definition
union_type.setDefaultvalue(Long.toString(dvalue));
union_type.setJavaDefaultvalue(Long.toString(dvalue));
}
else if(dist_type.getKind() == Kind.KIND_BOOLEAN)
else if(disc_type.getKind() == Kind.KIND_BOOLEAN)
{
if(1 == members.size() && 1 == ((UnionMember)members.get(0)).getLabels().size())
{
Expand Down Expand Up @@ -155,9 +156,9 @@ else if(2 == members.size() && 1 == ((UnionMember)members.get(0)).getLabels().si
throw new ParseException(null, "boolean switch is malformed.");
}
}
else if(dist_type.getKind() == Kind.KIND_ENUM)
else if(disc_type.getKind() == Kind.KIND_ENUM)
{
EnumTypeCode enume = (EnumTypeCode)dist_type;
EnumTypeCode enume = (EnumTypeCode)disc_type;
List<Member> list = new ArrayList<Member>(members);
List<Member> enum_members = new ArrayList<Member>();
enum_members.addAll(enume.getMembers());
Expand Down Expand Up @@ -199,23 +200,20 @@ else if (0 < members.size() && 0 < ((UnionMember)members.get(0)).getLabels().siz
}
}

public static String checkUnionLabel(TypeCode dist_type, String label, String scopeFile, int line)
public static String checkUnionLabel(TypeCode disc_type, String label)
{
// TODO Faltan tipos: short, unsigneds...
if(dist_type != null)
if(disc_type != null)
{
if(dist_type.getKind() == Kind.KIND_ENUM)
if(disc_type.getKind() == Kind.KIND_ENUM)
{
EnumTypeCode enume = (EnumTypeCode)dist_type;
EnumTypeCode enume = (EnumTypeCode)disc_type;

if(enume.getScope() != null)
{
if(label.contains("::"))
{
if(!label.startsWith(enume.getScope()))
{
//TODO
//throw new ParseException(label, "was not declared previously");
throw new ParseException(null, "was not declared previously");
}
else
Expand All @@ -228,11 +226,24 @@ public static String checkUnionLabel(TypeCode dist_type, String label, String sc
{
if(label.contains("::"))
{
//TODO
//throw new ParseException(label, "was not declared previously");
throw new ParseException(null, "was not declared previously");
}
}

boolean found = false;
for(Member member : enume.getMembers())
{
if (label.equals(member.getName()))
{
found = true;
break;
}
}

if (!found)
{
throw new ParseException(null, "Invalid enumerator label");
}
}
}

Expand Down

0 comments on commit 7c2d5a1

Please sign in to comment.