Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/main.cc

    re497c1d r6cf27a07  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // main.cc -- 
     7// main.cc --
    88//
    99// Author           : Richard C. Bilson
    1010// Created On       : Fri May 15 23:12:02 2015
    11 // Last Modified By : Rob Schluntz
    12 // Last Modified On : Tue Jul 28 13:05:02 2015
    13 // Update Count     : 165
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Tue Jul  5 15:23:11 2016
     13// Update Count     : 209
    1414//
    1515
     
    4040#include "MakeLibCfa.h"
    4141#include "InitTweak/Mutate.h"
    42 #include "InitTweak/RemoveInit.h"
     42#include "InitTweak/GenInit.h"
     43#include "InitTweak/FixInit.h"
    4344//#include "Explain/GenProlog.h"
    4445//#include "Try/Visit.h"
    4546
    46 #include "SemanticError.h"
    47 #include "UnimplementedError.h"
     47#include "Common/SemanticError.h"
     48#include "Common/UnimplementedError.h"
    4849
    4950#include "../config.h"
     
    5556
    5657static void parse( FILE * input, LinkageSpec::Type t, bool shouldExit = false );
    57 static void dump( std::list< Declaration * > & translationUnit );
     58static void dump( std::list< Declaration * > & translationUnit, std::ostream & out = std::cout );
    5859
    5960bool
    6061        astp = false,
    6162        bresolvep = false,
     63        bboxp = false,
     64        ctorinitp = false,
    6265        exprp = false,
    6366        expraltp = false,
     
    6972        resolvep = false,                                                                       // used in AlternativeFinder
    7073        symtabp = false,
     74        treep = false,
    7175        validp = false,
    7276        errorp = false,
    7377        codegenp = false;
    7478
    75 enum { Ast, Bresolver, Expr, ExprAlt, Grammar, LibCFA, Nopreamble, Parse, Prototypes, Resolver, Symbol, Validate, };
     79enum { Ast, Bbox, Bresolver, CtorInitFix, Expr, ExprAlt, Grammar, LibCFA, Nopreamble, Parse, Prototypes, Resolver, Symbol, Tree, Validate, };
    7680
    7781static struct option long_opts[] = {
    7882        { "ast", no_argument, 0, Ast },
     83        { "before-box", no_argument, 0, Bbox },
    7984        { "before-resolver", no_argument, 0, Bresolver },
     85        { "ctorinitfix", no_argument, 0, CtorInitFix },
    8086        { "expr", no_argument, 0, Expr },
    8187        { "expralt", no_argument, 0, ExprAlt },
     
    8793        { "resolver", no_argument, 0, Resolver },
    8894        { "symbol", no_argument, 0, Symbol },
     95        { "tree", no_argument, 0, Tree },
    8996        { "validate", no_argument, 0, Validate },
    9097        { 0, 0, 0, 0 }
     
    95102        std::ostream *output = &std::cout;
    96103        int long_index;
    97         std::list< Declaration* > translationUnit;
     104        std::list< Declaration * > translationUnit;
     105        const char *filename = NULL;
    98106
    99107        opterr = 0;                                                                                     // prevent getopt from printing error messages
    100        
     108
    101109        int c;
    102         while ( (c = getopt_long( argc, argv, "abefglnpqrsvyzD:", long_opts, &long_index )) != -1 ) {
     110        while ( (c = getopt_long( argc, argv, "abBcefglnpqrstvyzD:F:", long_opts, &long_index )) != -1 ) {
    103111                switch ( c ) {
    104112                  case Ast:
     
    110118                        bresolvep = true;
    111119                        break;
     120                  case 'B':                                                                             // print before resolver steps
     121                        bboxp = true;
     122                        break;
     123                  case CtorInitFix:
     124                  case 'c':
     125                        ctorinitp = true;
     126                        break;
    112127                  case Expr:
    113128                  case 'e':                                                                             // dump AST after expression analysis
     
    146161                        symtabp = true;
    147162                        break;
     163                  case Tree:
     164                  case 't':                                                                             // build in tree
     165                        treep = true;
     166                        break;
    148167                  case 'v':                                                                             // dump AST after decl validation pass
    149168                        validp = true;
     
    157176                  case 'D':                                                                             // ignore -Dxxx
    158177                        break;
     178                  case 'F':                                                                             // source file-name without suffix
     179                        filename = optarg;
     180                        break;
    159181                  case '?':
    160182                        cout << "Unknown option: '" << (char)optopt << "'" << endl;
    161                         exit(1);
     183                        exit( EXIT_FAILURE );
    162184                  default:
    163185                        abort();
     
    170192                        input = fopen( argv[ optind ], "r" );
    171193                        if ( ! input ) {
    172                                 std::cout << "Error: can't open " << argv[optind] << std::endl;
    173                                 exit( 1 );
     194                                std::cout << "Error: can't open " << argv[ optind ] << std::endl;
     195                                exit( EXIT_FAILURE );
    174196                        } // if
     197                        // if running cfa-cpp directly, might forget to pass -F option (and really shouldn't have to)
     198                        if ( filename == NULL ) filename = argv[ optind ];
     199                        // prelude filename comes in differently
     200                        if ( libcfap ) filename = "prelude.cf";
    175201                        optind += 1;
    176202                } else {
    177203                        input = stdin;
     204                        // if running cfa-cpp directly, might forget to pass -F option. Since this takes from stdin, pass
     205                        // a fake name along
     206                        if ( filename == NULL ) filename = "stdin";
    178207                } // if
    179208
     
    181210                        output = new ofstream( argv[ optind ] );
    182211                } // if
    183        
     212
    184213                Parser::get_parser().set_debug( grammarp );
    185214
    186                 // read in the builtins and the prelude
     215                // read in the builtins, extras, and the prelude
    187216                if ( ! nopreludep ) {                                                   // include gcc builtins
    188                         FILE * builtins = fopen( CFA_LIBDIR "/builtins.cf", "r" );
     217                        // -l is for initial build ONLY and builtins.cf is not in the lib directory so access it here.
     218                        FILE * builtins = fopen( libcfap | treep ? "builtins.cf" : CFA_LIBDIR "/builtins.cf", "r" );
    189219                        if ( builtins == NULL ) {
    190                                 std::cerr << "Error: can't open builtins" << std::endl;
    191                                 exit( 1 );
     220                                std::cerr << "Error: can't open builtins.cf" << std::endl;
     221                                exit( EXIT_FAILURE );
    192222                        } // if
    193 
    194223                        parse( builtins, LinkageSpec::Compiler );
    195224
     225                        // read the extra prelude in, if not generating the cfa library
     226                        FILE * extras = fopen( libcfap | treep ? "extras.cf" : CFA_LIBDIR "/extras.cf", "r" );
     227                        if ( extras == NULL ) {
     228                                std::cerr << "Error: can't open extras.cf" << std::endl;
     229                                exit( EXIT_FAILURE );
     230                        } // if
     231                        parse( extras, LinkageSpec::C );
     232
    196233                        if ( ! libcfap ) {
    197                                 // read the prelude in, if we're not generating the cfa library
    198                                 FILE * prelude = fopen( CFA_LIBDIR "/prelude.cf", "r" );
     234                                // read the prelude in, if not generating the cfa library
     235                                FILE * prelude = fopen( treep ? "prelude.cf" : CFA_LIBDIR "/prelude.cf", "r" );
    199236                                if ( prelude == NULL ) {
    200                                         std::cerr << "Error: can't open prelude" << std::endl;
    201                                         exit( 1 );
     237                                        std::cerr << "Error: can't open prelude.cf" << std::endl;
     238                                        exit( EXIT_FAILURE );
    202239                                } // if
    203                    
    204                     parse( prelude, LinkageSpec::Intrinsic );
     240
     241                                parse( prelude, LinkageSpec::Intrinsic );
    205242                        } // if
    206243                } // if
    207244
    208                 if ( libcfap ) {
    209                         parse( input, LinkageSpec::Intrinsic );
    210                 } else {
    211                         parse( input, LinkageSpec::Cforall, grammarp );
    212                 }
    213  
     245                parse( input, libcfap ? LinkageSpec::Intrinsic : LinkageSpec::Cforall, grammarp );
     246
    214247                if ( parsep ) {
    215248                        Parser::get_parser().get_parseTree()->printList( std::cout );
     
    226259                } // if
    227260
    228                 // add the assignment statement after the
    229                 // initialization of a type parameter
     261                // add the assignment statement after the initialization of a type parameter
    230262                OPTPRINT( "validate" )
    231263                SymTab::validate( translationUnit, symtabp );
     
    247279                OPTPRINT( "mutate" )
    248280                ControlStruct::mutate( translationUnit );
    249                 OPTPRINT( "fixNames" ) 
     281                OPTPRINT( "fixNames" )
    250282                CodeGen::fixNames( translationUnit );
    251                 OPTPRINT( "tweak" )
    252                 InitTweak::tweak( translationUnit );
     283                OPTPRINT( "tweakInit" )
     284                InitTweak::genInit( translationUnit );
    253285
    254286                if ( libcfap ) {
     
    266298                if ( exprp ) {
    267299                        dump( translationUnit );
     300                        return 0;
     301                }
     302
     303                // fix ObjectDecl - replaces ConstructorInit nodes
     304                OPTPRINT( "fixInit" )
     305                InitTweak::fix( translationUnit, filename, libcfap || treep );
     306                if ( ctorinitp ) {
     307                        dump ( translationUnit );
     308                        return 0;
    268309                }
    269310
     
    271312                GenPoly::copyParams( translationUnit );
    272313                OPTPRINT( "convertSpecializations" )
    273                 GenPoly::convertSpecializations( translationUnit );             
     314                GenPoly::convertSpecializations( translationUnit );
    274315                OPTPRINT( "convertLvalue" )
    275316                GenPoly::convertLvalue( translationUnit );
     317
     318                if ( bboxp ) {
     319                        dump( translationUnit );
     320                        return 0;
     321                }
    276322                OPTPRINT( "box" )
    277323                GenPoly::box( translationUnit );
     
    290336        } catch ( SemanticError &e ) {
    291337                if ( errorp ) {
    292                         dump( translationUnit );
     338                        std::cerr << "---AST at error:---" << std::endl;
     339                        dump( translationUnit, std::cerr );
     340                        std::cerr << std::endl << "---End of AST, begin error message:---\n" << std::endl;
    293341                }
    294342                e.print( std::cerr );
     
    312360        } // try
    313361
     362        deleteAll( translationUnit );
    314363        return 0;
    315364} // main
     
    329378}
    330379
    331 static void dump( std::list< Declaration * > & translationUnit ) {
     380static void dump( std::list< Declaration * > & translationUnit, std::ostream & out ) {
    332381        std::list< Declaration * > decls;
    333382        if ( noprotop ) {
    334                 filter( translationUnit.begin(), translationUnit.end(), 
     383                filter( translationUnit.begin(), translationUnit.end(),
    335384                                std::back_inserter( decls ), notPrelude );
    336385        } else {
     
    338387        }
    339388
    340         printAll( decls, std::cout );
     389        printAll( decls, out );
     390        deleteAll( translationUnit );
    341391}
    342392
Note: See TracChangeset for help on using the changeset viewer.