-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathcpp_type_tree.h
More file actions
88 lines (74 loc) · 2.14 KB
/
Copy pathcpp_type_tree.h
File metadata and controls
88 lines (74 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright (C) 2022 Satya Das and CppParser contributors
// SPDX-License-Identifier: MIT
#ifndef BC27B3FD_816E_47DE_BCA2_0692BAE4F6DE
#define BC27B3FD_816E_47DE_BCA2_0692BAE4F6DE
#include "cppast/cpp_entity.h"
#include <map>
#include <set>
#include <string>
namespace cppparser {
struct CppTypeTreeNode;
/**
* @brief Represents the tree of types in a C++ program.
*
* All C++ types of a program can be arranged in form of a tree.
* The root of the tree is the global namespace which contains other compound objects like namespace, class, struct,
* etc. And each of those compound object can form another branch of tree.
*
* @note This tree has no relation with inheritance hierarchy.
*/
using CppTypeTree = std::map<std::string, CppTypeTreeNode>;
struct CppEntityCmp
{
bool operator()(const cppast::CppEntity* lhs, const cppast::CppEntity* rhs) const
{
if (lhs->entityType() < rhs->entityType())
return true;
if (lhs->entityType() > rhs->entityType())
return false;
return lhs < rhs;
}
};
using CppEntitySet = std::set<const cppast::CppEntity*, CppEntityCmp>;
/**
* @brief A node in a CppTypeTree.
*/
struct CppTypeTreeNode
{
/**
* @brief Set of all entities that have same name in the same scope.
*
* This needs to be a set because same namespace can be defined multiple times.
* But members of all those definition will belong to single namespace.
* Also, a class can be forward declared before full definition.
*/
CppEntitySet cppEntitySet;
CppTypeTree children;
CppTypeTreeNode* parent;
CppTypeTreeNode()
: parent(nullptr)
{
}
bool has(const cppast::CppEntity* cppEntity) const
{
if (cppEntitySet.count(cppEntity))
return true;
for (const auto child : children)
{
if (child.second.has(cppEntity))
return true;
}
return false;
}
const cppast::CppEntity* getObjInSet(cppast::CppEntityType objType) const
{
for (const auto cppEntity : cppEntitySet)
{
if (cppEntity->entityType() == objType)
return cppEntity;
}
return nullptr;
}
};
} // namespace cppparser
#endif /* BC27B3FD_816E_47DE_BCA2_0692BAE4F6DE */