summaryrefslogtreecommitdiff
path: root/TAO/TAO_IDL/be/be_codegen.cpp
blob: 9b41fbee13dee9183f5a974000e818ac0a6bbc88 (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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/* -*- c++ -*- */
// ============================================================================
//
// = LIBRARY
//    TAO IDL
//
// = FILENAME
//    be_codegen.h
//
// = DESCRIPTION
//
//   Code generation
// = AUTHOR
//    Aniruddha Gokhale
//
// ============================================================================

#include	"idl.h"
#include	"idl_extern.h"
#include	"be.h"

static const int CHUNK = 100;

/* BE global Data */
TAO_CodeGen::TAO_CodeGen (void)
  : client_header_ (0),
    client_stubs_ (0),
    client_inline_ (0),
    server_header_ (0),
    server_skeletons_ (0),
    server_inline_ (0),
    curr_os_ (0),
    state_ (new TAO_CodeGen::CG_STATE [CHUNK]),
    top_ (0),
    size_ (CHUNK)
{
  // set the current code generation state
  this->state_ [this->top_++] = TAO_CodeGen::TAO_INITIAL;
}

// destructor
TAO_CodeGen::~TAO_CodeGen (void)
{
  delete client_header_;
  delete server_header_;
  delete client_stubs_;
  delete server_skeletons_;
  delete client_inline_;
  delete server_inline_;
  curr_os_ = 0;
  delete [] state_;
}

// factory method
be_state *
TAO_CodeGen::make_state (void)
{
  switch (this->state ())
    {
    case TAO_STRUCT_CH:
    case TAO_STRUCT_CS:
    case TAO_STRUCT_CI:
      return TAO_BE_STATE_STRUCT::instance ();
    case TAO_UNION_DISCTYPEDEFN_CH:
      return TAO_BE_STATE_UNION_DISCTYPEDEFN_CH::instance ();
    case TAO_UNION_DISCTYPEDEFN_CI:
      return TAO_BE_STATE_UNION_DISCTYPEDEFN_CI::instance ();
    case TAO_UNION_PUBLIC_CH:
      return TAO_BE_STATE_UNION_PUBLIC_CH::instance ();
    case TAO_UNION_PUBLIC_CI:
      return TAO_BE_STATE_UNION_PUBLIC_CI::instance ();
    case TAO_UNION_PUBLIC_CS:
    case TAO_UNION_PUBLIC_ASSIGN_CS:
      return TAO_BE_STATE_UNION_PUBLIC_CS::instance ();
    case TAO_UNION_PRIVATE_CH:
      return TAO_BE_STATE_UNION_PRIVATE_CH::instance ();
    case TAO_OPERATION_CH:
    case TAO_OPERATION_RETURN_TYPE_CS:
    case TAO_OPERATION_RETVAL_DECL_CS:
    case TAO_OPERATION_RETVAL_EXCEPTION_CS:
    case TAO_OPERATION_RETVAL_RETURN_CS:
    case TAO_OPERATION_SH:
    case TAO_OPERATION_RETVAL_DECL_SS:
    case TAO_OPERATION_RETVAL_ASSIGN_SS:
    case TAO_OPERATION_RESULT_SS:
      return TAO_BE_STATE_OPERATION::instance ();
    case TAO_ARGUMENT_CH:
    case TAO_ARGUMENT_CS:
    case TAO_ARGUMENT_PRE_DOCALL_CS:
    case TAO_ARGUMENT_DOCALL_CS:
    case TAO_ARGUMENT_POST_DOCALL_CS:
    case TAO_ARGUMENT_SH:
    case TAO_ARGUMENT_SS:
    case TAO_ARGUMENT_VARDECL_SS:
    case TAO_ARGUMENT_PRE_UPCALL_SS:
    case TAO_ARGUMENT_UPCALL_SS:
    case TAO_ARGUMENT_POST_UPCALL_SS:
      return TAO_BE_STATE_ARGUMENT::instance ();
    case TAO_TYPEDEF_CH:
    case TAO_TYPEDEF_CS:
    case TAO_TYPEDEF_CI:
      return TAO_BE_STATE_TYPEDEF::instance ();
    case TAO_ARRAY_DEFN_CH:
    case TAO_ARRAY_OTHER_CH:
    case TAO_ARRAY_DEFN_CI:
      return TAO_BE_STATE_ARRAY::instance ();
    case TAO_SEQUENCE_BASE_CH:
    case TAO_SEQUENCE_BASE_CS:
    case TAO_SEQUENCE_BASE_CI:
    case TAO_SEQUENCE_BODY_CH:
    case TAO_SEQUENCE_BODY_CS:
    case TAO_SEQUENCE_BODY_CI:
    case TAO_SEQELEM_RETTYPE_CH:
    case TAO_SEQELEM_RETTYPE_CI:
    case TAO_SEQELEM_RETTYPE_CS:
      return TAO_BE_STATE_SEQUENCE::instance ();
    case TAO_ATTRIBUTE_RETURN_TYPE_CH:
    case TAO_ATTRIBUTE_INPARAM_TYPE_CH:
    case TAO_ATTRIBUTE_RETURN_TYPE_CS:
    case TAO_ATTRIBUTE_RETVAL_DECL_CS:
    case TAO_ATTRIBUTE_RETVAL_EXCEPTION_CS:
    case TAO_ATTRIBUTE_RETVAL_RETURN_CS:
    case TAO_ATTRIBUTE_INPARAM_TYPE_CS:
    case TAO_ATTRIBUTE_PRE_DOCALL_CS:
    case TAO_ATTRIBUTE_DOCALL_CS:
    case TAO_ATTRIBUTE_POST_DOCALL_CS:
    case TAO_ATTRIBUTE_RETURN_TYPE_SH:
    case TAO_ATTRIBUTE_INPARAM_TYPE_SH:
    case TAO_ATTRIBUTE_RETVAL_DECL_SS:
    case TAO_ATTRIBUTE_RETVAL_ASSIGN_SS:
    case TAO_ATTRIBUTE_RESULT_SS:
    case TAO_ATTRIBUTE_INPARAM_TYPE_SS:
    case TAO_ATTRIBUTE_PRE_UPCALL_SS:
    case TAO_ATTRIBUTE_UPCALL_SS:
    case TAO_ATTRIBUTE_POST_UPCALL_SS:
      return TAO_BE_STATE_ATTRIBUTE::instance ();
    case TAO_EXCEPTION_CH:
    case TAO_EXCEPTION_CTOR_CH:
    case TAO_EXCEPTION_CS:
    case TAO_EXCEPTION_CTOR_CS:
    case TAO_EXCEPTION_CTOR_ASSIGN_CS:
    case TAO_EXCEPTION_CI:
      return TAO_BE_STATE_EXCEPTION::instance ();
    default:
      return 0;
    }
}

// change the string to all upcase
const char *
TAO_CodeGen::upcase (const char *str)
{
  static char upcase_str [NAMEBUFSIZE];

  ACE_OS::memset (upcase_str, '\0', NAMEBUFSIZE);
  // convert letters in str to upcase
  for (unsigned int i=0; i < ACE_OS::strlen (str); i++)
    {
      if (isalpha (str [i]))
        {
          upcase_str[i] = toupper (str [i]);
        }
      else
        {
          // copy it as it is
          upcase_str[i] = str[i];
        }
    }
  return upcase_str;
}

// set the client header stream
int
TAO_CodeGen::client_header (const char *fname)
{
  // retrieve the singleton instance to the outstream factory
  TAO_OutStream_Factory *factory = TAO_OUTSTREAM_FACTORY::instance ();

  // retrieve a specialized instance
  this->client_header_ = factory->make_outstream ();
  if (!this->client_header_)
    {
      return -1;
    }

  if (this->client_header_->open (fname, TAO_OutStream::TAO_CLI_HDR) == -1)
    return -1;
  else
    {
      // now generate the #if !defined clause
      static char macro_name [NAMEBUFSIZE];

      ACE_OS::memset (macro_name, '\0', NAMEBUFSIZE);
      const char *suffix = ACE_OS::strstr (fname, ".h");
      if (suffix == 0)
        return -1; // bad file name
      else
        {
          ACE_OS::sprintf (macro_name, "_TAO_IDL_");
          // convert letters in fname to upcase
          for (int i=0; i < (suffix - fname); i++)
            if (isalpha (fname [i]))
              macro_name[i+9] = toupper (fname [i]);
            else
              macro_name[i+9] = fname[i];

          ACE_OS::strcat (macro_name, "_H_");

          this->client_header_->print ("#if !defined (%s)\n", macro_name);
          this->client_header_->print ("#define %s\n\n", macro_name);
          *this->client_header_ << "#include \"tao/corba.h\"\n";

	  // We must include all the skeleton headers corresponding to
	  // IDL files included by the current IDL file.
	  for (size_t j = 0;
	       j < idl_global->n_include_file_names ();
	       ++j)
	    {
	      String* idl_name =
		idl_global->include_file_names()[j];

	      const char* client_hdr =
		IDL_GlobalData::be_get_client_hdr (idl_name);

	      this->client_header_->print ("#include \"%s\"\n",
					   client_hdr);
	    }
	  *this->client_header_ << "\n";
          return 0;
        }
    }
}

// get the client header stream
TAO_OutStream *
TAO_CodeGen::client_header (void)
{
  return this->client_header_;
}

// set the client stub stream
int
TAO_CodeGen::client_stubs (const char *fname)
{
  // retrieve the singleton instance to the outstream factory
  TAO_OutStream_Factory *factory = TAO_OUTSTREAM_FACTORY::instance ();

  // retrieve a specialized instance
  this->client_stubs_ = factory->make_outstream ();
  if (!this->client_stubs_)
    {
      return -1;
    }

  if (this->client_stubs_->open (fname, TAO_OutStream::TAO_CLI_IMPL) == -1)
    {
      return -1;
    }
  // generate the include statement for the client header
  *this->client_stubs_ << "#include \"" <<
    idl_global->be_get_client_hdr_fname () << "\"\n\n";

  // generate the code that includes the inline file if not included in the
  // header file
  *this->client_stubs_ << "#if !defined (__ACE_INLINE__)\n";
  *this->client_stubs_ << "#include \"" <<
    idl_global->be_get_client_inline_fname () << "\"\n";
  *this->client_stubs_ << "#endif // !defined INLINE\n\n";
  return 0;
}

// get the client stubs stream
TAO_OutStream *
TAO_CodeGen::client_stubs (void)
{
  return this->client_stubs_;
}

// set the client inline stream
int
TAO_CodeGen::client_inline (const char *fname)
{
  // retrieve the singleton instance to the outstream factory
  TAO_OutStream_Factory *factory = TAO_OUTSTREAM_FACTORY::instance ();

  // retrieve a specialized instance
  this->client_inline_ = factory->make_outstream ();
  if (!this->client_inline_)
    {
      return -1;
    }

  return this->client_inline_->open (fname, TAO_OutStream::TAO_CLI_INL);
}

// get the client inline stream
TAO_OutStream *
TAO_CodeGen::client_inline (void)
{
  return this->client_inline_;
}

// set the server header stream
int
TAO_CodeGen::server_header (const char *fname)
{
  // retrieve the singleton instance to the outstream factory
  TAO_OutStream_Factory *factory = TAO_OUTSTREAM_FACTORY::instance ();

  // retrieve a specialized instance
  this->server_header_ = factory->make_outstream ();
  if (!this->server_header_)
    {
      return -1;
    }

  if (this->server_header_->open (fname, TAO_OutStream::TAO_SVR_HDR) == -1)
    return -1;
  else
    {
      // now generate the #if !defined clause
      static char macro_name [NAMEBUFSIZE];

      ACE_OS::memset (macro_name, '\0', NAMEBUFSIZE);
      const char *suffix = ACE_OS::strstr (fname, ".h");
      if (suffix == 0)
	return -1; // bad file name
      else
	{
          ACE_OS::sprintf (macro_name, "_TAO_IDL_");
          // convert letters in fname to upcase
          for (int i=0; i < (suffix - fname); i++)
            {
              if (isalpha (fname [i]))
                {
                  macro_name[i+9] = toupper (fname [i]);
                }
            }
          ACE_OS::strcat (macro_name, "_H_");

          this->server_header_->print ("#if !defined (%s)\n", macro_name);
          this->server_header_->print ("#define %s\n\n", macro_name);

	  // We must include all the skeleton headers corresponding to
	  // IDL files included by the current IDL file.
	  for (size_t j = 0;
	       j < idl_global->n_include_file_names ();
	       ++j)
	    {
	      String* idl_name =
		idl_global->include_file_names()[j];

	      const char* server_hdr =
		IDL_GlobalData::be_get_server_hdr (idl_name);

	      this->server_header_->print ("#include \"%s\"\n",
					   server_hdr);
	    }
          // the server header should include the client header
          *this->server_header_ << "#include \"" <<
            idl_global->be_get_client_hdr_fname () << "\"\n\n";

          return 0;
        }
    }
}

// get the server header stream
TAO_OutStream *
TAO_CodeGen::server_header (void)
{
  return this->server_header_;
}

// set the server skeletons stream
int
TAO_CodeGen::server_skeletons (const char *fname)
{
  // retrieve the singleton instance to the outstream factory
  TAO_OutStream_Factory *factory = TAO_OUTSTREAM_FACTORY::instance ();

  // retrieve a specialized instance
  this->server_skeletons_ = factory->make_outstream ();
  if (!this->server_skeletons_)
    {
      return -1;
    }

  if (this->server_skeletons_->open (fname, TAO_OutStream::TAO_SVR_IMPL) == -1)
    {
      return -1;
    }

  // generate the include statement for the server header
  *this->server_skeletons_ << "#include \"" <<
    idl_global->be_get_server_hdr_fname () << "\"\n\n";

  // generate the code that includes the inline file if not included in the
  // header file
  *this->server_skeletons_ << "#if !defined (__ACE_INLINE__)\n";
  *this->server_skeletons_ << "#include \"" <<
    idl_global->be_get_server_inline_fname () << "\"\n";
  *this->server_skeletons_ << "#endif // !defined INLINE\n\n";
  return 0;
}

// get the server skeletons stream
TAO_OutStream *
TAO_CodeGen::server_skeletons (void)
{
  return this->server_skeletons_;
}

// set the server inline stream
int
TAO_CodeGen::server_inline (const char *fname)
{
  // retrieve the singleton instance to the outstream factory
  TAO_OutStream_Factory *factory = TAO_OUTSTREAM_FACTORY::instance ();

  // retrieve a specialized instance
  this->server_inline_ = factory->make_outstream ();
  if (!this->server_inline_)
    {
      return -1;
    }

  return this->server_inline_->open (fname, TAO_OutStream::TAO_SVR_INL);
}

// get the server inline stream
TAO_OutStream *
TAO_CodeGen::server_inline (void)
{
  return this->server_inline_;
}

// put the last #endif in the client and server headers
int
TAO_CodeGen::end_client_header (void)
{
  // insert the code to include the inline file
  *this->client_header_ << "\n#if defined (__ACE_INLINE__)\n";
  *this->client_header_ << "#include \"" <<
    idl_global->be_get_client_inline_fname () << "\"\n";
  *this->client_header_ << "#endif // defined INLINE\n\n";

  // code to put the last #endif
  *this->client_header_ << "\n#endif // if !defined\n";
  return 0;
}

int
TAO_CodeGen::end_server_header (void)
{
  // insert the code to include the inline file
  *this->server_header_ << "\n#if defined (__ACE_INLINE__)\n";
  *this->server_header_ << "#include \"" <<
    idl_global->be_get_server_inline_fname () << "\"\n";
  *this->server_header_ << "#endif // defined INLINE\n\n";

  // code to put the last #endif
  *this->server_header_ << "\n#endif // if !defined\n";
  return 0;
}

// We use the following helper functions to pass information. This class is the
// best place to pass such information rather than passing information through
// global variables spread everywhere. This class is a singleton and is
// effectively a global.

void
TAO_CodeGen::outstream (TAO_OutStream *os)
{
  this->curr_os_ = os;
}

TAO_OutStream *
TAO_CodeGen::outstream (void)
{
  return this->curr_os_;
}

void
TAO_CodeGen::push (TAO_CodeGen::CG_STATE s)
{
  if (this->top_ == this->size_)
    {
      TAO_CodeGen::CG_STATE *temp = this->state_;
      this->size_ += CHUNK;
      this->state_ = new TAO_CodeGen::CG_STATE [this->size_];
      for (int i=0; i < this->top_; i++)
        this->state_ [i] = temp [i];
      delete []temp;
    }
  this->state_[this->top_++] = s;
}

void
TAO_CodeGen::pop (void)
{
  this->top_--;
}

TAO_CodeGen::CG_STATE
TAO_CodeGen::state (void)
{
  return this->state_[this->top_ - 1]; // top points to the next free slot
}

void
TAO_CodeGen::reset (void)
{
  this->top_ = 1; // the 0th posn is always the INITIAL state
}

void
TAO_CodeGen::node (be_decl *n)
{
  this->node_ = n;
}

be_decl *
TAO_CodeGen::node (void)
{
  return this->node_;
}