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