| 1 | // | 
|---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo | 
|---|
| 3 | // | 
|---|
| 4 | // The contents of this file are covered under the licence agreement in the | 
|---|
| 5 | // file "LICENCE" distributed with Cforall. | 
|---|
| 6 | // | 
|---|
| 7 | // SemanticError.h -- | 
|---|
| 8 | // | 
|---|
| 9 | // Author           : Richard C. Bilson | 
|---|
| 10 | // Created On       : Mon May 18 07:44:20 2015 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr | 
|---|
| 12 | // Last Modified On : Tue Aug 29 22:03:36 2017 | 
|---|
| 13 | // Update Count     : 17 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #pragma once | 
|---|
| 17 |  | 
|---|
| 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 | 
|---|
| 25 |  | 
|---|
| 26 | struct error { | 
|---|
| 27 | std::string description; | 
|---|
| 28 | CodeLocation location; | 
|---|
| 29 |  | 
|---|
| 30 | error() = default; | 
|---|
| 31 | error( const std::string & str ) : description( str ) {} | 
|---|
| 32 |  | 
|---|
| 33 | void maybeSet( const CodeLocation & location ) { | 
|---|
| 34 | if( this->location.isUnset() ) { | 
|---|
| 35 | this->location = location; | 
|---|
| 36 | } | 
|---|
| 37 | } | 
|---|
| 38 | }; | 
|---|
| 39 |  | 
|---|
| 40 | class SemanticError : public std::exception { | 
|---|
| 41 | public: | 
|---|
| 42 | SemanticError(); | 
|---|
| 43 | SemanticError( std::string error ); | 
|---|
| 44 | template< typename T > SemanticError( const std::string & error, const T * obj ); | 
|---|
| 45 | ~SemanticError() throw() {} | 
|---|
| 46 |  | 
|---|
| 47 | static inline const std::string & error_str() { | 
|---|
| 48 | static std::string str = isatty( STDERR_FILENO ) ? "\e[31merror:\e[39m " : "error: "; | 
|---|
| 49 | return str; | 
|---|
| 50 | } | 
|---|
| 51 |  | 
|---|
| 52 | void append( SemanticError & other ); | 
|---|
| 53 | void append( const std::string & ); | 
|---|
| 54 | bool isEmpty() const; | 
|---|
| 55 | void print( std::ostream & os ); | 
|---|
| 56 |  | 
|---|
| 57 | void set_location( const CodeLocation & location ); | 
|---|
| 58 | // constructs an exception using the given message and the printed representation of the obj (T must have a print | 
|---|
| 59 | // method) | 
|---|
| 60 | private: | 
|---|
| 61 | std::list< error > errors; | 
|---|
| 62 | }; | 
|---|
| 63 |  | 
|---|
| 64 | template< typename T > | 
|---|
| 65 | SemanticError::SemanticError( const std::string & error, const T * obj ) { | 
|---|
| 66 | append( toString( error, obj ) ); | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 | // Local Variables: // | 
|---|
| 70 | // tab-width: 4 // | 
|---|
| 71 | // mode: c++ // | 
|---|
| 72 | // compile-command: "make install" // | 
|---|
| 73 | // End: // | 
|---|