source: src/main.cc@ 90ce35aa

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 90ce35aa was 4a60488, checked in by Andrew Beach <ajbeach@…>, 6 years ago

Merged from master taking the lvalue changes to expression and everything before that.

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