| [b87a5ed] | 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 | // | 
|---|
| [71f4e4f] | 7 | // main.cc -- | 
|---|
| [b87a5ed] | 8 | // | 
|---|
| [3e96559] | 9 | // Author           : Peter Buhr and Rob Schluntz | 
|---|
| [b87a5ed] | 10 | // Created On       : Fri May 15 23:12:02 2015 | 
|---|
| [046a890] | 11 | // Last Modified By : Andrew Beach | 
|---|
|  | 12 | // Last Modified On : Tue May 19 12:03:00 2020 | 
|---|
|  | 13 | // Update Count     : 634 | 
|---|
| [b87a5ed] | 14 | // | 
|---|
|  | 15 |  | 
|---|
| [bf2438c] | 16 | #include <cxxabi.h>                         // for __cxa_demangle | 
|---|
|  | 17 | #include <execinfo.h>                       // for backtrace, backtrace_symbols | 
|---|
|  | 18 | #include <getopt.h>                         // for no_argument, optind, geto... | 
|---|
| [08fc48f] | 19 | #include <cassert>                          // for assertf | 
|---|
| [bf2438c] | 20 | #include <cstdio>                           // for fopen, FILE, fclose, stdin | 
|---|
|  | 21 | #include <cstdlib>                          // for exit, free, abort, EXIT_F... | 
|---|
| [bffcd66] | 22 | #include <csignal>                          // for signal, SIGABRT, SIGSEGV | 
|---|
| [bf2438c] | 23 | #include <cstring>                          // for index | 
|---|
| [be9288a] | 24 | #include <fstream>                          // for ofstream | 
|---|
| [bf2438c] | 25 | #include <iostream>                         // for operator<<, basic_ostream | 
|---|
| [62ce290] | 26 | #include <iomanip> | 
|---|
| [bf2438c] | 27 | #include <iterator>                         // for back_inserter | 
|---|
|  | 28 | #include <list>                             // for list | 
|---|
| [08fc48f] | 29 | #include <string>                           // for char_traits, operator<< | 
|---|
| [e6955b1] | 30 |  | 
|---|
| [bffcd66] | 31 | using namespace std; | 
|---|
|  | 32 |  | 
|---|
|  | 33 |  | 
|---|
| [7f38b67a] | 34 | #include "CompilationState.h" | 
|---|
| [bf2438c] | 35 | #include "../config.h"                      // for CFA_LIBDIR | 
|---|
|  | 36 | #include "CodeGen/FixMain.h"                // for FixMain | 
|---|
|  | 37 | #include "CodeGen/FixNames.h"               // for fixNames | 
|---|
|  | 38 | #include "CodeGen/Generate.h"               // for generate | 
|---|
|  | 39 | #include "CodeTools/DeclStats.h"            // for printDeclStats | 
|---|
| [3b3491b6] | 40 | #include "CodeTools/ResolvProtoDump.h"      // for dumpAsResolvProto | 
|---|
| [bf2438c] | 41 | #include "CodeTools/TrackLoc.h"             // for fillLocations | 
|---|
|  | 42 | #include "Common/CompilerError.h"           // for CompilerError | 
|---|
| [7abee38] | 43 | #include "Common/Stats.h" | 
|---|
| [cbbd5b48] | 44 | #include "Common/PassVisitor.h" | 
|---|
| [bf2438c] | 45 | #include "Common/SemanticError.h"           // for SemanticError | 
|---|
|  | 46 | #include "Common/UnimplementedError.h"      // for UnimplementedError | 
|---|
|  | 47 | #include "Common/utility.h"                 // for deleteAll, filter, printAll | 
|---|
| [9f5ecf5] | 48 | #include "Concurrency/Waitfor.h"            // for generateWaitfor | 
|---|
| [bf2438c] | 49 | #include "ControlStruct/ExceptTranslate.h"  // for translateEHM | 
|---|
|  | 50 | #include "ControlStruct/Mutate.h"           // for mutate | 
|---|
|  | 51 | #include "GenPoly/Box.h"                    // for box | 
|---|
|  | 52 | #include "GenPoly/InstantiateGeneric.h"     // for instantiateGeneric | 
|---|
|  | 53 | #include "GenPoly/Lvalue.h"                 // for convertLvalue | 
|---|
|  | 54 | #include "GenPoly/Specialize.h"             // for convertSpecializations | 
|---|
|  | 55 | #include "InitTweak/FixInit.h"              // for fix | 
|---|
|  | 56 | #include "InitTweak/GenInit.h"              // for genInit | 
|---|
|  | 57 | #include "MakeLibCfa.h"                     // for makeLibCfa | 
|---|
|  | 58 | #include "Parser/ParseNode.h"               // for DeclarationNode, buildList | 
|---|
|  | 59 | #include "Parser/TypedefTable.h"            // for TypedefTable | 
|---|
|  | 60 | #include "ResolvExpr/AlternativePrinter.h"  // for AlternativePrinter | 
|---|
|  | 61 | #include "ResolvExpr/Resolver.h"            // for resolve | 
|---|
|  | 62 | #include "SymTab/Validate.h"                // for validate | 
|---|
| [bffcd66] | 63 | #include "SynTree/LinkageSpec.h"            // for Spec, Cforall, Intrinsic | 
|---|
| [bf2438c] | 64 | #include "SynTree/Declaration.h"            // for Declaration | 
|---|
|  | 65 | #include "SynTree/Visitor.h"                // for acceptAll | 
|---|
|  | 66 | #include "Tuples/Tuples.h"                  // for expandMemberTuples, expan... | 
|---|
| [a5f0529] | 67 | #include "Virtual/ExpandCasts.h"            // for expandCasts | 
|---|
| [51b73452] | 68 |  | 
|---|
| [4615ac8] | 69 |  | 
|---|
| [3e96559] | 70 | static void NewPass( const char * const name ) { | 
|---|
|  | 71 | Stats::Heap::newPass( name ); | 
|---|
| [1cb7fab2] | 72 | using namespace Stats::Counters; | 
|---|
| [b8665e3] | 73 | { | 
|---|
| [3e96559] | 74 | static auto group = build<CounterGroup>( "Pass Visitor" ); | 
|---|
|  | 75 | auto pass = build<CounterGroup>( name, group ); | 
|---|
| [b8665e3] | 76 | pass_visitor_stats.depth = 0; | 
|---|
| [3e96559] | 77 | pass_visitor_stats.avg = build<AverageCounter<double>>( "Average Depth", pass ); | 
|---|
|  | 78 | pass_visitor_stats.max = build<MaxCounter<double>>( "Max Depth", pass ); | 
|---|
| [b8665e3] | 79 | } | 
|---|
|  | 80 | { | 
|---|
| [3e96559] | 81 | static auto group = build<CounterGroup>( "Syntax Node" ); | 
|---|
|  | 82 | auto pass = build<CounterGroup>( name, group ); | 
|---|
|  | 83 | BaseSyntaxNode::new_nodes = build<SimpleCounter>( "Allocs", pass ); | 
|---|
| [b8665e3] | 84 | } | 
|---|
| [675716e] | 85 | } | 
|---|
|  | 86 |  | 
|---|
| [3e96559] | 87 | #define PASS( name, pass )                  \ | 
|---|
| [ecaeac6e] | 88 | if ( errorp ) { cerr << name << endl; } \ | 
|---|
| [675716e] | 89 | NewPass(name);                          \ | 
|---|
| [4f97937] | 90 | Stats::Time::StartBlock(name);          \ | 
|---|
|  | 91 | pass;                                   \ | 
|---|
|  | 92 | Stats::Time::StopBlock(); | 
|---|
| [0da3e2c] | 93 |  | 
|---|
| [8b7ee09] | 94 | LinkageSpec::Spec linkage = LinkageSpec::Cforall; | 
|---|
| [0da3e2c] | 95 | TypedefTable typedefTable; | 
|---|
| [cbaee0d] | 96 | DeclarationNode * parseTree = nullptr;                                  // program parse tree | 
|---|
| [81419b5] | 97 |  | 
|---|
| [ef22ad6] | 98 | static bool waiting_for_gdb = false;                                    // flag to set cfa-cpp to wait for gdb on start | 
|---|
| [dee1f89] | 99 |  | 
|---|
| [bffcd66] | 100 | static string PreludeDirector = ""; | 
|---|
| [4dcaed2] | 101 |  | 
|---|
| [e0bd0f9] | 102 | static void parse_cmdline( int argc, char *argv[] ); | 
|---|
| [8b7ee09] | 103 | static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit = false ); | 
|---|
| [e6955b1] | 104 | static void dump( list< Declaration * > & translationUnit, ostream & out = cout ); | 
|---|
|  | 105 |  | 
|---|
| [0afffee] | 106 | static void backtrace( int start ) {                                    // skip first N stack frames | 
|---|
| [74330e7] | 107 | enum { Frames = 50, };                                                          // maximum number of stack frames | 
|---|
| [e6955b1] | 108 | void * array[Frames]; | 
|---|
| [74330e7] | 109 | size_t size = ::backtrace( array, Frames ); | 
|---|
| [0afffee] | 110 | char ** messages = ::backtrace_symbols( array, size ); // does not demangle names | 
|---|
|  | 111 |  | 
|---|
|  | 112 | *index( messages[0], '(' ) = '\0';                                      // find executable name | 
|---|
|  | 113 | cerr << "Stack back trace for: " << messages[0] << endl; | 
|---|
| [e6955b1] | 114 |  | 
|---|
| [b542bfb] | 115 | // skip last 2 stack frames after main | 
|---|
| [74330e7] | 116 | for ( unsigned int i = start; i < size - 2 && messages != nullptr; i += 1 ) { | 
|---|
| [e6955b1] | 117 | char * mangled_name = nullptr, * offset_begin = nullptr, * offset_end = nullptr; | 
|---|
| [7006ba5] | 118 |  | 
|---|
|  | 119 | for ( char * p = messages[i]; *p; p += 1 ) {    // find parantheses and +offset | 
|---|
| [0afffee] | 120 | if ( *p == '(' ) { | 
|---|
| [46f6134] | 121 | mangled_name = p; | 
|---|
| [0afffee] | 122 | } else if ( *p == '+' ) { | 
|---|
| [e6955b1] | 123 | offset_begin = p; | 
|---|
| [0afffee] | 124 | } else if ( *p == ')' ) { | 
|---|
| [e6955b1] | 125 | offset_end = p; | 
|---|
|  | 126 | break; | 
|---|
|  | 127 | } // if | 
|---|
|  | 128 | } // for | 
|---|
|  | 129 |  | 
|---|
|  | 130 | // if line contains symbol, attempt to demangle | 
|---|
| [b542bfb] | 131 | int frameNo = i - start; | 
|---|
| [e6955b1] | 132 | if ( mangled_name && offset_begin && offset_end && mangled_name < offset_begin ) { | 
|---|
| [0afffee] | 133 | *mangled_name++ = '\0';                                         // delimit strings | 
|---|
| [e6955b1] | 134 | *offset_begin++ = '\0'; | 
|---|
|  | 135 | *offset_end++ = '\0'; | 
|---|
|  | 136 |  | 
|---|
| [0afffee] | 137 | int status; | 
|---|
| [e6955b1] | 138 | char * real_name = __cxxabiv1::__cxa_demangle( mangled_name, 0, 0, &status ); | 
|---|
| [0afffee] | 139 | // bug in __cxa_demangle for single-character lower-case non-mangled names | 
|---|
| [e6955b1] | 140 | if ( status == 0 ) {                                            // demangling successful ? | 
|---|
| [b542bfb] | 141 | cerr << "(" << frameNo << ") " << messages[i] << " : " | 
|---|
| [e6955b1] | 142 | << real_name << "+" << offset_begin << offset_end << endl; | 
|---|
|  | 143 | } else {                                                                        // otherwise, output mangled name | 
|---|
| [b542bfb] | 144 | cerr << "(" << frameNo << ") " << messages[i] << " : " | 
|---|
| [0afffee] | 145 | << mangled_name << "(/*unknown*/)+" << offset_begin << offset_end << endl; | 
|---|
| [e6955b1] | 146 | } // if | 
|---|
| [0afffee] | 147 |  | 
|---|
| [e6955b1] | 148 | free( real_name ); | 
|---|
|  | 149 | } else {                                                                                // otherwise, print the whole line | 
|---|
| [b542bfb] | 150 | cerr << "(" << frameNo << ") " << messages[i] << endl; | 
|---|
| [e6955b1] | 151 | } // if | 
|---|
|  | 152 | } // for | 
|---|
| [b542bfb] | 153 |  | 
|---|
| [e6955b1] | 154 | free( messages ); | 
|---|
| [b542bfb] | 155 | } // backtrace | 
|---|
|  | 156 |  | 
|---|
| [bffcd66] | 157 | #define SIGPARMS int sig __attribute__(( unused )), siginfo_t * sfp __attribute__(( unused )), ucontext_t * cxt __attribute__(( unused )) | 
|---|
|  | 158 |  | 
|---|
|  | 159 | static void Signal( int sig, void (*handler)(SIGPARMS), int flags ) { | 
|---|
|  | 160 | struct sigaction act; | 
|---|
|  | 161 |  | 
|---|
|  | 162 | act.sa_sigaction = (void (*)(int, siginfo_t *, void *))handler; | 
|---|
|  | 163 | act.sa_flags = flags; | 
|---|
|  | 164 |  | 
|---|
|  | 165 | if ( sigaction( sig, &act, nullptr ) == -1 ) { | 
|---|
|  | 166 | cerr << "*CFA runtime error* problem installing signal handler, error(" << errno << ") " << strerror( errno ) << endl; | 
|---|
|  | 167 | _exit( EXIT_FAILURE ); | 
|---|
|  | 168 | } // if | 
|---|
|  | 169 | } // Signal | 
|---|
|  | 170 |  | 
|---|
|  | 171 | static void sigSegvBusHandler( SIGPARMS ) { | 
|---|
|  | 172 | if ( sfp->si_addr == nullptr ) { | 
|---|
|  | 173 | cerr << "Null pointer (nullptr) dereference." << endl; | 
|---|
|  | 174 | } else { | 
|---|
|  | 175 | cerr << (sig == SIGSEGV ? "Segment fault" : "Bus error") << " at memory location " << sfp->si_addr << "." << endl | 
|---|
|  | 176 | << "Possible cause is reading outside the address space or writing to a protected area within the address space with an invalid pointer or subscript." << endl; | 
|---|
|  | 177 | } // if | 
|---|
| [b542bfb] | 178 | backtrace( 2 );                                                                         // skip first 2 stack frames | 
|---|
| [3e96559] | 179 | abort();                                                                                        // cause core dump for debugging | 
|---|
| [e6955b1] | 180 | } // sigSegvBusHandler | 
|---|
| [0da3e2c] | 181 |  | 
|---|
| [74330e7] | 182 | static void sigFpeHandler( SIGPARMS ) { | 
|---|
|  | 183 | const char * msg; | 
|---|
|  | 184 |  | 
|---|
|  | 185 | switch ( sfp->si_code ) { | 
|---|
|  | 186 | case FPE_INTDIV: case FPE_FLTDIV: msg = "divide by zero"; break; | 
|---|
|  | 187 | case FPE_FLTOVF: msg = "overflow"; break; | 
|---|
|  | 188 | case FPE_FLTUND: msg = "underflow"; break; | 
|---|
|  | 189 | case FPE_FLTRES: msg = "inexact result"; break; | 
|---|
|  | 190 | case FPE_FLTINV: msg = "invalid operation"; break; | 
|---|
|  | 191 | default: msg = "unknown"; | 
|---|
|  | 192 | } // choose | 
|---|
|  | 193 | cerr << "Computation error " << msg << " at location " << sfp->si_addr << endl | 
|---|
|  | 194 | << "Possible cause is constant-expression evaluation invalid." << endl; | 
|---|
|  | 195 | backtrace( 2 );                                                                         // skip first 2 stack frames | 
|---|
|  | 196 | abort();                                                                                        // cause core dump for debugging | 
|---|
|  | 197 | } // sigFpeHandler | 
|---|
|  | 198 |  | 
|---|
| [bffcd66] | 199 | static void sigAbortHandler( SIGPARMS ) { | 
|---|
| [b542bfb] | 200 | backtrace( 6 );                                                                         // skip first 6 stack frames | 
|---|
| [bffcd66] | 201 | Signal( SIGABRT, (void (*)(SIGPARMS))SIG_DFL, SA_SIGINFO );     // reset default signal handler | 
|---|
| [9be45a2] | 202 | raise( SIGABRT );                                                                       // reraise SIGABRT | 
|---|
| [b542bfb] | 203 | } // sigAbortHandler | 
|---|
|  | 204 |  | 
|---|
| [cbaee0d] | 205 | int main( int argc, char * argv[] ) { | 
|---|
| [3b8e52c] | 206 | FILE * input;                                                                           // use FILE rather than istream because yyin is FILE | 
|---|
| [d08beee] | 207 | ostream * output = & cout; | 
|---|
| [e6955b1] | 208 | list< Declaration * > translationUnit; | 
|---|
|  | 209 |  | 
|---|
| [bffcd66] | 210 | Signal( SIGSEGV, sigSegvBusHandler, SA_SIGINFO ); | 
|---|
|  | 211 | Signal( SIGBUS, sigSegvBusHandler, SA_SIGINFO ); | 
|---|
| [74330e7] | 212 | Signal( SIGFPE, sigFpeHandler, SA_SIGINFO ); | 
|---|
| [bffcd66] | 213 | Signal( SIGABRT, sigAbortHandler, SA_SIGINFO ); | 
|---|
| [b87a5ed] | 214 |  | 
|---|
| [bffcd66] | 215 | // cout << "main" << endl; | 
|---|
| [44bca7f] | 216 | // for ( int i = 0; i < argc; i += 1 ) { | 
|---|
| [bffcd66] | 217 | //      cout << '\t' << argv[i] << endl; | 
|---|
| [44bca7f] | 218 | // } // for | 
|---|
|  | 219 |  | 
|---|
| [e0bd0f9] | 220 | parse_cmdline( argc, argv );                                            // process command-line arguments | 
|---|
| [13de47bc] | 221 | CodeGen::FixMain::setReplaceMain( !nomainp ); | 
|---|
| [b87a5ed] | 222 |  | 
|---|
| [ef22ad6] | 223 | if ( waiting_for_gdb ) { | 
|---|
| [bffcd66] | 224 | cerr << "Waiting for gdb" << endl; | 
|---|
|  | 225 | cerr << "run :" << endl; | 
|---|
|  | 226 | cerr << "  gdb attach " << getpid() << endl; | 
|---|
| [dee1f89] | 227 | raise(SIGSTOP); | 
|---|
| [ef22ad6] | 228 | } // if | 
|---|
| [dee1f89] | 229 |  | 
|---|
| [b87a5ed] | 230 | try { | 
|---|
| [81419b5] | 231 | // choose to read the program from a file or stdin | 
|---|
| [3b8e52c] | 232 | if ( optind < argc ) {                                                  // any commands after the flags ? => input file name | 
|---|
| [b87a5ed] | 233 | input = fopen( argv[ optind ], "r" ); | 
|---|
| [e0bd0f9] | 234 | assertf( input, "cannot open %s because %s\n", argv[ optind ], strerror( errno ) ); | 
|---|
| [b87a5ed] | 235 | optind += 1; | 
|---|
| [3b8e52c] | 236 | } else {                                                                                // no input file name | 
|---|
| [b87a5ed] | 237 | input = stdin; | 
|---|
|  | 238 | } // if | 
|---|
|  | 239 |  | 
|---|
| [79eaeb7] | 240 | Stats::Time::StartGlobal(); | 
|---|
| [3c0d4cd] | 241 | NewPass("Parse"); | 
|---|
|  | 242 | Stats::Time::StartBlock("Parse"); | 
|---|
| [675716e] | 243 |  | 
|---|
| [159c62e] | 244 | // read in the builtins, extras, and the prelude | 
|---|
| [de62360d] | 245 | if ( ! nopreludep ) {                                                   // include gcc builtins | 
|---|
| [faf8857] | 246 | // -l is for initial build ONLY and builtins.cf is not in the lib directory so access it here. | 
|---|
| [807ce84] | 247 |  | 
|---|
| [37fe352] | 248 | assertf( !PreludeDirector.empty(), "Can't find prelude without option --prelude-dir must be used." ); | 
|---|
| [4dcaed2] | 249 |  | 
|---|
| [807ce84] | 250 | // Read to gcc builtins, if not generating the cfa library | 
|---|
| [37fe352] | 251 | FILE * gcc_builtins = fopen( (PreludeDirector + "/gcc-builtins.cf").c_str(), "r" ); | 
|---|
| [6ce3ae9] | 252 | assertf( gcc_builtins, "cannot open gcc-builtins.cf\n" ); | 
|---|
|  | 253 | parse( gcc_builtins, LinkageSpec::Compiler ); | 
|---|
| [81419b5] | 254 |  | 
|---|
| [159c62e] | 255 | // read the extra prelude in, if not generating the cfa library | 
|---|
| [37fe352] | 256 | FILE * extras = fopen( (PreludeDirector + "/extras.cf").c_str(), "r" ); | 
|---|
| [3b8e52c] | 257 | assertf( extras, "cannot open extras.cf\n" ); | 
|---|
| [f0994a1] | 258 | parse( extras, LinkageSpec::BuiltinC ); | 
|---|
| [159c62e] | 259 |  | 
|---|
| [81419b5] | 260 | if ( ! libcfap ) { | 
|---|
| [faf8857] | 261 | // read the prelude in, if not generating the cfa library | 
|---|
| [e523b07] | 262 | FILE * prelude = fopen( (PreludeDirector + "/prelude.cfa").c_str(), "r" ); | 
|---|
|  | 263 | assertf( prelude, "cannot open prelude.cfa\n" ); | 
|---|
| [35304009] | 264 | parse( prelude, LinkageSpec::Intrinsic ); | 
|---|
| [fa4805f] | 265 |  | 
|---|
|  | 266 | // Read to cfa builtins, if not generating the cfa library | 
|---|
| [37fe352] | 267 | FILE * builtins = fopen( (PreludeDirector + "/builtins.cf").c_str(), "r" ); | 
|---|
| [fa4805f] | 268 | assertf( builtins, "cannot open builtins.cf\n" ); | 
|---|
| [54d714e] | 269 | parse( builtins, LinkageSpec::BuiltinCFA ); | 
|---|
| [b87a5ed] | 270 | } // if | 
|---|
|  | 271 | } // if | 
|---|
| [81419b5] | 272 |  | 
|---|
| [926af74] | 273 | parse( input, libcfap ? LinkageSpec::Intrinsic : LinkageSpec::Cforall, yydebug ); | 
|---|
| [71f4e4f] | 274 |  | 
|---|
| [b87a5ed] | 275 | if ( parsep ) { | 
|---|
| [e6955b1] | 276 | parseTree->printList( cout ); | 
|---|
| [0da3e2c] | 277 | delete parseTree; | 
|---|
| [3e96559] | 278 | return EXIT_SUCCESS; | 
|---|
| [b87a5ed] | 279 | } // if | 
|---|
|  | 280 |  | 
|---|
| [0da3e2c] | 281 | buildList( parseTree, translationUnit ); | 
|---|
|  | 282 | delete parseTree; | 
|---|
| [cbaee0d] | 283 | parseTree = nullptr; | 
|---|
| [b87a5ed] | 284 |  | 
|---|
|  | 285 | if ( astp ) { | 
|---|
| [1ab4ce2] | 286 | dump( translationUnit ); | 
|---|
| [3e96559] | 287 | return EXIT_SUCCESS; | 
|---|
| [b87a5ed] | 288 | } // if | 
|---|
|  | 289 |  | 
|---|
| [036dd5f] | 290 | // Temporary: fill locations after parsing so that every node has a location, for early error messages. | 
|---|
|  | 291 | // Eventually we should pass the locations from the parser to every node, but this quick and dirty solution | 
|---|
|  | 292 | // works okay for now. | 
|---|
|  | 293 | CodeTools::fillLocations( translationUnit ); | 
|---|
| [3c0d4cd] | 294 | Stats::Time::StopBlock(); | 
|---|
| [036dd5f] | 295 |  | 
|---|
| [839ccbb] | 296 | // add the assignment statement after the initialization of a type parameter | 
|---|
| [675716e] | 297 | PASS( "Validate", SymTab::validate( translationUnit, symtabp ) ); | 
|---|
| [81419b5] | 298 | if ( symtabp ) { | 
|---|
| [46f6134] | 299 | deleteAll( translationUnit ); | 
|---|
| [3e96559] | 300 | return EXIT_SUCCESS; | 
|---|
| [b87a5ed] | 301 | } // if | 
|---|
|  | 302 |  | 
|---|
| [81419b5] | 303 | if ( expraltp ) { | 
|---|
| [bff09c8] | 304 | PassVisitor<ResolvExpr::AlternativePrinter> printer( cout ); | 
|---|
| [81419b5] | 305 | acceptAll( translationUnit, printer ); | 
|---|
| [3e96559] | 306 | return EXIT_SUCCESS; | 
|---|
| [b87a5ed] | 307 | } // if | 
|---|
|  | 308 |  | 
|---|
|  | 309 | if ( validp ) { | 
|---|
| [1ab4ce2] | 310 | dump( translationUnit ); | 
|---|
| [3e96559] | 311 | return EXIT_SUCCESS; | 
|---|
| [b87a5ed] | 312 | } // if | 
|---|
|  | 313 |  | 
|---|
| [046a890] | 314 | PASS( "Translate Throws", ControlStruct::translateThrows( translationUnit ) ); | 
|---|
| [675716e] | 315 | PASS( "Fix Labels", ControlStruct::fixLabels( translationUnit ) ); | 
|---|
|  | 316 | PASS( "Fix Names", CodeGen::fixNames( translationUnit ) ); | 
|---|
|  | 317 | PASS( "Gen Init", InitTweak::genInit( translationUnit ) ); | 
|---|
|  | 318 | PASS( "Expand Member Tuples" , Tuples::expandMemberTuples( translationUnit ) ); | 
|---|
| [81419b5] | 319 | if ( libcfap ) { | 
|---|
|  | 320 | // generate the bodies of cfa library functions | 
|---|
|  | 321 | LibCfa::makeLibCfa( translationUnit ); | 
|---|
| [b87a5ed] | 322 | } // if | 
|---|
|  | 323 |  | 
|---|
| [fa2de95] | 324 | if ( declstatsp ) { | 
|---|
|  | 325 | CodeTools::printDeclStats( translationUnit ); | 
|---|
|  | 326 | deleteAll( translationUnit ); | 
|---|
| [3e96559] | 327 | return EXIT_SUCCESS; | 
|---|
|  | 328 | } // if | 
|---|
| [fa2de95] | 329 |  | 
|---|
| [de62360d] | 330 | if ( bresolvep ) { | 
|---|
| [1ab4ce2] | 331 | dump( translationUnit ); | 
|---|
| [3e96559] | 332 | return EXIT_SUCCESS; | 
|---|
| [de62360d] | 333 | } // if | 
|---|
|  | 334 |  | 
|---|
| [76b378d] | 335 | CodeTools::fillLocations( translationUnit ); | 
|---|
|  | 336 |  | 
|---|
| [3b3491b6] | 337 | if ( resolvprotop ) { | 
|---|
|  | 338 | CodeTools::dumpAsResolvProto( translationUnit ); | 
|---|
| [3e96559] | 339 | return EXIT_SUCCESS; | 
|---|
|  | 340 | } // if | 
|---|
| [3b3491b6] | 341 |  | 
|---|
| [675716e] | 342 | PASS( "Resolve", ResolvExpr::resolve( translationUnit ) ); | 
|---|
| [81419b5] | 343 | if ( exprp ) { | 
|---|
| [1ab4ce2] | 344 | dump( translationUnit ); | 
|---|
| [3e96559] | 345 | return EXIT_SUCCESS; | 
|---|
| [926af74] | 346 | } // if | 
|---|
| [81419b5] | 347 |  | 
|---|
| [71f4e4f] | 348 | // fix ObjectDecl - replaces ConstructorInit nodes | 
|---|
| [675716e] | 349 | PASS( "Fix Init", InitTweak::fix( translationUnit, buildingLibrary() ) ); | 
|---|
| [ca1c11f] | 350 | if ( ctorinitp ) { | 
|---|
|  | 351 | dump ( translationUnit ); | 
|---|
| [3e96559] | 352 | return EXIT_SUCCESS; | 
|---|
| [926af74] | 353 | } // if | 
|---|
| [71f4e4f] | 354 |  | 
|---|
| [675716e] | 355 | PASS( "Expand Unique Expr", 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 | 
|---|
| [626dbc10] | 356 |  | 
|---|
| [046a890] | 357 | PASS( "Translate Tries" , ControlStruct::translateTries( translationUnit ) ); | 
|---|
| [6edd210] | 358 |  | 
|---|
| [675716e] | 359 | PASS( "Gen Waitfor" , Concurrency::generateWaitFor( translationUnit ) ); | 
|---|
| [307a732] | 360 |  | 
|---|
| [675716e] | 361 | PASS( "Convert Specializations",  GenPoly::convertSpecializations( translationUnit ) ); // needs to happen before tuple types are expanded | 
|---|
| [9f5ecf5] | 362 |  | 
|---|
| [675716e] | 363 | PASS( "Expand Tuples", Tuples::expandTuples( translationUnit ) ); // xxx - is this the right place for this? | 
|---|
| [626dbc10] | 364 |  | 
|---|
|  | 365 | if ( tuplep ) { | 
|---|
|  | 366 | dump( translationUnit ); | 
|---|
| [3e96559] | 367 | return EXIT_SUCCESS; | 
|---|
|  | 368 | } // if | 
|---|
| [141b786] | 369 |  | 
|---|
| [675716e] | 370 | PASS( "Virtual Expand Casts", Virtual::expandCasts( translationUnit ) ); // Must come after translateEHM | 
|---|
| [a5f0529] | 371 |  | 
|---|
| [675716e] | 372 | PASS( "Instantiate Generics", GenPoly::instantiateGeneric( translationUnit ) ); | 
|---|
| [53d3ab4b] | 373 | if ( genericsp ) { | 
|---|
|  | 374 | dump( translationUnit ); | 
|---|
| [3e96559] | 375 | return EXIT_SUCCESS; | 
|---|
|  | 376 | } // if | 
|---|
| [b4f8808] | 377 |  | 
|---|
| [675716e] | 378 | PASS( "Convert L-Value", GenPoly::convertLvalue( translationUnit ) ); | 
|---|
| [53d3ab4b] | 379 |  | 
|---|
| [fea7ca7] | 380 | if ( bboxp ) { | 
|---|
|  | 381 | dump( translationUnit ); | 
|---|
| [3e96559] | 382 | return EXIT_SUCCESS; | 
|---|
| [926af74] | 383 | } // if | 
|---|
| [675716e] | 384 | PASS( "Box", GenPoly::box( translationUnit ) ); | 
|---|
| [81419b5] | 385 |  | 
|---|
| [8905f56] | 386 | if ( bcodegenp ) { | 
|---|
|  | 387 | dump( translationUnit ); | 
|---|
| [3e96559] | 388 | return EXIT_SUCCESS; | 
|---|
|  | 389 | } // if | 
|---|
| [8905f56] | 390 |  | 
|---|
| [13de47bc] | 391 | if ( optind < argc ) {                                                  // any commands after the flags and input file ? => output file name | 
|---|
|  | 392 | output = new ofstream( argv[ optind ] ); | 
|---|
|  | 393 | } // if | 
|---|
| [0270824] | 394 |  | 
|---|
| [7b15d7a] | 395 | CodeTools::fillLocations( translationUnit ); | 
|---|
| [62ce290] | 396 | PASS( "Code Gen", CodeGen::generate( translationUnit, *output, ! genproto, prettycodegenp, true, linemarks ) ); | 
|---|
| [0270824] | 397 |  | 
|---|
| [37fe352] | 398 | CodeGen::FixMain::fix( *output, (PreludeDirector + "/bootloader.c").c_str() ); | 
|---|
| [e6955b1] | 399 | if ( output != &cout ) { | 
|---|
| [b87a5ed] | 400 | delete output; | 
|---|
|  | 401 | } // if | 
|---|
| [a16764a6] | 402 | } catch ( SemanticErrorException &e ) { | 
|---|
| [b87a5ed] | 403 | if ( errorp ) { | 
|---|
| [e6955b1] | 404 | cerr << "---AST at error:---" << endl; | 
|---|
|  | 405 | dump( translationUnit, cerr ); | 
|---|
|  | 406 | cerr << endl << "---End of AST, begin error message:---\n" << endl; | 
|---|
| [926af74] | 407 | } // if | 
|---|
| [d55d7a6] | 408 | e.print(); | 
|---|
| [e6955b1] | 409 | if ( output != &cout ) { | 
|---|
| [b87a5ed] | 410 | delete output; | 
|---|
|  | 411 | } // if | 
|---|
| [3e96559] | 412 | return EXIT_FAILURE; | 
|---|
| [b87a5ed] | 413 | } catch ( UnimplementedError &e ) { | 
|---|
| [e6955b1] | 414 | cout << "Sorry, " << e.get_what() << " is not currently implemented" << endl; | 
|---|
|  | 415 | if ( output != &cout ) { | 
|---|
| [b87a5ed] | 416 | delete output; | 
|---|
|  | 417 | } // if | 
|---|
| [3e96559] | 418 | return EXIT_FAILURE; | 
|---|
| [b87a5ed] | 419 | } catch ( CompilerError &e ) { | 
|---|
| [e6955b1] | 420 | cerr << "Compiler Error: " << e.get_what() << endl; | 
|---|
| [c850687] | 421 | cerr << "(please report bugs to [REDACTED])" << endl; | 
|---|
| [e6955b1] | 422 | if ( output != &cout ) { | 
|---|
| [b87a5ed] | 423 | delete output; | 
|---|
|  | 424 | } // if | 
|---|
| [3e96559] | 425 | return EXIT_FAILURE; | 
|---|
|  | 426 | } catch ( ... ) { | 
|---|
| [bffcd66] | 427 | exception_ptr eptr = current_exception(); | 
|---|
| [4990812] | 428 | try { | 
|---|
|  | 429 | if (eptr) { | 
|---|
| [bffcd66] | 430 | rethrow_exception(eptr); | 
|---|
| [3e96559] | 431 | } else { | 
|---|
| [bffcd66] | 432 | cerr << "Exception Uncaught and Unknown" << endl; | 
|---|
| [3e96559] | 433 | } // if | 
|---|
| [bffcd66] | 434 | } catch(const exception& e) { | 
|---|
|  | 435 | cerr << "Uncaught Exception \"" << e.what() << "\"\n"; | 
|---|
| [3e96559] | 436 | } // try | 
|---|
|  | 437 | return EXIT_FAILURE; | 
|---|
|  | 438 | } // try | 
|---|
| [b87a5ed] | 439 |  | 
|---|
| [39786813] | 440 | deleteAll( translationUnit ); | 
|---|
| [1cb7fab2] | 441 | Stats::print(); | 
|---|
| [3e96559] | 442 | return EXIT_SUCCESS; | 
|---|
| [d9a0e76] | 443 | } // main | 
|---|
| [51b73452] | 444 |  | 
|---|
| [0da3e2c] | 445 |  | 
|---|
| [7215000] | 446 | static const char optstring[] = ":c:ghlLmNnpdP:S:twW:D:"; | 
|---|
| [3e96559] | 447 |  | 
|---|
| [62ce290] | 448 | enum { PreludeDir = 128 }; | 
|---|
| [3e96559] | 449 | static struct option long_opts[] = { | 
|---|
| [1a69a90] | 450 | { "colors", required_argument, nullptr, 'c' }, | 
|---|
|  | 451 | { "gdb", no_argument, nullptr, 'g' }, | 
|---|
| [3e96559] | 452 | { "help", no_argument, nullptr, 'h' }, | 
|---|
|  | 453 | { "libcfa", no_argument, nullptr, 'l' }, | 
|---|
| [62ce290] | 454 | { "linemarks", no_argument, nullptr, 'L' }, | 
|---|
| [3e96559] | 455 | { "no-main", no_argument, 0, 'm' }, | 
|---|
| [62ce290] | 456 | { "no-linemarks", no_argument, nullptr, 'N' }, | 
|---|
|  | 457 | { "no-prelude", no_argument, nullptr, 'n' }, | 
|---|
| [3e96559] | 458 | { "prototypes", no_argument, nullptr, 'p' }, | 
|---|
| [7215000] | 459 | { "deterministic-out", no_argument, nullptr, 'd' }, | 
|---|
| [62ce290] | 460 | { "print", required_argument, nullptr, 'P' }, | 
|---|
|  | 461 | { "prelude-dir", required_argument, nullptr, PreludeDir }, | 
|---|
|  | 462 | { "statistics", required_argument, nullptr, 'S' }, | 
|---|
| [3e96559] | 463 | { "tree", no_argument, nullptr, 't' }, | 
|---|
|  | 464 | { "", no_argument, nullptr, 0 },                                        // -w | 
|---|
|  | 465 | { "", no_argument, nullptr, 0 },                                        // -W | 
|---|
|  | 466 | { "", no_argument, nullptr, 0 },                                        // -D | 
|---|
|  | 467 | { nullptr, 0, nullptr, 0 } | 
|---|
|  | 468 | }; // long_opts | 
|---|
|  | 469 |  | 
|---|
|  | 470 | static const char * description[] = { | 
|---|
| [49d3128] | 471 | "diagnostic color: never, always, or auto.",          // -c | 
|---|
|  | 472 | "wait for gdb to attach",                             // -g | 
|---|
|  | 473 | "print help message",                                 // -h | 
|---|
|  | 474 | "generate libcfa.c",                                  // -l | 
|---|
|  | 475 | "generate line marks",                                // -L | 
|---|
|  | 476 | "do not replace main",                                // -m | 
|---|
|  | 477 | "do not generate line marks",                         // -N | 
|---|
|  | 478 | "do not read prelude",                                // -n | 
|---|
| [62ce290] | 479 | "generate prototypes for prelude functions",            // -p | 
|---|
| [7215000] | 480 | "don't print output that isn't deterministic",        // -d | 
|---|
| [49d3128] | 481 | "print",                                              // -P | 
|---|
| [62ce290] | 482 | "<directory> prelude directory for debug/nodebug",      // no flag | 
|---|
|  | 483 | "<option-list> enable profiling information:\n          counters,heap,time,all,none", // -S | 
|---|
| [49d3128] | 484 | "building cfa standard lib",                          // -t | 
|---|
|  | 485 | "",                                                   // -w | 
|---|
|  | 486 | "",                                                   // -W | 
|---|
|  | 487 | "",                                                   // -D | 
|---|
| [3e96559] | 488 | }; // description | 
|---|
|  | 489 |  | 
|---|
| [0c0f548] | 490 | static_assert( sizeof( long_opts ) / sizeof( long_opts[0] ) - 1 == sizeof( description ) / sizeof( description[0] ), "Long opts and description must match" ); | 
|---|
| [62ce290] | 491 |  | 
|---|
|  | 492 | static struct Printopts { | 
|---|
|  | 493 | const char * name; | 
|---|
|  | 494 | int & flag; | 
|---|
|  | 495 | int val; | 
|---|
|  | 496 | const char * descript; | 
|---|
|  | 497 | } printopts[] = { | 
|---|
| [0e464f6] | 498 | { "ascodegen", codegenp, true, "print AST as codegen rather than AST" }, | 
|---|
|  | 499 | { "asterr", errorp, true, "print AST on error" }, | 
|---|
| [62ce290] | 500 | { "declstats", declstatsp, true, "code property statistics" }, | 
|---|
|  | 501 | { "parse", yydebug, true, "yacc (parsing) debug information" }, | 
|---|
|  | 502 | { "pretty", prettycodegenp, true, "prettyprint for ascodegen flag" }, | 
|---|
|  | 503 | { "rproto", resolvprotop, true, "resolver-proto instance" }, | 
|---|
| [0e464f6] | 504 | { "rsteps", resolvep, true, "print resolver steps" }, | 
|---|
|  | 505 | { "tree", parsep, true, "print parse tree" }, | 
|---|
|  | 506 | // code dumps | 
|---|
|  | 507 | { "ast", astp, true, "print AST after parsing" }, | 
|---|
|  | 508 | { "symevt", symtabp, true, "print AST after symbol table events" }, | 
|---|
|  | 509 | { "altexpr", expraltp, true, "print alternatives for expressions" }, | 
|---|
|  | 510 | { "astdecl", validp, true, "print AST after declaration validation pass" }, | 
|---|
|  | 511 | { "resolver", bresolvep, true, "print AST before resolver step" }, | 
|---|
|  | 512 | { "astexpr", exprp, true, "print AST after expression analysis" }, | 
|---|
|  | 513 | { "ctordtor", ctorinitp, true, "print AST after ctor/dtor are replaced" }, | 
|---|
|  | 514 | { "tuple", tuplep, true, "print AST after tuple expansion" }, | 
|---|
|  | 515 | { "astgen", genericsp, true, "print AST after instantiate generics" }, | 
|---|
|  | 516 | { "box", bboxp, true, "print AST before box step" }, | 
|---|
|  | 517 | { "codegen", bcodegenp, true, "print AST before code generation" }, | 
|---|
| [62ce290] | 518 | }; | 
|---|
|  | 519 | enum { printoptsSize = sizeof( printopts ) / sizeof( printopts[0] ) }; | 
|---|
|  | 520 |  | 
|---|
| [3e96559] | 521 | static void usage( char *argv[] ) { | 
|---|
| [e0bd0f9] | 522 | cout << "Usage: " << argv[0] << " [options] [input-file (default stdin)] [output-file (default stdout)], where options are:" << endl; | 
|---|
| [3e96559] | 523 | int i = 0, j = 1;                                                                       // j skips starting colon | 
|---|
|  | 524 | for ( ; long_opts[i].name != 0 && optstring[j] != '\0'; i += 1, j += 1 ) { | 
|---|
|  | 525 | if ( long_opts[i].name[0] != '\0' ) {                   // hidden option, internal usage only | 
|---|
| [62ce290] | 526 | if ( strcmp( long_opts[i].name, "prelude-dir" ) != 0 ) { // flag | 
|---|
|  | 527 | cout << "  -" << optstring[j] << ","; | 
|---|
|  | 528 | } else {                                                                        // no flag | 
|---|
|  | 529 | j -= 1;                                                                 // compensate | 
|---|
|  | 530 | cout << "     "; | 
|---|
|  | 531 | } // if | 
|---|
|  | 532 | cout << " --" << left << setw(12) << long_opts[i].name << "  "; | 
|---|
|  | 533 | if ( strcmp( long_opts[i].name, "print" ) == 0 ) { | 
|---|
|  | 534 | cout << "one of: " << endl; | 
|---|
|  | 535 | for ( int i = 0; i < printoptsSize; i += 1 ) { | 
|---|
|  | 536 | cout << setw(10) << " " << left << setw(10) << printopts[i].name << "  " << printopts[i].descript << endl; | 
|---|
|  | 537 | } // for | 
|---|
|  | 538 | } else { | 
|---|
|  | 539 | cout << description[i] << endl; | 
|---|
|  | 540 | } // if | 
|---|
| [3e96559] | 541 | } // if | 
|---|
| [62ce290] | 542 | if ( optstring[j + 1] == ':' ) j += 1; | 
|---|
| [3e96559] | 543 | } // for | 
|---|
|  | 544 | if ( long_opts[i].name != 0 || optstring[j] != '\0' ) assertf( false, "internal error, mismatch of option flags and names\n" ); | 
|---|
|  | 545 | exit( EXIT_FAILURE ); | 
|---|
|  | 546 | } // usage | 
|---|
|  | 547 |  | 
|---|
| [e0bd0f9] | 548 | static void parse_cmdline( int argc, char * argv[] ) { | 
|---|
| [0da3e2c] | 549 | opterr = 0;                                                                                     // (global) prevent getopt from printing error messages | 
|---|
|  | 550 |  | 
|---|
| [c5e5109] | 551 | bool Wsuppress = false, Werror = false; | 
|---|
| [0da3e2c] | 552 | int c; | 
|---|
| [3e96559] | 553 | while ( (c = getopt_long( argc, argv, optstring, long_opts, nullptr )) != -1 ) { | 
|---|
| [0da3e2c] | 554 | switch ( c ) { | 
|---|
| [1a69a90] | 555 | case 'c':                                                                             // diagnostic colors | 
|---|
|  | 556 | if ( strcmp( optarg, "always" ) == 0 ) { | 
|---|
|  | 557 | ErrorHelpers::colors = ErrorHelpers::Colors::Always; | 
|---|
|  | 558 | } else if ( strcmp( optarg, "never" ) == 0 ) { | 
|---|
|  | 559 | ErrorHelpers::colors = ErrorHelpers::Colors::Never; | 
|---|
|  | 560 | } else if ( strcmp( optarg, "auto" ) == 0 ) { | 
|---|
|  | 561 | ErrorHelpers::colors = ErrorHelpers::Colors::Auto; | 
|---|
|  | 562 | } // if | 
|---|
|  | 563 | break; | 
|---|
| [3e96559] | 564 | case 'h':                                                                             // help message | 
|---|
|  | 565 | usage( argv );                                                          // no return | 
|---|
| [53d3ab4b] | 566 | break; | 
|---|
| [3e96559] | 567 | case 'l':                                                                             // generate libcfa.c | 
|---|
| [0da3e2c] | 568 | libcfap = true; | 
|---|
|  | 569 | break; | 
|---|
| [62ce290] | 570 | case 'L':                                                                             // generate line marks | 
|---|
| [6de43b6] | 571 | linemarks = true; | 
|---|
| [c850687] | 572 | break; | 
|---|
| [3e96559] | 573 | case 'm':                                                                             // do not replace main | 
|---|
|  | 574 | nomainp = true; | 
|---|
| [0da3e2c] | 575 | break; | 
|---|
| [62ce290] | 576 | case 'N':                                                                             // do not generate line marks | 
|---|
| [6de43b6] | 577 | linemarks = false; | 
|---|
| [c59bde6] | 578 | break; | 
|---|
| [62ce290] | 579 | case 'n':                                                                             // do not read prelude | 
|---|
| [3e96559] | 580 | nopreludep = true; | 
|---|
| [0da3e2c] | 581 | break; | 
|---|
| [62ce290] | 582 | case 'p':                                                                             // generate prototypes for prelude functions | 
|---|
|  | 583 | genproto = true; | 
|---|
| [0da3e2c] | 584 | break; | 
|---|
| [7215000] | 585 | case 'd':                                     // don't print non-deterministic output | 
|---|
|  | 586 | deterministic_output = true; | 
|---|
|  | 587 | break; | 
|---|
| [62ce290] | 588 | case 'P':                                                                             // print options | 
|---|
|  | 589 | for ( int i = 0;; i += 1 ) { | 
|---|
|  | 590 | if ( i == printoptsSize ) { | 
|---|
|  | 591 | cout << "Unknown --print option " << optarg << endl; | 
|---|
|  | 592 | goto Default; | 
|---|
|  | 593 | } // if | 
|---|
|  | 594 | if ( strcmp( optarg, printopts[i].name ) == 0 ) { | 
|---|
|  | 595 | printopts[i].flag = printopts[i].val; | 
|---|
|  | 596 | break; | 
|---|
|  | 597 | } // if | 
|---|
|  | 598 | } // for | 
|---|
| [0da3e2c] | 599 | break; | 
|---|
| [62ce290] | 600 | case PreludeDir:                                                              // prelude directory for debug/nodebug, hidden | 
|---|
|  | 601 | PreludeDirector = optarg; | 
|---|
| [3b3491b6] | 602 | break; | 
|---|
| [3e96559] | 603 | case 'S':                                                                             // enable profiling information, argument comma separated list of names | 
|---|
|  | 604 | Stats::parse_params( optarg ); | 
|---|
| [ebcc940] | 605 | break; | 
|---|
| [dee1f89] | 606 | case 't':                                                                             // building cfa stdlib | 
|---|
| [0da3e2c] | 607 | treep = true; | 
|---|
|  | 608 | break; | 
|---|
| [dee1f89] | 609 | case 'g':                                                                             // wait for gdb | 
|---|
|  | 610 | waiting_for_gdb = true; | 
|---|
|  | 611 | break; | 
|---|
| [3e96559] | 612 | case 'w':                                                                             // suppress all warnings, hidden | 
|---|
| [c5e5109] | 613 | Wsuppress = true; | 
|---|
| [44bca7f] | 614 | break; | 
|---|
| [3e96559] | 615 | case 'W':                                                                             // coordinate gcc -W with CFA, hidden | 
|---|
| [44bca7f] | 616 | if ( strcmp( optarg, "all" ) == 0 ) { | 
|---|
| [68e9ace] | 617 | SemanticWarning_EnableAll(); | 
|---|
| [44bca7f] | 618 | } else if ( strcmp( optarg, "error" ) == 0 ) { | 
|---|
|  | 619 | Werror = true; | 
|---|
|  | 620 | } else { | 
|---|
|  | 621 | char * warning = optarg; | 
|---|
|  | 622 | Severity s; | 
|---|
|  | 623 | if ( strncmp( optarg, "no-", 3 ) == 0 ) { | 
|---|
|  | 624 | warning += 3; | 
|---|
|  | 625 | s = Severity::Suppress; | 
|---|
|  | 626 | } else { | 
|---|
|  | 627 | s = Severity::Warn; | 
|---|
|  | 628 | } // if | 
|---|
| [68e9ace] | 629 | SemanticWarning_Set( warning, s ); | 
|---|
| [44bca7f] | 630 | } // if | 
|---|
|  | 631 | break; | 
|---|
| [3e96559] | 632 | case 'D':                                                                             // ignore -Dxxx, forwarded by cpp, hidden | 
|---|
| [0da3e2c] | 633 | break; | 
|---|
| [3e96559] | 634 | case '?':                                                                             // unknown option | 
|---|
|  | 635 | if ( optopt ) {                                                         // short option ? | 
|---|
|  | 636 | cout << "Unknown option -" << (char)optopt << endl; | 
|---|
|  | 637 | } else { | 
|---|
|  | 638 | cout << "Unknown option " << argv[optind - 1] << endl; | 
|---|
|  | 639 | } // if | 
|---|
|  | 640 | goto Default; | 
|---|
|  | 641 | case ':':                                                                             // missing option | 
|---|
| [ae47a23] | 642 | if ( optopt ) {                                                         // short option ? | 
|---|
| [3e96559] | 643 | cout << "Missing option for -" << (char)optopt << endl; | 
|---|
| [ae47a23] | 644 | } else { | 
|---|
| [3e96559] | 645 | cout << "Missing option for " << argv[optind - 1] << endl; | 
|---|
| [ae47a23] | 646 | } // if | 
|---|
| [3e96559] | 647 | goto Default; | 
|---|
|  | 648 | Default: | 
|---|
|  | 649 | default: | 
|---|
|  | 650 | usage( argv );                                                          // no return | 
|---|
| [0da3e2c] | 651 | } // switch | 
|---|
|  | 652 | } // while | 
|---|
| [44bca7f] | 653 |  | 
|---|
|  | 654 | if ( Werror ) { | 
|---|
| [68e9ace] | 655 | SemanticWarning_WarningAsError(); | 
|---|
| [44bca7f] | 656 | } // if | 
|---|
| [c5e5109] | 657 | if ( Wsuppress ) { | 
|---|
|  | 658 | SemanticWarning_SuppressAll(); | 
|---|
|  | 659 | } // if | 
|---|
| [44bca7f] | 660 | // for ( const auto w : WarningFormats ) { | 
|---|
|  | 661 | //      cout << w.name << ' ' << (int)w.severity << endl; | 
|---|
|  | 662 | // } // for | 
|---|
| [0da3e2c] | 663 | } // parse_cmdline | 
|---|
|  | 664 |  | 
|---|
| [8b7ee09] | 665 | static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit ) { | 
|---|
| [0da3e2c] | 666 | extern int yyparse( void ); | 
|---|
| [cbaee0d] | 667 | extern FILE * yyin; | 
|---|
| [0da3e2c] | 668 | extern int yylineno; | 
|---|
|  | 669 |  | 
|---|
| [8b7ee09] | 670 | ::linkage = linkage;                                                            // set globals | 
|---|
| [0da3e2c] | 671 | yyin = input; | 
|---|
|  | 672 | yylineno = 1; | 
|---|
|  | 673 | int parseStatus = yyparse(); | 
|---|
| [81419b5] | 674 |  | 
|---|
|  | 675 | fclose( input ); | 
|---|
| [0da3e2c] | 676 | if ( shouldExit || parseStatus != 0 ) { | 
|---|
|  | 677 | exit( parseStatus ); | 
|---|
| [81419b5] | 678 | } // if | 
|---|
| [0da3e2c] | 679 | } // parse | 
|---|
| [81419b5] | 680 |  | 
|---|
| [1ab4ce2] | 681 | static bool notPrelude( Declaration * decl ) { | 
|---|
|  | 682 | return ! LinkageSpec::isBuiltin( decl->get_linkage() ); | 
|---|
| [0da3e2c] | 683 | } // notPrelude | 
|---|
| [1ab4ce2] | 684 |  | 
|---|
| [e6955b1] | 685 | static void dump( list< Declaration * > & translationUnit, ostream & out ) { | 
|---|
|  | 686 | list< Declaration * > decls; | 
|---|
| [926af74] | 687 |  | 
|---|
| [62ce290] | 688 | if ( genproto ) { | 
|---|
| [e6955b1] | 689 | filter( translationUnit.begin(), translationUnit.end(), back_inserter( decls ), notPrelude ); | 
|---|
| [1ab4ce2] | 690 | } else { | 
|---|
|  | 691 | decls = translationUnit; | 
|---|
| [926af74] | 692 | } // if | 
|---|
| [1ab4ce2] | 693 |  | 
|---|
| [e39241b] | 694 | // depending on commandline options, either generate code or dump the AST | 
|---|
|  | 695 | if ( codegenp ) { | 
|---|
| [62ce290] | 696 | CodeGen::generate( decls, out, ! genproto, prettycodegenp ); | 
|---|
| [e39241b] | 697 | } else { | 
|---|
|  | 698 | printAll( decls, out ); | 
|---|
| [3e96559] | 699 | } // if | 
|---|
| [7f5566b] | 700 | deleteAll( translationUnit ); | 
|---|
| [0da3e2c] | 701 | } // dump | 
|---|
| [1ab4ce2] | 702 |  | 
|---|
| [51b73452] | 703 | // Local Variables: // | 
|---|
| [b87a5ed] | 704 | // tab-width: 4 // | 
|---|
|  | 705 | // mode: c++ // | 
|---|
|  | 706 | // compile-command: "make install" // | 
|---|
| [51b73452] | 707 | // End:  // | 
|---|