source: src/main.cc @ 79eaeb7

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpersistent-indexerpthread-emulationqualifiedEnum
Last change on this file since 79eaeb7 was 79eaeb7, checked in by tdelisle <tdelisle@…>, 5 years ago

Improved printing, parent printing still incorrect

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