1 /*
 2  *  Copyright (c) 2020, 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.  Oracle designates this
 8  *  particular file as subject to the "Classpath" exception as provided
 9  *  by Oracle in the LICENSE file that accompanied this code.
10  *
11  *  This code is distributed in the hope that it will be useful, but WITHOUT
12  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  *  version 2 for more details (a copy is included in the LICENSE file that
15  *  accompanied this code).
16  *
17  *  You should have received a copy of the GNU General Public License version
18  *  2 along with this work; if not, write to the Free Software Foundation,
19  *  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  *   Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  *  or visit www.oracle.com if you need additional information or have any
23  *  questions.
24  *
25  */
26 package jdk.internal.jextract.impl;
27 
28 import jdk.incubator.jextract.Declaration;
29 import jdk.internal.clang.Cursor;
30 
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35 
36 /**
37  * This visitor handles certain typedef declarations.
38  *
39  * 1. Remove redundant typedefs.
40  * 2. Rename typedef'ed anonymous type definitions like
41  *        typedef struct { int x; int y; } Point;
42  */
43 final class TypedefHandler implements Declaration.Visitor<Void, Void> {
44 
45     TreeMaker maker;
46 
47     public TypedefHandler(TreeMaker maker) {
48         this.maker = maker;
49     }
50 
51     // Potential Tree instances that will go into transformed HeaderTree
52     // are collected in this list.
53     private List<Declaration> decls = new ArrayList<>();
54 
55     // Tree instances that are to be replaced from "decls" list are
56     // saved in the following Map. One or more Trees can replace a Tree.
57     private final Map<Cursor, List<Declaration>> replacements = new HashMap<>();
58 
59     public Declaration.Scoped transform(Declaration.Scoped ht) {
60         // Process all header declarations are collect potential
61         // declarations that will go into transformed HeaderTree
62         // into the this.decls field.
63         ht.accept(this, null);
64 
65 //        // Replace trees from this.decls with Trees found in this.replacements.
66 //        // We need this two step process so that named StructTree instances
67 //        // will replace with original unnamed StructTree instances.
68 //        List<Declaration> newDecls = decls.stream().flatMap(tx -> {
69 //            if (replacements.containsKey(tx.cursor())) {
70 //                return replacements.get(tx.cursor()).stream();
71 //            } else {
72 //                return Stream.of(tx);
73 //            }
74 //        }).collect(Collectors.toList());
75 //
76 //        return treeMaker.createHeader(ht.cursor(), ht.path(), newDecls);
77         return ht;
78     }
79 
80     @Override
81     public Void visitDeclaration(Declaration d, Void aVoid) {
82         decls.add(d);
83         return null;
84     }
85 
86     @Override
87     public Void visitScoped(Declaration.Scoped ht, Void v) {
88         ht.members().forEach(decl -> decl.accept(this, null));
89         return null;
90     }
91 }