1 /*
   2  * Copyright (c) 2019, 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.List;
  29 import java.util.logging.Logger;
  30 import com.sun.tools.jextract.parser.Parser;
  31 import com.sun.tools.jextract.tree.HeaderTree;
  32 import com.sun.tools.jextract.tree.SimpleTreeVisitor;
  33 import com.sun.tools.jextract.tree.Tree;
  34 import com.sun.tools.jextract.tree.TreeMaker;
  35 import com.sun.tools.jextract.tree.TreePhase;
  36 import com.sun.tools.jextract.tree.TreePrinter;
  37 import jdk.internal.clang.Cursor;
  38 
  39 /**
  40  * Handles builtin struct/union types by creating fake Trees in a
  41  * builtin header tree.
  42  */
  43 final class BuiltinTypesHandler extends SimpleTreeVisitor<Void, Void>
  44         implements TreePhase {
  45     private final TreeMaker treeMaker = new TreeMaker();
  46 
  47     // Potential Tree instances that will go into transformed HeaderTree
  48     // are collected in this list.
  49     private List<Tree> decls = new ArrayList<>();
  50     private final Log log;
  51 
  52     public BuiltinTypesHandler(Context ctx) {
  53         this.log = ctx.log;
  54     }
  55 
  56     @Override
  57     public HeaderTree transform(HeaderTree ht) {
  58         // Process all header declarations are collect potential
  59         // declarations that will go into transformed HeaderTree
  60         // into the this.decls field.
  61         ht.accept(this, null);
  62 
  63         return treeMaker.createHeader(ht.cursor(), ht.path(), decls);
  64     }
  65 
  66     @Override
  67     public Void defaultAction(Tree tree, Void v) {
  68         /*
  69          * There are built-in types like struct __va_list_tag. These are not exposed
  70          * as declaration cursor for any headers, but are available from Type objects.
  71          * We've to walk and check null file path to detect these Cursors and create Trees.
  72          */
  73         Utils.getBuiltinRecordTypes(tree.type()).forEach(c -> {
  74             decls.add(treeMaker.createTree(c));
  75         });
  76 
  77         decls.add(tree);
  78         return null;
  79     }
  80 
  81     @Override
  82     public Void visitHeader(HeaderTree ht, Void v) {
  83         ht.declarations().forEach(decl -> decl.accept(this, null));
  84         return null;
  85     }
  86 
  87     // test main to manually check this visitor
  88     public static void main(String[] args) {
  89         if (args.length == 0) {
  90             System.err.println("Expected a header file");
  91             return;
  92         }
  93 
  94         Context context = Context.createDefault();
  95         Parser p = new Parser(context, true);
  96         Path builtinInc = Paths.get(System.getProperty("java.home"), "conf", "jextract");
  97         List<String> clangArgs = List.of("-I" + builtinInc);
  98         HeaderTree header = p.parse(Paths.get(args[0]), clangArgs);
  99         TreePrinter printer = new TreePrinter();
 100         BuiltinTypesHandler handler = new BuiltinTypesHandler(context);
 101         handler.transform(header).accept(printer, null);
 102     }
 103 }