"Fossies" - the Fresh Open Source Software Archive

Member "asymptote-2.61/name.h" (18 Nov 2019, 5239 Bytes) of package /linux/misc/asymptote-2.61.src.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "name.h" see the Fossies "Dox" file reference documentation.

    1 /*****
    2  * name.h
    3  * Andy Hammerlindl 2002/07/14
    4  *
    5  * Qualified names (such as x, f, builtin.sin, a.b.c.d, etc.) can be used
    6  * either as variables or a type names.  This class stores qualified
    7  * names used in nameExp and nameTy in the abstract syntax, and
    8  * implements the exp and type functions.
    9  *****/
   10 
   11 #ifndef NAME_H
   12 #define NAME_H
   13 
   14 #include "absyn.h"
   15 #include "types.h"
   16 #include "frame.h"
   17 #include "access.h"
   18 
   19 namespace trans {
   20 class coenv;
   21 class varEntry;
   22 class tyEntry;
   23 }
   24 namespace types {
   25 class record;
   26 }
   27 
   28 namespace absyntax {
   29 
   30 using trans::coenv;
   31 using trans::action;
   32 using types::record;
   33 using std::ostream;
   34 
   35 class name : public absyn {
   36 public:
   37   name(position pos)
   38     : absyn(pos) {}
   39 
   40   // Helper function - ensures target and source match up, using casting in the
   41   // case of a read.  Issues errors on failure.
   42   void forceEquivalency(action act, coenv &e,
   43                         types::ty *target, types::ty *source);
   44 
   45   // Used for determining the type when the context does not establish
   46   // the name as a variable or a type.  First, the function looks for a
   47   // non-function variable fitting the description.  If one fits, the
   48   // type of the variable is returned.  Failing that, the function looks
   49   // for a fitting type and returns that.  If nothing is found, an
   50   // appropriate error is reported and ty_error is returned.
   51   // Because this is used only on qualifiers (ie. names to the left of a
   52   // dot), it does not look at function variables.
   53   // Tacit means that no error messages will be reported to the user.
   54   virtual types::ty *getType(coenv &e, bool tacit = false);
   55 
   56   // Pushes the highest level frame possible onto the stack.  Returning
   57   // the frame pushed.  If no frame can be pushed, returns 0.
   58   // NOTE: This duplicates some functionality with getVarEntry.
   59   virtual trans::frame *frameTrans(coenv &e);
   60   // Helper function for the case where the name is known to be a type.
   61   virtual trans::frame *tyFrameTrans(coenv &e) = 0;
   62 
   63   // Constructs the varEntry part of the tyEntry for the name.  Like
   64   // getType, this is called on the qualifier, instead of the full name.
   65   // This reports no errors, and returns 0 if there is no varEntry to
   66   // use.
   67   virtual trans::varEntry *getVarEntry(coenv &e) = 0;
   68 
   69   // As a variable:
   70   // Translates the name (much like an expression).
   71   virtual void varTrans(action act, coenv &e, types::ty *target) = 0;
   72   // Returns the possible variable types.  Unlike exp, returns 0 if none
   73   // match.
   74   virtual types::ty *varGetType(coenv &e) = 0;
   75   virtual trans::varEntry *getCallee(coenv &e, types::signature *sig) = 0;
   76 
   77   // As a type:
   78   // Determines the type, as used in a variable declaration.
   79   virtual types::ty *typeTrans(coenv &e, bool tacit = false) = 0;
   80   // Constructs the tyEntry of the name, needed so that we know the
   81   // parent frame for allocating new objects of that type.  Reports
   82   // errors as typeTrans() does with tacit=false.
   83   virtual trans::tyEntry *tyEntryTrans(coenv &e) = 0;
   84 
   85   virtual void prettyprint(ostream &out, Int indent) = 0;
   86   virtual void print(ostream& out) const {
   87     out << "<base name>";
   88   }
   89 
   90   virtual symbol getName() = 0;
   91 };
   92 
   93 inline ostream& operator<< (ostream& out, const name& n) {
   94   n.print(out);
   95   return out;
   96 }
   97 
   98 class simpleName : public name {
   99   symbol id;
  100 
  101 public:
  102   simpleName(position pos, symbol id)
  103     : name(pos), id(id) {}
  104 
  105   trans::varEntry *getVarEntry(coenv &e);
  106 
  107   // As a variable:
  108   void varTrans(action act, coenv &e, types::ty *target);
  109   types::ty *varGetType(coenv &);
  110   trans::varEntry *getCallee(coenv &e, types::signature *sig);
  111 
  112   // As a type:
  113   types::ty *typeTrans(coenv &e, bool tacit = false);
  114   virtual trans::tyEntry *tyEntryTrans(coenv &e);
  115   trans::frame *tyFrameTrans(coenv &e);
  116 
  117   void prettyprint(ostream &out, Int indent);
  118   void print(ostream& out) const {
  119     out << id;
  120   }
  121   symbol getName() {
  122     return id;
  123   }
  124 };
  125 
  126 
  127 class qualifiedName : public name {
  128   name *qualifier;
  129   symbol id;
  130 
  131   // Gets the record type associated with the qualifier. Reports an
  132   // error and returns null if the type is not a record.
  133   record *castToRecord(types::ty *t, bool tacit = false);
  134 
  135   // Translates as a virtual field, if possible.  qt is the type of the
  136   // qualifier.  Return true if there was a matching virtual field. 
  137   bool varTransVirtual(action act, coenv &e,
  138                        types::ty *target, types::ty *qt);
  139   
  140   // Translates as an ordinary (non-virtual) field of a record, r.
  141   void varTransField(action act, coenv &e,
  142                      types::ty *target, record *r);
  143 public:
  144   qualifiedName(position pos, name *qualifier, symbol id)
  145     : name(pos), qualifier(qualifier), id(id) {}
  146 
  147   trans::varEntry *getVarEntry(coenv &e);
  148 
  149   // As a variable:
  150   void varTrans(action act, coenv &, types::ty *target);
  151   types::ty *varGetType(coenv &);
  152   trans::varEntry *getCallee(coenv &e, types::signature *sig);
  153 
  154   // As a type:
  155   types::ty *typeTrans(coenv &e, bool tacit = false);
  156   trans::tyEntry *tyEntryTrans(coenv &e);
  157   trans::frame *tyFrameTrans(coenv &e);
  158 
  159   void prettyprint(ostream &out, Int indent);
  160   void print(ostream& out) const {
  161     out << *qualifier << "." << id;
  162   }
  163   symbol getName() {
  164     return id;
  165   }
  166 };
  167 
  168 } // namespace absyntax
  169 
  170 #endif