Changeset 04cdd9b for src/main.cc


Ignore:
Timestamp:
Aug 19, 2016, 2:42:04 PM (9 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
e85a8631
Parents:
03da511 (diff), ac71a86 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' into ctor

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/main.cc

    r03da511 r04cdd9b  
    1010// Created On       : Fri May 15 23:12:02 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Aug  6 16:17:41 2016
    13 // Update Count     : 210
     12// Last Modified On : Fri Aug 19 08:31:22 2016
     13// Update Count     : 350
    1414//
    1515
    1616#include <iostream>
    1717#include <fstream>
    18 #include <cstdlib>
    19 #include <cstdio>
    2018#include <getopt.h>
    21 #include "Parser/Parser.h"
    22 #include "Parser/ParseNode.h"
    23 #include "Parser/LinkageSpec.h"
    24 #include "SynTree/Declaration.h"
    25 #include "SynTree/Visitor.h"
     19#include "Parser/lex.h"
     20#include "Parser/parser.h"
     21#include "Parser/TypedefTable.h"
    2622#include "GenPoly/Lvalue.h"
    2723#include "GenPoly/Specialize.h"
     
    3228#include "CodeGen/FixNames.h"
    3329#include "ControlStruct/Mutate.h"
    34 #include "Tuples/Mutate.h"
    35 #include "Tuples/FunctionChecker.h"
    36 #include "SymTab/Mangler.h"
    37 #include "SymTab/Indexer.h"
    3830#include "SymTab/Validate.h"
    3931#include "ResolvExpr/AlternativePrinter.h"
    4032#include "ResolvExpr/Resolver.h"
    4133#include "MakeLibCfa.h"
    42 #include "InitTweak/Mutate.h"
    4334#include "InitTweak/GenInit.h"
    4435#include "InitTweak/FixInit.h"
    45 //#include "Explain/GenProlog.h"
    46 //#include "Try/Visit.h"
    47 
    48 #include "Common/SemanticError.h"
    4936#include "Common/UnimplementedError.h"
    5037
     
    5340using namespace std;
    5441
    55 #define OPTPRINT(x) \
    56         if ( errorp ) std::cerr << x << std::endl;
    57 
    58 static void parse( FILE * input, LinkageSpec::Type t, bool shouldExit = false );
    59 static void dump( std::list< Declaration * > & translationUnit, std::ostream & out = std::cout );
    60 
     42#define OPTPRINT(x) if ( errorp ) std::cerr << x << std::endl;
     43
     44
     45LinkageSpec::Spec linkage = LinkageSpec::Cforall;
     46TypedefTable typedefTable;
     47DeclarationNode * parseTree = nullptr;                                  // program parse tree
     48
     49extern int yydebug;                                                                             // set for -g flag (Grammar)
    6150bool
    6251        astp = false,
     
    6655        exprp = false,
    6756        expraltp = false,
    68         grammarp = false,
    6957        libcfap = false,
    7058        nopreludep = false,
     
    7866        codegenp = false;
    7967
    80 enum { Ast, Bbox, Bresolver, CtorInitFix, Expr, ExprAlt, Grammar, LibCFA, Nopreamble, Parse, Prototypes, Resolver, Symbol, Tree, Validate, };
    81 
    82 static struct option long_opts[] = {
    83         { "ast", no_argument, 0, Ast },
    84         { "before-box", no_argument, 0, Bbox },
    85         { "before-resolver", no_argument, 0, Bresolver },
    86         { "ctorinitfix", no_argument, 0, CtorInitFix },
    87         { "expr", no_argument, 0, Expr },
    88         { "expralt", no_argument, 0, ExprAlt },
    89         { "grammar", no_argument, 0, Grammar },
    90         { "libcfa", no_argument, 0, LibCFA },
    91         { "no-preamble", no_argument, 0, Nopreamble },
    92         { "parse", no_argument, 0, Parse },
    93         { "no-prototypes", no_argument, 0, Prototypes },
    94         { "resolver", no_argument, 0, Resolver },
    95         { "symbol", no_argument, 0, Symbol },
    96         { "tree", no_argument, 0, Tree },
    97         { "validate", no_argument, 0, Validate },
    98         { 0, 0, 0, 0 }
    99 };
    100 
    101 int main( int argc, char *argv[] ) {
    102         FILE *input;
    103         std::ostream *output = &std::cout;
    104         int long_index;
     68static void parse_cmdline( int argc, char *argv[], const char *& filename );
     69static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit = false );
     70static void dump( std::list< Declaration * > & translationUnit, std::ostream & out = std::cout );
     71
     72int main( int argc, char * argv[] ) {
     73        FILE * input;                                                                           // use FILE rather than istream because yyin is FILE
     74        std::ostream *output = & std::cout;
    10575        std::list< Declaration * > translationUnit;
    106         const char *filename = NULL;
    107 
    108         opterr = 0;                                                                                     // prevent getopt from printing error messages
    109 
    110         int c;
    111         while ( (c = getopt_long( argc, argv, "abBcefglnpqrstvyzD:F:", long_opts, &long_index )) != -1 ) {
    112                 switch ( c ) {
    113                   case Ast:
    114                   case 'a':                                                                             // dump AST
    115                         astp = true;
    116                         break;
    117                   case Bresolver:
    118                   case 'b':                                                                             // print before resolver steps
    119                         bresolvep = true;
    120                         break;
    121                   case 'B':                                                                             // print before resolver steps
    122                         bboxp = true;
    123                         break;
    124                   case CtorInitFix:
    125                   case 'c':
    126                         ctorinitp = true;
    127                         break;
    128                   case Expr:
    129                   case 'e':                                                                             // dump AST after expression analysis
    130                         exprp = true;
    131                         break;
    132                   case ExprAlt:
    133                   case 'f':                                                                             // print alternatives for expressions
    134                         expraltp = true;
    135                         break;
    136                   case Grammar:
    137                   case 'g':                                                                             // bison debugging info (grammar rules)
    138                         grammarp = true;
    139                         break;
    140                   case LibCFA:
    141                   case 'l':                                                                             // generate libcfa.c
    142                         libcfap = true;
    143                         break;
    144                   case Nopreamble:
    145                   case 'n':                                                                             // do not read preamble
    146                         nopreludep = true;
    147                         break;
    148                   case Prototypes:
    149                   case 'p':                                                                             // generate prototypes for preamble functions
    150                         noprotop = true;
    151                         break;
    152                   case Parse:
    153                   case 'q':                                                                             // dump parse tree
    154                         parsep = true;
    155                         break;
    156                   case Resolver:
    157                   case 'r':                                                                             // print resolver steps
    158                         resolvep = true;
    159                         break;
    160                   case Symbol:
    161                   case 's':                                                                             // print symbol table events
    162                         symtabp = true;
    163                         break;
    164                   case Tree:
    165                   case 't':                                                                             // build in tree
    166                         treep = true;
    167                         break;
    168                   case 'v':                                                                             // dump AST after decl validation pass
    169                         validp = true;
    170                         break;
    171                   case 'y':
    172                         errorp = true;
    173                         break;
    174                   case 'z':
    175                         codegenp = true;
    176                         break;
    177                   case 'D':                                                                             // ignore -Dxxx
    178                         break;
    179                   case 'F':                                                                             // source file-name without suffix
    180                         filename = optarg;
    181                         break;
    182                   case '?':
    183                         cout << "Unknown option: '" << (char)optopt << "'" << endl;
    184                         exit( EXIT_FAILURE );
    185                   default:
    186                         abort();
    187                 } // switch
    188         } // while
     76        const char *filename = nullptr;
     77
     78        parse_cmdline( argc, argv, filename );                          // process command-line arguments
    18979
    19080        try {
    19181                // choose to read the program from a file or stdin
    192                 if ( optind < argc ) {
     82                if ( optind < argc ) {                                                  // any commands after the flags ? => input file name
    19383                        input = fopen( argv[ optind ], "r" );
    194                         if ( ! input ) {
    195                                 std::cout << "Error: cannot open " << argv[ optind ] << std::endl;
    196                                 exit( EXIT_FAILURE );
    197                         } // if
     84                        assertf( input, "cannot open %s\n", argv[ optind ] );
    19885                        // if running cfa-cpp directly, might forget to pass -F option (and really shouldn't have to)
    199                         if ( filename == NULL ) filename = argv[ optind ];
     86                        if ( filename == nullptr ) filename = argv[ optind ];
    20087                        // prelude filename comes in differently
    20188                        if ( libcfap ) filename = "prelude.cf";
    20289                        optind += 1;
    203                 } else {
     90                } else {                                                                                // no input file name
    20491                        input = stdin;
    20592                        // if running cfa-cpp directly, might forget to pass -F option. Since this takes from stdin, pass
    20693                        // a fake name along
    207                         if ( filename == NULL ) filename = "stdin";
    208                 } // if
    209 
    210                 if ( optind < argc ) {
     94                        if ( filename == nullptr ) filename = "stdin";
     95                } // if
     96
     97                if ( optind < argc ) {                                                  // any commands after the flags and input file ? => output file name
    21198                        output = new ofstream( argv[ optind ] );
    21299                } // if
    213 
    214                 Parser::get_parser().set_debug( grammarp );
    215100
    216101                // read in the builtins, extras, and the prelude
     
    218103                        // -l is for initial build ONLY and builtins.cf is not in the lib directory so access it here.
    219104                        FILE * builtins = fopen( libcfap | treep ? "builtins.cf" : CFA_LIBDIR "/builtins.cf", "r" );
    220                         if ( builtins == NULL ) {
    221                                 std::cerr << "Error: cannot open builtins.cf" << std::endl;
    222                                 exit( EXIT_FAILURE );
    223                         } // if
     105                        assertf( builtins, "cannot open builtins.cf\n" );
    224106                        parse( builtins, LinkageSpec::Compiler );
    225107
    226108                        // read the extra prelude in, if not generating the cfa library
    227109                        FILE * extras = fopen( libcfap | treep ? "extras.cf" : CFA_LIBDIR "/extras.cf", "r" );
    228                         if ( extras == NULL ) {
    229                                 std::cerr << "Error: cannot open extras.cf" << std::endl;
    230                                 exit( EXIT_FAILURE );
    231                         } // if
     110                        assertf( extras, "cannot open extras.cf\n" );
    232111                        parse( extras, LinkageSpec::C );
    233112
     
    235114                                // read the prelude in, if not generating the cfa library
    236115                                FILE * prelude = fopen( treep ? "prelude.cf" : CFA_LIBDIR "/prelude.cf", "r" );
    237                                 if ( prelude == NULL ) {
    238                                         std::cerr << "Error: cannot open prelude.cf" << std::endl;
    239                                         exit( EXIT_FAILURE );
    240                                 } // if
    241 
     116                                assertf( prelude, "cannot open prelude.cf\n" );
    242117                                parse( prelude, LinkageSpec::Intrinsic );
    243118                        } // if
    244119                } // if
    245120
    246                 parse( input, libcfap ? LinkageSpec::Intrinsic : LinkageSpec::Cforall, grammarp );
     121                parse( input, libcfap ? LinkageSpec::Intrinsic : LinkageSpec::Cforall, yydebug );
    247122
    248123                if ( parsep ) {
    249                         Parser::get_parser().get_parseTree()->printList( std::cout );
    250                         Parser::get_parser().freeTree();
    251                         return 0;
    252                 } // if
    253 
    254                 buildList( Parser::get_parser().get_parseTree(), translationUnit );
    255 
    256                 Parser::get_parser().freeTree();
     124                        parseTree->printList( std::cout );
     125                        delete parseTree;
     126                        return 0;
     127                } // if
     128
     129                buildList( parseTree, translationUnit );
     130                delete parseTree;
     131                parseTree = nullptr;
     132
    257133                if ( astp ) {
    258134                        dump( translationUnit );
     
    300176                        dump( translationUnit );
    301177                        return 0;
    302                 }
     178                } // if
    303179
    304180                // fix ObjectDecl - replaces ConstructorInit nodes
     
    308184                        dump ( translationUnit );
    309185                        return 0;
    310                 }
     186                } // if
    311187
    312188                OPTPRINT("instantiateGenerics")
     
    322198                        dump( translationUnit );
    323199                        return 0;
    324                 }
     200                } // if
    325201                OPTPRINT( "box" )
    326202                GenPoly::box( translationUnit );
     
    342218                        dump( translationUnit, std::cerr );
    343219                        std::cerr << std::endl << "---End of AST, begin error message:---\n" << std::endl;
    344                 }
     220                } // if
    345221                e.print( std::cerr );
    346222                if ( output != &std::cout ) {
     
    367243} // main
    368244
    369 static void parse( FILE * input, LinkageSpec::Type linkage, bool shouldExit ) {
    370         Parser::get_parser().set_linkage( linkage );
    371         Parser::get_parser().parse( input );
     245void parse_cmdline( int argc, char * argv[], const char *& filename ) {
     246        enum { Ast, Bbox, Bresolver, CtorInitFix, Expr, ExprAlt, Grammar, LibCFA, Nopreamble, Parse, Prototypes, Resolver, Symbol, Tree, Validate, };
     247
     248        static struct option long_opts[] = {
     249                { "ast", no_argument, 0, Ast },
     250                { "before-box", no_argument, 0, Bbox },
     251                { "before-resolver", no_argument, 0, Bresolver },
     252                { "ctorinitfix", no_argument, 0, CtorInitFix },
     253                { "expr", no_argument, 0, Expr },
     254                { "expralt", no_argument, 0, ExprAlt },
     255                { "grammar", no_argument, 0, Grammar },
     256                { "libcfa", no_argument, 0, LibCFA },
     257                { "no-preamble", no_argument, 0, Nopreamble },
     258                { "parse", no_argument, 0, Parse },
     259                { "no-prototypes", no_argument, 0, Prototypes },
     260                { "resolver", no_argument, 0, Resolver },
     261                { "symbol", no_argument, 0, Symbol },
     262                { "tree", no_argument, 0, Tree },
     263                { "validate", no_argument, 0, Validate },
     264                { 0, 0, 0, 0 }
     265        }; // long_opts
     266        int long_index;
     267
     268        opterr = 0;                                                                                     // (global) prevent getopt from printing error messages
     269
     270        int c;
     271        while ( (c = getopt_long( argc, argv, "abBcefglnpqrstvyzD:F:", long_opts, &long_index )) != -1 ) {
     272                switch ( c ) {
     273                  case Ast:
     274                  case 'a':                                                                             // dump AST
     275                        astp = true;
     276                        break;
     277                  case Bresolver:
     278                  case 'b':                                                                             // print before resolver steps
     279                        bresolvep = true;
     280                        break;
     281                  case 'B':                                                                             // print before resolver steps
     282                        bboxp = true;
     283                        break;
     284                  case CtorInitFix:
     285                  case 'c':
     286                        ctorinitp = true;
     287                        break;
     288                  case Expr:
     289                  case 'e':                                                                             // dump AST after expression analysis
     290                        exprp = true;
     291                        break;
     292                  case ExprAlt:
     293                  case 'f':                                                                             // print alternatives for expressions
     294                        expraltp = true;
     295                        break;
     296                  case Grammar:
     297                  case 'g':                                                                             // bison debugging info (grammar rules)
     298                        yydebug = true;
     299                        break;
     300                  case LibCFA:
     301                  case 'l':                                                                             // generate libcfa.c
     302                        libcfap = true;
     303                        break;
     304                  case Nopreamble:
     305                  case 'n':                                                                             // do not read preamble
     306                        nopreludep = true;
     307                        break;
     308                  case Prototypes:
     309                  case 'p':                                                                             // generate prototypes for preamble functions
     310                        noprotop = true;
     311                        break;
     312                  case Parse:
     313                  case 'q':                                                                             // dump parse tree
     314                        parsep = true;
     315                        break;
     316                  case Resolver:
     317                  case 'r':                                                                             // print resolver steps
     318                        resolvep = true;
     319                        break;
     320                  case Symbol:
     321                  case 's':                                                                             // print symbol table events
     322                        symtabp = true;
     323                        break;
     324                  case Tree:
     325                  case 't':                                                                             // build in tree
     326                        treep = true;
     327                        break;
     328                  case 'v':                                                                             // dump AST after decl validation pass
     329                        validp = true;
     330                        break;
     331                  case 'y':
     332                        errorp = true;
     333                        break;
     334                  case 'z':
     335                        codegenp = true;
     336                        break;
     337                  case 'D':                                                                             // ignore -Dxxx
     338                        break;
     339                  case 'F':                                                                             // source file-name without suffix
     340                        filename = optarg;
     341                        break;
     342                  case '?':
     343                        assertf( false, "Unknown option: '%c'\n", (char)optopt );
     344                  default:
     345                        abort();
     346                } // switch
     347        } // while
     348} // parse_cmdline
     349
     350static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit ) {
     351        extern int yyparse( void );
     352        extern FILE * yyin;
     353        extern int yylineno;
     354
     355        ::linkage = linkage;                                                            // set globals
     356        yyin = input;
     357        yylineno = 1;
     358        typedefTable.enterScope();
     359        int parseStatus = yyparse();
    372360
    373361        fclose( input );
    374         if ( shouldExit || Parser::get_parser().get_parseStatus() != 0 ) {
    375                 exit( Parser::get_parser().get_parseStatus() );
     362        if ( shouldExit || parseStatus != 0 ) {
     363                exit( parseStatus );
    376364        } // if
    377 }
     365} // parse
    378366
    379367static bool notPrelude( Declaration * decl ) {
    380368        return ! LinkageSpec::isBuiltin( decl->get_linkage() );
    381 }
     369} // notPrelude
    382370
    383371static void dump( std::list< Declaration * > & translationUnit, std::ostream & out ) {
    384372        std::list< Declaration * > decls;
     373
    385374        if ( noprotop ) {
    386                 filter( translationUnit.begin(), translationUnit.end(),
    387                                 std::back_inserter( decls ), notPrelude );
     375                filter( translationUnit.begin(), translationUnit.end(), std::back_inserter( decls ), notPrelude );
    388376        } else {
    389377                decls = translationUnit;
    390         }
     378        } // if
    391379
    392380        printAll( decls, out );
    393381        deleteAll( translationUnit );
    394 }
    395 
     382} // dump
    396383
    397384// Local Variables: //
Note: See TracChangeset for help on using the changeset viewer.