Changeset dcbb03b for src/Common


Ignore:
Timestamp:
Mar 1, 2018, 9:29:03 AM (8 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
1f37ed02
Parents:
b002261 (diff), 446ffa3 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

Location:
src/Common
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • src/Common/PassVisitor.impl.h

    rb002261 rdcbb03b  
    6565        DeclList_t* beforeDecls = visitor.get_beforeDecls();
    6666        DeclList_t* afterDecls  = visitor.get_afterDecls();
    67         SemanticError errors;
     67        SemanticErrorException errors;
    6868
    6969        for ( std::list< Declaration* >::iterator i = decls.begin(); ; ++i ) {
     
    7676                        // run visitor on declaration
    7777                        maybeAccept_impl( *i, visitor );
    78                 } catch( SemanticError &e ) {
     78                } catch( SemanticErrorException &e ) {
    7979                        errors.append( e );
    8080                }
     
    9292        DeclList_t* beforeDecls = mutator.get_beforeDecls();
    9393        DeclList_t* afterDecls  = mutator.get_afterDecls();
    94         SemanticError errors;
     94        SemanticErrorException errors;
    9595
    9696        for ( std::list< Declaration* >::iterator i = decls.begin(); ; ++i ) {
     
    102102                        // run mutator on declaration
    103103                        maybeMutate_impl( *i, mutator );
    104                 } catch( SemanticError &e ) {
     104                } catch( SemanticErrorException &e ) {
    105105                        errors.append( e );
    106106                }
     
    125125inline void maybeAccept_impl( Container & container, PassVisitor< pass_type > & visitor ) {
    126126        if ( ! visitor.get_visit_children() ) return;
    127         SemanticError errors;
     127        SemanticErrorException errors;
    128128        for ( typename Container::iterator i = container.begin(); i != container.end(); ++i ) {
    129129                try {
     
    131131                                (*i)->accept( visitor );
    132132                        }
    133                 } catch( SemanticError &e ) {
     133                } catch( SemanticErrorException &e ) {
    134134                        errors.append( e );
    135135                }
     
    152152inline void maybeMutate_impl( Container & container, PassVisitor< pass_type > & mutator ) {
    153153        if ( ! mutator.get_visit_children() ) return;
    154         SemanticError errors;
     154        SemanticErrorException errors;
    155155        for ( typename Container::iterator i = container.begin(); i != container.end(); ++i ) {
    156156                try {
     
    159159                                assert( *i );
    160160                        } // if
    161                 } catch( SemanticError &e ) {
     161                } catch( SemanticErrorException &e ) {
    162162                        errors.append( e );
    163163                } // try
     
    172172void PassVisitor< pass_type >::handleStatementList( std::list< Statement * > & statements, func_t func ) {
    173173        if ( ! get_visit_children() ) return;
    174         SemanticError errors;
     174        SemanticErrorException errors;
    175175
    176176        // don't want statements from outer CompoundStmts to be added to this CompoundStmt
     
    195195                            || ( empty( beforeDecls ) && empty( afterDecls )) );
    196196
    197                 } catch ( SemanticError &e ) {
     197                } catch ( SemanticErrorException &e ) {
    198198                        errors.append( e );
    199199                }
     
    20132013}
    20142014
    2015 
     2015//--------------------------------------------------------------------------
     2016// VoidType
    20162017template< typename pass_type >
    20172018void PassVisitor< pass_type >::visit( VoidType * node ) {
     
    20322033}
    20332034
     2035//--------------------------------------------------------------------------
     2036// BasicType
    20342037template< typename pass_type >
    20352038void PassVisitor< pass_type >::visit( BasicType * node ) {
    2036         VISIT_BODY( node );
    2037 }
    2038 
     2039        VISIT_START( node );
     2040
     2041        maybeAccept_impl( node->forall, *this );
     2042
     2043        VISIT_END( node );
     2044}
     2045
     2046template< typename pass_type >
     2047Type * PassVisitor< pass_type >::mutate( BasicType * node ) {
     2048        MUTATE_START( node );
     2049
     2050        maybeMutate_impl( node->forall, *this );
     2051
     2052        MUTATE_END( Type, node );
     2053}
     2054
     2055//--------------------------------------------------------------------------
     2056// PointerType
    20392057template< typename pass_type >
    20402058void PassVisitor< pass_type >::visit( PointerType * node ) {
    2041         VISIT_BODY( node );
    2042 }
    2043 
     2059        VISIT_START( node );
     2060
     2061        maybeAccept_impl( node->forall, *this );
     2062        // xxx - should PointerType visit/mutate dimension?
     2063        maybeAccept_impl( node->base, *this );
     2064
     2065        VISIT_END( node );
     2066}
     2067
     2068template< typename pass_type >
     2069Type * PassVisitor< pass_type >::mutate( PointerType * node ) {
     2070        MUTATE_START( node );
     2071
     2072        maybeMutate_impl( node->forall, *this );
     2073        // xxx - should PointerType visit/mutate dimension?
     2074        maybeMutate_impl( node->base, *this );
     2075
     2076        MUTATE_END( Type, node );
     2077}
     2078
     2079//--------------------------------------------------------------------------
     2080// ArrayType
    20442081template< typename pass_type >
    20452082void PassVisitor< pass_type >::visit( ArrayType * node ) {
    2046         VISIT_BODY( node );
    2047 }
    2048 
     2083        VISIT_START( node );
     2084
     2085        maybeAccept_impl( node->forall, *this );
     2086        maybeAccept_impl( node->dimension, *this );
     2087        maybeAccept_impl( node->base, *this );
     2088
     2089        VISIT_END( node );
     2090}
     2091
     2092template< typename pass_type >
     2093Type * PassVisitor< pass_type >::mutate( ArrayType * node ) {
     2094        MUTATE_START( node );
     2095
     2096        maybeMutate_impl( node->forall, *this );
     2097        maybeMutate_impl( node->dimension, *this );
     2098        maybeMutate_impl( node->base, *this );
     2099
     2100        MUTATE_END( Type, node );
     2101}
     2102
     2103//--------------------------------------------------------------------------
     2104// ReferenceType
    20492105template< typename pass_type >
    20502106void PassVisitor< pass_type >::visit( ReferenceType * node ) {
    2051         VISIT_BODY( node );
    2052 }
    2053 
     2107        VISIT_START( node );
     2108
     2109        maybeAccept_impl( node->forall, *this );
     2110        maybeAccept_impl( node->base, *this );
     2111
     2112        VISIT_END( node );
     2113}
     2114
     2115template< typename pass_type >
     2116Type * PassVisitor< pass_type >::mutate( ReferenceType * node ) {
     2117        MUTATE_START( node );
     2118
     2119        maybeMutate_impl( node->forall, *this );
     2120        maybeMutate_impl( node->base, *this );
     2121
     2122        MUTATE_END( Type, node );
     2123}
     2124
     2125//--------------------------------------------------------------------------
     2126// FunctionType
    20542127template< typename pass_type >
    20552128void PassVisitor< pass_type >::visit( FunctionType * node ) {
    2056         VISIT_BODY( node );
     2129        VISIT_START( node );
     2130
     2131        maybeAccept_impl( node->forall, *this );
     2132        maybeAccept_impl( node->returnVals, *this );
     2133        maybeAccept_impl( node->parameters, *this );
     2134
     2135        VISIT_END( node );
     2136}
     2137
     2138template< typename pass_type >
     2139Type * PassVisitor< pass_type >::mutate( FunctionType * node ) {
     2140        MUTATE_START( node );
     2141
     2142        maybeMutate_impl( node->forall, *this );
     2143        maybeMutate_impl( node->returnVals, *this );
     2144        maybeMutate_impl( node->parameters, *this );
     2145
     2146        MUTATE_END( Type, node );
    20572147}
    20582148
     
    21252215template< typename pass_type >
    21262216void PassVisitor< pass_type >::visit( EnumInstType * node ) {
    2127         VISIT_BODY( node );
     2217        VISIT_START( node );
     2218
     2219        maybeAccept_impl( node->forall, *this );
     2220        maybeAccept_impl( node->parameters, *this );
     2221
     2222        VISIT_END( node );
    21282223}
    21292224
    21302225template< typename pass_type >
    21312226Type * PassVisitor< pass_type >::mutate( EnumInstType * node ) {
    2132         MUTATE_BODY( Type, node );
     2227        MUTATE_START( node );
     2228
     2229        maybeMutate_impl( node->forall, *this );
     2230        maybeMutate_impl( node->parameters, *this );
     2231
     2232        MUTATE_END( Type, node );
    21332233}
    21342234
     
    21592259template< typename pass_type >
    21602260void PassVisitor< pass_type >::visit( TypeInstType * node ) {
    2161         VISIT_BODY( node );
    2162 }
     2261        VISIT_START( node );
     2262
     2263        maybeAccept_impl( node->forall    , *this );
     2264        maybeAccept_impl( node->parameters, *this );
     2265
     2266        VISIT_END( node );
     2267}
     2268
     2269template< typename pass_type >
     2270Type * PassVisitor< pass_type >::mutate( TypeInstType * node ) {
     2271        MUTATE_START( node );
     2272
     2273        maybeMutate_impl( node->forall    , *this );
     2274        maybeMutate_impl( node->parameters, *this );
     2275
     2276        MUTATE_END( Type, node );
     2277}
     2278
    21632279
    21642280template< typename pass_type >
     
    22582374
    22592375template< typename pass_type >
    2260 Type * PassVisitor< pass_type >::mutate( BasicType * node ) {
    2261         MUTATE_BODY( Type, node );
    2262 }
    2263 
    2264 template< typename pass_type >
    2265 Type * PassVisitor< pass_type >::mutate( PointerType * node ) {
    2266         MUTATE_BODY( Type, node );
    2267 }
    2268 
    2269 template< typename pass_type >
    2270 Type * PassVisitor< pass_type >::mutate( ArrayType * node ) {
    2271         MUTATE_BODY( Type, node );
    2272 }
    2273 
    2274 template< typename pass_type >
    2275 Type * PassVisitor< pass_type >::mutate( ReferenceType * node ) {
    2276         MUTATE_BODY( Type, node );
    2277 }
    2278 
    2279 template< typename pass_type >
    2280 Type * PassVisitor< pass_type >::mutate( FunctionType * node ) {
    2281         MUTATE_BODY( Type, node );
    2282 }
    2283 
    2284 template< typename pass_type >
    2285 Type * PassVisitor< pass_type >::mutate( TypeInstType * node ) {
    2286         MUTATE_BODY( Type, node );
    2287 }
    2288 
    2289 template< typename pass_type >
    22902376Type * PassVisitor< pass_type >::mutate( TupleType * node ) {
    22912377        MUTATE_BODY( Type, node );
  • src/Common/SemanticError.cc

    rb002261 rdcbb03b  
    2323#include "SemanticError.h"
    2424
    25 SemanticError::SemanticError( CodeLocation location, std::string error ) {
     25SemanticErrorException::SemanticErrorException( CodeLocation location, std::string error ) {
    2626        append( location, error );
    2727}
    2828
    29 void SemanticError::append( SemanticError &other ) {
     29void SemanticErrorException::append( SemanticErrorException &other ) {
    3030        errors.splice( errors.end(), other.errors );
    3131}
    3232
    33 void SemanticError::append( CodeLocation location, const std::string & msg ) {
     33void SemanticErrorException::append( CodeLocation location, const std::string & msg ) {
    3434        errors.emplace_back( location, msg );
    3535}
    3636
    37 bool SemanticError::isEmpty() const {
     37bool SemanticErrorException::isEmpty() const {
    3838        return errors.empty();
    3939}
    4040
    41 void SemanticError::print() {
     41void SemanticErrorException::print() {
    4242        using std::to_string;
    4343        for( auto err : errors ) {
    44                 std::cerr << bold() << err.location << error_str() << reset_font() << err.description << std::endl;
     44                std::cerr << ErrorHelpers::bold() << err.location << ErrorHelpers::error_str() << ErrorHelpers::reset_font() << err.description << std::endl;
    4545        }
    4646}
    4747
    48 SemanticWarning::SemanticWarning( CodeLocation location, std::string msg ) {
    49         std::cerr << bold() << location << warning_str() << reset_font() << msg << std::endl;
     48void SemanticError( CodeLocation location, std::string error ) {
     49        throw SemanticErrorException(location, error);
     50}
     51
     52void SemanticWarningImpl( CodeLocation location, std::string msg ) {
     53        std::cerr << ErrorHelpers::bold() << location << ErrorHelpers::warning_str() << ErrorHelpers::reset_font() << msg << std::endl;
     54}
     55
     56//-----------------------------------------------------------------------------
     57// Helpers
     58namespace ErrorHelpers {
     59        const std::string & error_str() {
     60                static std::string str = isatty( STDERR_FILENO ) ? "\e[31merror:\e[39m " : "error: ";
     61                return str;
     62        }
     63
     64        const std::string & warning_str() {
     65                static std::string str = isatty( STDERR_FILENO ) ? "\e[95mwarning:\e[39m " : "warning: ";
     66                return str;
     67        }
     68
     69        const std::string & bold_ttycode() {
     70                static std::string str = isatty( STDERR_FILENO ) ? "\e[1m" : "";
     71                return str;
     72        }
     73
     74        const std::string & reset_font_ttycode() {
     75                static std::string str = isatty( STDERR_FILENO ) ? "\e[0m" : "";
     76                return str;
     77        }
     78
     79        std::string make_bold( const std::string & str ) {
     80                return bold_ttycode() + str + reset_font_ttycode();
     81        }
     82
     83        std::ostream & operator<<(std::ostream & os, bold) {
     84                os << bold_ttycode();
     85                return os;
     86        }
     87
     88        std::ostream & operator<<(std::ostream & os, reset_font) {
     89                os << reset_font_ttycode();
     90                return os;
     91        }
    5092}
    5193
  • src/Common/SemanticError.h

    rb002261 rdcbb03b  
    1616#pragma once
    1717
    18 #include <exception>                                                                    // for exception
    19 #include <iostream>                                                                             // for ostream
    20 #include <list>                                                                                 // for list
    21 #include <string>                                                                               // for string
    22 #include <unistd.h>                                                                             // for isatty
    23 
    24 #include "CodeLocation.h"                                                               // for CodeLocation, toString
     18#include "ErrorObjects.h"
    2519
    2620//-----------------------------------------------------------------------------
    2721// Errors
    28 struct error {
    29         CodeLocation location;
    30         std::string description;
    3122
    32         error() = default;
    33         error( CodeLocation loc, const std::string & str ) : location( loc ), description( str ) {}
    34 };
    35 
    36 class SemanticError : public std::exception {
    37   public:
    38         SemanticError() = default;
    39         SemanticError( CodeLocation location, std::string error );
    40         ~SemanticError() throw() {}
    41 
    42         // constructs an exception using the given message and the printed representation of the obj (T must have a print method)
    43         template< typename T > SemanticError(const T * obj, const std::string & error);
    44         template< typename T > SemanticError( CodeLocation location, const T * obj, const std::string & error);
    45 
    46         static inline const std::string & error_str() {
    47                 static std::string str = isatty( STDERR_FILENO ) ? "\e[31merror:\e[39m " : "error: ";
    48                 return str;
    49         }
    50 
    51         void append( SemanticError & other );
    52         void append( CodeLocation location, const std::string & );
    53         bool isEmpty() const;
    54         void print();
    55   private:
    56         std::list< error > errors;
    57 };
     23__attribute__((noreturn)) void SemanticError( CodeLocation location, std::string error );
    5824
    5925template< typename T >
    60 SemanticError::SemanticError( const T * obj, const std::string & error )
    61         : SemanticError( obj->location, toString( error, obj ) )
    62 {}
     26__attribute__((noreturn)) static inline void SemanticError( const T * obj, const std::string & error ) {
     27        SemanticError( obj->location, toString( error, obj ) );
     28}
    6329
    6430template< typename T >
    65 SemanticError::SemanticError( CodeLocation location, const T * obj, const std::string & error )
    66         : SemanticError( location, toString( error, obj ) )
    67 {}
     31__attribute__((noreturn)) static inline void SemanticError( CodeLocation location, const T * obj, const std::string & error ) {
     32        SemanticError( location, toString( error, obj ) );
     33}
    6834
    6935//-----------------------------------------------------------------------------
    7036// Warnings
    71 class SemanticWarning {
    72   public:
    73         SemanticWarning( CodeLocation location, std::string error );
    74         ~SemanticWarning() throw() {}
    7537
    76         // constructs an exception using the given message and the printed representation of the obj (T must have a print method)
    77         template< typename T > SemanticWarning(const T * obj, const std::string & error);
    78         template< typename T > SemanticWarning( CodeLocation location, const T * obj, const std::string & error);
     38constexpr const char * const WarningFormats[] = {
    7939
    80         static inline const std::string & warning_str() {
    81                 static std::string str = isatty( STDERR_FILENO ) ? "\e[95mwarning:\e[39m " : "warning: ";
    82                 return str;
    83         }
    84 
    85   private:
    8640};
    8741
    88 template< typename T >
    89 SemanticWarning::SemanticWarning( const T * obj, const std::string & error )
    90         : SemanticWarning( obj->location, toString( error, obj ) )
    91 {}
     42enum class Warning {
     43        NUMBER_OF_WARNINGS, //This MUST be the last warning
     44};
    9245
    93 template< typename T >
    94 SemanticWarning::SemanticWarning( CodeLocation location, const T * obj, const std::string & error )
    95         : SemanticWarning( location, toString( error, obj ) )
    96 {}
     46static_assert(
     47        (sizeof(WarningFormats) / sizeof(WarningFormats[0])) == ((unsigned long)Warning::NUMBER_OF_WARNINGS),
     48        "Each warning format should have a corresponding warning enum value"
     49);
     50
     51#define SemanticWarning(loc, id, ...) SemanticWarningImpl(loc, id, WarningFormats[id], __VA_ARGS__)
     52
     53void SemanticWarningImpl (CodeLocation loc, Warning warn, const char * const fmt, ...) __attribute__((format(printf, 3, 4)));
     54
    9755
    9856//-----------------------------------------------------------------------------
    9957// Helpers
    100 static inline const std::string & bold_ttycode() {
    101         static std::string str = isatty( STDERR_FILENO ) ? "\e[1m" : "";
    102         return str;
     58namespace ErrorHelpers {
     59        const std::string & error_str();
     60        const std::string & warning_str();
     61        const std::string & bold_ttycode();
     62        const std::string & reset_font_ttycode();
     63
     64        std::string make_bold( const std::string & str );
     65
     66        struct bold {};
     67        std::ostream & operator<<(std::ostream & os, bold);
     68
     69        struct reset_font {};
     70        std::ostream & operator<<(std::ostream & os, reset_font);
    10371}
    10472
    105 static inline const std::string & reset_font_ttycode() {
    106         static std::string str = isatty( STDERR_FILENO ) ? "\e[0m" : "";
    107         return str;
    108 }
    10973
    110 static inline std::string make_bold( const std::string & str ) {
    111         return bold_ttycode() + str + reset_font_ttycode();
    112 }
    11374
    114 struct bold {};
    115 static inline std::ostream & operator<<(std::ostream & os, bold) {
    116         os << bold_ttycode();
    117         return os;
    118 }
    119 
    120 struct reset_font {};
    121 static inline std::ostream & operator<<(std::ostream & os, reset_font) {
    122         os << reset_font_ttycode();
    123         return os;
    124 }
    12575
    12676// Local Variables: //
Note: See TracChangeset for help on using the changeset viewer.