1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 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.Arrays; 29 import java.util.List; 30 import java.util.stream.Collectors; 31 import com.sun.tools.jextract.parser.Parser; 32 import com.sun.tools.jextract.tree.EnumTree; 33 import com.sun.tools.jextract.tree.FieldTree; 34 import com.sun.tools.jextract.tree.LayoutUtils; 35 import com.sun.tools.jextract.tree.HeaderTree; 36 import com.sun.tools.jextract.tree.SimpleTreeVisitor; 37 import com.sun.tools.jextract.tree.StructTree; 38 import com.sun.tools.jextract.tree.Tree; 39 import com.sun.tools.jextract.tree.TreeMaker; 40 import com.sun.tools.jextract.tree.TreePhase; 41 import com.sun.tools.jextract.tree.TreePrinter; 42 import jdk.internal.clang.Cursor; 43 44 /** 45 * This tree visitor handles the tree empty names encountered in the tree 46 * so that subsequent passes need not check tree.name() for empty string. 47 * 48 * 1. Names are generated & set for anonymous structs & unions. 49 * 2. Anonymous (bit) FieldTree instances are removed. 50 */ 51 final class EmptyNameHandler extends SimpleTreeVisitor<Tree, Void> 52 implements TreePhase { 53 private final TreeMaker treeMaker = new TreeMaker(); 54 55 @Override 56 public HeaderTree transform(HeaderTree ht) { 57 return (HeaderTree)ht.accept(this, null); 58 } 59 60 // generate unique name for an empty name 61 private String generateName(Tree tree) { 62 return LayoutUtils.getName(tree); 63 } 64 65 @Override 66 public Tree defaultAction(Tree tree, Void v) { 67 return tree; 68 } 69 70 @Override 71 public Tree visitHeader(HeaderTree ht, Void v) { 72 List<Tree> decls = ht.declarations().stream(). 73 map(decl -> decl.accept(this, null)). 74 collect(Collectors.toList()); 75 return treeMaker.createHeader(ht.cursor(), ht.path(), decls); 76 } 77 78 @Override 79 public Tree visitField(FieldTree t, Void aVoid) { 80 if (t.name().isEmpty()) { 81 /* 82 * Skip anonymous fields. This happens in the following case: 83 * 84 * struct { 85 * int :23; // anonymous bit field 86 * int x:9; 87 * } 88 */ 89 90 return null; 91 } else { 92 return t; 93 } 94 } 95 96 @Override 97 public Tree visitStruct(StructTree s, Void v) { 98 List<Tree> newDecls = s.declarations().stream() 99 .map(decl -> decl.accept(this, null)) 100 .filter(d -> d != null) 101 .collect(Collectors.toList()); 102 103 return s.withNameAndDecls(generateName(s), newDecls); 104 } 105 106 // test main to manually check this visitor 107 public static void main(String[] args) { 108 if (args.length == 0) { 109 System.err.println("Expected a header file"); 110 return; 111 } 112 113 Context context = new Context(); 114 Parser p = new Parser(context,true); 115 List<Path> paths = Arrays.stream(args).map(Paths::get).collect(Collectors.toList()); 116 Path builtinInc = Paths.get(System.getProperty("java.home"), "conf", "jextract"); 117 List<String> clangArgs = List.of("-I" + builtinInc); 118 List<HeaderTree> headers = p.parse(paths, clangArgs); 119 TreePrinter printer = new TreePrinter(); 120 EmptyNameHandler handler = new EmptyNameHandler(); 121 for (HeaderTree ht : headers) { 122 handler.transform(ht).accept(printer, null); 123 } 124 } 125 }