@@ -96,7 +96,7 @@ Expression staticPtrMethod(String name, Expression... args) {
9696 return methodCall (expr (typeRef (ptrClass ())), name , args );
9797 }
9898 int iFile = 0 ;
99- public Pair <Element , List <Declaration >> convertToJava (Element element ) {
99+ public Pair <Element , List <Declaration >> convertToJava (Element element , final Identifier libraryClassName ) {
100100 //element = element.clone();
101101 try {
102102 PrintStream out = new PrintStream ("jnaerator-" + (iFile ++) + ".out" );
@@ -107,103 +107,7 @@ public Pair<Element, List<Declaration>> convertToJava(Element element) {
107107 }
108108 final List <Declaration > extraDeclarationsOut = new ArrayList <Declaration >();
109109
110- final Set <Pair <Element , Integer >> referencedElements = new HashSet <Pair <Element , Integer >>();
111- element .accept (new Scanner () {
112- @ Override
113- public void visitUnaryOp (UnaryOp unaryOp ) {
114- super .visitUnaryOp (unaryOp );
115-
116- if (unaryOp .getOperator () == UnaryOperator .Reference ) {
117- if (unaryOp .getOperand () instanceof VariableRef ) {
118- VariableRef vr = (VariableRef )unaryOp .getOperand ();
119- Element e = result .symbols .getVariable (vr .getName ());
120- if (e != null )
121- referencedElements .add (new Pair <Element , Integer >(e , e .getId ()));
122- }
123- }
124- }
125- });
126-
127- final Set <Element > varDeclTypeRefsTransformedToPointers = new HashSet <Element >();
128-
129- final Map <Integer , String > referencedElementsChangedNames = new HashMap <Integer , String >();
130- for (Pair <Element , Integer > kv : referencedElements ) {
131- Element e = kv .getKey ();
132- //int id = kv
133- if (e instanceof DirectDeclarator ) {
134- DirectDeclarator decl = (DirectDeclarator )e ;
135- String name = decl .getName ();
136- String changedName = "p" + StringUtils .capitalize (name );
137- referencedElementsChangedNames .put (e .getId (), changedName );
138- decl .setName (changedName );
139- VariablesDeclaration vd = (VariablesDeclaration )decl .getParentElement ();
140- TypeRef tr = vd .getValueType ();
141- //TypeRef wrapper = getWrapperType(tr);
142- //vd.setValueType(pointerToTypeRef(wrapper));
143- vd .setValueType (new Pointer (tr , PointerStyle .Pointer ));
144- varDeclTypeRefsTransformedToPointers .add (vd );
145- Expression defVal = decl .getDefaultValue ();
146- if (defVal != null ) {
147- decl .setDefaultValue (staticPtrMethod ("pointerTo" + primCapName (tr ), defVal ));
148- }
149- }
150- }
151-
152-
153- // First pass to detect referenced variables (no replacement here) :
154- element .accept (new Scanner () {
155- @ Override
156- public void visitIdentifier (Identifier identifier ) {
157- super .visitIdentifier (identifier );
158- Element e = result .symbols .getVariable (identifier );
159- if (e != null && isReferenced (e )) {
160- String changedName = referencedElementsChangedNames .get (e .getId ());
161- if (changedName != null ) {
162- Identifier replacedIdentifier = ident (changedName );
163- identifier .replaceBy (replacedIdentifier );
164- referencedElements .add (new Pair <Element , Integer >(replacedIdentifier , replacedIdentifier .getId ()));
165- }
166- }
167- }
168-
169- boolean isReferenced (Element e ) {
170- if (e == null )
171- return false ;
172- return referencedElements .contains (new Pair <Element , Integer >(e , e .getId ()));
173- }
174-
175- @ Override
176- public void visitVariableRef (VariableRef vr ) {
177- super .visitVariableRef (vr );
178- Identifier ident = vr .getName ();
179- if (isReferenced (ident )) {
180- vr .replaceBy (methodCall (varRef (ident ), "get" ));
181- }
182- }
183-
184- @ Override
185- public void visitUnaryOp (UnaryOp unaryOp ) {
186- if (unaryOp .getOperator () == UnaryOperator .Reference ) {
187- if (unaryOp .getOperand () instanceof VariableRef ) {
188- VariableRef vr = (VariableRef )unaryOp .getOperand ();
189-
190- Identifier ident = vr .getName ();
191- Element e = result .symbols .getVariable (ident );
192- if ((e != null || isReferenced (e )) || isReferenced (ident )) {
193- String changedName = referencedElementsChangedNames .get (e .getId ());
194- if (changedName != null ) {
195- Element rep = varRef (changedName );
196- unaryOp .replaceBy (rep );
197- visit (rep );
198- return ;
199- }
200- }
201- }
202- }
203- super .visitUnaryOp (unaryOp );
204- }
205- });
206-
110+ final ReferencedElements ref = findReferencedElements (element );
207111
208112 // Second pass : replacement !
209113 element .accept (new Scanner () {
@@ -462,7 +366,7 @@ public void visitVariablesDeclaration(VariablesDeclaration v) {
462366 TypeRef mutatedType = (TypeRef )mt ;
463367 if (decl .getDefaultValue () == null ) {
464368 TypeRef actualType = mutatedType ;
465- boolean referenced = varDeclTypeRefsTransformedToPointers .contains (v ) && (mutatedType instanceof Pointer );
369+ boolean referenced = ref . varDeclTypeRefsTransformedToPointers .contains (v ) && (mutatedType instanceof Pointer );
466370 if (referenced ) {
467371 actualType = ((Pointer )mutatedType ).getTarget ();
468372 }
@@ -487,7 +391,7 @@ public void visitVariablesDeclaration(VariablesDeclaration v) {
487391 }// else {
488392 TypeConversion .NL4JConversion conv = result .typeConverter .convertTypeToNL4J (
489393 mutatedType ,
490- null ,//callerLibraryName,
394+ libraryClassName ,//callerLibraryName,
491395 null ,//thisField("io"),
492396 null ,//varRef(name),
493397 -1 ,//fieldIndex,
@@ -642,4 +546,112 @@ TypeRef pointerToTypeRef(TypeRef targetType) {
642546 Class ptrClass () {
643547 return result .config .runtime .pointerClass ;
644548 }
549+
550+ static class ReferencedElements {
551+ final Set <Pair <Element , Integer >> referencedElements = new HashSet <Pair <Element , Integer >>();
552+ final Set <Element > varDeclTypeRefsTransformedToPointers = new HashSet <Element >();
553+ }
554+ private ReferencedElements findReferencedElements (Element element ) {
555+ final ReferencedElements ret = new ReferencedElements ();
556+ element .accept (new Scanner () {
557+ @ Override
558+ public void visitUnaryOp (UnaryOp unaryOp ) {
559+ super .visitUnaryOp (unaryOp );
560+
561+ if (unaryOp .getOperator () == UnaryOperator .Reference ) {
562+ if (unaryOp .getOperand () instanceof VariableRef ) {
563+ VariableRef vr = (VariableRef )unaryOp .getOperand ();
564+ Element e = result .symbols .getVariable (vr .getName ());
565+ if (e != null )
566+ ret .referencedElements .add (new Pair <Element , Integer >(e , e .getId ()));
567+ }
568+ }
569+ }
570+ });
571+
572+
573+
574+
575+ final Map <Integer , String > referencedElementsChangedNames = new HashMap <Integer , String >();
576+ for (Pair <Element , Integer > kv : ret .referencedElements ) {
577+ Element e = kv .getKey ();
578+ //int id = kv
579+ if (e instanceof DirectDeclarator ) {
580+ DirectDeclarator decl = (DirectDeclarator )e ;
581+ String name = decl .getName ();
582+ String changedName = "p" + StringUtils .capitalize (name );
583+ referencedElementsChangedNames .put (e .getId (), changedName );
584+ decl .setName (changedName );
585+ VariablesDeclaration vd = (VariablesDeclaration )decl .getParentElement ();
586+ TypeRef tr = vd .getValueType ();
587+ //TypeRef wrapper = getWrapperType(tr);
588+ //vd.setValueType(pointerToTypeRef(wrapper));
589+ vd .setValueType (new Pointer (tr , PointerStyle .Pointer ));
590+ ret .varDeclTypeRefsTransformedToPointers .add (vd );
591+ Expression defVal = decl .getDefaultValue ();
592+ if (defVal != null ) {
593+ decl .setDefaultValue (staticPtrMethod ("pointerTo" + primCapName (tr ), defVal ));
594+ } else {
595+ decl .setDefaultValue (staticPtrMethod ("allocate" + primCapName (tr )));
596+ }
597+ }
598+ }
599+
600+
601+ // First pass to detect referenced variables (no replacement here) :
602+ element .accept (new Scanner () {
603+ @ Override
604+ public void visitIdentifier (Identifier identifier ) {
605+ super .visitIdentifier (identifier );
606+ Element e = result .symbols .getVariable (identifier );
607+ if (e != null && isReferenced (e )) {
608+ String changedName = referencedElementsChangedNames .get (e .getId ());
609+ if (changedName != null ) {
610+ Identifier replacedIdentifier = ident (changedName );
611+ identifier .replaceBy (replacedIdentifier );
612+ ret .referencedElements .add (new Pair <Element , Integer >(replacedIdentifier , replacedIdentifier .getId ()));
613+ }
614+ }
615+ }
616+
617+ boolean isReferenced (Element e ) {
618+ if (e == null )
619+ return false ;
620+ return ret .referencedElements .contains (new Pair <Element , Integer >(e , e .getId ()));
621+ }
622+
623+ @ Override
624+ public void visitVariableRef (VariableRef vr ) {
625+ super .visitVariableRef (vr );
626+ Identifier ident = vr .getName ();
627+ if (isReferenced (ident )) {
628+ vr .replaceBy (methodCall (varRef (ident ), "get" ));
629+ }
630+ }
631+
632+ @ Override
633+ public void visitUnaryOp (UnaryOp unaryOp ) {
634+ if (unaryOp .getOperator () == UnaryOperator .Reference ) {
635+ if (unaryOp .getOperand () instanceof VariableRef ) {
636+ VariableRef vr = (VariableRef )unaryOp .getOperand ();
637+
638+ Identifier ident = vr .getName ();
639+ Element e = result .symbols .getVariable (ident );
640+ if ((e != null || isReferenced (e )) || isReferenced (ident )) {
641+ String changedName = referencedElementsChangedNames .get (e .getId ());
642+ if (changedName != null ) {
643+ Element rep = varRef (changedName );
644+ unaryOp .replaceBy (rep );
645+ visit (rep );
646+ return ;
647+ }
648+ }
649+ }
650+ }
651+ super .visitUnaryOp (unaryOp );
652+ }
653+ });
654+
655+ return ret ;
656+ }
645657}
0 commit comments