| 1 |
// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima). |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package com.eprosima.idl.generator.manager; |
| 16 |
|
| 17 |
import com.eprosima.idl.parser.exception.ParseException; |
| 18 |
import com.eprosima.idl.parser.grammar.IDLParser.DefinitionContext; |
| 19 |
import com.eprosima.idl.parser.typecode.*; |
| 20 |
import com.eprosima.idl.parser.tree.Definition; |
| 21 |
import com.eprosima.idl.parser.tree.ConstDeclaration; |
| 22 |
import java.util.ArrayList; |
| 23 |
import java.util.List; |
| 24 |
|
| 25 |
|
| 26 |
public class TemplateUtil |
| 27 |
{ |
| 28 |
public static String stripType(String type) |
| 29 |
{ |
| 30 |
if(type.startsWith("std::array")) |
| 31 |
return "std::array"; |
| 32 |
else if(type.startsWith("std::vector")) |
| 33 |
return "std::vector"; |
| 34 |
else |
| 35 |
return type; |
| 36 |
} |
| 37 |
|
| 38 |
/*! |
| 39 |
* This function tries to find a default discriminator value for the union following the guidelines extracted from |
| 40 |
* the standard. |
| 41 |
* |
| 42 |
* \b 6.14.2 Mapping for Union Types \b |
| 43 |
* "If there is a default case specified, the union is initialized to this default case. |
| 44 |
* In case the union has an implicit default member it is initialized to that case. |
| 45 |
* In all other cases it is initialized to the first discriminant value specified in IDL" |
| 46 |
*/ |
| 47 |
public static void find_and_set_default_discriminator_value(ArrayList<Definition> defs, UnionTypeCode union_type) |
| 48 |
{ |
| 49 |
List<Member> members = union_type.getMembers(); |
| 50 |
TypeCode disc_type = union_type.getDiscriminator().getTypecode(); |
| 51 |
|
| 52 |
if (Kind.KIND_ALIAS == disc_type.getKind()) |
| 53 |
{ |
| 54 |
disc_type = ((AliasTypeCode)disc_type).getContentTypeCode(); |
| 55 |
} |
| 56 |
|
| 57 |
if(disc_type != null) |
| 58 |
{ |
| 59 |
if(disc_type.getKind() == Kind.KIND_OCTET || |
| 60 |
disc_type.getKind() == Kind.KIND_INT8 || |
| 61 |
disc_type.getKind() == Kind.KIND_SHORT || |
| 62 |
disc_type.getKind() == Kind.KIND_LONG || |
| 63 |
disc_type.getKind() == Kind.KIND_LONGLONG || |
| 64 |
disc_type.getKind() == Kind.KIND_UINT8 || |
| 65 |
disc_type.getKind() == Kind.KIND_USHORT || |
| 66 |
disc_type.getKind() == Kind.KIND_ULONG || |
| 67 |
disc_type.getKind() == Kind.KIND_ULONGLONG || |
| 68 |
disc_type.getKind() == Kind.KIND_CHAR || |
| 69 |
disc_type.getKind() == Kind.KIND_WCHAR) |
| 70 |
{ |
| 71 |
long default_discriminator_value = -1; |
| 72 |
long minimum_valid_value = -1; |
| 73 |
|
| 74 |
// For primitive types, if the default case was not defined, use the highest unused value as the |
| 75 |
// implicit default value. |
| 76 |
switch (disc_type.getKind()) |
| 77 |
{ |
| 78 |
case Kind.KIND_OCTET: |
| 79 |
case Kind.KIND_INT8: |
| 80 |
case Kind.KIND_UINT8: |
| 81 |
case Kind.KIND_CHAR: |
| 82 |
default_discriminator_value = Byte.MAX_VALUE; |
| 83 |
minimum_valid_value = Byte.MIN_VALUE; |
| 84 |
break; |
| 85 |
case Kind.KIND_SHORT: |
| 86 |
case Kind.KIND_USHORT: |
| 87 |
case Kind.KIND_WCHAR: |
| 88 |
default_discriminator_value = Short.MAX_VALUE; |
| 89 |
minimum_valid_value = Short.MIN_VALUE; |
| 90 |
break; |
| 91 |
case Kind.KIND_LONG: |
| 92 |
case Kind.KIND_ULONG: |
| 93 |
default_discriminator_value = Integer.MAX_VALUE; |
| 94 |
minimum_valid_value = Integer.MIN_VALUE; |
| 95 |
break; |
| 96 |
default: |
| 97 |
default_discriminator_value = Long.MAX_VALUE; |
| 98 |
minimum_valid_value = Long.MIN_VALUE; |
| 99 |
} |
| 100 |
|
| 101 |
boolean found = true; |
| 102 |
List<Member> list = new ArrayList<Member>(members); |
| 103 |
|
| 104 |
do |
| 105 |
{ |
| 106 |
found = false; |
| 107 |
|
| 108 |
for(Member member : list) |
| 109 |
{ |
| 110 |
if(member instanceof UnionMember) |
| 111 |
{ |
| 112 |
UnionMember umember = (UnionMember)member; |
| 113 |
|
| 114 |
for(String label : umember.getLabels()) |
| 115 |
{ |
| 116 |
long value = 0; |
| 117 |
try |
| 118 |
{ |
| 119 |
value = Long.decode(label); |
| 120 |
} |
| 121 |
catch(NumberFormatException nfe) |
| 122 |
{ |
| 123 |
// It could be a const |
| 124 |
if (null != defs) |
| 125 |
{ |
| 126 |
for (Definition def : defs) |
| 127 |
{ |
| 128 |
if (def.isIsConstDeclaration()) |
| 129 |
{ |
| 130 |
ConstDeclaration decl = (ConstDeclaration)def; |
| 131 |
if (decl.getName().equals(label)) |
| 132 |
{ |
| 133 |
value = Long.decode(decl.getValue()); |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
if(default_discriminator_value == value) |
| 141 |
{ |
| 142 |
found = true; |
| 143 |
break; |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
if(found) |
| 149 |
{ |
| 150 |
// Possible default implicit value used. Try with the next lower value. |
| 151 |
--default_discriminator_value; |
| 152 |
break; |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
while(found && default_discriminator_value >= minimum_valid_value); |
| 157 |
|
| 158 |
if (found) |
| 159 |
{ |
| 160 |
// All values were used. Using value 0 as default implicit value. |
| 161 |
default_discriminator_value = 0; |
| 162 |
} |
| 163 |
|
| 164 |
union_type.setDefaultvalue(Long.toString(default_discriminator_value)); |
| 165 |
union_type.setJavaDefaultvalue(Long.toString(default_discriminator_value)); |
| 166 |
} |
| 167 |
else if(disc_type.getKind() == Kind.KIND_BOOLEAN) |
| 168 |
{ |
| 169 |
if(1 == members.size() && 1 == ((UnionMember)members.get(0)).getLabels().size()) |
| 170 |
{ |
| 171 |
if(((UnionMember)members.get(0)).getLabels().get(0).equals("true")) |
| 172 |
{ |
| 173 |
union_type.setDefaultvalue("false"); |
| 174 |
union_type.setJavaDefaultvalue("false"); |
| 175 |
} |
| 176 |
else if(((UnionMember)members.get(0)).getLabels().get(0).equals("false")) |
| 177 |
{ |
| 178 |
union_type.setDefaultvalue("true"); |
| 179 |
union_type.setJavaDefaultvalue("true"); |
| 180 |
} |
| 181 |
else |
| 182 |
{ |
| 183 |
throw new ParseException(null, "is not a valid label for a boolean discriminator."); |
| 184 |
} |
| 185 |
} |
| 186 |
else if(2 == members.size() && 1 == ((UnionMember)members.get(0)).getLabels().size() && |
| 187 |
1 == ((UnionMember)members.get(1)).getLabels().size()) |
| 188 |
{ |
| 189 |
|
| 190 |
union_type.setDefaultvalue(((UnionMember)members.get(0)).getLabels().get(0)); |
| 191 |
union_type.setJavaDefaultvalue(((UnionMember)members.get(0)).getLabels().get(0)); |
| 192 |
} |
| 193 |
else |
| 194 |
{ |
| 195 |
throw new ParseException(null, "boolean switch is malformed."); |
| 196 |
} |
| 197 |
} |
| 198 |
else if(disc_type.getKind() == Kind.KIND_ENUM) |
| 199 |
{ |
| 200 |
EnumTypeCode enume = (EnumTypeCode)disc_type; |
| 201 |
List<Member> list = new ArrayList<Member>(members); |
| 202 |
List<Member> enum_members = new ArrayList<Member>(); |
| 203 |
enum_members.addAll(enume.getMembers()); |
| 204 |
|
| 205 |
for(Member member : members) |
| 206 |
{ |
| 207 |
UnionMember umember = (UnionMember)member; |
| 208 |
|
| 209 |
for(String label : umember.getInternalLabels()) |
| 210 |
{ |
| 211 |
int count = 0; |
| 212 |
|
| 213 |
for(; count < enum_members.size(); ++count) |
| 214 |
{ |
| 215 |
if(((EnumMember)enum_members.get(count)).getName().equals(label)) |
| 216 |
break; |
| 217 |
} |
| 218 |
|
| 219 |
if(count < enum_members.size()) |
| 220 |
enum_members.remove(count); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
if(enum_members.size() > 0) |
| 225 |
{ |
| 226 |
union_type.setDefaultvalue(enume.getScopedname() + "::" + enum_members.get(0).getName()); |
| 227 |
union_type.setJavaDefaultvalue(enume.javapackage + enume.getJavaScopedname() + "." + enum_members.get(0).getName()); |
| 228 |
} |
| 229 |
else if (0 < members.size() && 0 < ((UnionMember)members.get(0)).getLabels().size()) |
| 230 |
{ |
| 231 |
union_type.setDefaultvalue(((UnionMember)members.get(0)).getLabels().get(0)); |
| 232 |
union_type.setJavaDefaultvalue(((UnionMember)members.get(0)).getLabels().get(0)); |
| 233 |
} |
| 234 |
} |
| 235 |
else |
| 236 |
{ |
| 237 |
throw new ParseException(null, "Not supported type discriminator."); |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
public static String checkUnionLabel(TypeCode disc_type, String label) |
| 243 |
{ |
| 244 |
if(disc_type != null) |
| 245 |
{ |
| 246 |
if(disc_type.getKind() == Kind.KIND_ENUM) |
| 247 |
{ |
| 248 |
EnumTypeCode enume = (EnumTypeCode)disc_type; |
| 249 |
|
| 250 |
if(enume.getScope() != null) |
| 251 |
{ |
| 252 |
if(label.contains("::")) |
| 253 |
{ |
| 254 |
if(!label.startsWith(enume.getScope())) |
| 255 |
{ |
| 256 |
throw new ParseException(null, "was not declared previously"); |
| 257 |
} |
| 258 |
else |
| 259 |
{ |
| 260 |
label = label.replaceFirst(enume.getScope() + "::", ""); |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
else |
| 265 |
{ |
| 266 |
if(label.contains("::")) |
| 267 |
{ |
| 268 |
throw new ParseException(null, "was not declared previously"); |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
boolean found = false; |
| 273 |
for(Member member : enume.getMembers()) |
| 274 |
{ |
| 275 |
if (label.equals(member.getName())) |
| 276 |
{ |
| 277 |
found = true; |
| 278 |
break; |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
if (!found) |
| 283 |
{ |
| 284 |
throw new ParseException(null, "Invalid enumerator label"); |
| 285 |
} |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
return label; |
| 290 |
} |
| 291 |
} |