source: src/main.cc@ 6fa409e

new-env
Last change on this file since 6fa409e was 28f3a19, checked in by Aaron Moss <a3moss@…>, 7 years ago

Merge branch 'master' into with_gc

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