source: src/main.cc @ c09e4bc

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since c09e4bc was 7880579, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

more refactoring of parser code

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