@SuppressWarnings("static-access") protected Declaration outputConstant( String name, Pair<Expression, TypeRef> x, Signatures signatures, Element element, String elementTypeDescription, Identifier libraryClassName, boolean addFileComment, boolean signalErrors, boolean forceInteger, boolean alreadyConverted) throws UnsupportedConversionException { try { if (result.typeConverter.isJavaKeyword(name)) throw new UnsupportedConversionException( element, "The name '" + name + "' is invalid for a Java field."); Pair<Expression, TypeRef> converted = alreadyConverted ? x : result.typeConverter.convertExpressionToJava(x.getFirst(), libraryClassName, true); // TypeRef tr = result.typeConverter.inferJavaType(converted); JavaPrim prim = result.typeConverter.getPrimitive(converted.getValue(), libraryClassName); if (forceInteger && prim == JavaPrim.Boolean) { prim = JavaPrim.Int; // tr = typeRef("int"); converted = pair( expr("true".equals(String.valueOf(converted.toString())) ? 1 : 0), typeRef(Integer.TYPE)); } if ((prim == null || converted.getValue() == null) && signalErrors) { if (result.config.limitComments) return null; return new EmptyDeclaration("Failed to infer type of " + converted); } else if (prim != JavaPrim.Void && converted.getValue() != null) { // if (prim == JavaPrim.Int) // tr = typeRef("long"); if (signatures.addVariable(name)) { String t = converted.toString(); if (t.contains("sizeof")) { converted = alreadyConverted ? x : result.typeConverter.convertExpressionToJava( x.getFirst(), libraryClassName, false); } // TypeRef tr = new TypeRef.SimpleTypeRef(result.typeConverter.typeToJNA(type, vs, // TypeConversion.TypeConversionMode.FieldType, callerLibraryClass)); TypeRef tr = converted.getValue(); Expression value = converted.getFirst(); if (result.config.castConstants) { if (!(value instanceof Constant) && !(value instanceof VariableRef)) value.setParenthesis(true); value = new Cast(tr, value); } Declaration declaration = new VariablesDeclaration(tr, new DirectDeclarator(name, value)); declaration.addModifiers(ModifierType.Public, ModifierType.Static, ModifierType.Final); if (!result.config.noComments) declaration.importComments( element, addFileComment ? getFileCommentContent(element) : null); return declaration; } } return skipDeclaration(element, elementTypeDescription); } catch (UnsupportedConversionException e) { return skipDeclaration(element, elementTypeDescription, e.toString()); } }
protected List<EnumItemResult> getEnumValuesAndCommentsByName( Enum e, Signatures signatures, Identifier libraryClassName) { List<EnumItemResult> ret = new ArrayList<EnumItemResult>(); Integer lastAdditiveValue = null; Expression lastRefValue = null; boolean failedOnceForThisEnum = false; for (com.ochafik.lang.jnaerator.parser.Enum.EnumItem item : e.getItems()) { EnumItemResult res = new EnumItemResult(); res.originalItem = item; try { if (item.getArguments().isEmpty()) { // no explicit value if (lastRefValue == null) { if (lastAdditiveValue != null) { lastAdditiveValue++; res.unconvertedValue = expr(lastAdditiveValue); } else { if (item == e.getItems().get(0)) { lastAdditiveValue = 0; res.unconvertedValue = expr(lastAdditiveValue); } else res.unconvertedValue = null; } } else { // has a last reference value if (lastAdditiveValue != null) lastAdditiveValue++; else lastAdditiveValue = 1; res.unconvertedValue = expr(lastRefValue.clone(), Expression.BinaryOperator.Plus, expr(lastAdditiveValue)); } } else { // has an explicit value failedOnceForThisEnum = false; // reset skipping lastAdditiveValue = null; lastRefValue = item.getArguments().get(0); res.unconvertedValue = lastRefValue; if (lastRefValue instanceof Expression.Constant) { try { lastAdditiveValue = ((Expression.Constant) lastRefValue).asInteger(); lastRefValue = null; } catch (Exception ex) { } } } res.convertedValue = result .typeConverter .convertExpressionToJava(res.unconvertedValue, libraryClassName, true) .getFirst(); } catch (Exception ex) { failedOnceForThisEnum = true; res.exceptionMessage = ex.toString(); } failedOnceForThisEnum = failedOnceForThisEnum || res.errorElement != null; if (failedOnceForThisEnum) res.errorElement = skipDeclaration(item); ret.add(res); } return ret; }