String getFileCommentContent(Element e) {
    if (e == null || result.config.limitComments) return null;

    String f = Element.getFileOfAscendency(e);
    if (f == null && e != null && e.getElementLine() >= 0)
      return "<i>native declaration : line " + e.getElementLine() + "</i>";

    return f == null ? null : getFileCommentContent(new File(f), e);
  }
  public void computeVariablesDependencies(Element e, final Set<Identifier> names) {
    e.accept(
        new Scanner() {

          @Override
          public void visitVariableRef(VariableRef variableRef) {
            names.add(variableRef.getName());
          }
        });
  }
  protected void outputNSString(
      String name,
      String value,
      DeclarationsHolder out,
      Signatures signatures,
      Element... elementsToTakeCommentsFrom) {

    if (!signatures.addVariable(name)) return;

    TypeRef tr = typeRef(String.class);
    VariablesDeclaration vd = new VariablesDeclaration(tr, new DirectDeclarator(name, expr(value)));
    if (!result.config.noComments)
      for (Element e : elementsToTakeCommentsFrom) {
        vd.addToCommentBefore(e.getCommentBefore());
        vd.addToCommentBefore(e.getCommentAfter());
      }
    vd.addModifiers(ModifierType.Public);
    out.addDeclaration(vd);
  }
  public EmptyDeclaration skipDeclaration(Element e, String... preMessages) {
    if (result.config.limitComments) return null;

    List<String> mess = new ArrayList<String>();
    if (preMessages != null) mess.addAll(Arrays.asList(preMessages));
    mess.addAll(
        Arrays.asList(
            "SKIPPED:",
            new Printer(null).formatComments(e, true, true, false).toString(),
            getFileCommentContent(e),
            e.toString().replace("*/", "* /")));
    return new EmptyDeclaration(mess.toArray(new String[0]));
  }
 String getFileCommentContent(File file, Element e) {
   if (file != null) {
     String path = result.config.relativizeFileForSourceComments(file.getAbsolutePath());
     String inCategoryStr = "";
     if (e instanceof Function) {
       Function fc = (Function) e;
       Struct parent;
       if (fc.getType() == Type.ObjCMethod
           && ((parent = as(fc.getParentElement(), Struct.class)) != null)
           && (parent.getCategoryName() != null)) {
         inCategoryStr = "from " + parent.getCategoryName() + " ";
       }
     }
     return "<i>"
         + inCategoryStr
         + "native declaration : "
         + path
         + (e == null || e.getElementLine() < 0 ? "" : ":" + e.getElementLine())
         + "</i>";
   } else if (e != null && e.getElementLine() >= 0) {
     return "<i>native declaration : <input>:" + e.getElementLine() + "</i>";
   }
   return null;
 }
  public Struct convertCallback(
      FunctionSignature functionSignature, Signatures signatures, Identifier callerLibraryName) {
    Identifier name =
        result.typeConverter.inferCallBackName(functionSignature, true, false, callerLibraryName);
    if (name == null) return null;

    name = result.typeConverter.getValidJavaArgumentName(name);

    Function function = functionSignature.getFunction();

    int i = 1;
    Identifier chosenName = name;
    while (!(signatures.addClass(chosenName))) {
      chosenName = ident(name.toString() + (++i));
    }

    Element parent = functionSignature.getParentElement();
    Element comel = parent != null && parent instanceof TypeDef ? parent : functionSignature;

    Struct callbackStruct = new Struct();
    configureCallbackStruct(callbackStruct);
    // callbackStruct.setParents(Arrays.asList(getCallbackType(functionSignature, chosenName)));
    callbackStruct.setTag(ident(chosenName));
    if (!result.config.noComments)
      callbackStruct.addToCommentBefore(
          comel.getCommentBefore(), comel.getCommentAfter(), getFileCommentContent(comel));
    convertFunction(function, new Signatures(), true, callbackStruct, callerLibraryName, -1);
    for (Declaration d : callbackStruct.getDeclarations()) {
      if (d instanceof Function) {
        callbackStruct.addAnnotations(callbackStruct.getAnnotations());
        callbackStruct.setAnnotations(null);
        break;
      }
    }
    return callbackStruct;
  }
 protected void collectParamComments(Function f) {
   for (Arg arg : f.getArgs()) {
     arg.moveAllCommentsBefore();
     TypeRef argType = arg.getValueType();
     if (argType != null) {
       if (!result.config.noComments) {
         argType.moveAllCommentsBefore();
         arg.addToCommentBefore(argType.getCommentBefore());
       }
       argType.stripDetails();
     }
     if (arg.getCommentBefore() != null) {
       if (!result.config.noComments)
         f.addToCommentBefore(
             "@param " + arg.getName() + " " + Element.cleanComment(arg.getCommentBefore()));
       arg.stripDetails();
     }
   }
 }
  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);
    }
  }