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