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 Optional<Tree> def = Optional.ofNullable(c.isDefinition()? null : createTree(c.getDefinition())); 88 return checkCache(c, EnumTree.class, ()->new EnumTree(c, def, fields)); 89 } 90 91 public FieldTree createField(Cursor c) { 92 checkCursorAny(c, CursorKind.EnumConstantDecl, CursorKind.FieldDecl); 93 return checkCache(c, FieldTree.class, ()->new FieldTree(c)); 94 } 95 96 public FunctionTree createFunction(Cursor c) { 97 checkCursorAny(c, CursorKind.FunctionDecl); 98 return checkCache(c, FunctionTree.class, ()->new FunctionTree(c)); 99 } 100 101 public MacroTree createMacro(Cursor c, Optional<Object> value) { 102 checkCursorAny(c, CursorKind.MacroDefinition); 103 return checkCache(c, MacroTree.class, ()->new MacroTree(c, value)); 104 } 105 106 public HeaderTree createHeader(Cursor c, Path path, List<Tree> decls) { 107 return checkCache(c, HeaderTree.class, ()->new HeaderTree(c, path, decls)); 108 } 109 110 public StructTree createStruct(Cursor c) { 111 checkCursorAny(c, CursorKind.StructDecl, CursorKind.UnionDecl); 112 List<Tree> decls = c.children().map(this::createTree).collect(Collectors.toList()); 113 return createStructCommon(c, decls); 114 } 115 116 public StructTree createStruct(Cursor c, List<Tree> declarations) { 117 checkCursorAny(c, CursorKind.StructDecl, CursorKind.UnionDecl); 118 return createStructCommon(c, declarations); 119 } 120 121 private StructTree createStructCommon(Cursor c, List<Tree> declarations) { 122 Optional<Tree> def = Optional.ofNullable(c.isDefinition()? null : createTree(c.getDefinition())); 123 return checkCache(c, StructTree.class, ()->new StructTree(c, def, declarations)); 124 } 125 126 public TypedefTree createTypedef(Cursor c) { 127 checkCursor(c, CursorKind.TypedefDecl); 128 Cursor dcl = c.type().canonicalType().getDeclarationCursor(); 129 Optional<Tree> def = Optional.ofNullable(dcl.isDefinition()? createTree(dcl) : null); 130 return checkCache(c, TypedefTree.class, ()->{ 131 return new TypedefTree(c, def); 132 }); 133 } 134 135 private VarTree createVar(Cursor c) { 136 checkCursor(c, CursorKind.VarDecl); 137 return checkCache(c, VarTree.class, ()->new VarTree(c)); 138 } 139 140 private void checkCursor(Cursor c, CursorKind k) { 141 if (c.kind() != k) { 142 throw new IllegalArgumentException("Invalid cursor kind"); 143 } 144 } 145 146 private void checkCursorAny(Cursor c, CursorKind... kinds) { 147 CursorKind expected = Objects.requireNonNull(c.kind()); 148 for (CursorKind k : kinds) { 149 if (k == expected) { 150 return; 151 } 152 } 153 throw new IllegalArgumentException("Invalid cursor kind"); 154 } 155 } --- EOF ---