Annotation of parser3/src/types/pa_vmethod_frame.h, revision 1.31
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.31 ! paf 11: static const char* IDENT_VMETHOD_FRAME_H="$Date: 2002/08/07 13:24:31 $";
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.25 paf 35: Value *get_element(const String& fname) {
36: if(junction.method->max_numbered_params_count==0) {
37: Value *result=static_cast<Value *>(my.get(fname));
1.1 paf 38: if(result)
39: return result;
40: }
1.25 paf 41: return fself->get_element(fname);
1.1 paf 42: }
1.4 paf 43: /// VMethodFrame: my or self_transparent
1.25 paf 44: void put_element(const String& fname, Value *value){
45: if(!(junction.method->max_numbered_params_count==0 && my.put_replace(fname, value)))
46: fself->put_element(fname, value);
1.1 paf 47: }
48:
1.4 paf 49: /// VMethodFrame: self_transparent
1.1 paf 50: VStateless_class* get_class() { return fself->get_class(); }
51:
1.30 paf 52: public: // WContext
1.1 paf 53:
1.24 paf 54: StringOrValue result() {
1.1 paf 55: // check the $result value
1.24 paf 56: Value *result_value=get_result_variable();
1.1 paf 57: // if we have one, return it, else return as usual: accumulated fstring or fvalue
1.24 paf 58: return result_value ? StringOrValue(0, result_value) : WContext::result();
1.1 paf 59: }
60:
61: public: // usage
62:
63: VMethodFrame(Pool& apool,
1.25 paf 64: const String& aname,
1.13 parser 65: const Junction& ajunction/*info: always method-junction*/) :
1.14 parser 66: WContext(apool, 0 /* empty */),
1.1 paf 67:
1.25 paf 68: fname(aname),
1.1 paf 69: junction(ajunction),
70: store_param_index(0),
1.25 paf 71:
72: my(apool),
73: fnumbered_params(apool, aname),
74:
1.15 parser 75: fself(0),
76: fresult_initial_void(0) {
1.1 paf 77:
1.25 paf 78: if(has_my()) { // this method uses named params?
79: const Method &method=*junction.method;
1.1 paf 80: if(method.locals_names) { // are there any local var names?
81: // remember them
1.25 paf 82: // those are flags that fname is local == to be looked up in 'my'
1.1 paf 83: for(int i=0; i<method.locals_names->size(); i++) {
1.25 paf 84: // speedup: not checking for clash with "result" fname
1.12 parser 85: Value *value=NEW VVoid(pool());
1.25 paf 86: const String& fname=*method.locals_names->get_string(i);
87: set_my_variable(fname, value);
1.1 paf 88: }
89: }
90: { // always there is one local: $result
1.15 parser 91: fresult_initial_void=NEW VVoid(pool());
1.18 paf 92: set_my_variable(*result_var_name, fresult_initial_void);
1.1 paf 93: }
94: }
95: }
96:
1.25 paf 97: const String& name() { return fname; }
98:
1.1 paf 99: void set_self(Value& aself) { fself=&aself; }
100: Value *self() { return fself; }
101:
1.27 paf 102: bool can_store_param() {
103: const Method& method=*junction.method;
104: return method.params_names && store_param_index<method.params_names->size();
105: }
1.26 paf 106: void store_param(Value *value) {
1.1 paf 107: const Method& method=*junction.method;
108: int max_params=
109: method.max_numbered_params_count?method.max_numbered_params_count:
110: method.params_names?method.params_names->size():
111: 0;
112: if(store_param_index==max_params)
1.23 paf 113: throw Exception("parser.runtime",
1.26 paf 114: &name(),
1.7 paf 115: "method of %s (%s) accepts maximum %d parameter(s)",
1.25 paf 116: junction.self.get_class()->name_cstr(),
1.1 paf 117: junction.self.type(),
118: max_params);
119:
120: if(method.max_numbered_params_count) { // are this method params numbered?
1.25 paf 121: fnumbered_params+=value;
1.1 paf 122: } else { // named param
1.25 paf 123: // speedup: not checking for clash with "result" fname
124: const String& fname=*method.params_names->get_string(store_param_index);
125: set_my_variable(fname, value);
1.1 paf 126: }
127: store_param_index++;
128: }
129: void fill_unspecified_params() {
130: const Method &method=*junction.method;
131: if(method.params_names) // there are any named parameters might need filling?
132: for(; store_param_index<method.params_names->size(); store_param_index++) {
1.25 paf 133: const String& fname=*method.params_names->get_string(store_param_index);
134: my.put(fname, NEW VVoid(pool()));
1.1 paf 135: }
136: }
137:
1.25 paf 138: MethodParams *numbered_params() { return &fnumbered_params; }
1.18 paf 139:
140: private:
141:
1.25 paf 142: bool has_my() {
143: return junction.method->max_numbered_params_count==0;
144: }
145:
146: void set_my_variable(const String& fname, Value *value) {
147: my.put(fname, value); // remember param
1.20 paf 148: }
149:
150: Value *get_result_variable() {
1.25 paf 151: Value *result=has_my()?static_cast<Value*>(my.get(*result_var_name)):0;
1.20 paf 152: return result && result!=fresult_initial_void ? result : 0;
1.18 paf 153: }
1.1 paf 154:
155: public:
156:
157: const Junction& junction;
158:
159: private:
1.25 paf 160: const String& fname;
161:
1.1 paf 162: int store_param_index;
1.25 paf 163: Hash my;/*OR*/MethodParams fnumbered_params;
1.1 paf 164: Value *fself;
1.15 parser 165:
166: private:
167: Value *fresult_initial_void;
1.1 paf 168:
169: };
170:
171: #endif
E-mail: