summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/IFR_Service/ifr_adding_visitor_operation.cpp
blob: 275f567fdda560dfa8d674d47541e8766cf89c45 (plain)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/* -*- c++ -*- */
// $Id$

#include "ifr_adding_visitor_operation.h"
#include "utl_exceptlist.h"
#include "utl_strlist.h"

ACE_RCSID(IFR_Service, ifr_adding_visitor_operation, "$Id$")

ifr_adding_visitor_operation::ifr_adding_visitor_operation (void)
  : index_ (0)
{
}

ifr_adding_visitor_operation::~ifr_adding_visitor_operation (void)
{
}

int 
ifr_adding_visitor_operation::visit_operation (AST_Operation *node)
{
  ACE_DECLARE_NEW_CORBA_ENV;
  ACE_TRY
    {
      // If this operation is already in the repository (for example, if
      // we are processing the IDL file a second time inadvertently), we
      // just return 0. The IDL file must be legal, otherwise the IDL
      // compiler front end would have told us.

      CORBA_Contained_var prev_def =
        be_global->repository ()->lookup_id (node->repoID (),
                                             ACE_TRY_ENV);
      ACE_TRY_CHECK;

      if (!CORBA::is_nil (prev_def.in ()))
        {
          return 0;
        }

      // Build the parameter list. Our overridden version of visit_argument
      // will look up each parameter and add its repository entry to
      // our params_ member.

      CORBA::ULong length = ACE_static_cast (CORBA::ULong, 
                                             node->argument_count ());

      this->params_.length (length);

      if (this->visit_scope (node) == -1)
        {
          ACE_ERROR_RETURN ((
              LM_ERROR,
              ACE_TEXT ("(%N:%l) ifr_adding_visitor_operation::")
              ACE_TEXT ("visit_operation -")
              ACE_TEXT (" visit_scope failed\n")
            ),  
            -1
          );
        }

      this->index_ = 0;

      // Build the exception list.

      UTL_ExceptList *excepts = node->exceptions ();

      if (excepts != 0)
        {
          length = ACE_static_cast (CORBA::ULong,
                                    excepts->length ());
        }
      else
        {
          length = 0;
        }

      CORBA_ExceptionDefSeq exceptions (length);
      exceptions.length (length);

      UTL_ExceptlistActiveIterator ex_iter (excepts);
      AST_Exception *ex = 0;
      CORBA::ULong i = 0;

      while (!ex_iter.is_done ())
        {
          ex = ex_iter.item ();

          prev_def =
            be_global->repository ()->lookup_id (ex->repoID (),
                                                 ACE_TRY_ENV);
          ACE_TRY_CHECK;

          exceptions[i++] = CORBA_ExceptionDef::_narrow (prev_def.in (),
                                                      ACE_TRY_ENV);
          ACE_TRY_CHECK;

          ex_iter.next ();
        }

      // Build the context list.

      UTL_StrList *ctx_list = node->context ();

      if (ctx_list != 0)
        {
          length = ACE_static_cast (CORBA::ULong,
                                    ctx_list->length ());
        }
      else
        {
          length = 0;
        }

      CORBA_ContextIdSeq contexts (length);
      contexts.length (length);

      UTL_StrlistActiveIterator ctx_iter (ctx_list);
      UTL_String *str = 0;
      i = 0;

      while (!ctx_iter.is_done ())
        {
          str = ctx_iter.item ();

          contexts[i++] = str->get_string ();

          ctx_iter.next ();
        }

      // Get the return type.

      AST_Type *return_type = node->return_type ();

      // If one of the operations's args is an interface, we just update
      // the current IR object holder. The forward declaration
      // (if any) will create a repository entry, and the full
      // definition will take care of the interface's scope. Trying 
      // to take care of the interface's scope at this point could
      // cause problems, if the types of all its members have not yet
      // been declared.
      if (return_type->node_type () == AST_Decl::NT_interface)
        {
          CORBA_Contained_var prev_def =
            be_global->repository ()->lookup_id (return_type->repoID (),
                                                 ACE_TRY_ENV);
          ACE_TRY_CHECK;

          this->ir_current_ = CORBA_IDLType::_narrow (prev_def.in (),
                                                   ACE_TRY_ENV);
          ACE_TRY_CHECK;
        }
      else
        {
          // Since this entry should already be in the repository,
          // this call will just look it up and update the current 
          // IR object holder.
          if (return_type->ast_accept (this) == -1)
            {
              ACE_ERROR_RETURN ((
                  LM_ERROR,
                  ACE_TEXT ("(%N:%l) ifr_adding_visitor_operation::")
                  ACE_TEXT ("visit_operation -")
                  ACE_TEXT (" failed to accept visitor\n")
                ),  
                -1
              );
            }
        }

      // Is the operation oneway?
      CORBA::OperationMode mode = node->flags () == AST_Operation::OP_oneway 
                                 ? CORBA::OP_ONEWAY 
                                 : CORBA::OP_NORMAL;

      // Create the repository entry.

      CORBA_Container_ptr current_scope = CORBA_Container::_nil ();

      if (be_global->ifr_scopes ().top (current_scope) == 0)
        {
          CORBA_InterfaceDef_var iface = 
            CORBA_InterfaceDef::_narrow (current_scope,
                                      ACE_TRY_ENV);
          ACE_TRY_CHECK;

          CORBA_OperationDef_var new_def =
            iface->create_operation (node->repoID (),
                                     node->local_name ()->get_string (),
                                     this->gen_version (node),
                                     this->ir_current_.in (),
                                     mode,
                                     this->params_,
                                     exceptions,
                                     contexts,
                                     ACE_TRY_ENV);
          ACE_TRY_CHECK;
        }
      else
        {
          ACE_ERROR_RETURN ((
              LM_ERROR,
              ACE_TEXT ("(%N:%l) ifr_adding_visitor_operation::")
              ACE_TEXT ("visit_operation -")
              ACE_TEXT (" scope stack is empty\n")
            ),  
            -1
          );
        }
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (
          ACE_ANY_EXCEPTION,
          ACE_TEXT ("ifr_adding_visitor_operation::visit_operation")
        );

      return -1;
    }
  ACE_ENDTRY;

  return 0;
}

