public Identifier getActualTaggedTypeName(TaggedTypeRef struct) {
    Identifier structName = null;
    Identifier tag = struct.getTag();
    if (tag == null || tag.isPlain() && tag.toString().startsWith("_")) {
      TypeDef parentDef = as(struct.getParentElement(), TypeDef.class);
      if (parentDef != null) {
        structName =
            new Identifier.SimpleIdentifier(JNAeratorUtils.findBestPlainStorageName(parentDef));
      } else if (tag != null) {
        String better = tag.toString().substring(1);
        Pair<TypeDef, Declarator> pair = result.typeDefs.get(better);
        if (pair != null
            && pair.getFirst().getValueType() != null
            && pair.getSecond() instanceof DirectDeclarator) {
          TypeRef tr = pair.getFirst().getValueType();
          DirectDeclarator dd = (DirectDeclarator) pair.getSecond();

          if (tr instanceof SimpleTypeRef) {
            if (tag.equals(((SimpleTypeRef) tr).getName())) structName = ident(dd.resolveName());
          } else if (tr instanceof TaggedTypeRef) {
            if (tag.equals(((TaggedTypeRef) tr).getTag())) structName = ident(dd.resolveName());
          }
        }
      }
    }
    if (structName == null || structName.toString().equals("")) structName = tag;
    return structName == null ? null : structName.clone();
  }
Пример #2
0
  public void visitFunctionCall(FunctionCall functionCall) {
    visitMemberRef(functionCall);
    visit(functionCall.getTarget());
    visit(functionCall.getFunction());

    for (Pair<String, Expression> x : copy(functionCall.getArguments()))
      if (x != null) visit(x.getSecond());
  }
Пример #3
0
 protected int indexOf(Element x, List<Pair<String, Expression>> list) {
   int i = 0;
   for (Pair<String, Expression> p : list) {
     if (p.getValue() == x) return i;
     i++;
   }
   return -1;
 }
  private Identifier getUndefinedTypeName(Identifier name) {

    String nameStr = name == null ? null : name.toString();
    String trimmed = StringUtils.trimUnderscores(nameStr);
    if (trimmed != null && !nameStr.equals(trimmed)) {
      String nicerName = trimmed;
      Pair<TypeDef, Declarator> pair = typeDefs.get(nicerName);
      if (pair != null) {
        String target = pair.getFirst().getValueType().toString();
        if (target.equals(nameStr)) // || target.equals(nameStr+"*"))
        name = ident(nameStr = nicerName);
      }
    }
    return name;
  }
Пример #5
0
  protected static Object cloneObject(Object value) throws CloneNotSupportedException {
    if (value == null) return null;

    Class<?> type = value.getClass();
    if (Element.class.isAssignableFrom(type)) return ((Element) value).clone();
    else if (EnumSet.class.isAssignableFrom(type)) return ((EnumSet<?>) value).clone();
    else if (Collection.class.isAssignableFrom(type)) return cloneElements((Collection<?>) value);
    else if (Map.class.isAssignableFrom(type)) return cloneElements((Map<?, ?>) value);
    else if (Pair.class.isAssignableFrom(type)) {
      Pair<?, ?> pair = (Pair<?, ?>) value;
      return new Pair<Object, Object>(cloneObject(pair.getFirst()), cloneObject(pair.getSecond()));
    }
    // else if (value instanceof String || type.isPrimitive())
    return value;

    // throw new CloneNotSupportedException();
  }
  @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 addMissingMethods(
     Class<?> originalLib, Signatures existingSignatures, Struct outputLib) {
   for (Pair<Function, String> f : getMethodsAndTheirSignatures(originalLib).getFirst())
     if (existingSignatures.addMethod(f.getSecond()))
       outputLib.addDeclaration(f.getFirst().clone());
 }
Пример #8
0
 public void setArguments(List<Pair<String, Expression>> arguments) {
   for (Pair<String, Expression> p : this.arguments) p.getSecond().setParentElement(null);
   this.arguments.clear();
   for (Pair<String, Expression> p : arguments) addArgument(p.getFirst(), p.getSecond());
 }