From 56099f083d7a68722ace316be4d288d21caabaee Mon Sep 17 00:00:00 2001 From: fche Date: Wed, 2 Mar 2005 01:28:50 +0000 Subject: * some semantic analysis 2005-03-01 Frank Ch. Eigler * parse.cxx: Implement left-associativity for several types of operators. Add some more statement types. Parse functions. Be able to print tokens. Simplify error generating functions. Save tokens in all parse tree nodes. * parse.h: Corresponding changes. * staptree.cxx: Move tree-printing functions here. Add many new functions for symbol and type resolution. * staptree.h: Corresponding changes. * semtest.cxx: New semantic analysis pass & test driver. * testsuite/sem*/*: New tests. * parsetest.cxx: Separated parse test driver. * testsuite/parse*/*: Adapt tests to parsetest driver. * Makefile.am: Build semtest. Run its tests. * Makefile.in: Regenerated. * parse.cxx, parse.h: New files: parser. --- staptree.h | 316 +++++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 202 insertions(+), 114 deletions(-) (limited to 'staptree.h') diff --git a/staptree.h b/staptree.h index f7eff830..80bffa7e 100644 --- a/staptree.h +++ b/staptree.h @@ -10,46 +10,53 @@ using namespace std; -struct source_location -{ - // source co-ordinates - string lexeme; - string source_file; - unsigned source_line; -}; +enum exp_type + { + pe_unknown, + pe_long, + pe_string, + pe_stats + }; +ostream& operator << (ostream& o, const exp_type& e); +struct token; +struct symresolution_info; +struct typeresolution_info; struct expression { - enum { pe_void, pe_unknown, pe_long, pe_string } type; - source_location loc; + exp_type type; + const token* tok; virtual void print (ostream& o) = 0; + expression (); virtual ~expression (); + virtual void resolve_symbols (symresolution_info& r) = 0; + virtual void resolve_types (typeresolution_info& r, exp_type t) = 0; }; +ostream& operator << (ostream& o, expression& k); -inline ostream& operator << (ostream& o, expression& k) -{ - k.print (o); - return o; -} struct literal: public expression { + void resolve_symbols (symresolution_info& r); + void resolve_types (typeresolution_info& r, exp_type t); }; + struct literal_string: public literal { string value; - literal_string (const string& v): value (v) {} - void print (ostream& o) { o << '"' << value << '"'; } + literal_string (const string& v); + void print (ostream& o); }; + struct literal_number: public literal { long value; - literal_number (long v): value(v) {} - void print (ostream& o) { o << value; } + literal_number (long v); + void print (ostream& o); }; @@ -58,49 +65,58 @@ struct binary_expression: public expression expression* left; string op; expression* right; - void print (ostream& o) { o << '(' << *left << ")" - << op - << '(' << *right << ")"; } + void print (ostream& o); + void resolve_symbols (symresolution_info& r); + void resolve_types (typeresolution_info& r, exp_type t); }; + struct unary_expression: public expression { string op; expression* operand; - void print (ostream& o) { o << op << '(' << *operand << ")"; } + void print (ostream& o); + void resolve_symbols (symresolution_info& r); + void resolve_types (typeresolution_info& r, exp_type t); }; + struct pre_crement: public unary_expression { }; + struct post_crement: public unary_expression { - void print (ostream& o) { o << '(' << *operand << ")" << op; } - - + void print (ostream& o); }; + struct logical_or_expr: public binary_expression { }; + struct logical_and_expr: public binary_expression { }; + struct array_in: public binary_expression { }; + struct comparison: public binary_expression { }; + struct concatenation: public binary_expression { }; + struct exponentiation: public binary_expression { }; @@ -111,70 +127,160 @@ struct ternary_expression: public expression expression* cond; expression* truevalue; expression* falsevalue; - void print (ostream& o) { o << "(" << *cond << ") ? (" - << *truevalue << ") : (" - << *falsevalue << ")"; } + void print (ostream& o); + void resolve_symbols (symresolution_info& r); + void resolve_types (typeresolution_info& r, exp_type t); +}; + + +struct assignment: public binary_expression +{ }; +class vardecl; struct symbol: public expression { string name; - void print (ostream& o) { o << name; } + vardecl *referent; + symbol (); + void print (ostream& o); + void resolve_symbols (symresolution_info& r); + void resolve_types (typeresolution_info& r, exp_type t); }; -struct arrayindex: public symbol + +struct arrayindex: public expression { + string base; vector indexes; - void print (ostream& o) - { - symbol::print(o); - o << "["; - for (unsigned i=0; i0 ? ", " : "") << *indexes[i]; - o << "]"; - } + vardecl *referent; + arrayindex (); + void print (ostream& o); + void resolve_symbols (symresolution_info& r); + void resolve_types (typeresolution_info& r, exp_type t); }; -struct functioncall: public symbol + + +class functiondecl; +struct functioncall: public expression { + string function; vector args; - void print (ostream& o) - { - symbol::print(o); - o << "("; - for (unsigned i=0; i0 ? ", " : "") << *args[i]; - o << ")"; - } + functiondecl *referent; + functioncall (); + void print (ostream& o); + void resolve_symbols (symresolution_info& r); + void resolve_types (typeresolution_info& r, exp_type t); }; +// ------------------------------------------------------------------------ + + +struct stapfile; +struct symboldecl; +struct symresolution_info +{ + vector& locals; // includes incoming function parameters + vector& globals; + vector& files; + stapfile* current_file; + functiondecl* current_function; + + symresolution_info (vector& l, + vector& g, + vector& f, + stapfile* cfil, + functiondecl* cfun); + symresolution_info (vector& l, + vector& g, + vector& f, + stapfile* cfil); + + vardecl* find (const string& name); + + void unresolved (const token* tok); + unsigned num_unresolved; +}; + + +struct typeresolution_info +{ + unsigned num_newly_resolved; + unsigned num_still_unresolved; + bool assert_resolvability; + functiondecl* current_function; + + void mismatch (const token* tok, exp_type t1, + exp_type t2); + void unresolved (const token* tok); + void resolved (const token* tok, exp_type t); + void invalid (const token* tok, exp_type t); +}; + + +struct symboldecl // unique object per (possibly implicit) + // symbol declaration +{ + const token* tok; + string name; + exp_type type; + symboldecl (); + virtual ~symboldecl (); + virtual void print (ostream &o) = 0; + virtual void printsig (ostream &o) = 0; +}; + + +ostream& operator << (ostream& o, symboldecl& k); + + +struct vardecl: public symboldecl +{ + void print (ostream& o); + void printsig (ostream& o); + vardecl (); + vardecl (unsigned arity); + vector index_types; // for arrays only +}; + + +struct block; +struct functiondecl: public symboldecl +{ + vector formal_args; + vector locals; + block* body; + functiondecl (); + void print (ostream& o); + void printsig (ostream& o); +}; + + +// ------------------------------------------------------------------------ + + struct statement { - source_location loc; virtual void print (ostream& o) = 0; + const token* tok; + statement (); virtual ~statement (); + virtual void resolve_symbols (symresolution_info& r) = 0; + virtual void resolve_types (typeresolution_info& r) = 0; }; - -inline ostream& operator << (ostream& o, statement& k) -{ - k.print (o); - return o; -} +ostream& operator << (ostream& o, statement& k); struct block: public statement { vector statements; - void print (ostream& o) - { - o << "{" << endl; - for (unsigned i=0; i" << endl; } + void print (ostream& o); + void resolve_symbols (symresolution_info& r); + void resolve_types (typeresolution_info& r); }; + struct null_statement: public statement { - void print (ostream& o) - { o << ";"; } - + void print (ostream& o); + void resolve_symbols (symresolution_info& r) {} + void resolve_types (typeresolution_info& r) {} }; -struct assignment: public expression -{ - expression* lvalue; // XXX: consider type for lvalues; see parse_variable () - string op; - expression* rvalue; - - void print (ostream& o) - { o << *lvalue << " " << op << " " << *rvalue; } -}; struct expr_statement: public statement { expression* value; // executed for side-effects - void print (ostream& o) - { o << *value; } + void print (ostream& o); + void resolve_symbols (symresolution_info& r); + void resolve_types (typeresolution_info& r); }; + struct if_statement: public statement { expression* condition; statement* thenblock; statement* elseblock; - void print (ostream& o) - { o << "if (" << *condition << ") " << endl - << *thenblock << endl; - if (elseblock) - o << "else " << *elseblock << endl; } + void print (ostream& o); + void resolve_symbols (symresolution_info& r); + void resolve_types (typeresolution_info& r); +}; + + +struct return_statement: public expr_statement +{ + void print (ostream& o); + void resolve_types (typeresolution_info& r); +}; + + +struct delete_statement: public expr_statement +{ + void print (ostream& o); }; -struct probe; +struct probe; struct stapfile { string name; vector probes; - vector globals; - + vector functions; + vector globals; void print (ostream& o); }; @@ -238,41 +350,17 @@ struct stapfile struct probe_point_spec // inherit from something or other? { string functor; + const token* tok; literal* arg; - - void print (ostream& o) - { o << functor; - if (arg) - o << "(" << *arg << ")"; - } + void print (ostream& o); }; struct probe { - // map locals; vector location; + const token* tok; block* body; - - void print (ostream& o) - { o << "probe " << endl; - for(unsigned i=0; i0 ? ":" : ""); - location[i]->print (o); - } - o << endl; - o << *body; - } + vector locals; + void print (ostream& o); }; - - - -inline void stapfile::print (ostream& o) -{ o << "# file " << name << endl; - for(unsigned i=0; iprint (o); - o << endl; - } - } -- cgit