int 
ifr_adding_visitor_operation::visit_argument (AST_Argument *node)
{
  // Get the parameter's name.
  this->params_[this->index_].name = node->local_name ()->get_string ();

  AST_Type *arg_type = node->field_type ();

  ACE_DECLARE_NEW_CORBA_ENV;
  ACE_TRY
    {
      // If one of the operations's args is an interface, we just update
      // the current IR object holder. The forward declaration
      // (if any) will create a repository entry, and the full
      // definition will take care of the interface's scope. Trying 
      // to take care of the interface's scope at this point could
      // cause problems, if the types of all its members have not yet
      // been declared.
      if (arg_type->node_type () == AST_Decl::NT_interface)
        {
          CORBA_Contained_var prev_def =
            be_global->repository ()->lookup_id (arg_type->repoID (),
                                                 ACE_TRY_ENV);
          ACE_TRY_CHECK;

          this->ir_current_ = CORBA_IDLType::_narrow (prev_def.in (),
                                                   ACE_TRY_ENV);
          ACE_TRY_CHECK;
        }
      else
        {
          // Since this type must already have a repository entry, the call
          // wil just update the current IR object holder.
          if (arg_type->ast_accept (this) == -1)
            {
              ACE_ERROR_RETURN ((
                  LM_ERROR,
                  ACE_TEXT ("(%N:%l) ifr_adding_visitor_operation::")
                  ACE_TEXT ("visit_argument - failed to accept visitor\n")
                ),  
                -1
              );
            }
        }

      this->params_[this->index_].type_def = 
        CORBA_IDLType::_duplicate (this->ir_current_.in ());

      // Fortunately, AST_Field::Direction and CORBA_ParameterMode 
      // are ordered identically.
      this->params_[this->index_].mode = 
        (CORBA::ParameterMode) node->direction ();

      // IfR method create_operation does not use this - it just needs
      // to be non-null for marshaling.
      this->params_[this->index_].type =
        CORBA::TypeCode::_duplicate (CORBA::_tc_null);

      ++this->index_;
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (
          ACE_ANY_EXCEPTION,
          ACE_TEXT ("ifr_adding_visitor_operation::visit_argument")
        );

      return -1;
    }
  ACE_ENDTRY;

  return 0;
}