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.TreePrinter;
  41 import jdk.internal.clang.Cursor;
  42 
  43 /**
  44  * This tree visitor handles the tree empty names encountered in the tree
  45  * so that subsequent passes need not check tree.name() for empty string.
  46  *
  47  * 1. Names are generated & set for anonymous structs & unions.
  48  * 2. Anonymous (bit) FieldTree instances are removed.
  49  */
  50 final class EmptyNameHandler extends SimpleTreeVisitor<Tree, Void> {
  51     private final TreeMaker treeMaker = new TreeMaker();
  52 
  53     HeaderTree transform(HeaderTree ht) {
  54         return (HeaderTree)ht.accept(this, null);
  55     }
  56 
  57     // generate unique name for an empty name
  58     private String generateName(Tree tree) {
  59         return LayoutUtils.getName(tree);
  60     }
  61 
  62     @Override
  63     public Tree defaultAction(Tree tree, Void v) {
  64         return tree;
  65     }
  66 
  67     @Override
  68     public Tree visitHeader(HeaderTree ht, Void v) {
  69         List<Tree> decls =  ht.declarations().stream().
  70             map(decl -> decl.accept(this, null)).
  71             collect(Collectors.toList());
  72         return treeMaker.createHeader(ht.cursor(), ht.path(), decls);
  73     }
  74 
  75     @Override
  76     public Tree visitStruct(StructTree s, Void v) {
  77         // Common simple case. No nested names and no anonymous field names.
  78         // We just need to check struct name itself is empty or not.
  79         if (s.nestedTypes().isEmpty() && !hasAnonymousFields(s)) {
  80             /*
  81              * Examples:
  82              *
  83              *   struct { int i } x; // global variable of anon. struct type
  84              *   void func(struct { int x; } p); // param of anon. struct type
  85              */
  86             if (s.name().isEmpty()) {
  87                 return s.withName(generateName(s));
  88             } else {
  89                 // all fine with this struct
  90                 return s;
  91             }
  92         } else {
  93             // handle all nested types
  94             return renameRecursively(s);
  95         }
  96     }
  97 
  98     // does the given struct has any anonymous (bit) field?
  99     private boolean hasAnonymousFields(StructTree s) {
 100         return s.fields().stream().map(f -> f.name().isEmpty()).findFirst().isPresent();
 101     }
 102 
 103     private StructTree renameRecursively(StructTree s) {
 104         List<Tree> newDecls = s.declarations().stream().map(decl -> {
 105             if (decl instanceof StructTree) {
 106                 return renameRecursively((StructTree)decl);
 107             } else if (decl instanceof FieldTree && decl.name().isEmpty()) {
 108                 /*
 109                  * Skip anonymous fields. This happens in the following case:
 110                  *
 111                  * struct {
 112                  *    int  :23; // anonymous bit field
 113                  *    int x:9;
 114                  * }
 115                  */
 116 
 117                 return null;
 118             } else {
 119                 return decl;
 120             }
 121         }).filter(d -> d != null).collect(Collectors.toList());
 122 
 123         return s.withNameAndDecls(generateName(s), newDecls);
 124     }
 125 
 126     // test main to manually check this visitor
 127     public static void main(String[] args) {
 128         if (args.length == 0) {
 129             System.err.println("Expected a header file");
 130             return;
 131         }
 132 
 133         Parser p = new Parser(true);
 134         List<Path> paths = Arrays.stream(args).map(Paths::get).collect(Collectors.toList());
 135         Path builtinInc = Paths.get(System.getProperty("java.home"), "conf", "jextract");
 136         List<String> clangArgs = List.of("-I" + builtinInc);
 137         List<HeaderTree> headers = p.parse(paths, clangArgs);
 138         TreePrinter printer = new TreePrinter();
 139         EmptyNameHandler handler = new EmptyNameHandler();
 140         for (HeaderTree ht : headers) {
 141             handler.transform(ht).accept(printer, null);
 142         }
 143     }
 144 }