~ubuntu-branches/ubuntu/maverick/aspectc++/maverick

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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
// This file is part of PUMA.
// Copyright (C) 1999-2003  The PUMA developer team.
//                                                                
// This program is free software;  you can redistribute it and/or 
// modify it under the terms of the GNU General Public License as 
// published by the Free Software Foundation; either version 2 of 
// the License, or (at your option) any later version.            
//                                                                
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  
// GNU General Public License for more details.                   
//                                                                
// You should have received a copy of the GNU General Public      
// License along with this program; if not, write to the Free     
// Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
// MA  02111-1307  USA                                            

#include "Puma/CAttributeInfo.h"
#include "Puma/CFunctionInfo.h"
#include "Puma/CArgumentInfo.h"
#include "Puma/CTypedefInfo.h"
#include "Puma/CWStrLiteral.h"
#include "Puma/CStrLiteral.h"
#include "Puma/CObjectInfo.h"
#include "Puma/CSemVisitor.h"
#include "Puma/CLocalScope.h"
#include "Puma/CUnionInfo.h"
#include "Puma/CStructure.h"
#include "Puma/CClassInfo.h"
#include "Puma/CScopeInfo.h"
#include "Puma/CEnumInfo.h"
#include "Puma/CFileInfo.h"
#include "Puma/CTypeInfo.h"
#include "Puma/CConstant.h"
#include "Puma/CSemExpr.h"
#include "Puma/Limits.h"
#include "Puma/CTree.h"

