TypeRef valueType = v.getValueType(); for (Declarator vs : v.getDeclarators()) { if (vs.getDefaultValue() != null) { continue; String name = vs.resolveName(); if (name == null || name.length() == 0) { name = "anonymous" + (nextAnonymousFieldId++); mutatedType = (TypeRef) vs.mutateTypeKeepingParent(valueType); vs = new DirectDeclarator(vs.resolveName()); List<Declaration> vds = convertVariablesDeclarationToBridJ(name, mutatedType, iChild, vs.getBits(), isGlobal, holderName, callerLibraryClass, callerLibrary, v, vs); if (vs.getBits() > 0) { for (Declaration vd : vds) { vd.addAnnotation(new Annotation(result.config.runtime.typeRef(JNAeratorConfig.Runtime.Ann.Bits), expr(vs.getBits())));
public void visitDeclarator(Declarator e) { if (e.isParenthesized()) { append('('); } implode(e.getModifiers(), " ").space(!e.getModifiers().isEmpty()); if (e instanceof DirectDeclarator) { append(((DirectDeclarator) e).getName()); } else if (e instanceof PointerDeclarator) { PointerDeclarator d = (PointerDeclarator) e; append(d.getPointerStyle(), d.getTarget()); } else if (e instanceof FunctionDeclarator) { FunctionDeclarator d = (FunctionDeclarator) e; append(d.getTarget(), '(').implode(d.getArgs(), ", ").append(")"); } else if (e instanceof ArrayDeclarator) { ArrayDeclarator d = (ArrayDeclarator) e; append(d.getTarget(), '[').implode(d.getDimensions(), "][").append("]"); } if (e.isParenthesized()) { append(')'); } if (e.getBits() >= 0) { append(":", e.getBits()); } if (e.getDefaultValue() != null) { append(" = ", e.getDefaultValue()); } }
String rname = value == null ? null : value.resolveName(); if (rname != null) { if (name.equals("id")) { Declarator.MutableByDeclarator mt = p.getValue().mutateType(p.getFirst().getValueType()); return mt instanceof TypeRef ? (TypeRef) mt : null;
@Override public boolean replaceChild(Element child, Element by) { if (child == getDefaultValue()) { setDefaultValue((Expression) by); return true; } return super.replaceChild(child, by); }
@Override public void visitTypeDef(TypeDef typeDef) { super.visitTypeDef(typeDef); List<Declarator> declaratorsToRemove = null; for (Declarator d : typeDef.getDeclarators()) { String n = d.resolveName(); if (forcedTypeDefs.contains(n) && !seenOnce.add(n)) { if (declaratorsToRemove == null) { declaratorsToRemove = new ArrayList<Declarator>(); } declaratorsToRemove.add(d); } } if (declaratorsToRemove != null) { for (Declarator d : declaratorsToRemove) { d.replaceBy(null); } } } });
Declarator.MutableByDeclarator mutatedType = vs.mutateType(v.getValueType()); if (mutatedType instanceof Function) { Function f = (Function) mutatedType; f.setName(new SimpleIdentifier(vs.resolveName())); decl = (Function) mutatedType; decl.importDetails(v, false); if (mutatedType instanceof TypeRef) { TypeRef tr = (TypeRef) mutatedType; decl = new VariablesDeclaration(tr, new DirectDeclarator(vs.resolveName(), vs.getBits(), vs.getDefaultValue())); decl.importDetails(v, false); decl.importDetails(vs, false); decl = new VariablesDeclaration(vt == null ? null : vt.clone(), vs.clone()); decl.importDetails(v, false); decl.importDetails(vs, false);
MutableByDeclarator mt = decl instanceof DirectDeclarator ? vt : decl.mutateTypeKeepingParent(vt); if (decl.getDefaultValue() == null) { TypeRef actualType = mutatedType; boolean referenced = ref.varDeclTypeRefsTransformedToPointers.contains(v) && (mutatedType instanceof Pointer); decl.setDefaultValue(referenced ? staticPtrMethod("allocate", result.typeConverter.typeLiteral(actualType)) : new Expression.New(actualType.clone())); Expression vr = varRef(new SimpleIdentifier(decl.resolveName())); Statement deleteStat = stat(methodCall(expr(typeRef(BridJ.class)), "delete", referenced ? methodCall(vr, "get") : vr)); deleteStat.setCommentAfter("// object would end up being deleted by the GC later, but in C++ it would be deleted here."); if (conv.arrayLengths != null && (mutatedType instanceof TargettedTypeRef) && decl.getDefaultValue() == null) { v.setDeclarators(Arrays.asList((Declarator) new DirectDeclarator(decl.resolveName(), newAllocateArray(((TargettedTypeRef) mutatedType).getTarget(), conv.arrayLengths))));
public String getName() { return declarator == null ? null : declarator.resolveName(); } public void setName(String name) {
@Override public void visitArg(Arg arg) { Declarator d = arg.getDeclarator(); if (d == null) { TypeRef tr = arg.getValueType(); if (tr instanceof TypeRef.Pointer) { TypeRef target = ((TypeRef.Pointer)tr).getTarget(); if (target instanceof TypeRef.FunctionSignature) { TypeRef.FunctionSignature fs = (TypeRef.FunctionSignature) target; Identifier name = fs.getFunction() == null ? null : fs.getFunction().getName(); if (name != null) { arg.setDeclarator(new DirectDeclarator(name.toString())); fs.getFunction().setName(null); } } } } else if (!(d instanceof DirectDeclarator)) { MutableByDeclarator type = d.mutateType(arg.getValueType()); if (type instanceof TypeRef) { arg.setValueType((TypeRef) type); arg.setDeclarator(new DirectDeclarator(d.resolveName(), d.getBits(), arg.getDefaultValue())); } else { type = null; } } super.visitArg(arg); } private static final boolean mutateDeclaratorTypes = true;
String name = vs.resolveName(); if (vs instanceof DirectDeclarator && name.equals(bestName) && ttr != null && origName != null) { DirectDeclarator rep = new DirectDeclarator(origName); vs.replaceBy(rep); ttr.setTag(ident(bestName)); vs = rep; Declarator.MutableByDeclarator type = vs.mutateType(v.getValueType()); if (type instanceof TypeRef) { TypeRef tr = (TypeRef) type;
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; String name = decl.resolveName(); convertConstant(name, prim, mutatedType, decl.getDefaultValue(), v, decl, out, signatures, out.getResolvedJavaIdentifier()); } catch (UnsupportedConversionException ex) { out.addDeclaration(skipDeclaration(decl, ex.getMessage()));
TypeRef valueType = v.getValueType(); for (Declarator vs : v.getDeclarators()) { String name = vs.resolveName(); if (name == null || name.length() == 0) { name = "anonymous" + (nextAnonymousFieldId++); mutatedType = (TypeRef) vs.mutateTypeKeepingParent(valueType); vs = new DirectDeclarator(vs.resolveName()); if (d.getBits() > 0) { int bits = d.getBits(); if (!result.config.runtime.hasBitFields) { throw new UnsupportedConversionException(d, "This runtime does not support bit fields : " + result.config.runtime + " (please use BridJ instead)");
public final MutableByDeclarator mutateTypeKeepingParent(MutableByDeclarator type) { if (type == null) return null; MutableByDeclarator mutated = mutateType(type); if (mutated == null) throw new RuntimeException("Mutation of " + type + " yielded NULL!"); ((Element)mutated).setParentElement(((Element)type).getParentElement()); return mutated; } public static class DirectDeclarator extends Declarator {
@Override protected void visitStoredDeclarations(StoredDeclarations d) { super.visitStoredDeclarations(d); if (d.getDeclarators().size() == 1) { Declarator declarator = d.getDeclarators().get(0); if (declarator instanceof FunctionDeclarator) { FunctionDeclarator fd = (FunctionDeclarator) declarator; Function f = new Function(Function.Type.CFunction, null, d.getValueType(), fd.getArgs()); f.addModifiers(fd.getTarget().getModifiers()); FunctionSignature fs = new FunctionSignature(f); d.setValueType(fs); Declarator target = fd.getTarget(); Declarator newTarget = target == null ? new DirectDeclarator(fd.resolveName(), fd.getDefaultValue()) : target.clone(); d.setDeclarators(Arrays.asList(newTarget)); d.accept(this); } } }
Identifier name = result.typeConverter.getValidJavaArgumentName(ident(d.resolveName())); TypeRef type = (TypeRef) d.mutateTypeKeepingParent(globals.getValueType()); if (type == null) { continue;
@Test public void addSibling() { VariablesDeclaration d = new VariablesDeclaration(); Declarator s1 = new Declarator.DirectDeclarator(), s2 = new Declarator.DirectDeclarator(); d.setDeclarators(Arrays.asList(s1)); s1.insertSibling(s2, false); List<Declarator> list = d.getDeclarators(); assertEquals("Failed to add after", 2, list.size()); assertSame("Added, but not after", s1, list.get(0)); assertSame(s2, list.get(1)); s2.replaceBy(null); list = d.getDeclarators(); assertEquals("Failed to remove added element", 1, list.size()); assertSame("Removed bad element", s1, list.get(0)); d.setDeclarators(Arrays.asList(s1)); s1.insertSibling(s2, true); list = d.getDeclarators(); assertEquals("Failed to add before", 2, list.size()); assertSame("Added, but not before", s2, list.get(0)); assertSame(s1, list.get(1)); s2.replaceBy(null); list = d.getDeclarators(); assertEquals("Failed to remove added element", 1, list.size()); assertSame("Removed bad element", s1, list.get(0)); } }
if (!result.config.noComments) { vd.setCommentBefore(v.getCommentBefore()); vd.addToCommentBefore(decl.getCommentBefore()); vd.addToCommentBefore(decl.getCommentAfter()); vd.addToCommentBefore(v.getCommentAfter());
public void visitDeclarator(Declarator declarator) { visitModifiableElement(declarator); visit(declarator.getDefaultValue()); }
decl.setDefaultValue((dv!=null?dv.expr:null));
public void setDefaultValue(Expression defaultValue) { this.defaultValue = changeValue(this, this.defaultValue, defaultValue); } public Expression getDefaultValue() {