2 //=============================================================================
4 * @file be_visitor_scope.cpp
6 * Visitor for the base be_scope node. This serves to maintain the current
7 * state (context) of code generation for the derived visitor.
9 * @author Aniruddha Gokhale
11 //=============================================================================
14 #include "be_argument.h"
16 #include "be_visitor_scope.h"
17 #include "be_visitor_context.h"
18 #include "ace/Log_Msg.h"
20 // ******************************************************
21 // Generic visitor for a scope.
22 // All elements that give rise to a scope inherit from
24 // ******************************************************
26 be_visitor_scope::be_visitor_scope (be_visitor_context
*ctx
)
27 : be_visitor_decl (ctx
),
32 be_visitor_scope::~be_visitor_scope ()
36 // Visit the scope and its elements.
38 be_visitor_scope::visit_scope (be_scope
*node
)
42 ACE_ERROR_RETURN ((LM_ERROR
,
43 "(%N:%l) be_visitor_scope::visit_scope - "
44 "nill node passed\n"),
48 // Proceed if the number of members in our scope is greater than 0.
49 this->elem_number_
= 0;
51 for (UTL_ScopeActiveIterator
si (node
, UTL_Scope::IK_decls
);
55 AST_Decl
*d
= si
.item ();
59 ACE_ERROR_RETURN ((LM_ERROR
,
60 "(%N:%l) be_visitor_scope::visit_scope - "
61 "bad node in this scope\n"),
65 if (d
->node_type () == AST_Decl::NT_annotation_decl
)
70 be_decl
*bd
= dynamic_cast<be_decl
*> (d
);
72 // Set the scope node as "node" in which the code is being
73 // generated so that elements in the node's scope can use it
74 // for code generation.
75 this->ctx_
->scope (node
);
77 // Set the node to be visited.
78 this->ctx_
->node (bd
);
81 // Do any pre processing using the next item info.
82 if (this->pre_process (bd
) == -1)
84 ACE_ERROR_RETURN ((LM_ERROR
,
85 "(%N:%l) be_visitor_scope::visit_scope - "
86 "pre processing failed\n"),
91 if (bd
== nullptr || bd
->accept (this) == -1)
93 ACE_ERROR_RETURN ((LM_ERROR
,
94 "(%N:%l) be_visitor_scope::visit_scope - "
95 "codegen for scope failed\n"),
99 // Do any post processing using this item info.
100 if (this->post_process (bd
) == -1)
102 ACE_ERROR_RETURN ((LM_ERROR
,
103 "(%N:%l) be_visitor_scope::visit_scope - "
104 "post processing failed\n"),
113 be_visitor_scope::post_process (be_decl
*)
119 be_visitor_scope::pre_process (be_decl
*)
125 be_visitor_scope::elem_number ()
127 // Return the current element that we are working on.
128 return this->elem_number_
;
131 // Find the element that succeeds "elem" in the list.
133 be_visitor_scope::next_elem (be_decl
*elem
,
136 be_decl
*ctx_scope
= this->ctx_
->scope ()->decl ();
137 be_scope
*node
= nullptr;
139 if (ctx_scope
!= nullptr)
141 node
= ctx_scope
->scope ();
144 if (ctx_scope
== nullptr || node
== nullptr)
146 ACE_ERROR_RETURN ((LM_ERROR
,
147 "(%N:%l) be_visitor_scope::next_elem - "
154 // Initialize an iterator to iterate thru our scope.
155 for (UTL_ScopeActiveIterator
si (node
, UTL_Scope::IK_decls
);
159 be_decl
*bd
= dynamic_cast<be_decl
*> (si
.item ());
163 ACE_ERROR_RETURN ((LM_ERROR
,
164 "(%N:%l) be_visitor_scope::next_elem - "
165 "bad node in this scope\n"),
174 // Find who is next to me.
179 // Nobody left in the list.
183 successor
= dynamic_cast<be_decl
*> (si
.item ());
185 if (successor
== nullptr)
187 ACE_ERROR_RETURN ((LM_ERROR
,
188 "(%N:%l) be_visitor_scope::next_elem - "
189 "bad node in this scope\n"),
193 // Nothing else to do.
201 be_visitor_scope::last_node (be_decl
*bd
)
203 be_decl
*next
= nullptr;
204 (void) this->next_elem (bd
,
207 return (next
== nullptr);
211 be_visitor_scope::last_inout_or_out_node (be_decl
*)
213 // Return true if we are the last inout or out argument.
214 be_decl
*next
= nullptr;
215 (void) this->next_elem (this->ctx_
->node (),
218 while (next
!= nullptr)
220 be_argument
*arg
= dynamic_cast<be_argument
*> (next
);
222 if (arg
->direction () == AST_Argument::dir_INOUT
223 || arg
->direction () == AST_Argument::dir_OUT
)
229 be_decl
*next_next
= nullptr;
230 this->next_elem (next
,
236 // I am the last one.