namespace Puma {


// print semantic error messages

#define SEM_MSG(node__,mesg__) \
  err << node__->token ()->location () << mesg__ << endMessage
  
#define SEM_ERROR(node__,mesg__) \
  SEM_MSG (node__, sev_error << mesg__)

#define SEM_WARNING(node__,mesg__) \
  SEM_MSG (node__, sev_warning << mesg__)


CSemVisitor::CSemVisitor (ErrorSink &e, CStructure *scope) : err (e) {
  current_scope = scope;
}

CTypeInfo *CSemVisitor::resolveExpr (CTree *expr, CTree *node) const {
  CSemExpr cse (err, current_scope);
  return cse.resolveExpr (expr, node);
}

void CSemVisitor::resolveInit (CObjectInfo *info, CTree *expr,
                                     CTree *node) const {
  CSemExpr cse (err, current_scope);
  cse.resolveInit (info, expr, node);
}

void CSemVisitor::run (CTree *node, CStructure *scope) {
  current_scope = scope;
  if (! current_scope && node->NodeName () == CT_Program::NodeId ())
    current_scope = ((CT_Program*)node)->Scope ();
  if (current_scope)
    visit (node);
}

void CSemVisitor::pre_visit (CTree *node) {
  if (! node) 
    return;

  const char *id = node->NodeName ();
  if (id == CT_Program::NodeId ())
    pre_action ((CT_Program*)node);
  else if (id == CT_ArgDeclList::NodeId ())
    pre_action ((CT_ArgDeclList*)node);
  else if (id == CT_ArgNameList::NodeId ())
    pre_action ((CT_ArgNameList*)node);
  else if (id == CT_ArgDeclSeq::NodeId ())
    pre_action ((CT_ArgDeclSeq*)node);
  else if (id == CT_MembList::NodeId ())
    pre_action ((CT_MembList*)node);
  else if (id == CT_CmpdStmt::NodeId ())
    pre_action ((CT_CmpdStmt*)node);
  else if (id == CT_IfStmt::NodeId ())
    pre_action ((CT_IfStmt*)node);
  else if (id == CT_IfElseStmt::NodeId ())
    pre_action ((CT_IfElseStmt*)node);
  else if (id == CT_SwitchStmt::NodeId ())
    pre_action ((CT_SwitchStmt*)node);
  else if (id == CT_ForStmt::NodeId ())
    pre_action ((CT_ForStmt*)node);
  else if (id == CT_WhileStmt::NodeId ())
    pre_action ((CT_WhileStmt*)node);
  else if (id == CT_DoStmt::NodeId ())
    pre_action ((CT_DoStmt*)node);
  else if (id == CT_ExprStmt::NodeId ())
    pre_action ((CT_ExprStmt*)node);
  else if (id == CT_CaseStmt::NodeId ())
    pre_action ((CT_CaseStmt*)node);
  else if (id == CT_GotoStmt::NodeId ())
    pre_action ((CT_GotoStmt*)node);
  else if (id == CT_ReturnStmt::NodeId ())
    pre_action ((CT_ReturnStmt*)node);
  else if (id == CT_InitDeclarator::NodeId ())
    pre_action ((CT_InitDeclarator*)node);
  else if (id == CT_ArrayDeclarator::NodeId ())
    pre_action ((CT_ArrayDeclarator*)node);
  else if (id == CT_BitFieldDeclarator::NodeId ())
    pre_action ((CT_BitFieldDeclarator*)node);
  else if (id == CT_EnumDef::NodeId ())
    pre_action ((CT_EnumDef*)node);
  else if (id == CT_CmpdLiteral::NodeId ())
    pre_action ((CT_CmpdLiteral*)node);
  else if (id == CT_ExprList::NodeId ())
    pre_action ((CT_ExprList*)node);
}

void CSemVisitor::post_visit (CTree *node) {
  if (! node) 
    return;

  const char *id = node->NodeName ();
  if (id == CT_Program::NodeId ())
    post_action ((CT_Program*)node);
  else if (id == CT_ArgDeclList::NodeId ())
    post_action ((CT_ArgDeclList*)node);
  else if (id == CT_ArgNameList::NodeId ())
    post_action ((CT_ArgNameList*)node);
  else if (id == CT_ArgDeclSeq::NodeId ())
    post_action ((CT_ArgDeclSeq*)node);
  else if (id == CT_MembList::NodeId ())
    post_action ((CT_MembList*)node);
  else if (id == CT_CmpdStmt::NodeId ())
    post_action ((CT_CmpdStmt*)node);
  else if (id == CT_IfStmt::NodeId ())
    post_action ((CT_IfStmt*)node);
  else if (id == CT_IfElseStmt::NodeId ())
    post_action ((CT_IfElseStmt*)node);
  else if (id == CT_SwitchStmt::NodeId ())
    post_action ((CT_SwitchStmt*)node);
  else if (id == CT_ForStmt::NodeId ())
    post_action ((CT_ForStmt*)node);
  else if (id == CT_WhileStmt::NodeId ())
    post_action ((CT_WhileStmt*)node);
}



///////////////////////////////////////////////////////
// pre_action functions ///////////////////////////////
///////////////////////////////////////////////////////



void CSemVisitor::pre_action (CT_Program *node) {
  current_scope = node->Scope ();
}

void CSemVisitor::pre_action (CT_ArgDeclList *node) {
  current_scope = node->Scope ();
}

void CSemVisitor::pre_action (CT_ArgNameList *node) {
  current_scope = node->Scope ();
}

void CSemVisitor::pre_action (CT_ArgDeclSeq *node) {
  current_scope = node->Scope ();
}

void CSemVisitor::pre_action (CT_MembList *node) {
  current_scope = node->Scope ();
}

void CSemVisitor::pre_action (CT_CmpdStmt *node) {
  current_scope = node->Scope ();
}

void CSemVisitor::pre_action (CT_IfStmt *node) {
  CTypeInfo *type;
  CTree *expr;
  
  current_scope = node->Scope ();
  
  expr = node->Condition ();
  if (expr) {
    type = resolveExpr (expr, node);
    if (! type->isScalar ())
      SEM_ERROR (node, "controlling expression of `if' statement not scalar");
  }
}

void CSemVisitor::pre_action (CT_IfElseStmt *node) {
  CTypeInfo *type;
  CTree *expr;
  
  current_scope = node->Scope ();
  
  expr = node->Condition ();
  if (expr) {
    type = resolveExpr (expr, node);
    if (! type->isScalar ())
      SEM_ERROR (node, "controlling expression of `if' statement not scalar");
  }
}

void CSemVisitor::pre_action (CT_SwitchStmt *node) {
  CTypeInfo *type;
  CTree *expr;
  
  current_scope = node->Scope ();
  
  expr = node->Condition ();
  if (expr) {
    type = resolveExpr (expr, node);
    type = CSemExpr::promote (node->Condition (), node);
    if (! type->isInteger ())
      SEM_ERROR (node, 
        "controlling expression of `switch' statement not an integer");
  }
}

void CSemVisitor::pre_action (CT_ForStmt *node) {
  CTypeInfo *type;
  CTree *expr;
  
  current_scope = node->Scope ();
  
  expr = node->Condition ();
  if (expr) {
    type = resolveExpr (expr, node);
    if (! type->isScalar ())
      SEM_ERROR (node, "controlling expression of `for' statement not scalar");
  }
  expr = node->Expr ();
  if (expr)
    // there are no further constraints on this expression
    resolveExpr (expr, node); 
}

void CSemVisitor::pre_action (CT_WhileStmt *node) {
  CTypeInfo *type;
  CTree *expr;
  
  current_scope = node->Scope ();
  
  expr = node->Condition ();
  if (expr) {
    type = resolveExpr (expr, node);
    if (! type->isScalar ())
      SEM_ERROR (node, "controlling expression of `while' statement not scalar");
  }
}

void CSemVisitor::pre_action (CT_DoStmt *node) {
  CTypeInfo *type;
  CTree *expr;
  
  expr = node->Expr ();
  if (expr) {
    type = resolveExpr (expr, node);
    if (! type->isScalar ())
      SEM_ERROR (node, "controlling expression of `do' statement not scalar");
  }
}

void CSemVisitor::pre_action (CT_ExprStmt *node) {
  if (node->Expr ())
    resolveExpr (node->Expr (), node);
  prune ();
}

void CSemVisitor::pre_action (CT_CaseStmt *node) {
  if (node->Expr ()) {
    resolveExpr (node->Expr (), node);
    if (! CSemExpr::isConstIntExpr (node->Expr ()))
      SEM_ERROR (node, "label of `case' statement not a valid integer constant");
  }
}

void CSemVisitor::pre_action (CT_GotoStmt *node) {
  CFunctionInfo *finfo;
  const char *name;
  
  if (! current_scope->LocalScope ())
    SEM_ERROR (node, "`goto' outside of function");
  else {
    finfo = current_scope->LocalScope ()->Function ();
    if (finfo) {
      name = node->Label ()->Text ();
      if (! finfo->Label (name))
        SEM_ERROR (node, "label `" << name << "' used but not defined");
    }
  }
}

void CSemVisitor::pre_action (CT_ReturnStmt *node) {
  CFunctionInfo *finfo = 0;
  CTypeInfo *t2 = 0, *t1 = 0;
  CConstant *v = 0;
  CTree *expr = 0;
  
  if (! current_scope->LocalScope ())
    SEM_ERROR (node, "`return' outside of function");
  else {
    finfo = current_scope->LocalScope ()->Function ();
    if (finfo)
      t1 = finfo->TypeInfo ()->ReturnType ();
    else
      t1 = &CTYPE_UNDEFINED;
  }
    
  if (node->Expr ()) {
    t2 = resolveExpr (node->Expr (), node);
    expr = node->Expr ();
    if (t1->isVoid ()) 
      SEM_ERROR (node, "`return' with a value, in function returning void");
    else {
      v = expr->Value () ? expr->Value ()->Constant () : 0;
      if (t1->isScalar ()) {
        if (t1->isArithmetic () && t2->isArithmetic ()) {
          if (! CSemExpr::compatible (t1, t2))
            CSemExpr::cast_to (t1, expr, node);
        } else if (! (t1->isPointer () && t2->isPointer () && 
                      (CSemExpr::compatibleBase (t1, t2) ||
                       t1->VirtualType ()->BaseType ()->isVoid () || 
                       t2->VirtualType ()->BaseType ()->isVoid ()) &&
                      (t2->isConst () ? t1->isConst () : true) &&
                      (t2->isVolatile () ? t1->isVolatile () : true) &&
                      (t2->isRestrict () ? t1->isRestrict () : true)) &&
                   ! (t1->isPointer () && t2->isInteger () && v && v->isNull ()) &&
                   ! (t1->VirtualType ()->is_bool () && t2->isPointer ())) {
          SEM_ERROR (node, "incompatible types in `return' statement");
        } else if (! CSemExpr::compatible (t1, t2))
          CSemExpr::cast_to (t1, expr, node);
      } else if (t1->isRecord ()) {
        if (! CSemExpr::compatible (t1, t2))
          SEM_ERROR (node, "incompatible types in `return' statement");
      } else
        SEM_ERROR (node, "invalid return type");
    }
  } else if (! t1->isVoid ()) 
    SEM_ERROR (node, "`return' with no value, in function returning non-void");

  prune ();
}

void CSemVisitor::pre_action (CT_InitDeclarator *node) {
  CTree *init = node->Initializer ();
  if (init)
    resolveInit (node->Object (), init, node);
}

void CSemVisitor::pre_action (CT_ArrayDeclarator *node) {
  CT_SimpleName *name;
  const char *array;
  CTypeInfo *type;
  CConstant *v;
  CTree *expr;
  
  expr = node->Delimiter ()->Expr ();
  if (expr) {
    name = findName (node);
    array = name ? name->Text () : "";
    type = resolveExpr (expr, node->Delimiter ());
    expr = node->Delimiter ()->Expr ();
    
    if (! type->isInteger ())
      SEM_ERROR (node, "size of array `" << array << "' has non-integer type");
    else if (CSemExpr::isConstIntExpr (expr)) {
      v = expr->Value ()->Constant ();
      if (/*v->isNull () ||*/ v->isNegative ())
        SEM_ERROR (node, "array `" << array << "' has invalid size");
      else if (node->Type ()->TypeArray ())
        node->Type ()->TypeArray ()->Dimension (v->convert_to_uint ());
    }
  }
}

void CSemVisitor::pre_action (CT_BitFieldDeclarator *node) {
  CObjectInfo *info;
  const char *name;
  CConstant *v;
  CTree *expr;
  
  expr = node->Expr ();
  if (expr) {
    resolveExpr (expr, node);
    expr = node->Expr ();
    info = node->Object ();
    name = info ? info->Name ().c_str () : "";
    
    if (! CSemExpr::isConstIntExpr (expr)) 
      SEM_ERROR (node, "width of bit-field `" << name
        << "' isn't integer constant expression");
    else {
      v = expr->Value ()->Constant ();
      if (v->isNegative ())
        SEM_ERROR (node, "negative width in bit-field `" << name << "'");
      else if (info && info->TypeInfo () && info->TypeInfo ()->TypeBitField ())
        info->TypeInfo ()->TypeBitField ()->Dimension (v->convert_to_uint ());
    }
  }
}

void CSemVisitor::pre_action (CT_EnumDef *node) {
  CT_EnumeratorList *el = node->Enumerators ();
  CT_Enumerator *e;
  CTypeInfo *type;
  CConstant *v;
  CTree *init;
  int entries;
  long value;
  
  if (el) {
    value = -1;
    entries = el->Entries ();
    for (int i = 0; i < entries; i++) {
      value++;
      e = (CT_Enumerator*)el->Entry (i);
      init = e->Initializer ();
      if (init) {
        init = e->Initializer ();
        type = resolveExpr (init->Son (1), init);
        init = init->Son (1);
        
        if (! CSemExpr::isConstIntExpr (init)) 
          SEM_ERROR (node, "value for enumerator `" << e->Name ()->Text ()
            << "' isn't integer constant expression");
        else {
          v = init->Value ()->Constant ();
          if (type->VirtualType ()->is_signed ())
            value = v->convert_to_int ();
          else 
            value = v->convert_to_uint ();
        }
      } 
      if (e->Object ())
        e->Object ()->EnumeratorInfo ()->Value (value);
    }
  }
}

void CSemVisitor::pre_action (CT_ExprList *node) {
  // do not visit the expression list 
  // it's evaluated when the init declarator is visited
  prune ();
}

void CSemVisitor::pre_action (CT_CmpdLiteral *node) {
  // do not visit the compound literal
  // it's evaluated when the init declarator is visited
  prune ();
}



///////////////////////////////////////////////////////
// post_action functions //////////////////////////////
///////////////////////////////////////////////////////



void CSemVisitor::post_action (CT_Program *node) {
  current_scope = node->Scope ();
}

void CSemVisitor::post_action (CT_ArgDeclList *node) {
  current_scope = node->Scope ()->Parent ()->Structure ();
}

void CSemVisitor::post_action (CT_ArgNameList *node) {
  current_scope = node->Scope ()->Parent ()->Structure ();
}

void CSemVisitor::post_action (CT_ArgDeclSeq *node) {
  current_scope = node->Scope ()->Parent ()->Structure ();
}

void CSemVisitor::post_action (CT_MembList *node) {
  current_scope = node->Scope ()->Parent ()->Structure ();
}

void CSemVisitor::post_action (CT_CmpdStmt *node) {
  current_scope = node->Scope ()->Parent ()->Structure ();
}

void CSemVisitor::post_action (CT_IfStmt *node) {
  current_scope = node->Scope ()->Parent ()->Structure ();
}

void CSemVisitor::post_action (CT_IfElseStmt *node) {
  current_scope = node->Scope ()->Parent ()->Structure ();
}

void CSemVisitor::post_action (CT_SwitchStmt *node) {
  current_scope = node->Scope ()->Parent ()->Structure ();
}

void CSemVisitor::post_action (CT_ForStmt *node) {
  current_scope = node->Scope ()->Parent ()->Structure ();
}

void CSemVisitor::post_action (CT_WhileStmt *node) {
  current_scope = node->Scope ()->Parent ()->Structure ();
}



///////////////////////////////////////////////////////
// common helper //////////////////////////////////////
///////////////////////////////////////////////////////



CT_SimpleName *CSemVisitor::findName (CTree *node) const {
  const char *id = node->NodeName ();
  if (id == CT_InitDeclarator::NodeId ())
    return findName (((CT_InitDeclarator*)node)->Declarator ());
  else if (id == CT_FctDeclarator::NodeId ())
    return findName (((CT_FctDeclarator*)node)->Declarator ());
  else if (id == CT_ArrayDeclarator::NodeId ())
    return findName (((CT_ArrayDeclarator*)node)->Declarator ());
  else if (id == CT_BracedDeclarator::NodeId ())
    return findName (((CT_BracedDeclarator*)node)->Declarator ());
  else if (id == CT_BitFieldDeclarator::NodeId ())
    return findName (((CT_BitFieldDeclarator*)node)->Declarator ());
  else if (id == CT_RefDeclarator::NodeId ())
    return findName (((CT_RefDeclarator*)node)->Declarator ());
  else if (id == CT_PtrDeclarator::NodeId ())
    return findName (((CT_PtrDeclarator*)node)->Declarator ());
  else if (id == CT_MembPtrDeclarator::NodeId ())
    return findName (((CT_MembPtrDeclarator*)node)->Declarator ());
  else if (id == CT_SimpleName::NodeId () || id == CT_PrivateName::NodeId ())
    return (CT_SimpleName*)node;
  return (CT_SimpleName*)0;
}


} // namespace Puma