[51b7345] | 1 | #include <list> |
---|
| 2 | #include <cassert> |
---|
| 3 | |
---|
| 4 | #include "SynTree/Type.h" |
---|
| 5 | #include "SynTree/Statement.h" |
---|
| 6 | #include "SynTree/Expression.h" |
---|
| 7 | #include "SynTree/Declaration.h" |
---|
| 8 | |
---|
| 9 | #include "LabelTypeChecker.h" |
---|
| 10 | |
---|
| 11 | |
---|
| 12 | namespace ControlStruct { |
---|
[d9a0e76] | 13 | void LabelTypeChecker::visit(UntypedExpr *untypedExpr){ |
---|
| 14 | assert( untypedExpr != 0 ); |
---|
| 15 | NameExpr *fname; |
---|
| 16 | if ( ((fname = dynamic_cast<NameExpr *>(untypedExpr->get_function())) != 0) |
---|
| 17 | && fname->get_name() == std::string("LabAddress") ) |
---|
| 18 | std::cerr << "Taking the label of an address." << std::endl; |
---|
| 19 | else { |
---|
| 20 | acceptAll( untypedExpr->get_results(), *this ); |
---|
| 21 | acceptAll( untypedExpr->get_args(), *this ); |
---|
| 22 | } // if |
---|
| 23 | return; |
---|
[51b7345] | 24 | } |
---|
| 25 | |
---|
[d9a0e76] | 26 | void LabelTypeChecker::visit(CompoundStmt *compoundStmt) { |
---|
| 27 | index.enterScope(); |
---|
| 28 | acceptAll( compoundStmt->get_kids(), *this ); |
---|
| 29 | index.leaveScope(); |
---|
| 30 | } |
---|
[51b7345] | 31 | |
---|
[d9a0e76] | 32 | void LabelTypeChecker::visit(DeclStmt *declStmt){ |
---|
| 33 | declStmt->accept( index ); |
---|
[51b7345] | 34 | |
---|
[d9a0e76] | 35 | //ObjectDecl *odecl = 0; |
---|
| 36 | // if ( ( odecl = dynamic_cast<ObjectDecl *>(declStmt->get_decl()) ) != 0 ){ |
---|
| 37 | return; |
---|
| 38 | } |
---|
[51b7345] | 39 | |
---|
[d9a0e76] | 40 | void LabelTypeChecker::visit(BranchStmt *branchStmt) { |
---|
| 41 | if ( branchStmt->get_type() != BranchStmt::Goto ) return; |
---|
| 42 | Expression *target; |
---|
| 43 | if ( (target = branchStmt->get_computedTarget()) == 0 ) return; |
---|
[51b7345] | 44 | |
---|
[d9a0e76] | 45 | NameExpr *name; |
---|
| 46 | if ( ((name = dynamic_cast<NameExpr *>(target)) == 0) ) |
---|
| 47 | return; // Not a name expression |
---|
[51b7345] | 48 | |
---|
[d9a0e76] | 49 | std::list< DeclarationWithType * > interps; |
---|
| 50 | index.lookupId(name->get_name(), interps); |
---|
| 51 | if ( interps.size() != 1) |
---|
| 52 | // in case of multiple declarations |
---|
| 53 | throw SemanticError("Illegal label expression: " + name->get_name() ); |
---|
| 54 | |
---|
| 55 | PointerType *ptr; |
---|
| 56 | if ( (ptr = dynamic_cast<PointerType *>(interps.front()->get_type())) != 0 ) |
---|
| 57 | if ( dynamic_cast<VoidType *>(ptr->get_base()) != 0 ) |
---|
| 58 | return; |
---|
| 59 | else |
---|
| 60 | throw SemanticError("Wrong type of parameter for computed goto"); |
---|
| 61 | else |
---|
| 62 | throw SemanticError("Wrong type of parameter for computed goto"); |
---|
[51b7345] | 63 | |
---|
| 64 | return; |
---|
[d9a0e76] | 65 | } |
---|
[51b7345] | 66 | } // namespace ControlStruct |
---|