Polaris: Symbol.h Source File

Symbol.h

Go to the documentation of this file.
00001 ///
00002 #ifndef _SYMBOL_H
00003 #define _SYMBOL_H
00004 ///
00005 /// \class Symbol 
00006 /// \brief An abstract class to represent Fortran symbols (identifiers)
00007 /// \defgroup Polaris
00008 /// \ingroup Polaris
00009 ///  C++ VDL
00010 /// \see Symbol.h
00011 /// \see Symbol.h
00012 /// \see Symbol.cc
00013 ///
00014 /// \endcode
00015 /// \section Description Description
00016 /// The abstract class Symbol represents a Fortran symbol in the C++ VDL
00017 /// symbol table.  Since this class is abstract, all of the real
00018 /// work is done by inherited classes.  This class sets up all
00019 /// possible methods for inherited classes and either defines
00020 /// default values for them or defines default errors for
00021 /// accessing them (for these, they are valid only if a derived
00022 /// classes inherits them, since not all possible fields are
00023 /// for all class types.
00024 ///
00025 /// The reason for the structure of this class and its subclasses
00026 /// is that it was intended for Symtab to contain a list of
00027 /// pointers to the base class Symbol.  Thus the Symtab list may
00028 /// contain pointers actually to any derived class of Symbol.  All
00029 /// possible methods are supported by Symbol, thus no typecasting
00030 /// is necessary.  However, the sym_class() method needs to be
00031 /// checked before accessing these methods, since some of them
00032 /// will give error messages for the inherited classes that
00033 /// do not support them.
00034 ///
00035 /// Note:  Although the source for all derived classes are in separate
00036 /// files, they are related enough that their header files are all
00037 /// included from this file and their methods are all contained in
00038 /// the single source file Symbol.cc
00039 ///
00040 #ifdef POLARIS_GNU_PRAGMAS
00041 #pragma interface
00042 #endif
00043 ///
00044 #include "../ClassNames.h"
00045 #include "../String.h"
00046 #include "../ArrayBounds.h"
00047 #include "../ArrayDims.h"
00048 #include "../BinRep.h"
00049 #include "../RelinkableDef.h"
00050 #include "../Dictionary.h"
00051 #include "../Collection/List.h"
00052 #include "../p-assert.h"
00053 #include "../Type.h"
00054 #include "../VoidPtrDef.h"
00055 ///
00056 class CommonBlock;
00057 class Equivalence;
00058 class Expression;
00059 class ExprTable;
00060 class Statement;
00061 class SharedDims;
00062 class VDL;
00063 
00064 ///< The SYMBOL_CLASS enumeration defines the possible types of Symbol's
00065 enum SYMBOL_CLASS {
00066     UNDEFINED_CLASS = 0,
00067     PROGRAM_CLASS,              ///< A program unit starting with the
00068                                 ///< PROGRAM statement
00069     FUNCTION_CLASS,
00070     SUBROUTINE_CLASS,
00071     VARIABLE_CLASS,
00072     SYMBOLIC_CONSTANT_CLASS,    ///< i.e. defined in a parameter stmt
00073     BLOCK_DATA_CLASS,
00074     NAMELIST_CLASS
00075 
00076     ///< Note to programmer:  If this enum is changed in any way, make sure
00077     ///< to update the corresponding entries in the class_name[] array
00078     ///< in Symbol.cc
00079 };
00080 
00081 
00082 enum EXTERNAL_BOOL {
00083     IS_EXTERNAL = 1,
00084     NOT_EXTERNAL = 0
00085 };
00086 
00087 enum INTRINSIC_BOOL {
00088     IS_INTRINSIC = 1,
00089     NOT_INTRINSIC = 0
00090 };
00091 
00092 enum INITIAL_BOOL {
00093     IS_INITIALIZED = 1,
00094     NOT_INITIALIZED = 0
00095 };
00096 
00097 enum FORMAL_BOOL {
00098     IS_FORMAL = 1,
00099     NOT_FORMAL = 0
00100 };
00101 
00102 enum ALLOC_BOOL {
00103     IS_ALLOCATABLE = 1,
00104     NOT_ALLOCATABLE = 0
00105 };
00106 
00107 enum SAVED_BOOL {
00108     IS_SAVED = 1,
00109     NOT_SAVED = 0
00110 };
00111 
00112 enum GLOBAL_BOOL {
00113     IS_GLOBAL = 1,
00114     NOT_GLOBAL = 0
00115 };
00116 
00117 
00118 class Symbol : public RelinkableDef {
00119     friend ostream & operator << (ostream & o, const Symbol & other);
00120     friend class Symtab;
00121     friend class PostPass;
00122 
00123  protected:
00124     SYMBOL_CLASS    _sym_class;
00125     BinRep         *_overflow;
00126     int             _last_renaming_append;
00127 
00128     ///< _last_renaming_append is an integer which caches the last
00129     ///< appended integer which was added to a name of this base
00130     ///< to avoid naming conflicts in the symbol table.  I.e.
00131     ///< If this symbol name were "I", and three other "I"'s needed
00132     ///< to be inserted with naming conflicts avoided, then this
00133     ///< value would cached for "I" the values 0, 1, and 2 as the
00134     ///< other "I" symbols were renamed to "I0", "I1", and "I2".
00135     ///< It is not absolutely necessary, but it should save time
00136     ///< reducing multiple renaming attempts.
00137 
00138     void            _ref_error(const char *func_name) const;
00139     int             _get_size(const BinRep & insp, 
00140                               const BinRep & binstr_context) const;
00141     virtual  void _dims(ArrayDims *);
00142  public:
00143     inline          Symbol(const char *name, SYMBOL_CLASS cl);
00144 
00145     ///< Constructor -- requires Symbol name and a SYMBOL_CLASS
00146     ///< Note:  class Symbol is abstract, so you cannot create
00147     ///< an object of type Symbol.  Create one of the derived
00148     ///< classes instead, or, if you are trying to copy a current
00149     ///< symbol, consider the clone() method.
00150 
00151     virtual Symbol *clone() const = 0;
00152     ///< Clone constructor -- returns base pointer to a new clone
00153     ///< of the correct derived class of this Symbol
00154 
00155     virtual Symbol *clone_all() const = 0;
00156     ///< Clone constructor -- returns base pointer to a new clone
00157     ///< of the correct derived class of this Symbol
00158 
00159     Definition *definition_clone() const;
00160     ///< Clone constructor which returns a Definition* rather than
00161     ///< a Symbol* by calling clone()
00162 
00163     virtual         ~Symbol();
00164     ///< Destructor.
00165 
00166     virtual void    print(ostream & o) const = 0;
00167     ///< Print onto stream 'o'
00168 
00169     inline SYMBOL_CLASS sym_class() const;
00170     ///< Return the type of this Symbol
00171 
00172     inline const char *name_ref() const;
00173     ///< Return the Symbol name
00174 
00175     inline void     name(const char *new_name);
00176     ///< Change the Symbol name (such a change would be reflected in
00177     ///< everything else in the Program which points to this Symbol)
00178 
00179     inline BinRep  *overflow_ref() const;
00180 
00181     virtual void    fill_in(const BinRep & binstr,
00182                             ExprTable & exprs,
00183                             const Dictionary<VoidPtrDef>&stmts,
00184                             const Dictionary<VoidPtrDef>&commons,
00185                             const Dictionary<VoidPtrDef>&equivalences) = 0;
00186     ///< Fill in all relevant info from a binstr object
00187 
00188     
00189     ///< None of the following methods are valid (they give error messages)
00190     ///< in a derived class unless they are specifically overridden in that
00191     ///< class.  There is ONE exception to this rule:  The type() method
00192     ///< returns a VOID type if there is no type associated with the
00193     ///< Symbol subclass (e.g. ProgramSymbol::type() always returns a
00194     ///< void Type, so that ProgramSymbol::type().is_void() always returns 1)
00195 
00196     virtual INITIAL_BOOL initial_value() const;
00197     virtual void    initial_value(INITIAL_BOOL ourbool);
00198 
00199     virtual EXTERNAL_BOOL external() const;
00200     virtual void    external(EXTERNAL_BOOL ourbool);
00201 
00202     virtual INTRINSIC_BOOL intrinsic() const;
00203     virtual void    intrinsic(INTRINSIC_BOOL ourbool);
00204 
00205     virtual const Statement *entry_ref() const;
00206     virtual Statement       *entry_ref() ;
00207     virtual void    entry(Statement * s);
00208 
00209     virtual const   Type & type() const;
00210     ///< Returns the type of the symbol, or else returns type VOID_TYPE
00211     ///< if the symbol does not use a type (such as a SubroutineSymbol)
00212 
00213     virtual void    type(const Type & type);
00214 
00215     virtual Equivalence *equivalence_ref() const;
00216     virtual void    equivalence(Equivalence & e, int byte_base);
00217     virtual void    clear_equivalence();
00218 
00219     virtual void shared_expr(Expression *shared);
00220     ///< 'shared' must be an expression whose 'symbol' is this itself.
00221     ///< and it must DISTRIBUTE_OP or ID_OP
00222     virtual void set_to_private();
00223     ///< this symbol turns back to be a private by resetting _shared_expr to 0
00224     virtual Expression *shared_expr() const;
00225     ///<_ If thie is VariableSymbol and shared data object, it returns to the
00226     ///<_ pointer to its correspoding distribution expression.
00227     ///<_ The returned expression must be either ID_OP or DISTRIBUTE_OP.
00228     ///<_ If this is private, it returns 0. (only applicable to Cray T3D)
00229 
00230     virtual const ArrayDims & dim() const;
00231     virtual ArrayDims &       dim() ;
00232 
00233     virtual const SharedDims & shared_dim() const;
00234     virtual SharedDims &       shared_dim() ;
00235 
00236     virtual FORMAL_BOOL formal() const;
00237     virtual void    formal(FORMAL_BOOL ourbool);
00238 
00239     virtual ALLOC_BOOL allocatable() const;
00240     virtual void    allocatable(ALLOC_BOOL ourbool);
00241 
00242     virtual GLOBAL_BOOL global() const;
00243     virtual void    global(GLOBAL_BOOL ourbool);
00244 
00245     virtual SAVED_BOOL saved() const;
00246     virtual void    saved(SAVED_BOOL ourbool);
00247 
00248     virtual const CommonBlock *common_ref() const;
00249     virtual CommonBlock       *common_ref() ;
00250 
00251     virtual void    common(CommonBlock & cb, int index);
00252     ///< Insert this variable into common block cb at position
00253     ///< index (0 = first element)
00254 
00255     virtual void    clear_common();
00256     ///< Remove this variable from a common block if it is in one
00257 
00258     virtual const Expression *expr_ref() const;
00259     virtual Expression       *expr_ref() ;
00260     virtual void    expr(Expression * e);
00261 
00262     virtual int     is_entry();
00263 
00264     virtual int     is_scalar() const;
00265 
00266     virtual int     is_array() const;
00267 
00268     virtual int     is_shared() const;
00269 
00270     virtual int     is_pointer() const;
00271 
00272     virtual const Symbol & assoc_variable() const;
00273     virtual Symbol &       assoc_variable() ;
00274 
00275     virtual int     size() const;
00276     ///< Return the size in bytes of the memory refered to by the symbol.
00277 
00278     virtual int     structures_OK() const;
00279     ///< Check the structure of the data for errors or inconsistency
00280     ///< Return 0 and print error message if problems found, otherwise
00281     ///< return 1 without message.
00282  
00283     virtual void    exchange_convert( VDL &vdl );
00284     ///< Convert the Symbol into the exchange format.
00285  
00286     void            renaming_suggestion(String & new_name);
00287     ///< Given that some other symbol with the same name as this
00288     ///< symbol needs to be inserted into the same symbol table,
00289     ///< give a new name suggestion for the other symbol.
00290     ///< For example, if this symbol is named "I", the first time
00291     ///< this function is called, it will suggest "I0" for the
00292     ///< other symbol.  The next time it will suggest "I1", etc.
00293     ///<
00294     ///< This name suggestion is returned in the String
00295     ///< parameter 'new_name'.
00296     ///<
00297     ///< (\note  It should be rare that this function will be
00298     ///< be used directly, since there could still be naming
00299     ///< conflicts in a symbol table after using this function.
00300     ///< The preferred method is to create a new symbol with
00301     ///< the name being the base name that is desired, and then
00302     ///< to use the Symtab::rename_and_ins() function, which
00303     ///< references this function as many times as is needed.)
00304 };
00305 
00306 Symbol *make_symbol(const char *name, const BinRep & binstr);
00307 
00308 char   *upcase_ch( const char *string );
00309 
00310 inline 
00311 Symbol::Symbol(const char *name, SYMBOL_CLASS cl)
00312     : RelinkableDef("")
00313 {
00314     #ifdef CLASS_INSTANCE_REGISTRY
00315     register_instance(SYMBOL, sizeof(Symbol), this);
00316     #endif
00317 
00318     char *up_name = upcase_ch(name);
00319 
00320     _sym_class = cl;
00321     _overflow = 0;
00322     _last_renaming_append = -1;
00323 
00324     rename(up_name);
00325 
00326     delete [] up_name;
00327 }
00328 
00329 inline BinRep * 
00330 Symbol::overflow_ref() const
00331 {
00332     return _overflow;
00333 }
00334 
00335 inline SYMBOL_CLASS 
00336 Symbol::sym_class() const
00337 {
00338     return _sym_class;
00339 }
00340 
00341 inline const char *
00342 Symbol::name_ref() const 
00343 {
00344     return (const char *)_tag;
00345 }
00346 
00347 inline void 
00348 Symbol::name(const char *new_name)
00349 {
00350     _tag = upcase_ch(new_name);
00351 }
00352 
00353 inline int
00354 Symbol::is_pointer() const
00355 {
00356     return 0;
00357 }
00358 
00359 #endif
00360 
 © 1995-2005 University of Illinois, Urbana-Champaign. All rights reserved.  Fri Mar 25 23:06:14 2005