public List<Define> reorderDefines(Collection<Define> defines) {
    List<Define> reordered = new ArrayList<Define>(defines.size());
    HashSet<Identifier> added = new HashSet<Identifier>(), all = new HashSet<Identifier>();

    Map<String, Pair<Define, Set<Identifier>>> pending =
        new HashMap<String, Pair<Define, Set<Identifier>>>();
    for (Define define : defines) {
      Set<Identifier> dependencies = new TreeSet<Identifier>();
      computeVariablesDependencies(define.getValue(), dependencies);
      all.add(ident(define.getName()));
      if (dependencies.isEmpty()) {
        reordered.add(define);
        added.add(ident(define.getName()));
      } else {
        pending.put(define.getName(), new Pair<Define, Set<Identifier>>(define, dependencies));
      }
    }

    boolean progressed;
    do {
      progressed = false;
      for (Iterator<Map.Entry<String, Pair<Define, Set<Identifier>>>> it =
              pending.entrySet().iterator();
          it.hasNext(); ) {
        Map.Entry<String, Pair<Define, Set<Identifier>>> e = it.next();
        Set<Identifier> dependencies = e.getValue().getSecond();
        String name = e.getKey();
        boolean missesDep = false;
        for (Identifier dependency : dependencies) {
          if (!added.contains(dependency)) {
            missesDep = true;
            if (!all.contains(dependency)) {
              it.remove();
              all.remove(name);
            }

            break;
          }
        }
        if (missesDep) continue;

        it.remove();
        reordered.add(e.getValue().getFirst());
        added.add(ident(name));
        progressed = true;
      }
    } while (!pending.isEmpty() && progressed);

    return reordered;
  }
  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;
  }