@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()); } }
public void convertFunction( Function function, Signatures signatures, boolean isCallback, final DeclarationsHolder out, final Identifier libraryClassName, int iConstructor) { if (result.config.functionsAccepter != null && !result.config.functionsAccepter.adapt(function)) return; // if (function.findParentOfType(Template)) String library = result.getLibrary(function); Identifier functionName = function.getName(); boolean isMethod = function.getParentElement() instanceof Struct; if (functionName == null || isCallback) { if (function.getParentElement() instanceof FunctionSignature) functionName = ident(result.config.callbackInvokeMethodName); else return; } if (function.getParentElement() instanceof FriendDeclaration) return; String n = functionName.toString(); if (n.contains("<") || n.startsWith("~")) return; if (result.config.beautifyNames) functionName = ident(result.typeConverter.beautify(n, false)); functionName = result.typeConverter.getValidJavaMethodName(functionName); if (functionName == null) return; // if (functionName.equals("operator")) // functionName String sig = function.computeSignature(SignatureType.JavaStyle); DeclarationsHolder objOut = result.config.reification && !isCallback && !isMethod ? new DeclarationsHolder() { @Override public void addDeclaration(Declaration d) { out.addDeclaration(d); if (d instanceof Function) { Function f = (Function) d; List<Arg> args = f.getArgs(); List<TypeRef> trs = new ArrayList<TypeRef>(2); trs.add(f.getValueType()); if (!args.isEmpty()) trs.add(args.get(0).getValueType()); for (TypeRef tr : trs) { if (tr instanceof SimpleTypeRef) { Identifier id = ((SimpleTypeRef) tr).getName(); if (result.isFakePointer(id)) { result.addFunctionReifiableInFakePointer(id, libraryClassName, f); } } } } } @Override public List<Declaration> getDeclarations() { return out.getDeclarations(); } } : out; try { convertFunction( function, signatures, isCallback, objOut, libraryClassName, sig, functionName, library, iConstructor); } catch (UnsupportedConversionException ex) { Declaration d = skipDeclaration(function); if (d != null) { d.addToCommentBefore(ex.toString()); out.addDeclaration(d); } } }
public void convertConstants( String library, List<Define> defines, Element sourcesRoot, final Signatures signatures, final DeclarationsHolder out, final Identifier libraryClassName) { // final List<Define> defines = new ArrayList<Define>(); final Map<String, String> constants = Result.getMap(result.stringConstants, library); // sourcesRoot.accept( new Scanner() { // @Override // public void visitDefine(Define define) { // super.visitDefine(define); // if (elementsFilter.accept(define)) // defines.add(define); // } @Override public void visitVariablesDeclaration(VariablesDeclaration v) { super.visitVariablesDeclaration(v); // if (!elementsFilter.accept(v)) // return; if (v.findParentOfType(Struct.class) != null) return; if (v.getValueType() instanceof FunctionSignature) return; for (Declarator decl : v.getDeclarators()) { if (!(decl instanceof DirectDeclarator)) continue; // TODO provide a mapping of exported values TypeRef mutatedType = (TypeRef) decl.mutateTypeKeepingParent(v.getValueType()); if (mutatedType == null || !mutatedType.getModifiers().contains(ModifierType.Const) || mutatedType.getModifiers().contains(ModifierType.Extern) || decl.getDefaultValue() == null) continue; // TypeRef type = v.getValueType(); String name = decl.resolveName(); JavaPrim prim = result.typeConverter.getPrimitive(mutatedType, libraryClassName); if (prim == null) { if (mutatedType.toString().contains("NSString")) { String value = constants.get(name); if (value != null) outputNSString(name, value, out, signatures, v, decl); } continue; } try { // DirectDeclarator dd = (DirectDeclarator)decl; Pair<Expression, TypeRef> val = result.typeConverter.convertExpressionToJava( decl.getDefaultValue(), libraryClassName, true); if (!signatures.addVariable(name)) continue; // TODO TypeRef tr = prim == JavaPrim.NativeLong || prim == JavaPrim.NativeSize ? typeRef("long") : primRef(prim) // result.typeConverter.convertTypeToJNA(mutatedType, // TypeConversion.TypeConversionMode.FieldType, libraryClassName) ; VariablesDeclaration vd = new VariablesDeclaration(tr, new DirectDeclarator(name, val.getFirst())); if (!result.config.noComments) { vd.setCommentBefore(v.getCommentBefore()); vd.addToCommentBefore(decl.getCommentBefore()); vd.addToCommentBefore(decl.getCommentAfter()); vd.addToCommentBefore(v.getCommentAfter()); } // if (result.config.runtime == // JNAeratorConfig.Runtime.BridJ) vd.addModifiers(ModifierType.Public, ModifierType.Static, ModifierType.Final); out.addDeclaration(vd); } catch (UnsupportedConversionException e) { out.addDeclaration(skipDeclaration(v, e.toString())); } } } }); if (defines != null) { for (Define define : reorderDefines(defines)) { if (define.getValue() == null) continue; try { // System.out.println("Define " + define.getName() + " = " + define.getValue()); out.addDeclaration( outputConstant( define.getName(), define.getValue(), signatures, define.getValue(), "define", libraryClassName, true, false, false)); } catch (UnsupportedConversionException ex) { out.addDeclaration(skipDeclaration(define, ex.toString())); } } } for (Map.Entry<String, String> e : constants.entrySet()) { outputNSString(e.getKey(), e.getValue(), out, signatures); } }