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.tree;
  24 
  25 import java.nio.file.Path;
  26 import java.util.Collections;
  27 import java.util.HashMap;
  28 import java.util.List;
  29 import java.util.Map;
  30 import java.util.ArrayList;
  31 import java.util.Objects;
  32 import java.util.Optional;
  33 import java.util.function.Supplier;
  34 import java.util.stream.Stream;
  35 import java.util.stream.Collectors;
  36 import jdk.internal.clang.Cursor;
  37 import jdk.internal.clang.CursorKind;
  38 import jdk.internal.clang.Type;
  39 
  40 public class TreeMaker {
  41     private final Map<Cursor, Tree> treeCache = new HashMap<>();
  42 
  43     public TreeMaker() {}
  44 
  45     private <T extends Tree> T checkCache(Cursor c, Class<T> clazz, Supplier<Tree> factory) {
  46         return clazz.cast(treeCache.computeIfAbsent(c, cx->factory.get()));
  47     }
  48 
  49     public Tree createTree(Cursor c) {
  50         switch (Objects.requireNonNull(c).kind()) {
  51             case EnumDecl:
  52                 return createEnum(c);
  53             case EnumConstantDecl:
  54             case FieldDecl:
  55                 return createField(c);
  56             case FunctionDecl:
  57                 return createFunction(c);
  58             case TypedefDecl:
  59                 return createTypedef(c);
  60             case StructDecl:
  61             case UnionDecl:
  62                 return createStruct(c);
  63             case VarDecl:
  64                 return createVar(c);
  65             default:
  66                 return checkCache(c, Tree.class, ()->new Tree(c));
  67         }
  68     }
  69 
  70     private static Stream<Cursor> enumConstants(Cursor c) {
  71         return c.children().filter(cx -> cx.kind() == CursorKind.EnumConstantDecl);
  72     }
  73 
  74     public EnumTree createEnum(Cursor c) {
  75         checkCursor(c, CursorKind.EnumDecl);
  76         List<FieldTree> consts = new ArrayList<>();
  77         enumConstants(c).forEachOrdered(cx -> consts.add((FieldTree)createTree(cx)));
  78         return createEnumCommon(c, consts);
  79     }
  80 
  81     public EnumTree createEnum(Cursor c, List<FieldTree> fields) {
  82         checkCursor(c, CursorKind.EnumDecl);
  83         return createEnumCommon(c, fields);
  84     }
  85 
  86     private EnumTree createEnumCommon(Cursor c, List<FieldTree> fields) {
  87         // If the current Cursor is not a definition, get the definition
  88         // and wrap it only if that is a valid definition.
  89         Optional<Tree> def = Optional.ofNullable(
  90             (c.isDefinition() || c.getDefinition().isInvalid())?
  91             null : createTree(c.getDefinition())
  92         );
  93         return checkCache(c, EnumTree.class, ()->new EnumTree(c, def, fields));
  94     }
  95 
  96     public FieldTree createField(Cursor c) {
  97         checkCursorAny(c, CursorKind.EnumConstantDecl, CursorKind.FieldDecl);
  98         return checkCache(c, FieldTree.class, ()->new FieldTree(c));
  99     }
 100 
 101     public FunctionTree createFunction(Cursor c) {
 102         checkCursorAny(c, CursorKind.FunctionDecl);
 103         return checkCache(c, FunctionTree.class, ()->new FunctionTree(c));
 104     }
 105 
 106     public MacroTree createMacro(Cursor c, Optional<Object> value) {
 107         checkCursorAny(c, CursorKind.MacroDefinition);
 108         return checkCache(c, MacroTree.class, ()->new MacroTree(c, value));
 109     }
 110 
 111     public HeaderTree createHeader(Cursor c, Path path, List<Tree> decls) {
 112         return checkCache(c, HeaderTree.class, ()->new HeaderTree(c, path, decls));
 113     }
 114 
 115     public StructTree createStruct(Cursor c) {
 116         checkCursorAny(c, CursorKind.StructDecl, CursorKind.UnionDecl);
 117         List<Tree> decls = c.children().map(this::createTree).collect(Collectors.toList());
 118         return createStructCommon(c, decls);
 119     }
 120 
 121     public StructTree createStruct(Cursor c, List<Tree> declarations) {
 122         checkCursorAny(c, CursorKind.StructDecl, CursorKind.UnionDecl);
 123         return createStructCommon(c, declarations);
 124     }
 125 
 126     private StructTree createStructCommon(Cursor c, List<Tree> declarations) {
 127         // If the current Cursor is not a definition, get the definition
 128         // and wrap it only if that is a valid definition.
 129         Optional<Tree> def = Optional.ofNullable(
 130             (c.isDefinition() || c.getDefinition().isInvalid())?
 131             null : createTree(c.getDefinition())
 132         );
 133         return checkCache(c, StructTree.class, ()->new StructTree(c, def, declarations));
 134     }
 135 
 136     public TypedefTree createTypedef(Cursor c) {
 137         checkCursor(c, CursorKind.TypedefDecl);
 138         Cursor dcl = c.type().canonicalType().getDeclarationCursor();
 139         Optional<Tree> def = Optional.ofNullable(dcl.isDefinition()? createTree(dcl) : null);
 140         return checkCache(c, TypedefTree.class, ()->{
 141             return new TypedefTree(c, def);
 142         });
 143     }
 144 
 145     private VarTree createVar(Cursor c) {
 146         checkCursor(c, CursorKind.VarDecl);
 147         return checkCache(c, VarTree.class, ()->new VarTree(c));
 148     }
 149 
 150     private void checkCursor(Cursor c, CursorKind k) {
 151         if (c.kind() != k) {
 152             throw new IllegalArgumentException("Invalid cursor kind");
 153         }
 154     }
 155 
 156     private void checkCursorAny(Cursor c, CursorKind... kinds) {
 157         CursorKind expected = Objects.requireNonNull(c.kind());
 158         for (CursorKind k : kinds) {
 159             if (k == expected) {
 160                 return;
 161             }
 162         }
 163         throw new IllegalArgumentException("Invalid cursor kind");
 164     }
 165 }