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.foreign.layout.Layout; 26 import java.util.ArrayList; 27 import java.util.Collections; 28 import java.util.List; 29 import java.util.function.BiFunction; 30 import jdk.internal.clang.Cursor; 31 import jdk.internal.clang.CursorKind; 32 33 public class StructTree extends Tree { 34 private final List<Tree> declarations; 35 36 public StructTree(Cursor c, List<Tree> declarations) { 37 super(c); 38 this.declarations = Collections.unmodifiableList(declarations); 39 } 40 41 public List<Tree> declarations() { 42 return declarations; 43 } 44 45 public List<FieldTree> fields() { 46 // Note that we have to include fields from nested annoymous unions and structs 47 // in the containing struct. 48 List<FieldTree> fields = new ArrayList<>(); 49 for (Tree decl : declarations) { 50 if (decl instanceof FieldTree) { 51 fields.add((FieldTree)decl); 52 } else if (decl instanceof StructTree) { 53 StructTree s = (StructTree)decl; 54 if (s.isAnonymous()) { 55 fields.addAll(s.fields()); 56 } 57 } 58 } 59 return Collections.unmodifiableList(fields); 60 } 61 62 public List<Tree> nestedTypes() { 63 // C structs and unions can have nested structs, unions and enums. 64 // And (even deeply) nested types are hoisted to the containing scope. 65 // i.e., nested structs/unions/enums are not scoped. 66 List<Tree> nested = new ArrayList<>(); 67 for (Tree decl : declarations) { 68 if (decl instanceof EnumTree) { 69 nested.add(decl); 70 } else if (decl instanceof StructTree) { 71 StructTree s = (StructTree)decl; 72 if (!s.isAnonymous()) { 73 nested.add(s); 74 } 75 nested.addAll(s.nestedTypes()); 76 } 77 } 78 return Collections.unmodifiableList(nested); 79 } 80 81 @Override 82 public <R,D> R accept(TreeVisitor<R,D> visitor, D data) { 83 return visitor.visitStruct(this, data); 84 } 85 86 public boolean isUnion() { 87 return cursor().kind() == CursorKind.UnionDecl; 88 } 89 90 public boolean isAnonymous() { 91 return cursor().isAnonymousStruct(); 92 } 93 94 public Layout layout(BiFunction<FieldTree, Layout, Layout> fieldMapper) { 95 TreeMaker m = new TreeMaker(); 96 return LayoutUtils.getRecordLayout(cursor().type(), (cursor, layout) -> { 97 return fieldMapper.apply(m.createField(cursor), layout); 98 }); 99 } 100 }