source: src/main.cc@ cbaee0d

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since cbaee0d was cbaee0d, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

more refactoring of parser code

  • Property mode set to 100644
File size: 11.0 KB
RevLine 
[b87a5ed]1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
[71f4e4f]7// main.cc --
[b87a5ed]8//
[843054c2]9// Author : Richard C. Bilson
[b87a5ed]10// Created On : Fri May 15 23:12:02 2015
[dd51906]11// Last Modified By : Peter A. Buhr
[cbaee0d]12// Last Modified On : Tue Aug 16 21:33:49 2016
13// Update Count : 260
[b87a5ed]14//
15
[51b73452]16#include <iostream>
17#include <fstream>
[b87a5ed]18#include <getopt.h>
[0da3e2c]19#include "Parser/lex.h"
20#include "Parser/parser.h"
21#include "Parser/TypedefTable.h"
[51b73452]22#include "SynTree/Declaration.h"
23#include "SynTree/Visitor.h"
24#include "GenPoly/Lvalue.h"
25#include "GenPoly/Specialize.h"
26#include "GenPoly/Box.h"
27#include "GenPoly/CopyParams.h"
[ea5daeb]28#include "GenPoly/InstantiateGeneric.h"
[51b73452]29#include "CodeGen/Generate.h"
30#include "CodeGen/FixNames.h"
31#include "ControlStruct/Mutate.h"
32#include "Tuples/Mutate.h"
33#include "Tuples/FunctionChecker.h"
34#include "SymTab/Mangler.h"
35#include "SymTab/Indexer.h"
36#include "SymTab/Validate.h"
37#include "ResolvExpr/AlternativePrinter.h"
38#include "ResolvExpr/Resolver.h"
39#include "MakeLibCfa.h"
40#include "InitTweak/Mutate.h"
[a0fdbd5]41#include "InitTweak/GenInit.h"
[71f4e4f]42#include "InitTweak/FixInit.h"
[51b73452]43
[d3b7937]44#include "Common/SemanticError.h"
45#include "Common/UnimplementedError.h"
[51b73452]46
47#include "../config.h"
48
49using namespace std;
50
[0da3e2c]51#define OPTPRINT(x) if ( errorp ) std::cerr << x << std::endl;
[81419b5]52
[0da3e2c]53extern int yydebug;
54
55LinkageSpec::Type linkage = LinkageSpec::Cforall;
56TypedefTable typedefTable;
[cbaee0d]57DeclarationNode * parseTree = nullptr; // program parse tree
[81419b5]58
[b87a5ed]59bool
60 astp = false,
[de62360d]61 bresolvep = false,
[fea7ca7]62 bboxp = false,
[ca1c11f]63 ctorinitp = false,
[b87a5ed]64 exprp = false,
65 expraltp = false,
66 grammarp = false,
67 libcfap = false,
[de62360d]68 nopreludep = false,
[1ab4ce2]69 noprotop = false,
[de62360d]70 parsep = false,
[b87a5ed]71 resolvep = false, // used in AlternativeFinder
72 symtabp = false,
[d3b7937]73 treep = false,
[b87a5ed]74 validp = false,
[de62360d]75 errorp = false,
76 codegenp = false;
[b87a5ed]77
[0da3e2c]78static void parse( FILE * input, LinkageSpec::Type t, bool shouldExit = false );
79static void dump( std::list< Declaration * > & translationUnit, std::ostream & out = std::cout );
[cbaee0d]80static void parse_cmdline( int argc, char *argv[], const char *& filename );
[0da3e2c]81
[51b73452]82
[cbaee0d]83int main( int argc, char * argv[] ) {
84 FILE * input;
85 std::ostream *output = & std::cout;
[7b937575]86 std::list< Declaration * > translationUnit;
[d029162e]87 const char *filename = NULL;
[b87a5ed]88
[0da3e2c]89 parse_cmdline( argc, argv, filename ); // process command-line arguments
[b87a5ed]90
91 try {
[81419b5]92 // choose to read the program from a file or stdin
[b87a5ed]93 if ( optind < argc ) {
94 input = fopen( argv[ optind ], "r" );
95 if ( ! input ) {
[35f9114]96 std::cout << "Error: cannot open " << argv[ optind ] << std::endl;
[dd51906]97 exit( EXIT_FAILURE );
[b87a5ed]98 } // if
[d029162e]99 // if running cfa-cpp directly, might forget to pass -F option (and really shouldn't have to)
100 if ( filename == NULL ) filename = argv[ optind ];
[37024fd]101 // prelude filename comes in differently
102 if ( libcfap ) filename = "prelude.cf";
[b87a5ed]103 optind += 1;
104 } else {
105 input = stdin;
[2a7e29b]106 // if running cfa-cpp directly, might forget to pass -F option. Since this takes from stdin, pass
107 // a fake name along
108 if ( filename == NULL ) filename = "stdin";
[b87a5ed]109 } // if
110
111 if ( optind < argc ) {
112 output = new ofstream( argv[ optind ] );
113 } // if
[71f4e4f]114
[0da3e2c]115 yydebug = grammarp;
[81419b5]116
[159c62e]117 // read in the builtins, extras, and the prelude
[de62360d]118 if ( ! nopreludep ) { // include gcc builtins
[faf8857]119 // -l is for initial build ONLY and builtins.cf is not in the lib directory so access it here.
[d3b7937]120 FILE * builtins = fopen( libcfap | treep ? "builtins.cf" : CFA_LIBDIR "/builtins.cf", "r" );
[b87a5ed]121 if ( builtins == NULL ) {
[35f9114]122 std::cerr << "Error: cannot open builtins.cf" << std::endl;
[dd51906]123 exit( EXIT_FAILURE );
[b87a5ed]124 } // if
[81419b5]125 parse( builtins, LinkageSpec::Compiler );
126
[159c62e]127 // read the extra prelude in, if not generating the cfa library
128 FILE * extras = fopen( libcfap | treep ? "extras.cf" : CFA_LIBDIR "/extras.cf", "r" );
129 if ( extras == NULL ) {
[35f9114]130 std::cerr << "Error: cannot open extras.cf" << std::endl;
[159c62e]131 exit( EXIT_FAILURE );
132 } // if
133 parse( extras, LinkageSpec::C );
134
[81419b5]135 if ( ! libcfap ) {
[faf8857]136 // read the prelude in, if not generating the cfa library
[d3b7937]137 FILE * prelude = fopen( treep ? "prelude.cf" : CFA_LIBDIR "/prelude.cf", "r" );
[81419b5]138 if ( prelude == NULL ) {
[35f9114]139 std::cerr << "Error: cannot open prelude.cf" << std::endl;
[dd51906]140 exit( EXIT_FAILURE );
[81419b5]141 } // if
[71f4e4f]142
[35304009]143 parse( prelude, LinkageSpec::Intrinsic );
[b87a5ed]144 } // if
145 } // if
[81419b5]146
[71f4e4f]147 parse( input, libcfap ? LinkageSpec::Intrinsic : LinkageSpec::Cforall, grammarp );
148
[b87a5ed]149 if ( parsep ) {
[0da3e2c]150 parseTree->printList( std::cout );
151 delete parseTree;
[b87a5ed]152 return 0;
153 } // if
154
[0da3e2c]155 buildList( parseTree, translationUnit );
156 delete parseTree;
[cbaee0d]157 parseTree = nullptr;
[b87a5ed]158
159 if ( astp ) {
[1ab4ce2]160 dump( translationUnit );
[b87a5ed]161 return 0;
162 } // if
163
[839ccbb]164 // add the assignment statement after the initialization of a type parameter
[81419b5]165 OPTPRINT( "validate" )
166 SymTab::validate( translationUnit, symtabp );
167 if ( symtabp ) {
[b87a5ed]168 return 0;
169 } // if
170
[81419b5]171 if ( expraltp ) {
172 ResolvExpr::AlternativePrinter printer( std::cout );
173 acceptAll( translationUnit, printer );
[b87a5ed]174 return 0;
175 } // if
176
177 if ( validp ) {
[1ab4ce2]178 dump( translationUnit );
[b87a5ed]179 return 0;
180 } // if
181
[81419b5]182 OPTPRINT( "mutate" )
183 ControlStruct::mutate( translationUnit );
[71f4e4f]184 OPTPRINT( "fixNames" )
[81419b5]185 CodeGen::fixNames( translationUnit );
[f1e012b]186 OPTPRINT( "tweakInit" )
[a0fdbd5]187 InitTweak::genInit( translationUnit );
[b87a5ed]188
[81419b5]189 if ( libcfap ) {
190 // generate the bodies of cfa library functions
191 LibCfa::makeLibCfa( translationUnit );
[b87a5ed]192 } // if
193
[de62360d]194 if ( bresolvep ) {
[1ab4ce2]195 dump( translationUnit );
[de62360d]196 return 0;
197 } // if
198
[81419b5]199 OPTPRINT( "resolve" )
[b87a5ed]200 ResolvExpr::resolve( translationUnit );
[81419b5]201 if ( exprp ) {
[1ab4ce2]202 dump( translationUnit );
[ca1c11f]203 return 0;
[81419b5]204 }
205
[71f4e4f]206 // fix ObjectDecl - replaces ConstructorInit nodes
[6cf27a07]207 OPTPRINT( "fixInit" )
208 InitTweak::fix( translationUnit, filename, libcfap || treep );
[ca1c11f]209 if ( ctorinitp ) {
210 dump ( translationUnit );
211 return 0;
212 }
[71f4e4f]213
[ea5daeb]214 OPTPRINT("instantiateGenerics")
215 GenPoly::instantiateGeneric( translationUnit );
[81419b5]216 OPTPRINT( "copyParams" );
[b87a5ed]217 GenPoly::copyParams( translationUnit );
[81419b5]218 OPTPRINT( "convertSpecializations" )
[698664b3]219 GenPoly::convertSpecializations( translationUnit );
[81419b5]220 OPTPRINT( "convertLvalue" )
[b87a5ed]221 GenPoly::convertLvalue( translationUnit );
[fea7ca7]222
223 if ( bboxp ) {
224 dump( translationUnit );
225 return 0;
226 }
[81419b5]227 OPTPRINT( "box" )
[b87a5ed]228 GenPoly::box( translationUnit );
[81419b5]229
[2871210]230 // print tree right before code generation
[81419b5]231 if ( codegenp ) {
[1ab4ce2]232 dump( translationUnit );
[81419b5]233 return 0;
234 } // if
[b87a5ed]235
[1ab4ce2]236 CodeGen::generate( translationUnit, *output, ! noprotop );
[b87a5ed]237
238 if ( output != &std::cout ) {
239 delete output;
240 } // if
241 } catch ( SemanticError &e ) {
242 if ( errorp ) {
[f77f12e2]243 std::cerr << "---AST at error:---" << std::endl;
244 dump( translationUnit, std::cerr );
245 std::cerr << std::endl << "---End of AST, begin error message:---\n" << std::endl;
[b87a5ed]246 }
[81419b5]247 e.print( std::cerr );
[b87a5ed]248 if ( output != &std::cout ) {
249 delete output;
250 } // if
251 return 1;
252 } catch ( UnimplementedError &e ) {
253 std::cout << "Sorry, " << e.get_what() << " is not currently implemented" << std::endl;
254 if ( output != &std::cout ) {
255 delete output;
256 } // if
257 return 1;
258 } catch ( CompilerError &e ) {
259 std::cerr << "Compiler Error: " << e.get_what() << std::endl;
260 std::cerr << "(please report bugs to " << std::endl;
261 if ( output != &std::cout ) {
262 delete output;
263 } // if
264 return 1;
265 } // try
266
[39786813]267 deleteAll( translationUnit );
[b87a5ed]268 return 0;
[d9a0e76]269} // main
[51b73452]270
[cbaee0d]271void parse_cmdline( int argc, char * argv[], const char *& filename ) {
[0da3e2c]272 enum { Ast, Bbox, Bresolver, CtorInitFix, Expr, ExprAlt, Grammar, LibCFA, Nopreamble, Parse, Prototypes, Resolver, Symbol, Tree, Validate, };
273
274 static struct option long_opts[] = {
275 { "ast", no_argument, 0, Ast },
276 { "before-box", no_argument, 0, Bbox },
277 { "before-resolver", no_argument, 0, Bresolver },
278 { "ctorinitfix", no_argument, 0, CtorInitFix },
279 { "expr", no_argument, 0, Expr },
280 { "expralt", no_argument, 0, ExprAlt },
281 { "grammar", no_argument, 0, Grammar },
282 { "libcfa", no_argument, 0, LibCFA },
283 { "no-preamble", no_argument, 0, Nopreamble },
284 { "parse", no_argument, 0, Parse },
285 { "no-prototypes", no_argument, 0, Prototypes },
286 { "resolver", no_argument, 0, Resolver },
287 { "symbol", no_argument, 0, Symbol },
288 { "tree", no_argument, 0, Tree },
289 { "validate", no_argument, 0, Validate },
290 { 0, 0, 0, 0 }
291 }; // long_opts
292 int long_index;
293
294 opterr = 0; // (global) prevent getopt from printing error messages
295
296 int c;
297 while ( (c = getopt_long( argc, argv, "abBcefglnpqrstvyzD:F:", long_opts, &long_index )) != -1 ) {
298 switch ( c ) {
299 case Ast:
300 case 'a': // dump AST
301 astp = true;
302 break;
303 case Bresolver:
304 case 'b': // print before resolver steps
305 bresolvep = true;
306 break;
307 case 'B': // print before resolver steps
308 bboxp = true;
309 break;
310 case CtorInitFix:
311 case 'c':
312 ctorinitp = true;
313 break;
314 case Expr:
315 case 'e': // dump AST after expression analysis
316 exprp = true;
317 break;
318 case ExprAlt:
319 case 'f': // print alternatives for expressions
320 expraltp = true;
321 break;
322 case Grammar:
323 case 'g': // bison debugging info (grammar rules)
324 grammarp = true;
325 break;
326 case LibCFA:
327 case 'l': // generate libcfa.c
328 libcfap = true;
329 break;
330 case Nopreamble:
331 case 'n': // do not read preamble
332 nopreludep = true;
333 break;
334 case Prototypes:
335 case 'p': // generate prototypes for preamble functions
336 noprotop = true;
337 break;
338 case Parse:
339 case 'q': // dump parse tree
340 parsep = true;
341 break;
342 case Resolver:
343 case 'r': // print resolver steps
344 resolvep = true;
345 break;
346 case Symbol:
347 case 's': // print symbol table events
348 symtabp = true;
349 break;
350 case Tree:
351 case 't': // build in tree
352 treep = true;
353 break;
354 case 'v': // dump AST after decl validation pass
355 validp = true;
356 break;
357 case 'y':
358 errorp = true;
359 break;
360 case 'z':
361 codegenp = true;
362 break;
363 case 'D': // ignore -Dxxx
364 break;
365 case 'F': // source file-name without suffix
366 filename = optarg;
367 break;
368 case '?':
369 cout << "Unknown option: '" << (char)optopt << "'" << endl;
370 exit( EXIT_FAILURE );
371 default:
372 abort();
373 } // switch
374 } // while
375} // parse_cmdline
376
377static void parse( FILE * input, LinkageSpec::Type link, bool shouldExit ) {
378 extern int yyparse( void );
[cbaee0d]379 extern FILE * yyin;
[0da3e2c]380 extern int yylineno;
381
382 linkage = link; // set globals
383 yyin = input;
384 yylineno = 1;
385 typedefTable.enterScope();
386 int parseStatus = yyparse();
[81419b5]387
388 fclose( input );
[0da3e2c]389 if ( shouldExit || parseStatus != 0 ) {
390 exit( parseStatus );
[81419b5]391 } // if
[0da3e2c]392} // parse
[81419b5]393
[1ab4ce2]394static bool notPrelude( Declaration * decl ) {
395 return ! LinkageSpec::isBuiltin( decl->get_linkage() );
[0da3e2c]396} // notPrelude
[1ab4ce2]397
[f77f12e2]398static void dump( std::list< Declaration * > & translationUnit, std::ostream & out ) {
[1ab4ce2]399 std::list< Declaration * > decls;
400 if ( noprotop ) {
[7880579]401 filter( translationUnit.begin(), translationUnit.end(), std::back_inserter( decls ), notPrelude );
[1ab4ce2]402 } else {
403 decls = translationUnit;
404 }
405
[f77f12e2]406 printAll( decls, out );
[7f5566b]407 deleteAll( translationUnit );
[0da3e2c]408} // dump
[1ab4ce2]409
[51b73452]410// Local Variables: //
[b87a5ed]411// tab-width: 4 //
412// mode: c++ //
413// compile-command: "make install" //
[51b73452]414// End: //
Note: See TracBrowser for help on using the repository browser.