logo

class

compiler::Compiler

sys::Obj
  compiler::Compiler
   1  //
   2  // Copyright (c) 2006, Brian Frank and Andy Frank
   3  // Licensed under the Academic Free License version 3.0
   4  //
   5  // History:
   6  //    3 Sep 05  Brian Frank  Creation
   7  //   18 May 06  Brian Frank  Ported from Java to Fan
   8  //
   9  
  10  **
  11  ** Compiler manages the top level process of the compiler pipeline.
  12  ** There are a couple different "pipelines" used to accomplish
  13  ** various twists on compiling Fan code (from memory, files, etc).
  14  ** The pipelines are implemented as discrete CompilerSteps.
  15  ** As the steps are executed, the Compiler instance itself stores
  16  ** the state as we move from files -> ast -> resolved ast -> code.
  17  **
  18  ** Error reporting is managed via the Compiler.errors list.  If
  19  ** the compiler encounters problems it accumulates the errors as
  20  ** CompileExceptions in this list, then raises the first exception
  21  ** to the caller.  All errors go thru the CompilerSupport.err()
  22  ** methods for logging.  To log an error and continue we simply
  23  ** call err().  To fail fast, we code something like: throw err().
  24  ** Or at the end of a step we may call bombIfErr() which throws the
  25  ** first exception if any errors have accumulated.
  26  **
  27  class Compiler
  28  {
  29  
  30  //////////////////////////////////////////////////////////////////////////
  31  // Construction
  32  //////////////////////////////////////////////////////////////////////////
  33  
  34    **
  35    ** Construct with reasonable defaults
  36    **
  37    new make(CompilerInput input)
  38    {
  39      if (input.log == null)
  40        throw ArgErr.make("CompilerInput.log is null")
  41  
  42      this.input  = input
  43      this.log    = input.log
  44      this.errors = CompilerErr[,]
  45    }
  46  
  47  //////////////////////////////////////////////////////////////////////////
  48  // Compile
  49  //////////////////////////////////////////////////////////////////////////
  50  
  51    **
  52    ** Compile fan source code from the configured CompilerInput
  53    ** into a fan pod and return the resulting CompilerOutput.
  54    **
  55    CompilerOutput compile()
  56    {
  57      log.info("Compile [${input.podName}]")
  58      log.indent
  59  
  60      InitInput.make(this).run
  61      ResolveDepends.make(this).run
  62      ScanForUsingsAndTypes.make(this).run
  63      ResolveImports.make(this).run
  64      Parse.make(this).run
  65      OrderByInheritance.make(this).run
  66      CheckInheritance.make(this).run
  67      Inherit.make(this).run
  68      DefaultCtor.make(this).run
  69      InitEnum.make(this).run
  70      InitClosures.make(this).run
  71      Normalize.make(this).run
  72      ResolveExpr.make(this).run
  73      CheckErrors.make(this).run
  74      CheckParamDefs.make(this).run
  75      ClosureVars.make(this).run
  76  //  pod.dump
  77      Assemble.make(this).run
  78  //  fpod.dump
  79      GenerateOutput.make(this).run
  80  
  81      log.unindent
  82      return output
  83    }
  84  
  85  //////////////////////////////////////////////////////////////////////////
  86  // Fields
  87  //////////////////////////////////////////////////////////////////////////
  88  
  89    CompilerInput input       // ctor
  90    CompilerLog log           // ctor
  91    CompilerErr[] errors      // accumulated errors
  92    Namespace ns              // InitInput
  93    PodDef pod                // InitInput
  94    Bool isSys := false       // InitInput; are we compiling sys itself
  95    File[] srcFiles           // FindSourceFiles
  96    File[] resFiles           // FindSourceFiles
  97    TypeDef[] types           // Parse
  98    ClosureExpr[] closures    // Parse
  99    FPod fpod                 // Assemble
 100    CompilerOutput output     // GenerateOutput
 101  
 102  }