source: src/main.cc @ 2c60af75

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 2c60af75 was e0bd0f9, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

remove -F flag and fix usage message

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