source: src/main.cc@ 76f7fc7

ADT arm-eh ast-experimental cleanup-dtors enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 76f7fc7 was 3e96559, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

add --help option for cfa-cpp to show options, and restructure the option handling code

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