Skip to content

Commit 1691b54

Browse files
committed
Store UserDate in Tree node
1 parent b339647 commit 1691b54

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

runtime/Cpp/include/antlr3commontree.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ class CommonTree : public ImplTraits::AllocPolicyType
4747
//typedef CommonTree TokenType;
4848
typedef typename AllocPolicyType::template VectorType<TreeTypePtr> ChildrenType;
4949
typedef typename AllocPolicyType::template ListType<TreeTypePtr> ChildListType;
50-
51-
private:
50+
typedef typename ImplTraits::TreeUserDataType UserDataType;
51+
protected:
5252
/// The list of all the children that belong to this node. They are not part of the node
5353
/// as they belong to the common tree node that implements this.
5454
///
@@ -139,6 +139,8 @@ class CommonTree : public ImplTraits::AllocPolicyType
139139
void freshenParentAndChildIndexesDeeply(ANTLR_UINT32 offset);
140140
// Prepare tree node to be re-used
141141
void reuse();
142+
143+
UserDataType UserData;
142144
};
143145

144146
}

runtime/Cpp/include/antlr3commontree.inl

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ CommonTree<ImplTraits>::CommonTree()
1313
template<class ImplTraits>
1414
CommonTree<ImplTraits>::CommonTree( const CommonTree& ctree )
1515
:m_children( ctree.m_children)
16+
,UserData(ctree.UserData)
1617
{
1718
m_startIndex = ctree.m_startIndex;
1819
m_stopIndex = ctree.m_stopIndex;
@@ -33,6 +34,7 @@ CommonTree<ImplTraits>::CommonTree( const CommonTokenType* token )
3334

3435
template<class ImplTraits>
3536
CommonTree<ImplTraits>::CommonTree( const CommonTree* tree )
37+
:UserData(tree->UserData)
3638
{
3739
m_startIndex = tree->get_startIndex();
3840
m_stopIndex = tree->get_stopIndex();
@@ -96,7 +98,9 @@ void CommonTree<ImplTraits>::addChild(TreeTypePtr& child)
9698
if ((*i) != NULL)
9799
{
98100
m_children.push_back(std::move(*i));
99-
m_children.back()->set_parent(this);
101+
// static_cast to possible subtype (if TreeType trait defined)
102+
TreeType* tree = static_cast<TreeType*>(this);
103+
m_children.back()->set_parent(tree);
100104
m_children.back()->set_childIndex(m_children.size() - 1);
101105
}
102106
}
@@ -116,7 +120,9 @@ void CommonTree<ImplTraits>::addChild(TreeTypePtr& child)
116120
{
117121
// Tree we are adding is not a Nil and might have children to copy
118122
m_children.push_back( std::move(child) );
119-
m_children.back()->set_parent(this);
123+
// static_cast to possible subtype (if TreeType trait defined)
124+
TreeType* tree = static_cast<TreeType*>(this);
125+
m_children.back()->set_parent(tree);
120126
m_children.back()->set_childIndex(m_children.size() - 1);
121127
}
122128
}
@@ -185,7 +191,8 @@ void CommonTree<ImplTraits>::replaceChildren(ANTLR_INT32 startChildIndex, ANTLR_
185191
{
186192
TreeType *child = newChildrenRef.at(j);
187193
m_children[i] = child;
188-
child->set_parent(this);
194+
TreeType* tree = static_cast<TreeType*>(this);
195+
child->set_parent(tree);
189196
child->set_childIndex(i);
190197
j++;
191198
}
@@ -398,7 +405,8 @@ void CommonTree<ImplTraits>::setChild(ANTLR_UINT32 i, TreeTypePtr child)
398405
m_children.resize(i+1);
399406

400407
m_children[i] = child;
401-
child->set_parent(this);
408+
TreeType* tree = static_cast<TreeType*>(this);
409+
child->set_parent(tree);
402410
child->set_childIndex(i);
403411
}
404412

@@ -469,7 +477,8 @@ void CommonTree<ImplTraits>::freshenParentAndChildIndexes(ANTLR_UINT32 offset)
469477
for(; i != m_children.end(); ++i, ++c)
470478
{
471479
(*i)->set_childIndex(c);
472-
(*i)->set_parent(this);
480+
TreeType* tree = static_cast<TreeType*>(this);
481+
(*i)->set_parent(tree);
473482
}
474483
}
475484

runtime/Cpp/include/antlr3traits.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class CustomTraitsBase
5959
};
6060

6161
typedef Empty TreeType;
62+
typedef Empty TreeUserDataType;
6263
typedef Empty TreeAdaptorType;
6364
typedef Empty TreeStoreType;
6465

@@ -276,6 +277,8 @@ class Traits : public TraitsBase<UserTraits>
276277
// TreeType
277278
typedef typename TraitsSelector< typename UserTraits<TraitsType>::TreeType,
278279
CommonTree<TraitsType> >::selected TreeType;
280+
typedef typename TraitsSelector< typename UserTraits<TraitsType>::TreeUserDataType,
281+
Empty >::selected TreeUserDataType;
279282
// TreeAdaptorType
280283
typedef typename TraitsSelector< typename UserTraits<TraitsType>::TreeAdaptorType,
281284
CommonTreeAdaptor<TraitsType> >::selected TreeAdaptorType;

0 commit comments

Comments
 (0)