< prev index next >

src/jdk.jextract/share/classes/com/sun/tools/jextract/TypedefHandler.java

Print this page




  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package com.sun.tools.jextract;
  24 
  25 import java.nio.file.Path;
  26 import java.nio.file.Paths;
  27 import java.util.ArrayList;
  28 import java.util.HashMap;
  29 import java.util.List;
  30 import java.util.Map;
  31 import java.util.Objects;
  32 import java.util.Optional;
  33 import java.util.logging.Level;
  34 import java.util.logging.Logger;


  35 import com.sun.tools.jextract.parser.Parser;
  36 import com.sun.tools.jextract.tree.HeaderTree;
  37 import com.sun.tools.jextract.tree.SimpleTreeVisitor;
  38 import com.sun.tools.jextract.tree.StructTree;
  39 import com.sun.tools.jextract.tree.Tree;
  40 import com.sun.tools.jextract.tree.TreeMaker;
  41 import com.sun.tools.jextract.tree.TreePhase;
  42 import com.sun.tools.jextract.tree.TreePrinter;
  43 import com.sun.tools.jextract.tree.TypedefTree;
  44 import jdk.internal.clang.Cursor;
  45 
  46 /**
  47  * This visitor handles certain typedef declarations.
  48  *
  49  * 1. Remove redundant typedefs.
  50  * 2. Rename typedef'ed anonymous type definitions like
  51  *        typedef struct { int x; int y; } Point;
  52  */
  53 final class TypedefHandler extends SimpleTreeVisitor<Void, Void>
  54         implements TreePhase {


  61     // Tree instances that are to be replaced from "decls" list are
  62     // saved in the following Map. One or more Trees can replace a Tree.
  63     private final Map<Cursor, List<Tree>> replacements = new HashMap<>();
  64 
  65     private final Log log;
  66 
  67     public TypedefHandler(Context ctx) {
  68         this.log = ctx.log;
  69     }
  70 
  71     @Override
  72     public HeaderTree transform(HeaderTree ht) {
  73         // Process all header declarations are collect potential
  74         // declarations that will go into transformed HeaderTree
  75         // into the this.decls field.
  76         ht.accept(this, null);
  77 
  78         // Replace trees from this.decls with Trees found in this.replacements.
  79         // We need this two step process so that named StructTree instances
  80         // will replace with original unnamed StructTree instances.
  81         List<Tree> newDecls = new ArrayList<>();
  82         decls.stream().forEach(tx -> {
  83             if (replacements.containsKey(tx.cursor())) {
  84                 newDecls.addAll(replacements.get(tx.cursor()));
  85             } else {
  86                 newDecls.add(tx);
  87             }
  88         });
  89 
  90         return treeMaker.createHeader(ht.cursor(), ht.path(), newDecls);
  91     }
  92 
  93     @Override
  94     public Void defaultAction(Tree tree, Void v) {
  95         decls.add(tree);
  96         return null;
  97     }
  98 
  99     @Override
 100     public Void visitHeader(HeaderTree ht, Void v) {
 101         ht.declarations().forEach(decl -> decl.accept(this, null));
 102         return null;
 103     }
 104 
 105     @Override
 106     public Void visitTypedef(TypedefTree tt, Void v) {
 107         Optional<Tree> def = tt.typeDefinition();
 108         if (def.isPresent()) {
 109             Tree defTree = def.get();
 110             if (defTree instanceof StructTree && defTree.name().isEmpty()) {
 111                 /*
 112                  * typedef struct { int x; int y; } Point
 113                  *
 114                  * is mapped to two Cursors by clang. First one for anonymous struct decl.
 115                  * and second one for typedef decl. We map it as a single named struct
 116                  * declaration.
 117                  */
 118                 Cursor dc = defTree.cursor();
 119 
 120                 /*
 121                  * There may be more than one typedef on underlying anonymous struct/union.
 122                  * Example:
 123                  *
 124                  * typedef struct { int x; int y; } Point;
 125                  * typedef Point Vector;
 126                  */
 127                 List<Tree> trees = replacements.containsKey(dc)?
 128                     replacements.get(defTree.cursor()) : new ArrayList<>();

 129                 trees.add(((StructTree)defTree).withName(tt.name()));
 130                 replacements.put(dc, trees);
 131                 log.print(Level.FINE, () -> "Typedef " + defTree.type().spelling() + " as " + tt.name());
 132                 return null;
 133             } else if (defTree.name().equals(tt.name())) {
 134                 /*
 135                  * Remove redundant typedef like:
 136                  *
 137                  * typedef struct Point { int x; int y; } Point
 138                  * typedef enum Color { R, G, B} Color
 139                  * typedef struct Undef Undef
 140                  */
 141                 return null;
 142             }
 143         }
 144 
 145         decls.add(tt);
 146         return null;
 147     }
 148 
 149     // test main to manually check this visitor
 150     public static void main(String[] args) {


  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package com.sun.tools.jextract;
  24 
  25 import java.nio.file.Path;
  26 import java.nio.file.Paths;
  27 import java.util.ArrayList;
  28 import java.util.HashMap;
  29 import java.util.List;
  30 import java.util.Map;
  31 import java.util.Objects;
  32 import java.util.Optional;
  33 import java.util.logging.Level;
  34 import java.util.logging.Logger;
  35 import java.util.stream.Collectors;
  36 import java.util.stream.Stream;
  37 import com.sun.tools.jextract.parser.Parser;
  38 import com.sun.tools.jextract.tree.HeaderTree;
  39 import com.sun.tools.jextract.tree.SimpleTreeVisitor;
  40 import com.sun.tools.jextract.tree.StructTree;
  41 import com.sun.tools.jextract.tree.Tree;
  42 import com.sun.tools.jextract.tree.TreeMaker;
  43 import com.sun.tools.jextract.tree.TreePhase;
  44 import com.sun.tools.jextract.tree.TreePrinter;
  45 import com.sun.tools.jextract.tree.TypedefTree;
  46 import jdk.internal.clang.Cursor;
  47 
  48 /**
  49  * This visitor handles certain typedef declarations.
  50  *
  51  * 1. Remove redundant typedefs.
  52  * 2. Rename typedef'ed anonymous type definitions like
  53  *        typedef struct { int x; int y; } Point;
  54  */
  55 final class TypedefHandler extends SimpleTreeVisitor<Void, Void>
  56         implements TreePhase {


  63     // Tree instances that are to be replaced from "decls" list are
  64     // saved in the following Map. One or more Trees can replace a Tree.
  65     private final Map<Cursor, List<Tree>> replacements = new HashMap<>();
  66 
  67     private final Log log;
  68 
  69     public TypedefHandler(Context ctx) {
  70         this.log = ctx.log;
  71     }
  72 
  73     @Override
  74     public HeaderTree transform(HeaderTree ht) {
  75         // Process all header declarations are collect potential
  76         // declarations that will go into transformed HeaderTree
  77         // into the this.decls field.
  78         ht.accept(this, null);
  79 
  80         // Replace trees from this.decls with Trees found in this.replacements.
  81         // We need this two step process so that named StructTree instances
  82         // will replace with original unnamed StructTree instances.
  83         List<Tree> newDecls = decls.stream().flatMap(tx -> {

  84             if (replacements.containsKey(tx.cursor())) {
  85                 return replacements.get(tx.cursor()).stream();
  86             } else {
  87                 return Stream.of(tx);
  88             }
  89         }).collect(Collectors.toList());
  90 
  91         return treeMaker.createHeader(ht.cursor(), ht.path(), newDecls);
  92     }
  93 
  94     @Override
  95     public Void defaultAction(Tree tree, Void v) {
  96         decls.add(tree);
  97         return null;
  98     }
  99 
 100     @Override
 101     public Void visitHeader(HeaderTree ht, Void v) {
 102         ht.declarations().forEach(decl -> decl.accept(this, null));
 103         return null;
 104     }
 105 
 106     @Override
 107     public Void visitTypedef(TypedefTree tt, Void v) {
 108         Optional<Tree> def = tt.typeDefinition();
 109         if (def.isPresent()) {
 110             Tree defTree = def.get();
 111             if (defTree instanceof StructTree && defTree.name().isEmpty()) {
 112                 /*
 113                  * typedef struct { int x; int y; } Point
 114                  *
 115                  * is mapped to two Cursors by clang. First one for anonymous struct decl.
 116                  * and second one for typedef decl. We map it as a single named struct
 117                  * declaration.
 118                  *



 119                  * There may be more than one typedef on underlying anonymous struct/union.
 120                  * Example:
 121                  *
 122                  * typedef struct { int x; int y; } Point;
 123                  * typedef Point Vector;
 124                  */
 125 
 126                 Cursor dc = defTree.cursor();
 127                 List<Tree> trees = replacements.computeIfAbsent(dc, k -> new ArrayList<>());
 128                 trees.add(((StructTree)defTree).withName(tt.name()));

 129                 log.print(Level.FINE, () -> "Typedef " + defTree.type().spelling() + " as " + tt.name());
 130                 return null;
 131             } else if (defTree.name().equals(tt.name())) {
 132                 /*
 133                  * Remove redundant typedef like:
 134                  *
 135                  * typedef struct Point { int x; int y; } Point
 136                  * typedef enum Color { R, G, B} Color
 137                  * typedef struct Undef Undef
 138                  */
 139                 return null;
 140             }
 141         }
 142 
 143         decls.add(tt);
 144         return null;
 145     }
 146 
 147     // test main to manually check this visitor
 148     public static void main(String[] args) {
< prev index next >