summaryrefslogtreecommitdiff
path: root/TAO_IDL/be/be_visitor_argument/argument.cpp
blob: 9013b758b4f0b45d2a3ed0073bd65aa9f0060032 (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

//=============================================================================
/**
 *  @file    argument.cpp
 *
 *  $Id$
 *
 *  generic visitor for Argument node
 *
 *
 *  @author Aniruddha Gokhale
 */
//=============================================================================

be_visitor_args::be_visitor_args (be_visitor_context *ctx)
  : be_visitor_decl (ctx),
    fixed_direction_ (-1)
{
}

be_visitor_args::~be_visitor_args (void)
{
}

int be_visitor_args::visit_argument (be_argument *)
{
  return -1;
}

// Helper that returns the type name either as a nested type name (for header
// files) or as a fully scoped name. In addition, we make sure that if the type
// is an alias, we use that name.
const char *
be_visitor_args::type_name (be_type *node,
                            const char *suffix)
{
  static char namebuf [NAMEBUFSIZE];
  ACE_OS::memset (namebuf,
                  '\0',
                  NAMEBUFSIZE);

  be_type *bt = 0;

  // Use the typedefed name if that is the one
  // used in the IDL defn.
  if (this->ctx_->alias ())
    {
      bt = this->ctx_->alias ();
    }
  else
    {
      bt = node;
    }

  ACE_OS::sprintf (namebuf,
                   "::%s",
                   bt->full_name ());

  if (suffix)
    {
      ACE_OS::strcat (namebuf,
                      suffix);
    }

  return namebuf;
}

// Helper that returns the direction type of the argument
AST_Argument::Direction
be_visitor_args::direction (void)
{
  if (this->fixed_direction_ != -1)
    {
      return AST_Argument::Direction (this->fixed_direction_);
    }

  // Grab the argument node. We know that our context has stored the right
  // argument node.
  be_argument *arg =
    be_argument::narrow_from_decl (this->ctx_->node ());

  return arg->direction ();
}

void
be_visitor_args::set_fixed_direction (AST_Argument::Direction direction)
{
  this->fixed_direction_ = direction;
}

int
be_visitor_args::gen_pd_arg (be_predefined_type *node,
                             bool for_stub)
{
  TAO_CodeGen::CG_SUB_STATE ss = this->ctx_->sub_state ();
  AST_Argument::Direction d = this->direction ();
  
  bool in_arg = (d == AST_Argument::dir_IN);
  bool out_arg = (d == AST_Argument::dir_OUT);
  bool out_stream = (ss == TAO_CodeGen::TAO_CDR_OUTPUT);
  bool in_stream = (ss == TAO_CodeGen::TAO_CDR_INPUT);
  
  bool skip = (in_arg && for_stub && in_stream)
              || (in_arg && !for_stub && out_stream)
              || (out_arg && for_stub && out_stream)
              || (out_arg && !for_stub && in_stream);
              
  if (skip)
    {
      return 0;
    }
              
  TAO_OutStream *os = this->ctx_->stream ();
  const char *var_call = "";
  const char *any_deref = "";
    
  AST_PredefinedType::PredefinedType pt = node->pt ();
  bool is_any = (pt == AST_PredefinedType::PT_any);
    
  if (for_stub)
    {
      if (in_stream && out_arg)
        {
          var_call = ".ptr ()";
          any_deref = "*";
        }
    }
  else if (out_stream)
    {
      var_call = ".in ()";
      
      if (is_any && !out_arg)
        {
          var_call = "";
        }
    }
  else if (!is_any)
    {
      var_call = ".out ()";
    }
    
  ACE_CString to_from_str = (in_stream
                             ? "::ACE_InputCDR::to_"
                             : "::ACE_OutputCDR::from_");
                             
  const char *to_from = to_from_str.c_str ();
  
  be_argument *arg =
    be_argument::narrow_from_decl (this->ctx_->node ());
  const char *lname = arg->local_name ()->get_string ();
  
  switch (pt)
  {
    case AST_PredefinedType::PT_any:
      *os << any_deref; // No break.
    case AST_PredefinedType::PT_pseudo:
    case AST_PredefinedType::PT_object:
      *os << lname << var_call;
      break;
    case AST_PredefinedType::PT_char:
      *os << to_from << "char (" << lname << ")";
      break;
    case AST_PredefinedType::PT_wchar:
      *os << to_from << "wchar (" << lname << ")";
      break;
    case AST_PredefinedType::PT_boolean:
      *os << to_from << "boolean (" << lname << ")";
      break;
    case AST_PredefinedType::PT_octet:
      *os << to_from << "octet (" << lname << ")";
      break;
    default:
      *os << lname;
      break;
  }
    
  return 0;
}