ACAV f0ba4b7c9529
Abstract Syntax Tree (AST) visualization tool for C, C++, and Objective-C
Loading...
Searching...
No Matches
AstNode.h
Go to the documentation of this file.
1/*$!{
2* Aurora Clang AST Viewer (ACAV)
3*
4* Copyright (c) 2026 Min Liu
5* Copyright (c) 2026 Michael David Adams
6*
7* SPDX-License-Identifier: GPL-2.0-or-later
8*
9* This program is free software; you can redistribute it and/or modify
10* it under the terms of the GNU General Public License as published by
11* the Free Software Foundation; either version 2 of the License, or
12* (at your option) any later version.
13*
14* This program is distributed in the hope that it will be useful,
15* but WITHOUT ANY WARRANTY; without even the implied warranty of
16* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17* GNU General Public License for more details.
18*
19* You should have received a copy of the GNU General Public License along
20* with this program; if not, see <https://www.gnu.org/licenses/>.
21}$!*/
22
25#pragma once
26
28#include "core/SourceLocation.h"
30#include <nlohmann/json.hpp>
31#include <unordered_map>
32#include <vector>
33
34namespace acav {
35
36#ifdef ACAV_ENABLE_STRING_STATS
38struct TypeDeduplicationStats {
39 std::size_t totalTypeLookups;
40 std::size_t cacheHits;
41 std::size_t uniqueTypeNodes;
42 std::size_t totalTypeReferences;
43 std::size_t estimatedSavedNodes;
44 std::size_t estimatedSavedBytes;
45 double deduplicationRatio;
46 double savingsPercent;
47};
48#endif
49
51using AcavJson =
52 nlohmann::basic_json<std::map, std::vector, InternedString, bool, int64_t,
53 uint64_t, double, std::allocator,
54 nlohmann::adl_serializer, std::vector<uint8_t>>;
55
56// Forward declarations
57class AstNode;
58class AstViewNode;
59
66class AstContext {
67public:
68 AstContext() = default;
69 ~AstContext();
70
71 AstContext(const AstContext &) = delete;
72 AstContext &operator=(const AstContext &) = delete;
73 AstContext(AstContext &&) = delete;
74 AstContext &operator=(AstContext &&) = delete;
75
80 AstNode *createAstNode(AcavJson properties, const SourceRange &range);
81
86
104 AstNode *getOrCreateTypeNode(const void *typePtr, AcavJson properties,
105 const SourceRange &range);
106
108 AstNode *findTypeNode(const void *typePtr) const;
109
113 void indexNode(AstViewNode *node);
114
117
119 const SourceLocationIndex &getLocationIndex() const { return locationIndex_; }
120
122 std::size_t getAstNodeCount() const { return allAstNodes_.size(); }
124 std::size_t getAstViewNodeCount() const { return allAstViewNodes_.size(); }
125
126#ifdef ACAV_ENABLE_STRING_STATS
128 TypeDeduplicationStats getTypeDeduplicationStats() const;
130 void printTypeDeduplicationStats(const char *label = nullptr) const;
131#endif
132
133private:
134 std::vector<AstNode *> allAstNodes_;
135 std::vector<AstViewNode *> allAstViewNodes_;
136
140 std::unordered_map<const void *, AstNode *> typeDeduplicationMap_;
141
143 SourceLocationIndex locationIndex_;
144
145#ifdef ACAV_ENABLE_STRING_STATS
147 mutable std::size_t typeLookupCount_ = 0;
148 mutable std::size_t typeCacheHits_ = 0;
149#endif
150};
151
162class AstNode {
163public:
165 const AcavJson &getProperties() const { return properties_; }
167 AcavJson &getProperties() { return properties_; }
169 const SourceRange &getSourceRange() const { return sourceRange_; }
170
172 std::size_t getUseCount() const { return refCount_; }
174 void hold() { ++refCount_; }
176 void release() {
177 if (refCount_ > 0)
178 --refCount_;
179 }
180
181private:
182 // Constructor is private - only AstContext can create nodes
183 AstNode(AcavJson properties, const SourceRange &sourceRange);
184 friend class AstContext;
185
186 AcavJson properties_; // Node metadata (kind, name, type, etc.)
187 SourceRange sourceRange_; // Source location
188 std::size_t refCount_ = 0; // Number of AstViewNodes using this
189};
190
195class AstViewNode {
196public:
197 ~AstViewNode();
198
199 AstViewNode(const AstViewNode &) = delete;
200 AstViewNode &operator=(const AstViewNode &) = delete;
201 AstViewNode(AstViewNode &&) = delete;
202 AstViewNode &operator=(AstViewNode &&) = delete;
203
205 AstNode *getNode() const { return node_; }
207 AstViewNode *getParent() const { return parent_; }
209 const std::vector<AstViewNode *> &getChildren() const { return children_; }
210
212 void setParent(AstViewNode *parent) { parent_ = parent; }
214 void addChild(AstViewNode *child);
215
217 const AcavJson &getProperties() const { return node_->getProperties(); }
219 const SourceRange &getSourceRange() const { return node_->getSourceRange(); }
220
221private:
222 // Constructor is private - only AstContext can create nodes
223 explicit AstViewNode(AstNode *node);
224 friend class AstContext;
225
226 AstNode *node_; // Pointer to data (may be shared)
227 AstViewNode *parent_ = nullptr; // Parent in tree
228 std::vector<AstViewNode *> children_; // Children in tree (owned)
229};
230
231} // namespace acav
nlohmann::basic_json< std::map, std::vector, InternedString, bool, int64_t, uint64_t, double, std::allocator, nlohmann::adl_serializer, std::vector< uint8_t > > AcavJson
Custom JSON type using InternedString for automatic string deduplication.
Definition AstNode.h:51
Memory-efficient immutable string with automatic deduplication.
Source-to-AST node lookup index.
Source code location representation.
Memory manager for AST nodes in a translation unit.
Definition AstNode.h:66
std::size_t getAstViewNodeCount() const
Get total number of view nodes.
Definition AstNode.h:124
AstNode * findTypeNode(const void *typePtr) const
Find existing deduplicated Type node (or nullptr).
Definition AstNode.cpp:128
AstViewNode * createAstViewNode(AstNode *node)
Create a new AST view node wrapping data.
Definition AstNode.cpp:97
std::size_t getAstNodeCount() const
Get total number of data nodes.
Definition AstNode.h:122
const SourceLocationIndex & getLocationIndex() const
Get location index for source-to-AST queries.
Definition AstNode.h:119
void finalizeLocationIndex()
Finalize location index (call after all nodes indexed).
Definition AstNode.cpp:142
void indexNode(AstViewNode *node)
Add node to location index.
Definition AstNode.cpp:136
AstNode * getOrCreateTypeNode(const void *typePtr, AcavJson properties, const SourceRange &range)
Get or create deduplicated Type node.
Definition AstNode.cpp:103
AstNode * createAstNode(AcavJson properties, const SourceRange &range)
Create a new AST data node.
Definition AstNode.cpp:90
Pure data container for AST node properties.
Definition AstNode.h:162
void hold()
Increment reference count.
Definition AstNode.h:174
AcavJson & getProperties()
Get mutable properties.
Definition AstNode.h:167
void release()
Decrement reference count.
Definition AstNode.h:176
const AcavJson & getProperties() const
Get node properties (kind, name, type, etc.).
Definition AstNode.h:165
std::size_t getUseCount() const
Get number of view nodes referencing this data.
Definition AstNode.h:172
const SourceRange & getSourceRange() const
Get source location range.
Definition AstNode.h:169
Represents node in AST tree hierarchy.
Definition AstNode.h:195
void setParent(AstViewNode *parent)
Set parent node.
Definition AstNode.h:212
AstNode * getNode() const
Get underlying data node.
Definition AstNode.h:205
const AcavJson & getProperties() const
Get node properties (delegates to data node).
Definition AstNode.h:217
const SourceRange & getSourceRange() const
Get source range (delegates to data node).
Definition AstNode.h:219
const std::vector< AstViewNode * > & getChildren() const
Get children in tree hierarchy.
Definition AstNode.h:209
AstViewNode * getParent() const
Get parent in tree hierarchy.
Definition AstNode.h:207
void addChild(AstViewNode *child)
Add child node.
Definition AstNode.cpp:53
Immutable string with automatic deduplication via global pool.
Index for source-to-AST node lookup.
Represents a span of source code (begin to end).