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.util.List; 26 import com.sun.tools.jextract.tree.EnumTree; 27 import com.sun.tools.jextract.tree.FieldTree; 28 import com.sun.tools.jextract.tree.FunctionTree; 29 import com.sun.tools.jextract.tree.HeaderTree; 30 import com.sun.tools.jextract.tree.MacroTree; 31 import com.sun.tools.jextract.tree.SimpleTreeVisitor; 32 import com.sun.tools.jextract.tree.StructTree; 33 import com.sun.tools.jextract.tree.Tree; 34 import com.sun.tools.jextract.tree.TreeMaker; 35 import com.sun.tools.jextract.tree.VarTree; 36 37 public class TreePrinter extends SimpleTreeVisitor<Void, Void> { 38 @Override 39 public Void defaultAction(Tree t, Void v) { 40 System.out.println(t.getClass().getSimpleName()); 41 System.out.println(t); 42 return null; 43 } 44 45 @Override 46 public Void visitEnum(EnumTree e, Void v) { 47 defaultAction(e, v); 48 List<? extends FieldTree> fields = e.constants(); 49 if (! fields.isEmpty()) { 50 System.out.println("fields"); 51 for (FieldTree f : fields) { 52 System.out.println(f.name() + " = " + f.enumConstant().get()); 53 f.accept(this, v); 54 } 55 } 56 return null; 57 } 58 59 @Override 60 public Void visitFunction(FunctionTree f, Void v) { 61 defaultAction(f, v); 62 System.out.printf("%s layout = %s\n\n", f.name(), f.function()); 63 return null; 64 } 65 66 @Override 67 public Void visitMacro(MacroTree m, Void v) { 68 defaultAction(m, v); 69 if (m.isConstant()) { 70 System.out.printf("Macro %s = %s\n\n", m.name(), m.value().get()); 71 } 72 return null; 73 } 74 75 @Override 76 public Void visitHeader(HeaderTree t, Void v) { 77 System.out.println("HeaderTree @ " + t.path()); 78 int i = 0; 79 for (Tree decl : t.declarations()) { 80 System.out.println("--> header declaration: " + i++); 81 decl.accept(this, v); 82 } 83 return null; 84 } 85 86 @Override 87 public Void visitStruct(StructTree s, Void v) { 88 defaultAction(s, v); 89 System.out.printf("%s layout = %s\n\n", s.name(), s.layout((ft, l) -> l)); 90 List<? extends FieldTree> fields = s.fields(); 91 if (! fields.isEmpty()) { 92 System.out.println("--> fields"); 93 for (FieldTree f : fields) { 94 f.accept(this, v); 95 } 96 } 97 List<? extends Tree> nested = s.nestedTypes(); 98 if (! nested.isEmpty()) { 99 System.out.println("--> nested types"); 100 for (Tree nt : nested) { 101 nt.accept(this, v); 102 } 103 } 104 return null; 105 } 106 107 @Override 108 public Void visitVar(VarTree t, Void v) { 109 defaultAction(t, v); 110 System.out.printf("%s layout = %s\n\n", t.name(), t.layout()); 111 return null; 112 } 113 }