pt-id.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 1996-2012 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #if !defined (octave_tree_identifier_h)
00024 #define octave_tree_identifier_h 1
00025 
00026 #include <iosfwd>
00027 #include <string>
00028 
00029 class octave_value;
00030 class octave_value_list;
00031 class octave_function;
00032 
00033 class tree_walker;
00034 
00035 #include "pt-bp.h"
00036 #include "pt-exp.h"
00037 #include "symtab.h"
00038 
00039 // Symbols from the symbol table.
00040 
00041 class
00042 tree_identifier : public tree_expression
00043 {
00044   friend class tree_index_expression;
00045 
00046 public:
00047 
00048   tree_identifier (int l = -1, int c = -1)
00049     : tree_expression (l, c), sym (), scope (-1) { }
00050 
00051   tree_identifier (const symbol_table::symbol_record& s,
00052                    int l = -1, int c = -1,
00053                    symbol_table::scope_id sc = symbol_table::current_scope ())
00054     : tree_expression (l, c), sym (s), scope (sc) { }
00055 
00056   ~tree_identifier (void) { }
00057 
00058   bool has_magic_end (void) const { return (name () == "__end__"); }
00059 
00060   bool is_identifier (void) const { return true; }
00061 
00062   // The name doesn't change with scope, so use sym instead of
00063   // accessing it through sym so that this function may remain const.
00064   std::string name (void) const { return sym.name (); }
00065 
00066   bool is_defined (void) { return xsym().is_defined (); }
00067 
00068   virtual bool is_variable (void) { return xsym().is_variable (); }
00069 
00070   virtual bool is_black_hole (void) { return false; }
00071 
00072   // Try to find a definition for an identifier.  Here's how:
00073   //
00074   //   * If the identifier is already defined and is a function defined
00075   //     in an function file that has been modified since the last time
00076   //     we parsed it, parse it again.
00077   //
00078   //   * If the identifier is not defined, try to find a builtin
00079   //     variable or an already compiled function with the same name.
00080   //
00081   //   * If the identifier is still undefined, try looking for an
00082   //     function file to parse.
00083   //
00084   //   * On systems that support dynamic linking, we prefer .oct files,
00085   //     then .mex files, then .m files.
00086 
00087   octave_value
00088   do_lookup (const octave_value_list& args = octave_value_list ())
00089   {
00090     return xsym().find (args);
00091   }
00092 
00093   void mark_global (void) { xsym().mark_global (); }
00094 
00095   void mark_as_static (void) { xsym().init_persistent (); }
00096 
00097   void mark_as_formal_parameter (void) { xsym().mark_formal (); }
00098 
00099   // We really need to know whether this symbol referst to a variable
00100   // or a function, but we may not know that yet.
00101 
00102   bool lvalue_ok (void) const { return true; }
00103 
00104   octave_value rvalue1 (int nargout = 1);
00105 
00106   octave_value_list rvalue (int nargout);
00107 
00108   octave_lvalue lvalue (void);
00109 
00110   void eval_undefined_error (void);
00111 
00112   tree_identifier *dup (symbol_table::scope_id scope,
00113                         symbol_table::context_id context) const;
00114 
00115   void accept (tree_walker& tw);
00116 
00117 private:
00118 
00119   // The symbol record that this identifier references.
00120   symbol_table::symbol_record sym;
00121 
00122   symbol_table::scope_id scope;
00123 
00124   // A script may be executed in multiple scopes.  If the last one was
00125   // different from the one we are in now, update sym to be from the
00126   // new scope.
00127   symbol_table::symbol_record& xsym (void)
00128   {
00129     symbol_table::scope_id curr_scope = symbol_table::current_scope ();
00130 
00131     if (scope != curr_scope)
00132       {
00133         scope = curr_scope;
00134         sym = symbol_table::insert (sym.name ());
00135       }
00136 
00137     return sym;
00138   }
00139 
00140   // No copying!
00141 
00142   tree_identifier (const tree_identifier&);
00143 
00144   tree_identifier& operator = (const tree_identifier&);
00145 };
00146 
00147 class tree_black_hole : public tree_identifier
00148 {
00149 public:
00150 
00151   tree_black_hole (int l = -1, int c = -1)
00152     : tree_identifier (l, c) { }
00153 
00154   std::string name (void) const { return "~"; }
00155 
00156   bool is_variable (void) { return false; }
00157 
00158   bool is_black_hole (void) { return true; }
00159 
00160   tree_black_hole *dup (void) const
00161     { return new tree_black_hole; }
00162 
00163   octave_lvalue lvalue (void)
00164     {
00165       return octave_lvalue (0); // black hole lvalue
00166     }
00167 };
00168 
00169 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines