summaryrefslogtreecommitdiff
path: root/TAO/TAO_IDL/be/be_visitor_array/array_ci.cpp
blob: 2ffa66396f25e0afb139c6c9d1e3d666f449d7b4 (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
// ============================================================================
//
// = LIBRARY
//    TAO IDL
//
// = FILENAME
//    array_ci.cpp
//
// = DESCRIPTION
//    Visitor generating code for Arrays in the client inline.
//
// = AUTHOR
//    Aniruddha Gokhale
//
// ============================================================================

ACE_RCSID (be_visitor_array,
           array_ci,
           "$Id$")


// ************************************************************************
//  visitor for array declaration in client inline
// ************************************************************************

be_visitor_array_ci::be_visitor_array_ci (be_visitor_context *ctx)
  : be_visitor_array (ctx)
{
}

be_visitor_array_ci::~be_visitor_array_ci (void)
{
}

int be_visitor_array_ci::visit_array (be_array *node)
{
  // Nothing to do if we are imported or code is already generated.
  if (node->imported () || node->cli_inline_gen ())
    {
      return 0;
    }

  this->ctx_->node (node); // save the array node

  // If we contain an anonymous sequence, generate code for it here.
  be_type *bt = be_type::narrow_from_decl (node->base_type ());
  AST_Decl::NodeType nt = bt->node_type ();

  if (!bt)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%N:%l) be_visitor_array_ci::"
                         "visit_array - "
                         "bad base type\n"),
                        -1);
    }

  if (nt == AST_Decl::NT_sequence)
    {
      if (this->gen_anonymous_base_type (bt,
                                         TAO_CodeGen::TAO_ROOT_CI)
          == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_visitor_array_ci::"
                             "visit_array - "
                             "gen_anonymous_base_type failed\n"),
                            -1);
        }
    }

  // If the array is an anonymous member and if its element type
  // is a declaration (not a reference), we must generate code for
  // the declaration.
  if (this->ctx_->alias () == 0 // Not a typedef.
      && bt->is_child (this->ctx_->scope ()))
    {
      int status = 0;
      be_visitor_context ctx (*this->ctx_);

      switch (nt)
      {
        case AST_Decl::NT_struct:
          {
            be_visitor_structure_ci sc_visitor (&ctx);
            status = bt->accept (&sc_visitor);
            break;
          }
        case AST_Decl::NT_union:
          {
            be_visitor_union_ci uc_visitor (&ctx);
            status = bt->accept (&uc_visitor);
            break;
          }
        default:
          break;
      }

      if (status == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_visitor_array_ch::"
                             "visit_array - "
                             "array base type codegen failed\n"),
                            -1);
        }
    }

  // To hold the full and local.
  char fname [NAMEBUFSIZE];
  char lname [NAMEBUFSIZE];
  ACE_OS::memset (fname,
                  '\0',
                  NAMEBUFSIZE);
  ACE_OS::memset (lname,
                  '\0',
                  NAMEBUFSIZE);

  if (this->ctx_->tdef ())
    {
      // Typedefed node.
      ACE_OS::sprintf (fname, "%s",
                       node->full_name ());
      ACE_OS::sprintf (lname, "%s",
                       node->local_name ()->get_string ());
    }
  else
    {
      // For anonymous arrays ...
      // we have to generate a name for us that has an underscope prepended to
      // our local name. This needs to be inserted after the parents's name.
      if (node->is_nested ())
        {
          be_decl *parent =
            be_scope::narrow_from_scope (node->defined_in ())->decl ();
          ACE_OS::sprintf (fname,
                           "%s::_%s",
                           parent->full_name (),
                           node->local_name ()->get_string ());
          ACE_OS::sprintf (lname,
                           "_%s",
                           node->local_name ()->get_string ());
        }
      else
        {
          ACE_OS::sprintf (fname,
                           "_%s",
                           node->full_name ());
          ACE_OS::sprintf (lname,
                           "_%s",
                           node->local_name ()->get_string ());
        }
    }

  TAO_OutStream *os = this->ctx_->stream ();

  *os << be_nl << be_nl << "// TAO_IDL - Generated from" << be_nl
      << "// " << __FILE__ << ":" << __LINE__;

  // Generate the array traits specialization definitions,
  // guarded by #ifdef on unaliased array element type and length.

  ACE_CString unique;

  if (nt == AST_Decl::NT_typedef)
    {
      be_typedef *td = be_typedef::narrow_from_decl (bt);
      unique = td->primitive_base_type ()->flat_name ();
    }
  else
    {
      unique = bt->flat_name ();
    }

  char buf[NAMEBUFSIZE];

  for (unsigned long i = 0; i < node->n_dims (); ++i)
    {
      ACE_OS::memset (buf,
                      '\0',
                      NAMEBUFSIZE);
      ACE_OS::sprintf (buf,
                       "_%ld",
                       node->dims ()[i]->ev ()->u.ulval);
      unique += buf;
    }

  unique += "_traits";

  *os << be_nl << be_nl
      << "ACE_TEMPLATE_CLASS_MEMBER_SPECIALIZATION" << be_nl
      << "ACE_INLINE" << be_nl
      << "void" << be_nl
      << "TAO::Array_Traits<" << be_idt << be_idt_nl
      << fname << "," << be_nl
      << fname << "_slice," << be_nl
      << fname << "_tag" << be_uidt_nl
      << ">::tao_free ("<< be_idt_nl
      << fname << "_slice * _tao_slice" << be_uidt_nl
      << ")" << be_uidt_nl
      << "{" << be_idt_nl
      << fname << "_free (_tao_slice);" << be_uidt_nl
      << "}";

  *os << be_nl << be_nl
      << "ACE_TEMPLATE_CLASS_MEMBER_SPECIALIZATION" << be_nl
      << "ACE_INLINE" << be_nl
      << fname << "_slice *" << be_nl
      << "TAO::Array_Traits<" << be_idt << be_idt_nl
      << fname << "," << be_nl
      << fname << "_slice," << be_nl
      << fname << "_tag" << be_uidt_nl
      << ">::tao_dup (" << be_idt_nl
      << "const " << fname << "_slice * _tao_slice" << be_uidt_nl
      << ")" << be_uidt_nl
      << "{" << be_idt_nl
      << "return " << fname << "_dup (_tao_slice);" << be_uidt_nl
      << "}";

  *os << be_nl << be_nl
      << "ACE_TEMPLATE_CLASS_MEMBER_SPECIALIZATION" << be_nl
      << "ACE_INLINE" << be_nl
      << "void" << be_nl
      << "TAO::Array_Traits<" << be_idt << be_idt_nl
      << fname << "," << be_nl
      << fname << "_slice," << be_nl
      << fname << "_tag" << be_uidt_nl
      << ">::tao_copy (" << be_idt_nl
      << fname << "_slice * _tao_to," << be_nl
      << "const " << fname << "_slice * _tao_from" << be_uidt_nl
      << ")" << be_uidt_nl
      << "{" << be_idt_nl
      << fname << "_copy (_tao_to, _tao_from);" << be_uidt_nl
      << "}";

  *os << be_nl << be_nl
      << "ACE_TEMPLATE_CLASS_MEMBER_SPECIALIZATION" << be_nl
      << "ACE_INLINE" << be_nl
      << fname << "_slice *" << be_nl
      << "TAO::Array_Traits<" << be_idt << be_idt_nl
      << fname << "," << be_nl
      << fname << "_slice," << be_nl
      << fname << "_tag" << be_uidt_nl
      << ">::tao_alloc (void)" << be_uidt_nl
      << "{" << be_idt_nl
      << "return " << fname << "_alloc ();" << be_uidt_nl
      << "}";

  *os << be_nl;

  node->cli_inline_gen (I_TRUE);
  return 0;
}