xref: /aosp_15_r20/external/antlr/runtime/C/include/antlr3commontree.h (revision 16467b971bd3e2009fad32dd79016f2c7e421deb)
1*16467b97STreehugger Robot /** Interface for an ANTLR3 common tree which is what gets
2*16467b97STreehugger Robot  *  passed around by the AST producing parser.
3*16467b97STreehugger Robot  */
4*16467b97STreehugger Robot 
5*16467b97STreehugger Robot #ifndef	_ANTLR3_COMMON_TREE_H
6*16467b97STreehugger Robot #define	_ANTLR3_COMMON_TREE_H
7*16467b97STreehugger Robot 
8*16467b97STreehugger Robot // [The "BSD licence"]
9*16467b97STreehugger Robot // Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC
10*16467b97STreehugger Robot // http://www.temporal-wave.com
11*16467b97STreehugger Robot // http://www.linkedin.com/in/jimidle
12*16467b97STreehugger Robot //
13*16467b97STreehugger Robot // All rights reserved.
14*16467b97STreehugger Robot //
15*16467b97STreehugger Robot // Redistribution and use in source and binary forms, with or without
16*16467b97STreehugger Robot // modification, are permitted provided that the following conditions
17*16467b97STreehugger Robot // are met:
18*16467b97STreehugger Robot // 1. Redistributions of source code must retain the above copyright
19*16467b97STreehugger Robot //    notice, this list of conditions and the following disclaimer.
20*16467b97STreehugger Robot // 2. Redistributions in binary form must reproduce the above copyright
21*16467b97STreehugger Robot //    notice, this list of conditions and the following disclaimer in the
22*16467b97STreehugger Robot //    documentation and/or other materials provided with the distribution.
23*16467b97STreehugger Robot // 3. The name of the author may not be used to endorse or promote products
24*16467b97STreehugger Robot //    derived from this software without specific prior written permission.
25*16467b97STreehugger Robot //
26*16467b97STreehugger Robot // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27*16467b97STreehugger Robot // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28*16467b97STreehugger Robot // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29*16467b97STreehugger Robot // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30*16467b97STreehugger Robot // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31*16467b97STreehugger Robot // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32*16467b97STreehugger Robot // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33*16467b97STreehugger Robot // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34*16467b97STreehugger Robot // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35*16467b97STreehugger Robot // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36*16467b97STreehugger Robot 
37*16467b97STreehugger Robot #include    <antlr3defs.h>
38*16467b97STreehugger Robot #include    <antlr3basetree.h>
39*16467b97STreehugger Robot #include    <antlr3commontoken.h>
40*16467b97STreehugger Robot 
41*16467b97STreehugger Robot #ifdef __cplusplus
42*16467b97STreehugger Robot extern "C" {
43*16467b97STreehugger Robot #endif
44*16467b97STreehugger Robot 
45*16467b97STreehugger Robot typedef struct ANTLR3_COMMON_TREE_struct
46*16467b97STreehugger Robot {
47*16467b97STreehugger Robot 
48*16467b97STreehugger Robot 	/// Not used by ANTLR, but if a super structure is created above
49*16467b97STreehugger Robot     /// this structure, it can be used to point to the start of the super
50*16467b97STreehugger Robot     /// structure, where additional data and function pointers can be stored.
51*16467b97STreehugger Robot     ///
52*16467b97STreehugger Robot     void					* super;
53*16467b97STreehugger Robot 
54*16467b97STreehugger Robot     /// Start token index that encases this tree
55*16467b97STreehugger Robot     ///
56*16467b97STreehugger Robot     ANTLR3_MARKER			  startIndex;
57*16467b97STreehugger Robot 
58*16467b97STreehugger Robot     /// End token that encases this tree
59*16467b97STreehugger Robot     ///
60*16467b97STreehugger Robot     ANTLR3_MARKER			  stopIndex;
61*16467b97STreehugger Robot 
62*16467b97STreehugger Robot     /// A single token, this is the payload for the tree
63*16467b97STreehugger Robot     ///
64*16467b97STreehugger Robot     pANTLR3_COMMON_TOKEN      token;
65*16467b97STreehugger Robot 
66*16467b97STreehugger Robot 	/// Points to the node that has this node as a child.
67*16467b97STreehugger Robot 	/// If this is NULL, then this is the root node.
68*16467b97STreehugger Robot 	///
69*16467b97STreehugger Robot 	pANTLR3_COMMON_TREE		  parent;
70*16467b97STreehugger Robot 
71*16467b97STreehugger Robot 	/// What index is this particular node in the child list it
72*16467b97STreehugger Robot 	/// belongs to?
73*16467b97STreehugger Robot 	///
74*16467b97STreehugger Robot 	ANTLR3_INT32			  childIndex;
75*16467b97STreehugger Robot 
76*16467b97STreehugger Robot 	/// Pointer to the tree factory that manufactured this
77*16467b97STreehugger Robot 	/// token. This can be used by duplication methods and so on
78*16467b97STreehugger Robot 	/// to manufacture another auto-tracked common tree structure
79*16467b97STreehugger Robot 	///
80*16467b97STreehugger Robot 	pANTLR3_ARBORETUM	factory;
81*16467b97STreehugger Robot 
82*16467b97STreehugger Robot     /// An encapsulated BASE TREE structure (NOT a pointer)
83*16467b97STreehugger Robot     /// that performs a lot of the dirty work of node management
84*16467b97STreehugger Robot     /// To this we add just a few functions that are specific to the
85*16467b97STreehugger Robot     /// payload. You can further abstract common tree so long
86*16467b97STreehugger Robot     /// as you always have a baseTree pointer in the top structure
87*16467b97STreehugger Robot     /// and copy it from the next one down.
88*16467b97STreehugger Robot     /// So, lets say we have a structure JIMS_TREE.
89*16467b97STreehugger Robot     /// It needs an ANTLR3_BASE_TREE that will support all the
90*16467b97STreehugger Robot     /// general tree duplication stuff.
91*16467b97STreehugger Robot     /// It needs a ANTLR3_COMMON_TREE structure embedded or completely
92*16467b97STreehugger Robot     /// provides the equivalent interface.
93*16467b97STreehugger Robot     /// It provides it's own methods and data.
94*16467b97STreehugger Robot     /// To create a new one of these, the function provided to
95*16467b97STreehugger Robot     /// the tree adaptor (see comments there) should allocate the
96*16467b97STreehugger Robot     /// memory for a new JIMS_TREE structure, then call
97*16467b97STreehugger Robot     /// antlr3InitCommonTree(<addressofembeddedCOMMON_TREE>)
98*16467b97STreehugger Robot     /// antlr3BaseTreeNew(<addressofBASETREE>)
99*16467b97STreehugger Robot     /// The interfaces for BASE_TREE and COMMON_TREE will then
100*16467b97STreehugger Robot     /// be initialized. You then call and you can override them or just init
101*16467b97STreehugger Robot     /// JIMS_TREE (note that the base tree in common tree will be ignored)
102*16467b97STreehugger Robot     /// just the top level base tree is used). Codegen will take care of the rest.
103*16467b97STreehugger Robot     ///
104*16467b97STreehugger Robot     ANTLR3_BASE_TREE	    baseTree;
105*16467b97STreehugger Robot 
106*16467b97STreehugger Robot }
107*16467b97STreehugger Robot     ANTLR3_COMMON_TREE;
108*16467b97STreehugger Robot 
109*16467b97STreehugger Robot /// \brief ANTLR3 Tree factory interface to create lots of trees efficiently
110*16467b97STreehugger Robot ///  rather than creating and freeing lots of little bits of memory.
111*16467b97STreehugger Robot ///
112*16467b97STreehugger Robot typedef	struct ANTLR3_ARBORETUM_struct
113*16467b97STreehugger Robot {
114*16467b97STreehugger Robot     /// Pointers to the array of tokens that this factory has produced so far
115*16467b97STreehugger Robot     ///
116*16467b97STreehugger Robot     pANTLR3_COMMON_TREE    *pools;
117*16467b97STreehugger Robot 
118*16467b97STreehugger Robot     /// Current pool tokens we are allocating from
119*16467b97STreehugger Robot     ///
120*16467b97STreehugger Robot     ANTLR3_INT32			thisPool;
121*16467b97STreehugger Robot 
122*16467b97STreehugger Robot     /// The next token to throw out from the pool, will cause a new pool allocation
123*16467b97STreehugger Robot     ///  if this exceeds the available tokenCount
124*16467b97STreehugger Robot     ///
125*16467b97STreehugger Robot     ANTLR3_UINT32			nextTree;
126*16467b97STreehugger Robot 
127*16467b97STreehugger Robot     /// Trick to initialize tokens and their API quickly, we set up this token when the
128*16467b97STreehugger Robot     /// factory is created, then just copy the memory it uses into the new token.
129*16467b97STreehugger Robot     ///
130*16467b97STreehugger Robot     ANTLR3_COMMON_TREE	    unTruc;
131*16467b97STreehugger Robot 
132*16467b97STreehugger Robot     /// Pointer to a vector factory that is used to create child list vectors
133*16467b97STreehugger Robot     /// for any child nodes that need them. This means that we auto track the
134*16467b97STreehugger Robot     /// vectors and auto free them when we close the factory. It also means
135*16467b97STreehugger Robot     /// that all rewriting trees can use the same tree factory and the same
136*16467b97STreehugger Robot     /// vector factory and we do not dup any nodes unless we must do so
137*16467b97STreehugger Robot     /// explicitly because of context such as an empty rewrite stream and
138*16467b97STreehugger Robot     /// ->IMAGINARY[ID] so on. This makes memory tracking much simpler and
139*16467b97STreehugger Robot     /// tempts no errors.
140*16467b97STreehugger Robot     ///
141*16467b97STreehugger Robot     pANTLR3_VECTOR_FACTORY   vFactory;
142*16467b97STreehugger Robot 
143*16467b97STreehugger Robot     /// A resuse stack for reclaiming Nil nodes that were used in rewrites
144*16467b97STreehugger Robot     /// and are now dead. The nilNode() method will eat one of these before
145*16467b97STreehugger Robot     /// creating a new node.
146*16467b97STreehugger Robot     ///
147*16467b97STreehugger Robot     pANTLR3_STACK           nilStack;
148*16467b97STreehugger Robot 
149*16467b97STreehugger Robot     /// Pointer to a function that returns a new tree
150*16467b97STreehugger Robot     ///
151*16467b97STreehugger Robot     pANTLR3_BASE_TREE	    (*newTree)		(struct ANTLR3_ARBORETUM_struct * factory);
152*16467b97STreehugger Robot     pANTLR3_BASE_TREE	    (*newFromTree)	(struct ANTLR3_ARBORETUM_struct * factory, pANTLR3_COMMON_TREE tree);
153*16467b97STreehugger Robot     pANTLR3_BASE_TREE	    (*newFromToken)	(struct ANTLR3_ARBORETUM_struct * factory, pANTLR3_COMMON_TOKEN token);
154*16467b97STreehugger Robot 
155*16467b97STreehugger Robot     /// Pointer to a function the destroys the factory
156*16467b97STreehugger Robot     ///
157*16467b97STreehugger Robot     void		    (*close)	    (struct ANTLR3_ARBORETUM_struct * factory);
158*16467b97STreehugger Robot }
159*16467b97STreehugger Robot     ANTLR3_ARBORETUM;
160*16467b97STreehugger Robot 
161*16467b97STreehugger Robot #ifdef __cplusplus
162*16467b97STreehugger Robot }
163*16467b97STreehugger Robot #endif
164*16467b97STreehugger Robot 
165*16467b97STreehugger Robot #endif
166*16467b97STreehugger Robot 
167*16467b97STreehugger Robot 
168