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