Annotation of parser3/src/types/pa_vmethod_frame.h, revision 1.32
1.3 paf 1: /** @file
1.5 paf 2: Parser: @b method_frame write context
1.3 paf 3:
1.21 paf 4: Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)
1.22 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1 paf 6: */
7:
8: #ifndef PA_VMETHOD_FRAME_H
9: #define PA_VMETHOD_FRAME_H
1.28 paf 10:
1.32 ! paf 11: static const char* IDENT_VMETHOD_FRAME_H="$Date: 2002/08/12 10:32:53 $";
1.1 paf 12:
13: #include "pa_wcontext.h"
1.12 parser 14: #include "pa_vvoid.h"
1.1 paf 15: #include "pa_vjunction.h"
16:
1.4 paf 17: /** Method frame write context
18: accepts values written by method code
19: also handles method parameters and local variables
1.3 paf 20: */
1.1 paf 21: class VMethodFrame : public WContext {
22: public: // Value
23:
24: const char *type() const { return "method_frame"; }
1.20 paf 25:
26: /// VMethodFrame: $result | parent get_string(=accumulated fstring)
27: const String *get_string() {
28: // check the $result value
29: Value *result=get_result_variable();
30: // if we have one, return it's string value, else return as usual: accumulated fstring or fvalue
31: return result ? result->get_string() : WContext::get_string();
32: }
33:
1.4 paf 34: /// VMethodFrame: my or self_transparent
1.32 ! paf 35: Value *get_element(const String& aname, Value *aself) {
1.25 paf 36: if(junction.method->max_numbered_params_count==0) {
1.32 ! paf 37: if(Value *result=static_cast<Value *>(my.get(aname)))
1.1 paf 38: return result;
39: }
1.32 ! paf 40: return fself->get_element(aname, aself);
1.1 paf 41: }
1.4 paf 42: /// VMethodFrame: my or self_transparent
1.32 ! paf 43: /*override*/ bool put_element(const String& aname, Value *avalue, bool replace) {
! 44: if(junction.method->max_numbered_params_count==0 && my.put_replace(aname, avalue))
! 45: return true;
! 46:
! 47: return fself->put_element(aname, avalue, replace);
1.1 paf 48: }
49:
1.4 paf 50: /// VMethodFrame: self_transparent
1.1 paf 51: VStateless_class* get_class() { return fself->get_class(); }
52:
1.30 paf 53: public: // WContext
1.1 paf 54:
1.24 paf 55: StringOrValue result() {
1.1 paf 56: // check the $result value
1.24 paf 57: Value *result_value=get_result_variable();
1.1 paf 58: // if we have one, return it, else return as usual: accumulated fstring or fvalue
1.24 paf 59: return result_value ? StringOrValue(0, result_value) : WContext::result();
1.1 paf 60: }
61:
62: public: // usage
63:
64: VMethodFrame(Pool& apool,
1.25 paf 65: const String& aname,
1.13 parser 66: const Junction& ajunction/*info: always method-junction*/) :
1.14 parser 67: WContext(apool, 0 /* empty */),
1.1 paf 68:
1.25 paf 69: fname(aname),
1.1 paf 70: junction(ajunction),
71: store_param_index(0),
1.25 paf 72:
73: my(apool),
74: fnumbered_params(apool, aname),
75:
1.15 parser 76: fself(0),
77: fresult_initial_void(0) {
1.1 paf 78:
1.25 paf 79: if(has_my()) { // this method uses named params?
80: const Method &method=*junction.method;
1.1 paf 81: if(method.locals_names) { // are there any local var names?
82: // remember them
1.25 paf 83: // those are flags that fname is local == to be looked up in 'my'
1.1 paf 84: for(int i=0; i<method.locals_names->size(); i++) {
1.25 paf 85: // speedup: not checking for clash with "result" fname
1.12 parser 86: Value *value=NEW VVoid(pool());
1.25 paf 87: const String& fname=*method.locals_names->get_string(i);
88: set_my_variable(fname, value);
1.1 paf 89: }
90: }
91: { // always there is one local: $result
1.15 parser 92: fresult_initial_void=NEW VVoid(pool());
1.18 paf 93: set_my_variable(*result_var_name, fresult_initial_void);
1.1 paf 94: }
95: }
96: }
97:
1.25 paf 98: const String& name() { return fname; }
99:
1.1 paf 100: void set_self(Value& aself) { fself=&aself; }
101: Value *self() { return fself; }
102:
1.27 paf 103: bool can_store_param() {
104: const Method& method=*junction.method;
105: return method.params_names && store_param_index<method.params_names->size();
106: }
1.26 paf 107: void store_param(Value *value) {
1.1 paf 108: const Method& method=*junction.method;
109: int max_params=
110: method.max_numbered_params_count?method.max_numbered_params_count:
111: method.params_names?method.params_names->size():
112: 0;
113: if(store_param_index==max_params)
1.23 paf 114: throw Exception("parser.runtime",
1.26 paf 115: &name(),
1.7 paf 116: "method of %s (%s) accepts maximum %d parameter(s)",
1.25 paf 117: junction.self.get_class()->name_cstr(),
1.1 paf 118: junction.self.type(),
119: max_params);
120:
121: if(method.max_numbered_params_count) { // are this method params numbered?
1.25 paf 122: fnumbered_params+=value;
1.1 paf 123: } else { // named param
1.25 paf 124: // speedup: not checking for clash with "result" fname
125: const String& fname=*method.params_names->get_string(store_param_index);
126: set_my_variable(fname, value);
1.1 paf 127: }
128: store_param_index++;
129: }
130: void fill_unspecified_params() {
131: const Method &method=*junction.method;
132: if(method.params_names) // there are any named parameters might need filling?
133: for(; store_param_index<method.params_names->size(); store_param_index++) {
1.25 paf 134: const String& fname=*method.params_names->get_string(store_param_index);
135: my.put(fname, NEW VVoid(pool()));
1.1 paf 136: }
137: }
138:
1.25 paf 139: MethodParams *numbered_params() { return &fnumbered_params; }
1.18 paf 140:
141: private:
142:
1.25 paf 143: bool has_my() {
144: return junction.method->max_numbered_params_count==0;
145: }
146:
147: void set_my_variable(const String& fname, Value *value) {
148: my.put(fname, value); // remember param
1.20 paf 149: }
150:
151: Value *get_result_variable() {
1.25 paf 152: Value *result=has_my()?static_cast<Value*>(my.get(*result_var_name)):0;
1.20 paf 153: return result && result!=fresult_initial_void ? result : 0;
1.18 paf 154: }
1.1 paf 155:
156: public:
157:
158: const Junction& junction;
159:
160: private:
1.25 paf 161: const String& fname;
162:
1.1 paf 163: int store_param_index;
1.25 paf 164: Hash my;/*OR*/MethodParams fnumbered_params;
1.1 paf 165: Value *fself;
1.15 parser 166:
167: private:
168: Value *fresult_initial_void;
1.1 paf 169:
170: };
171:
172: #endif
E-mail: