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