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.List;
  28 import java.util.ArrayList;
  29 import java.util.Objects;
  30 import java.util.Optional;
  31 import java.util.stream.Stream;
  32 import java.util.stream.Collectors;
  33 import jdk.internal.clang.Cursor;
  34 import jdk.internal.clang.CursorKind;
  35 import jdk.internal.clang.Type;
  36 
  37 public class TreeMaker {
  38     public TreeMaker() {}
  39 
  40     public Tree createTree(Cursor c) {
  41         switch (Objects.requireNonNull(c).kind()) {
  42             case EnumDecl:
  43                 return createEnum(c);
  44             case EnumConstantDecl:
  45             case FieldDecl:
  46                 return createField(c);
  47             case FunctionDecl:
  48                 return createFunction(c);
  49             case TypedefDecl:
  50                 return createTypedef(c);
  51             case StructDecl:
  52             case UnionDecl:
  53                 return createStruct(c);
  54             case VarDecl:
  55                 return createVar(c);
  56             default:
  57                 return new Tree(c);
  58         }
  59     }
  60 
  61     private static Stream<Cursor> enumConstants(Cursor c) {
  62         return c.children().filter(cx -> cx.kind() == CursorKind.EnumConstantDecl);
  63     }
  64 
  65     public EnumTree createEnum(Cursor c) {
  66         checkCursor(c, CursorKind.EnumDecl);
  67         List<FieldTree> consts = new ArrayList<>();
  68         enumConstants(c).forEachOrdered(cx -> consts.add((FieldTree)createTree(cx)));
  69         return createEnumCommon(c, consts);
  70     }
  71 
  72     public EnumTree createEnum(Cursor c, List<FieldTree> fields) {
  73         checkCursor(c, CursorKind.EnumDecl);
  74         return createEnumCommon(c, fields);
  75     }
  76 
  77     private EnumTree createEnumCommon(Cursor c, List<FieldTree> fields) {
  78         return new EnumTree(c, fields);
  79     }
  80 
  81     public FieldTree createField(Cursor c) {
  82         checkCursorAny(c, CursorKind.EnumConstantDecl, CursorKind.FieldDecl);
  83         return new FieldTree(c);
  84     }
  85 
  86     public FunctionTree createFunction(Cursor c) {
  87         checkCursorAny(c, CursorKind.FunctionDecl);
  88         return new FunctionTree(c);
  89     }
  90 
  91     public MacroTree createMacro(Cursor c, Optional<Object> value) {
  92         checkCursorAny(c, CursorKind.MacroDefinition);
  93         return new MacroTree(c, value);
  94     }
  95 
  96     public HeaderTree createHeader(Cursor c, Path path, List<Tree> decls) {
  97         return new HeaderTree(c, path, decls);
  98     }
  99 
 100     public StructTree createStruct(Cursor c) {
 101         checkCursorAny(c, CursorKind.StructDecl, CursorKind.UnionDecl);
 102         List<Tree> decls = c.children().map(this::createTree).collect(Collectors.toList());
 103         return createStructCommon(c, decls);
 104     }
 105 
 106     public StructTree createStruct(Cursor c, List<Tree> declarations) {
 107         checkCursorAny(c, CursorKind.StructDecl, CursorKind.UnionDecl);
 108         return createStructCommon(c, declarations);
 109     }
 110 
 111     private StructTree createStructCommon(Cursor c, List<Tree> declarations) {
 112         return new StructTree(c, declarations);
 113     }
 114 
 115     public TypedefTree createTypedef(Cursor c) {
 116         checkCursor(c, CursorKind.TypedefDecl);
 117         return new TypedefTree(c);
 118     }
 119 
 120     public VarTree createVar(Cursor c) {
 121         checkCursor(c, CursorKind.VarDecl);
 122         return new VarTree(c);
 123     }
 124 
 125     private void checkCursor(Cursor c, CursorKind k) {
 126         if (c.kind() != k) {
 127             throw new IllegalArgumentException("Invalid cursor kind");
 128         }
 129     }
 130 
 131     private void checkCursorAny(Cursor c, CursorKind... kinds) {
 132         CursorKind expected = Objects.requireNonNull(c.kind());
 133         for (CursorKind k : kinds) {
 134             if (k == expected) {
 135                 return;
 136             }
 137         }
 138         throw new IllegalArgumentException("Invalid cursor kind");
 139     }
 140 }