| 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 | // main.cc -- | 
|---|
| 8 | // | 
|---|
| 9 | // Author           : Richard C. Bilson | 
|---|
| 10 | // Created On       : Fri May 15 23:12:02 2015 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr | 
|---|
| 12 | // Last Modified On : Mon May  7 14:35:57 2018 | 
|---|
| 13 | // Update Count     : 492 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #include <cxxabi.h>                         // for __cxa_demangle | 
|---|
| 17 | #include <execinfo.h>                       // for backtrace, backtrace_symbols | 
|---|
| 18 | #include <getopt.h>                         // for no_argument, optind, geto... | 
|---|
| 19 | #include <signal.h>                         // for signal, SIGABRT, SIGSEGV | 
|---|
| 20 | #include <cassert>                          // for assertf | 
|---|
| 21 | #include <cstdio>                           // for fopen, FILE, fclose, stdin | 
|---|
| 22 | #include <cstdlib>                          // for exit, free, abort, EXIT_F... | 
|---|
| 23 | #include <cstring>                          // for index | 
|---|
| 24 | #include <fstream>                          // for ofstream | 
|---|
| 25 | #include <iostream>                         // for operator<<, basic_ostream | 
|---|
| 26 | #include <iterator>                         // for back_inserter | 
|---|
| 27 | #include <list>                             // for list | 
|---|
| 28 | #include <string>                           // for char_traits, operator<< | 
|---|
| 29 |  | 
|---|
| 30 | #include "../config.h"                      // for CFA_LIBDIR | 
|---|
| 31 | #include "CodeGen/FixMain.h"                // for FixMain | 
|---|
| 32 | #include "CodeGen/FixNames.h"               // for fixNames | 
|---|
| 33 | #include "CodeGen/Generate.h"               // for generate | 
|---|
| 34 | #include "CodeTools/DeclStats.h"            // for printDeclStats | 
|---|
| 35 | #include "CodeTools/TrackLoc.h"             // for fillLocations | 
|---|
| 36 | #include "Common/CompilerError.h"           // for CompilerError | 
|---|
| 37 | #include "Common/Heap.h" | 
|---|
| 38 | #include "Common/PassVisitor.h" | 
|---|
| 39 | #include "Common/SemanticError.h"           // for SemanticError | 
|---|
| 40 | #include "Common/UnimplementedError.h"      // for UnimplementedError | 
|---|
| 41 | #include "Common/utility.h"                 // for deleteAll, filter, printAll | 
|---|
| 42 | #include "Concurrency/Waitfor.h"            // for generateWaitfor | 
|---|
| 43 | #include "ControlStruct/ExceptTranslate.h"  // for translateEHM | 
|---|
| 44 | #include "ControlStruct/Mutate.h"           // for mutate | 
|---|
| 45 | #include "GenPoly/Box.h"                    // for box | 
|---|
| 46 | #include "GenPoly/InstantiateGeneric.h"     // for instantiateGeneric | 
|---|
| 47 | #include "GenPoly/Lvalue.h"                 // for convertLvalue | 
|---|
| 48 | #include "GenPoly/Specialize.h"             // for convertSpecializations | 
|---|
| 49 | #include "InitTweak/FixInit.h"              // for fix | 
|---|
| 50 | #include "InitTweak/GenInit.h"              // for genInit | 
|---|
| 51 | #include "MakeLibCfa.h"                     // for makeLibCfa | 
|---|
| 52 | #include "Parser/LinkageSpec.h"             // for Spec, Cforall, Intrinsic | 
|---|
| 53 | #include "Parser/ParseNode.h"               // for DeclarationNode, buildList | 
|---|
| 54 | #include "Parser/TypedefTable.h"            // for TypedefTable | 
|---|
| 55 | #include "ResolvExpr/AlternativePrinter.h"  // for AlternativePrinter | 
|---|
| 56 | #include "ResolvExpr/Resolver.h"            // for resolve | 
|---|
| 57 | #include "SymTab/Validate.h"                // for validate | 
|---|
| 58 | #include "SynTree/Declaration.h"            // for Declaration | 
|---|
| 59 | #include "SynTree/Visitor.h"                // for acceptAll | 
|---|
| 60 | #include "Tuples/Tuples.h"                  // for expandMemberTuples, expan... | 
|---|
| 61 | #include "Virtual/ExpandCasts.h"            // for expandCasts | 
|---|
| 62 |  | 
|---|
| 63 | using namespace std; | 
|---|
| 64 |  | 
|---|
| 65 | #define PASS(name, pass)                   \ | 
|---|
| 66 | if ( errorp ) { cerr << name << endl; } \ | 
|---|
| 67 | HeapStats::newPass(name);               \ | 
|---|
| 68 | pass; | 
|---|
| 69 |  | 
|---|
| 70 | LinkageSpec::Spec linkage = LinkageSpec::Cforall; | 
|---|
| 71 | TypedefTable typedefTable; | 
|---|
| 72 | DeclarationNode * parseTree = nullptr;                                  // program parse tree | 
|---|
| 73 |  | 
|---|
| 74 | extern int yydebug;                                                                             // set for -g flag (Grammar) | 
|---|
| 75 | bool | 
|---|
| 76 | astp = false, | 
|---|
| 77 | bresolvep = false, | 
|---|
| 78 | bboxp = false, | 
|---|
| 79 | bcodegenp = false, | 
|---|
| 80 | ctorinitp = false, | 
|---|
| 81 | declstatsp = false, | 
|---|
| 82 | exprp = false, | 
|---|
| 83 | expraltp = false, | 
|---|
| 84 | genericsp = false, | 
|---|
| 85 | libcfap = false, | 
|---|
| 86 | nopreludep = false, | 
|---|
| 87 | noprotop = false, | 
|---|
| 88 | nomainp = false, | 
|---|
| 89 | parsep = false, | 
|---|
| 90 | resolvep = false,                                                                       // used in AlternativeFinder | 
|---|
| 91 | symtabp = false, | 
|---|
| 92 | treep = false, | 
|---|
| 93 | tuplep = false, | 
|---|
| 94 | validp = false, | 
|---|
| 95 | errorp = false, | 
|---|
| 96 | codegenp = false, | 
|---|
| 97 | prettycodegenp = false, | 
|---|
| 98 | linemarks = false; | 
|---|
| 99 |  | 
|---|
| 100 | static void parse_cmdline( int argc, char *argv[], const char *& filename ); | 
|---|
| 101 | static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit = false ); | 
|---|
| 102 | static void dump( list< Declaration * > & translationUnit, ostream & out = cout ); | 
|---|
| 103 |  | 
|---|
| 104 | static void backtrace( int start ) {                                    // skip first N stack frames | 
|---|
| 105 | enum { Frames = 50 }; | 
|---|
| 106 | void * array[Frames]; | 
|---|
| 107 | int size = ::backtrace( array, Frames ); | 
|---|
| 108 | char ** messages = ::backtrace_symbols( array, size ); // does not demangle names | 
|---|
| 109 |  | 
|---|
| 110 | *index( messages[0], '(' ) = '\0';                                      // find executable name | 
|---|
| 111 | cerr << "Stack back trace for: " << messages[0] << endl; | 
|---|
| 112 |  | 
|---|
| 113 | // skip last 2 stack frames after main | 
|---|
| 114 | for ( int i = start; i < size - 2 && messages != nullptr; i += 1 ) { | 
|---|
| 115 | char * mangled_name = nullptr, * offset_begin = nullptr, * offset_end = nullptr; | 
|---|
| 116 | for ( char *p = messages[i]; *p; ++p ) {        // find parantheses and +offset | 
|---|
| 117 | if ( *p == '(' ) { | 
|---|
| 118 | mangled_name = p; | 
|---|
| 119 | } else if ( *p == '+' ) { | 
|---|
| 120 | offset_begin = p; | 
|---|
| 121 | } else if ( *p == ')' ) { | 
|---|
| 122 | offset_end = p; | 
|---|
| 123 | break; | 
|---|
| 124 | } // if | 
|---|
| 125 | } // for | 
|---|
| 126 |  | 
|---|
| 127 | // if line contains symbol, attempt to demangle | 
|---|
| 128 | int frameNo = i - start; | 
|---|
| 129 | if ( mangled_name && offset_begin && offset_end && mangled_name < offset_begin ) { | 
|---|
| 130 | *mangled_name++ = '\0';                                         // delimit strings | 
|---|
| 131 | *offset_begin++ = '\0'; | 
|---|
| 132 | *offset_end++ = '\0'; | 
|---|
| 133 |  | 
|---|
| 134 | int status; | 
|---|
| 135 | char * real_name = __cxxabiv1::__cxa_demangle( mangled_name, 0, 0, &status ); | 
|---|
| 136 | // bug in __cxa_demangle for single-character lower-case non-mangled names | 
|---|
| 137 | if ( status == 0 ) {                                            // demangling successful ? | 
|---|
| 138 | cerr << "(" << frameNo << ") " << messages[i] << " : " | 
|---|
| 139 | << real_name << "+" << offset_begin << offset_end << endl; | 
|---|
| 140 | } else {                                                                        // otherwise, output mangled name | 
|---|
| 141 | cerr << "(" << frameNo << ") " << messages[i] << " : " | 
|---|
| 142 | << mangled_name << "(/*unknown*/)+" << offset_begin << offset_end << endl; | 
|---|
| 143 | } // if | 
|---|
| 144 |  | 
|---|
| 145 | free( real_name ); | 
|---|
| 146 | } else {                                                                                // otherwise, print the whole line | 
|---|
| 147 | cerr << "(" << frameNo << ") " << messages[i] << endl; | 
|---|
| 148 | } // if | 
|---|
| 149 | } // for | 
|---|
| 150 |  | 
|---|
| 151 | free( messages ); | 
|---|
| 152 | } // backtrace | 
|---|
| 153 |  | 
|---|
| 154 | void sigSegvBusHandler( int sig_num ) { | 
|---|
| 155 | cerr << "*CFA runtime error* program cfa-cpp terminated with " | 
|---|
| 156 | <<     (sig_num == SIGSEGV ? "segment fault" : "bus error") | 
|---|
| 157 | << "." << endl; | 
|---|
| 158 | backtrace( 2 );                                                                         // skip first 2 stack frames | 
|---|
| 159 | exit( EXIT_FAILURE ); | 
|---|
| 160 | } // sigSegvBusHandler | 
|---|
| 161 |  | 
|---|
| 162 | void sigAbortHandler( __attribute__((unused)) int sig_num ) { | 
|---|
| 163 | backtrace( 6 );                                                                         // skip first 6 stack frames | 
|---|
| 164 | signal( SIGABRT, SIG_DFL);                                                      // reset default signal handler | 
|---|
| 165 | raise( SIGABRT );                                                                   // reraise SIGABRT | 
|---|
| 166 | } // sigAbortHandler | 
|---|
| 167 |  | 
|---|
| 168 |  | 
|---|
| 169 | int main( int argc, char * argv[] ) { | 
|---|
| 170 | FILE * input;                                                                           // use FILE rather than istream because yyin is FILE | 
|---|
| 171 | ostream *output = & cout; | 
|---|
| 172 | const char *filename = nullptr; | 
|---|
| 173 | list< Declaration * > translationUnit; | 
|---|
| 174 |  | 
|---|
| 175 | signal( SIGSEGV, sigSegvBusHandler ); | 
|---|
| 176 | signal( SIGBUS, sigSegvBusHandler ); | 
|---|
| 177 | signal( SIGABRT, sigAbortHandler ); | 
|---|
| 178 |  | 
|---|
| 179 | // std::cout << "main" << std::endl; | 
|---|
| 180 | // for ( int i = 0; i < argc; i += 1 ) { | 
|---|
| 181 | //      std::cout << '\t' << argv[i] << std::endl; | 
|---|
| 182 | // } // for | 
|---|
| 183 |  | 
|---|
| 184 | parse_cmdline( argc, argv, filename );                          // process command-line arguments | 
|---|
| 185 | CodeGen::FixMain::setReplaceMain( !nomainp ); | 
|---|
| 186 |  | 
|---|
| 187 | try { | 
|---|
| 188 | // choose to read the program from a file or stdin | 
|---|
| 189 | if ( optind < argc ) {                                                  // any commands after the flags ? => input file name | 
|---|
| 190 | input = fopen( argv[ optind ], "r" ); | 
|---|
| 191 | assertf( input, "cannot open %s\n", argv[ optind ] ); | 
|---|
| 192 | // if running cfa-cpp directly, might forget to pass -F option (and really shouldn't have to) | 
|---|
| 193 | if ( filename == nullptr ) filename = argv[ optind ]; | 
|---|
| 194 | // prelude filename comes in differently | 
|---|
| 195 | if ( libcfap ) filename = "prelude.cf"; | 
|---|
| 196 | optind += 1; | 
|---|
| 197 | } else {                                                                                // no input file name | 
|---|
| 198 | input = stdin; | 
|---|
| 199 | // if running cfa-cpp directly, might forget to pass -F option. Since this takes from stdin, pass | 
|---|
| 200 | // a fake name along | 
|---|
| 201 | if ( filename == nullptr ) filename = "stdin"; | 
|---|
| 202 | } // if | 
|---|
| 203 |  | 
|---|
| 204 | // read in the builtins, extras, and the prelude | 
|---|
| 205 | if ( ! nopreludep ) {                                                   // include gcc builtins | 
|---|
| 206 | // -l is for initial build ONLY and builtins.cf is not in the lib directory so access it here. | 
|---|
| 207 |  | 
|---|
| 208 | // Read to gcc builtins, if not generating the cfa library | 
|---|
| 209 | FILE * gcc_builtins = fopen( libcfap | treep ? "../prelude/gcc-builtins.cf" : CFA_LIBDIR "/gcc-builtins.cf", "r" ); | 
|---|
| 210 | assertf( gcc_builtins, "cannot open gcc-builtins.cf\n" ); | 
|---|
| 211 | parse( gcc_builtins, LinkageSpec::Compiler ); | 
|---|
| 212 |  | 
|---|
| 213 | // read the extra prelude in, if not generating the cfa library | 
|---|
| 214 | FILE * extras = fopen( libcfap | treep ? "../prelude/extras.cf" : CFA_LIBDIR "/extras.cf", "r" ); | 
|---|
| 215 | assertf( extras, "cannot open extras.cf\n" ); | 
|---|
| 216 | parse( extras, LinkageSpec::BuiltinC ); | 
|---|
| 217 |  | 
|---|
| 218 | if ( ! libcfap ) { | 
|---|
| 219 | // read the prelude in, if not generating the cfa library | 
|---|
| 220 | FILE * prelude = fopen( treep ? "../prelude/prelude.cf" : CFA_LIBDIR "/prelude.cf", "r" ); | 
|---|
| 221 | assertf( prelude, "cannot open prelude.cf\n" ); | 
|---|
| 222 | parse( prelude, LinkageSpec::Intrinsic ); | 
|---|
| 223 |  | 
|---|
| 224 | // Read to cfa builtins, if not generating the cfa library | 
|---|
| 225 | FILE * builtins = fopen( libcfap | treep ? "../prelude/builtins.cf" : CFA_LIBDIR "/builtins.cf", "r" ); | 
|---|
| 226 | assertf( builtins, "cannot open builtins.cf\n" ); | 
|---|
| 227 | parse( builtins, LinkageSpec::BuiltinCFA ); | 
|---|
| 228 | } // if | 
|---|
| 229 | } // if | 
|---|
| 230 |  | 
|---|
| 231 | parse( input, libcfap ? LinkageSpec::Intrinsic : LinkageSpec::Cforall, yydebug ); | 
|---|
| 232 |  | 
|---|
| 233 | if ( parsep ) { | 
|---|
| 234 | parseTree->printList( cout ); | 
|---|
| 235 | delete parseTree; | 
|---|
| 236 | return 0; | 
|---|
| 237 | } // if | 
|---|
| 238 |  | 
|---|
| 239 | buildList( parseTree, translationUnit ); | 
|---|
| 240 | delete parseTree; | 
|---|
| 241 | parseTree = nullptr; | 
|---|
| 242 |  | 
|---|
| 243 | if ( astp ) { | 
|---|
| 244 | dump( translationUnit ); | 
|---|
| 245 | return 0; | 
|---|
| 246 | } // if | 
|---|
| 247 |  | 
|---|
| 248 | // add the assignment statement after the initialization of a type parameter | 
|---|
| 249 | PASS( "validate", SymTab::validate( translationUnit, symtabp ) ); | 
|---|
| 250 | if ( symtabp ) { | 
|---|
| 251 | deleteAll( translationUnit ); | 
|---|
| 252 | return 0; | 
|---|
| 253 | } // if | 
|---|
| 254 |  | 
|---|
| 255 | if ( expraltp ) { | 
|---|
| 256 | PassVisitor<ResolvExpr::AlternativePrinter> printer( cout ); | 
|---|
| 257 | acceptAll( translationUnit, printer ); | 
|---|
| 258 | return 0; | 
|---|
| 259 | } // if | 
|---|
| 260 |  | 
|---|
| 261 | if ( validp ) { | 
|---|
| 262 | dump( translationUnit ); | 
|---|
| 263 | return 0; | 
|---|
| 264 | } // if | 
|---|
| 265 |  | 
|---|
| 266 | PASS( "mutate", ControlStruct::mutate( translationUnit ) ); | 
|---|
| 267 | PASS( "fixNames", CodeGen::fixNames( translationUnit ) ); | 
|---|
| 268 | PASS( "genInit", InitTweak::genInit( translationUnit ) ); | 
|---|
| 269 | PASS( "expandMemberTuples" , Tuples::expandMemberTuples( translationUnit ) ); | 
|---|
| 270 | if ( libcfap ) { | 
|---|
| 271 | // generate the bodies of cfa library functions | 
|---|
| 272 | LibCfa::makeLibCfa( translationUnit ); | 
|---|
| 273 | } // if | 
|---|
| 274 |  | 
|---|
| 275 | if ( declstatsp ) { | 
|---|
| 276 | CodeTools::printDeclStats( translationUnit ); | 
|---|
| 277 | deleteAll( translationUnit ); | 
|---|
| 278 | return 0; | 
|---|
| 279 | } | 
|---|
| 280 |  | 
|---|
| 281 | if ( bresolvep ) { | 
|---|
| 282 | dump( translationUnit ); | 
|---|
| 283 | return 0; | 
|---|
| 284 | } // if | 
|---|
| 285 |  | 
|---|
| 286 | CodeTools::fillLocations( translationUnit ); | 
|---|
| 287 |  | 
|---|
| 288 | PASS( "resolve", ResolvExpr::resolve( translationUnit ) ); | 
|---|
| 289 | if ( exprp ) { | 
|---|
| 290 | dump( translationUnit ); | 
|---|
| 291 | return 0; | 
|---|
| 292 | } // if | 
|---|
| 293 |  | 
|---|
| 294 | // fix ObjectDecl - replaces ConstructorInit nodes | 
|---|
| 295 | PASS( "fixInit", InitTweak::fix( translationUnit, filename, libcfap || treep ) ); | 
|---|
| 296 | if ( ctorinitp ) { | 
|---|
| 297 | dump ( translationUnit ); | 
|---|
| 298 | return 0; | 
|---|
| 299 | } // if | 
|---|
| 300 |  | 
|---|
| 301 | PASS( "expandUniqueExpr", Tuples::expandUniqueExpr( translationUnit ) ); // xxx - is this the right place for this? want to expand ASAP so tha, sequent passes don't need to worry about double-visiting a unique expr - needs to go after InitTweak::fix so that copy constructed return declarations are reused | 
|---|
| 302 | Tuples::expandUniqueExpr( translationUnit ); | 
|---|
| 303 |  | 
|---|
| 304 | PASS( "translateEHM" , ControlStruct::translateEHM( translationUnit ) ); | 
|---|
| 305 |  | 
|---|
| 306 | PASS( "generateWaitfor" , Concurrency::generateWaitFor( translationUnit ) ); | 
|---|
| 307 |  | 
|---|
| 308 | PASS( "convertSpecializations",  GenPoly::convertSpecializations( translationUnit ) ); // needs to happen before tuple types are expanded | 
|---|
| 309 |  | 
|---|
| 310 | PASS( "expandTuples", Tuples::expandTuples( translationUnit ) ); // xxx - is this the right place for this? | 
|---|
| 311 |  | 
|---|
| 312 | if ( tuplep ) { | 
|---|
| 313 | dump( translationUnit ); | 
|---|
| 314 | return 0; | 
|---|
| 315 | } | 
|---|
| 316 |  | 
|---|
| 317 | PASS( "virtual expandCasts", Virtual::expandCasts( translationUnit ) ); // Must come after translateEHM | 
|---|
| 318 |  | 
|---|
| 319 | PASS( "instantiateGenerics", GenPoly::instantiateGeneric( translationUnit ) ); | 
|---|
| 320 | if ( genericsp ) { | 
|---|
| 321 | dump( translationUnit ); | 
|---|
| 322 | return 0; | 
|---|
| 323 | } | 
|---|
| 324 | PASS( "convertLvalue", GenPoly::convertLvalue( translationUnit ) ); | 
|---|
| 325 |  | 
|---|
| 326 |  | 
|---|
| 327 | if ( bboxp ) { | 
|---|
| 328 | dump( translationUnit ); | 
|---|
| 329 | return 0; | 
|---|
| 330 | } // if | 
|---|
| 331 | PASS( "box", GenPoly::box( translationUnit ) ); | 
|---|
| 332 |  | 
|---|
| 333 | if ( bcodegenp ) { | 
|---|
| 334 | dump( translationUnit ); | 
|---|
| 335 | return 0; | 
|---|
| 336 | } | 
|---|
| 337 |  | 
|---|
| 338 | if ( optind < argc ) {                                                  // any commands after the flags and input file ? => output file name | 
|---|
| 339 | output = new ofstream( argv[ optind ] ); | 
|---|
| 340 | } // if | 
|---|
| 341 |  | 
|---|
| 342 | CodeTools::fillLocations( translationUnit ); | 
|---|
| 343 | PASS( "codegen", CodeGen::generate( translationUnit, *output, ! noprotop, prettycodegenp, true, linemarks ) ); | 
|---|
| 344 |  | 
|---|
| 345 | CodeGen::FixMain::fix( *output, treep ? "../prelude/bootloader.c" : CFA_LIBDIR "/bootloader.c" ); | 
|---|
| 346 | if ( output != &cout ) { | 
|---|
| 347 | delete output; | 
|---|
| 348 | } // if | 
|---|
| 349 | } catch ( SemanticErrorException &e ) { | 
|---|
| 350 | if ( errorp ) { | 
|---|
| 351 | cerr << "---AST at error:---" << endl; | 
|---|
| 352 | dump( translationUnit, cerr ); | 
|---|
| 353 | cerr << endl << "---End of AST, begin error message:---\n" << endl; | 
|---|
| 354 | } // if | 
|---|
| 355 | e.print(); | 
|---|
| 356 | if ( output != &cout ) { | 
|---|
| 357 | delete output; | 
|---|
| 358 | } // if | 
|---|
| 359 | return 1; | 
|---|
| 360 | } catch ( UnimplementedError &e ) { | 
|---|
| 361 | cout << "Sorry, " << e.get_what() << " is not currently implemented" << endl; | 
|---|
| 362 | if ( output != &cout ) { | 
|---|
| 363 | delete output; | 
|---|
| 364 | } // if | 
|---|
| 365 | return 1; | 
|---|
| 366 | } catch ( CompilerError &e ) { | 
|---|
| 367 | cerr << "Compiler Error: " << e.get_what() << endl; | 
|---|
| 368 | cerr << "(please report bugs to [REDACTED])" << endl; | 
|---|
| 369 | if ( output != &cout ) { | 
|---|
| 370 | delete output; | 
|---|
| 371 | } // if | 
|---|
| 372 | return 1; | 
|---|
| 373 | } catch(...) { | 
|---|
| 374 | std::exception_ptr eptr = std::current_exception(); | 
|---|
| 375 | try { | 
|---|
| 376 | if (eptr) { | 
|---|
| 377 | std::rethrow_exception(eptr); | 
|---|
| 378 | } | 
|---|
| 379 | else { | 
|---|
| 380 | std::cerr << "Exception Uncaught and Unkown" << std::endl; | 
|---|
| 381 | } | 
|---|
| 382 | } catch(const std::exception& e) { | 
|---|
| 383 | std::cerr << "Unaught Exception \"" << e.what() << "\"\n"; | 
|---|
| 384 | } | 
|---|
| 385 | return 1; | 
|---|
| 386 | }// try | 
|---|
| 387 |  | 
|---|
| 388 | deleteAll( translationUnit ); | 
|---|
| 389 | if(!libcfap && !treep) HeapStats::printStats(); | 
|---|
| 390 | return 0; | 
|---|
| 391 | } // main | 
|---|
| 392 |  | 
|---|
| 393 | void parse_cmdline( int argc, char * argv[], const char *& filename ) { | 
|---|
| 394 | enum { Ast, Bbox, Bresolver, CtorInitFix, DeclStats, Expr, ExprAlt, Grammar, LibCFA, Linemarks, Nolinemarks, Nopreamble, Parse, Prototypes, Resolver, Symbol, Tree, TupleExpansion, Validate, }; | 
|---|
| 395 |  | 
|---|
| 396 | static struct option long_opts[] = { | 
|---|
| 397 | { "ast", no_argument, 0, Ast }, | 
|---|
| 398 | { "before-box", no_argument, 0, Bbox }, | 
|---|
| 399 | { "before-resolver", no_argument, 0, Bresolver }, | 
|---|
| 400 | { "ctorinitfix", no_argument, 0, CtorInitFix }, | 
|---|
| 401 | { "decl-stats", no_argument, 0, DeclStats }, | 
|---|
| 402 | { "expr", no_argument, 0, Expr }, | 
|---|
| 403 | { "expralt", no_argument, 0, ExprAlt }, | 
|---|
| 404 | { "grammar", no_argument, 0, Grammar }, | 
|---|
| 405 | { "libcfa", no_argument, 0, LibCFA }, | 
|---|
| 406 | { "line-marks", no_argument, 0, Linemarks }, | 
|---|
| 407 | { "no-line-marks", no_argument, 0, Nolinemarks }, | 
|---|
| 408 | { "no-preamble", no_argument, 0, Nopreamble }, | 
|---|
| 409 | { "parse", no_argument, 0, Parse }, | 
|---|
| 410 | { "no-prototypes", no_argument, 0, Prototypes }, | 
|---|
| 411 | { "resolver", no_argument, 0, Resolver }, | 
|---|
| 412 | { "symbol", no_argument, 0, Symbol }, | 
|---|
| 413 | { "tree", no_argument, 0, Tree }, | 
|---|
| 414 | { "tuple-expansion", no_argument, 0, TupleExpansion }, | 
|---|
| 415 | { "validate", no_argument, 0, Validate }, | 
|---|
| 416 | { 0, 0, 0, 0 } | 
|---|
| 417 | }; // long_opts | 
|---|
| 418 | int long_index; | 
|---|
| 419 |  | 
|---|
| 420 | opterr = 0;                                                                                     // (global) prevent getopt from printing error messages | 
|---|
| 421 |  | 
|---|
| 422 | bool Wsuppress = false, Werror = false; | 
|---|
| 423 | int c; | 
|---|
| 424 | while ( (c = getopt_long( argc, argv, "abBcCdefgGlLmnNpqrstTvwW:yzZD:F:", long_opts, &long_index )) != -1 ) { | 
|---|
| 425 | switch ( c ) { | 
|---|
| 426 | case Ast: | 
|---|
| 427 | case 'a':                                                                             // dump AST | 
|---|
| 428 | astp = true; | 
|---|
| 429 | break; | 
|---|
| 430 | case Bresolver: | 
|---|
| 431 | case 'b':                                                                             // print before resolver steps | 
|---|
| 432 | bresolvep = true; | 
|---|
| 433 | break; | 
|---|
| 434 | case 'B':                                                                             // print before box steps | 
|---|
| 435 | bboxp = true; | 
|---|
| 436 | break; | 
|---|
| 437 | case CtorInitFix: | 
|---|
| 438 | case 'c':                                                                             // print after constructors and destructors are replaced | 
|---|
| 439 | ctorinitp = true; | 
|---|
| 440 | break; | 
|---|
| 441 | case 'C':                                                                             // print before code generation | 
|---|
| 442 | bcodegenp = true; | 
|---|
| 443 | break; | 
|---|
| 444 | case DeclStats: | 
|---|
| 445 | case 'd': | 
|---|
| 446 | declstatsp = true; | 
|---|
| 447 | break; | 
|---|
| 448 | case Expr: | 
|---|
| 449 | case 'e':                                                                             // dump AST after expression analysis | 
|---|
| 450 | exprp = true; | 
|---|
| 451 | break; | 
|---|
| 452 | case ExprAlt: | 
|---|
| 453 | case 'f':                                                                             // print alternatives for expressions | 
|---|
| 454 | expraltp = true; | 
|---|
| 455 | break; | 
|---|
| 456 | case Grammar: | 
|---|
| 457 | case 'g':                                                                             // bison debugging info (grammar rules) | 
|---|
| 458 | yydebug = true; | 
|---|
| 459 | break; | 
|---|
| 460 | case 'G':                                                                             // dump AST after instantiate generics | 
|---|
| 461 | genericsp = true; | 
|---|
| 462 | break; | 
|---|
| 463 | case LibCFA: | 
|---|
| 464 | case 'l':                                                                             // generate libcfa.c | 
|---|
| 465 | libcfap = true; | 
|---|
| 466 | break; | 
|---|
| 467 | case Linemarks: | 
|---|
| 468 | case 'L':                                                                             // print lines marks | 
|---|
| 469 | linemarks = true; | 
|---|
| 470 | break; | 
|---|
| 471 | case Nopreamble: | 
|---|
| 472 | case 'n':                                                                             // do not read preamble | 
|---|
| 473 | nopreludep = true; | 
|---|
| 474 | break; | 
|---|
| 475 | case Nolinemarks: | 
|---|
| 476 | case 'N':                                                                             // suppress line marks | 
|---|
| 477 | linemarks = false; | 
|---|
| 478 | break; | 
|---|
| 479 | case Prototypes: | 
|---|
| 480 | case 'p':                                                                             // generate prototypes for preamble functions | 
|---|
| 481 | noprotop = true; | 
|---|
| 482 | break; | 
|---|
| 483 | case 'm':                                                                             // don't replace the main | 
|---|
| 484 | nomainp = true; | 
|---|
| 485 | break; | 
|---|
| 486 | case Parse: | 
|---|
| 487 | case 'q':                                                                             // dump parse tree | 
|---|
| 488 | parsep = true; | 
|---|
| 489 | break; | 
|---|
| 490 | case Resolver: | 
|---|
| 491 | case 'r':                                                                             // print resolver steps | 
|---|
| 492 | resolvep = true; | 
|---|
| 493 | break; | 
|---|
| 494 | case Symbol: | 
|---|
| 495 | case 's':                                                                             // print symbol table events | 
|---|
| 496 | symtabp = true; | 
|---|
| 497 | break; | 
|---|
| 498 | case Tree: | 
|---|
| 499 | case 't':                                                                             // build in tree | 
|---|
| 500 | treep = true; | 
|---|
| 501 | break; | 
|---|
| 502 | case TupleExpansion: | 
|---|
| 503 | case 'T':                                                                             // print after tuple expansion | 
|---|
| 504 | tuplep = true; | 
|---|
| 505 | break; | 
|---|
| 506 | case 'v':                                                                             // dump AST after decl validation pass | 
|---|
| 507 | validp = true; | 
|---|
| 508 | break; | 
|---|
| 509 | case 'w': | 
|---|
| 510 | Wsuppress = true; | 
|---|
| 511 | break; | 
|---|
| 512 | case 'W': | 
|---|
| 513 | if ( strcmp( optarg, "all" ) == 0 ) { | 
|---|
| 514 | SemanticWarning_EnableAll(); | 
|---|
| 515 | } else if ( strcmp( optarg, "error" ) == 0 ) { | 
|---|
| 516 | Werror = true; | 
|---|
| 517 | } else { | 
|---|
| 518 | char * warning = optarg; | 
|---|
| 519 | Severity s; | 
|---|
| 520 | if ( strncmp( optarg, "no-", 3 ) == 0 ) { | 
|---|
| 521 | warning += 3; | 
|---|
| 522 | s = Severity::Suppress; | 
|---|
| 523 | } else { | 
|---|
| 524 | s = Severity::Warn; | 
|---|
| 525 | } // if | 
|---|
| 526 | SemanticWarning_Set( warning, s ); | 
|---|
| 527 | } // if | 
|---|
| 528 | break; | 
|---|
| 529 | case 'y':                                                                             // dump AST on error | 
|---|
| 530 | errorp = true; | 
|---|
| 531 | break; | 
|---|
| 532 | case 'z':                                                                             // dump as codegen rather than AST | 
|---|
| 533 | codegenp = true; | 
|---|
| 534 | break; | 
|---|
| 535 | case 'Z':                                                                       // prettyprint during codegen (i.e. print unmangled names, etc.) | 
|---|
| 536 | prettycodegenp = true; | 
|---|
| 537 | break; | 
|---|
| 538 | case 'D':                                                                             // ignore -Dxxx | 
|---|
| 539 | break; | 
|---|
| 540 | case 'F':                                                                             // source file-name without suffix | 
|---|
| 541 | filename = optarg; | 
|---|
| 542 | break; | 
|---|
| 543 | case '?': | 
|---|
| 544 | if ( optopt ) {                                                         // short option ? | 
|---|
| 545 | assertf( false, "Unknown option: -%c\n", (char)optopt ); | 
|---|
| 546 | } else { | 
|---|
| 547 | assertf( false, "Unknown option: %s\n", argv[optind - 1] ); | 
|---|
| 548 | } // if | 
|---|
| 549 | #if defined(__GNUC__) && __GNUC__ >= 7 | 
|---|
| 550 | __attribute__((fallthrough)); | 
|---|
| 551 | #endif | 
|---|
| 552 | default: | 
|---|
| 553 | abort(); | 
|---|
| 554 | } // switch | 
|---|
| 555 | } // while | 
|---|
| 556 |  | 
|---|
| 557 | if ( Werror ) { | 
|---|
| 558 | SemanticWarning_WarningAsError(); | 
|---|
| 559 | } // if | 
|---|
| 560 | if ( Wsuppress ) { | 
|---|
| 561 | SemanticWarning_SuppressAll(); | 
|---|
| 562 | } // if | 
|---|
| 563 | // for ( const auto w : WarningFormats ) { | 
|---|
| 564 | //      cout << w.name << ' ' << (int)w.severity << endl; | 
|---|
| 565 | // } // for | 
|---|
| 566 | } // parse_cmdline | 
|---|
| 567 |  | 
|---|
| 568 | static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit ) { | 
|---|
| 569 | extern int yyparse( void ); | 
|---|
| 570 | extern FILE * yyin; | 
|---|
| 571 | extern int yylineno; | 
|---|
| 572 |  | 
|---|
| 573 | ::linkage = linkage;                                                            // set globals | 
|---|
| 574 | yyin = input; | 
|---|
| 575 | yylineno = 1; | 
|---|
| 576 | typedefTable.enterScope(); | 
|---|
| 577 | int parseStatus = yyparse(); | 
|---|
| 578 |  | 
|---|
| 579 | fclose( input ); | 
|---|
| 580 | if ( shouldExit || parseStatus != 0 ) { | 
|---|
| 581 | exit( parseStatus ); | 
|---|
| 582 | } // if | 
|---|
| 583 | } // parse | 
|---|
| 584 |  | 
|---|
| 585 | static bool notPrelude( Declaration * decl ) { | 
|---|
| 586 | return ! LinkageSpec::isBuiltin( decl->get_linkage() ); | 
|---|
| 587 | } // notPrelude | 
|---|
| 588 |  | 
|---|
| 589 | static void dump( list< Declaration * > & translationUnit, ostream & out ) { | 
|---|
| 590 | list< Declaration * > decls; | 
|---|
| 591 |  | 
|---|
| 592 | if ( noprotop ) { | 
|---|
| 593 | filter( translationUnit.begin(), translationUnit.end(), back_inserter( decls ), notPrelude ); | 
|---|
| 594 | } else { | 
|---|
| 595 | decls = translationUnit; | 
|---|
| 596 | } // if | 
|---|
| 597 |  | 
|---|
| 598 | // depending on commandline options, either generate code or dump the AST | 
|---|
| 599 | if ( codegenp ) { | 
|---|
| 600 | CodeGen::generate( decls, out, ! noprotop, prettycodegenp ); | 
|---|
| 601 | } else { | 
|---|
| 602 | printAll( decls, out ); | 
|---|
| 603 | } | 
|---|
| 604 | deleteAll( translationUnit ); | 
|---|
| 605 | } // dump | 
|---|
| 606 |  | 
|---|
| 607 | // Local Variables: // | 
|---|
| 608 | // tab-width: 4 // | 
|---|
| 609 | // mode: c++ // | 
|---|
| 610 | // compile-command: "make install" // | 
|---|
| 611 | // End:  // | 
|---|