Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / TAO_IDL / ast / ast_operation.cpp
blob8a787e2168a8ef044e0e2f9626d14d3edeedc5e7
1 /*
3 COPYRIGHT
5 Copyright 1992, 1993, 1994 Sun Microsystems, Inc. Printed in the United
6 States of America. All Rights Reserved.
8 This product is protected by copyright and distributed under the following
9 license restricting its use.
11 The Interface Definition Language Compiler Front End (CFE) is made
12 available for your use provided that you include this license and copyright
13 notice on all media and documentation and the software program in which
14 this product is incorporated in whole or part. You may copy and extend
15 functionality (but may not remove functionality) of the Interface
16 Definition Language CFE without charge, but you are not authorized to
17 license or distribute it to anyone else except as part of a product or
18 program developed by you or with the express written consent of Sun
19 Microsystems, Inc. ("Sun").
21 The names of Sun Microsystems, Inc. and any of its subsidiaries or
22 affiliates may not be used in advertising or publicity pertaining to
23 distribution of Interface Definition Language CFE as permitted herein.
25 This license is effective until terminated by Sun for failure to comply
26 with this license. Upon termination, you shall destroy or return all code
27 and documentation for the Interface Definition Language CFE.
29 INTERFACE DEFINITION LANGUAGE CFE IS PROVIDED AS IS WITH NO WARRANTIES OF
30 ANY KIND INCLUDING THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS
31 FOR A PARTICULAR PURPOSE, NONINFRINGEMENT, OR ARISING FROM A COURSE OF
32 DEALING, USAGE OR TRADE PRACTICE.
34 INTERFACE DEFINITION LANGUAGE CFE IS PROVIDED WITH NO SUPPORT AND WITHOUT
35 ANY OBLIGATION ON THE PART OF Sun OR ANY OF ITS SUBSIDIARIES OR AFFILIATES
36 TO ASSIST IN ITS USE, CORRECTION, MODIFICATION OR ENHANCEMENT.
38 SUN OR ANY OF ITS SUBSIDIARIES OR AFFILIATES SHALL HAVE NO LIABILITY WITH
39 RESPECT TO THE INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY
40 INTERFACE DEFINITION LANGUAGE CFE OR ANY PART THEREOF.
42 IN NO EVENT WILL SUN OR ANY OF ITS SUBSIDIARIES OR AFFILIATES BE LIABLE FOR
43 ANY LOST REVENUE OR PROFITS OR OTHER SPECIAL, INDIRECT AND CONSEQUENTIAL
44 DAMAGES, EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
46 Use, duplication, or disclosure by the government is subject to
47 restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
48 Technical Data and Computer Software clause at DFARS 252.227-7013 and FAR
49 52.227-19.
51 Sun, Sun Microsystems and the Sun logo are trademarks or registered
52 trademarks of Sun Microsystems, Inc.
54 SunSoft, Inc.
55 2550 Garcia Avenue
56 Mountain View, California 94043
58 NOTE:
60 SunOS, SunSoft, Sun, Solaris, Sun Microsystems or the Sun logo are
61 trademarks or registered trademarks of Sun Microsystems, Inc.
65 // AST_Operation nodes denote IDL operation declarations
66 // AST_Operations are a subclass of AST_Decl (they are not a type!)
67 // and of UTL_Scope (the arguments are managed in a scope).
68 // AST_Operations have a return type (a subclass of AST_Type),
69 // a bitfield for denoting various properties of the operation (the
70 // values are ORed together from constants defined in the enum
71 // AST_Operation::FLags), a name (a UTL_ScopedName), a context
72 // (implemented as a list of Strings, a UTL_StrList), and a raises
73 // clause (implemented as an array of AST_Exceptions).
75 #include "ast_operation.h"
76 #include "ast_predefined_type.h"
77 #include "ast_argument.h"
78 #include "ast_exception.h"
79 #include "ast_param_holder.h"
80 #include "ast_typedef.h"
81 #include "ast_visitor.h"
83 #include "utl_err.h"
84 #include "utl_namelist.h"
85 #include "utl_exceptlist.h"
86 #include "utl_identifier.h"
87 #include "utl_string.h"
88 #include "utl_strlist.h"
90 #include "global_extern.h"
92 AST_Decl::NodeType const
93 AST_Operation::NT = AST_Decl::NT_op;
95 AST_Operation::AST_Operation (AST_Type *rt,
96 Flags fl,
97 UTL_ScopedName *n,
98 bool local,
99 bool abstract)
100 : COMMON_Base (local,
101 abstract),
102 AST_Decl(AST_Decl::NT_op,
104 UTL_Scope(AST_Decl::NT_op),
105 pd_return_type (rt),
106 pd_flags (fl),
107 pd_context (0),
108 pd_exceptions (0),
109 argument_count_ (-1),
110 has_in_arguments_ (false),
111 has_native_ (0)
113 AST_PredefinedType *pdt = 0;
115 // Check that if the operation is oneway, the return type must be void.
116 if (rt != 0 && pd_flags == OP_oneway)
118 if (rt->node_type () != AST_Decl::NT_pre_defined)
120 idl_global->err ()->error1 (UTL_Error::EIDL_NONVOID_ONEWAY,
121 this);
123 else
125 pdt = dynamic_cast<AST_PredefinedType*> (rt);
127 if (pdt == 0 || pdt->pt () != AST_PredefinedType::PT_void)
129 idl_global->err ()->error1 (UTL_Error::EIDL_NONVOID_ONEWAY,
130 this);
136 AST_Operation::~AST_Operation (void)
140 // Public operations.
142 bool
143 AST_Operation::void_return_type (void)
145 AST_Type* type = this->return_type ();
147 return (type->node_type () == AST_Decl::NT_pre_defined
148 && (dynamic_cast<AST_PredefinedType*> (type)->pt ()
149 == AST_PredefinedType::PT_void));
152 // Return the member count.
154 AST_Operation::argument_count (void)
156 this->compute_argument_attr ();
158 return this->argument_count_;
161 // Return the IN/INOUT member flag.
162 bool
163 AST_Operation::has_in_arguments (void)
165 this->compute_argument_attr ();
167 return this->has_in_arguments_;
171 AST_Operation::count_arguments_with_direction (int direction_mask)
173 int count = 0;
175 for (UTL_ScopeActiveIterator si (this, UTL_Scope::IK_decls);
176 !si.is_done ();
177 si.next ())
179 AST_Argument *arg = dynamic_cast<AST_Argument*> (si.item ());
181 if ((arg->direction () & direction_mask) != 0)
183 ++count;
187 return count;
191 // Return if any argument or the return type is a <native> type.
193 AST_Operation::has_native (void)
195 this->compute_argument_attr ();
197 return this->has_native_;
200 void
201 AST_Operation::destroy (void)
203 // No need to delete our exception list, the
204 // destroy() method does it. The UTL_ExceptList
205 // destroy() method does NOT delete the contained
206 // exception nodes.
208 if (this->pd_exceptions != 0)
210 this->pd_exceptions->destroy ();
211 this->pd_exceptions = 0;
214 this->UTL_Scope::destroy ();
215 this->AST_Decl::destroy ();
218 UTL_ExceptList *
219 AST_Operation::be_add_exceptions (UTL_ExceptList *t)
221 if (this->pd_exceptions != 0)
223 idl_global->err ()->error1 (UTL_Error::EIDL_ILLEGAL_RAISES,
224 this);
226 else
228 this->pd_exceptions = t;
231 return this->pd_exceptions;
234 // Private operations.
236 // Compute total number of members.
238 AST_Operation::compute_argument_attr (void)
240 if (this->argument_count_ != -1)
242 return 0;
245 AST_Decl *d = 0;
246 AST_Type *type = 0;
247 AST_Argument *arg = 0;
249 this->argument_count_ = 0;
251 // If there are elements in this scope.
252 if (this->nmembers () > 0)
254 // Instantiate a scope iterator.
255 for (UTL_ScopeActiveIterator si (this, UTL_Scope::IK_decls);
256 !si.is_done ();
257 si.next ())
259 // Get the next AST decl node.
260 d = si.item ();
262 if (d->node_type () == AST_Decl::NT_argument)
264 this->argument_count_++;
266 arg = dynamic_cast<AST_Argument*> (d);
268 if (arg->direction() == AST_Argument::dir_IN ||
269 arg->direction() == AST_Argument::dir_INOUT)
271 this->has_in_arguments_ = true;
275 type = dynamic_cast<AST_Type*> (arg->field_type ());
277 if (type->node_type () == AST_Decl::NT_native)
279 this->has_native_ = 1;
285 type = dynamic_cast<AST_Type*> (this->return_type ());
287 if (type->node_type () == AST_Decl::NT_native)
289 this->has_native_ = 1;
292 return 0;
295 // Add this context (a UTL_StrList) to this scope.
296 UTL_StrList *
297 AST_Operation::fe_add_context (UTL_StrList *t)
299 this->pd_context = t;
301 return t;
304 // Add these exceptions (identified by name) to this scope.
305 // This looks up each name to resolve it to the name of a known
306 // exception, and then adds the referenced exception to the list
307 // exceptions that this operation can raise.
309 // NOTE: No attempt is made to ensure that exceptions are mentioned
310 // only once..
311 UTL_NameList *
312 AST_Operation::fe_add_exceptions (UTL_NameList *t)
314 if (0 == t)
316 return 0;
319 UTL_ScopedName *nl_n = 0;
320 AST_Type *fe = 0;
321 AST_Decl *d = 0;
323 this->pd_exceptions = 0;
325 for (UTL_NamelistActiveIterator nl_i (t); !nl_i.is_done (); nl_i.next ())
327 nl_n = nl_i.item ();
328 d = this->lookup_by_name (nl_n, true);
330 if (d == 0)
332 idl_global->err ()->lookup_error (nl_n);
333 return 0;
336 AST_Decl::NodeType nt = d->node_type ();
338 switch (nt)
340 case AST_Decl::NT_except:
341 break;
342 case AST_Decl::NT_param_holder:
344 AST_Param_Holder *ph = dynamic_cast<AST_Param_Holder*> (d);
346 nt = ph->info ()->type_;
348 if (nt != AST_Decl::NT_except
349 && nt != AST_Decl::NT_type)
351 idl_global->err ()->mismatched_template_param (
352 ph->info ()->name_.c_str ());
355 break;
357 case AST_Decl::NT_typedef:
359 AST_Typedef *td = dynamic_cast<AST_Typedef*> (d);
361 nt = td->primitive_base_type ()->node_type ();
363 if (nt != AST_Decl::NT_except)
365 idl_global->err ()->error1 (
366 UTL_Error::EIDL_ILLEGAL_RAISES,
367 this);
370 break;
372 case AST_Decl::NT_native:
374 // This is the only use case for this node type.
375 int compare =
376 ACE_OS::strcmp (d->local_name ()->get_string (),
377 "UserExceptionBase");
379 if (compare != 0)
381 idl_global->err ()->error1 (
382 UTL_Error::EIDL_ILLEGAL_RAISES,
383 this);
386 break;
388 default:
389 idl_global->err ()->error1 (
390 UTL_Error::EIDL_ILLEGAL_RAISES,
391 this);
393 break;
396 bool oneway_op =
397 (this->flags () == AST_Operation::OP_oneway);
399 fe = dynamic_cast<AST_Type*> (d);
401 if (oneway_op && fe != 0)
403 idl_global->err ()->error1 (UTL_Error::EIDL_ILLEGAL_RAISES,
404 this);
407 if (fe == 0)
409 idl_global->err ()->error1 (UTL_Error::EIDL_ILLEGAL_RAISES,
410 this);
411 return 0;
414 if (this->pd_exceptions == 0)
416 ACE_NEW_RETURN (this->pd_exceptions,
417 UTL_ExceptList (fe,
421 else
423 UTL_ExceptList *el = 0;
424 ACE_NEW_RETURN (el,
425 UTL_ExceptList (fe,
429 this->pd_exceptions->nconc (el);
433 // This return value is never used, it's easier to
434 // destroy it here and return 0 than to destroy it
435 // each place it is passed in.
436 t->destroy ();
437 delete t;
438 t = 0;
439 return 0;
442 AST_Argument *
443 AST_Operation::fe_add_argument (AST_Argument *t)
445 return dynamic_cast<AST_Argument*> (this->fe_add_decl (t));
448 // Dump this AST_Operation node (an operation) to the ostream o.
449 void
450 AST_Operation::dump (ACE_OSTREAM_TYPE &o)
452 AST_Decl *d = 0;
453 AST_Type *e = 0;
454 UTL_String *s = 0;
456 if (this->pd_flags == OP_oneway)
458 this->dump_i (o, "oneway ");
460 else if (this->pd_flags == OP_idempotent)
462 this->dump_i (o, "idempotent ");
465 this->pd_return_type->name ()->dump (o);
466 this->dump_i (o, " ");
467 this->local_name ()->dump (o);
468 this->dump_i (o, "(");
470 // Must advance the iterator explicity inside the loop.
471 for (UTL_ScopeActiveIterator i (this, IK_decls); !i.is_done ();)
473 d = i.item ();
474 d->dump (o);
475 i.next ();
477 if (!i.is_done())
479 this->dump_i (o, ", ");
483 this->dump_i (o, ")");
485 if (this->pd_exceptions != 0)
487 this->dump_i (o, " raises(");
489 // Must advance the iterator explicity inside the loop.
490 for (UTL_ExceptlistActiveIterator ei (this->pd_exceptions);
491 !ei.is_done ();)
493 e = ei.item ();
494 ei.next ();
495 e->local_name ()->dump (o);
497 if (!ei.is_done())
499 this->dump_i (o, ", ");
503 this->dump_i (o, ")");
506 if (this->pd_context != 0)
508 this->dump_i (o, " context(");
510 // Must advance the iterator explicity inside the loop.
511 for (UTL_StrlistActiveIterator si (this->pd_context); !si.is_done();)
513 s = si.item ();
514 si.next ();
515 this->dump_i (o, s->get_string ());
517 if (!si.is_done())
519 this->dump_i (o, ", ");
523 this->dump_i (o, ")");
528 AST_Operation::ast_accept (ast_visitor *visitor)
530 return visitor->visit_operation (this);
533 // Data accessors
535 AST_Type *
536 AST_Operation::return_type (void)
538 return this->pd_return_type;
541 AST_Operation::Flags
542 AST_Operation::flags (void)
544 return this->pd_flags;
547 UTL_StrList *
548 AST_Operation::context (void)
550 return this->pd_context;
553 UTL_ExceptList *
554 AST_Operation::exceptions (void)
556 return this->pd_exceptions;
559 bool
560 AST_Operation::annotatable () const
562 return true;
565 IMPL_NARROW_FROM_DECL(AST_Operation)
566 IMPL_NARROW_FROM_SCOPE(AST_Operation)