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