00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef MORE_LANG_CX_EXPR_H
00033 #define MORE_LANG_CX_EXPR_H
00034
00035 #include <more/lang/ct_type.h>
00036 #include <more/lang/ct_proto.h>
00037 #include <list>
00038 #include <utility>
00039 #include <stdarg.h>
00040
00041 namespace more {
00042 namespace lang {
00043
00044
00045
00046
00047
00048
00049
00050
00051 struct cx_expr : gc
00052 {
00053 virtual ~cx_expr();
00054
00055
00056
00057
00058
00059 virtual void print_as_expr(std::ostream& os) const;
00060
00061
00062
00063
00064 virtual void print_as_stmt(std::ostream& os) const;
00065
00066
00067 virtual void append_ctors_to(cx_block* blk) = 0;
00068
00069
00070
00071
00072 virtual void append_dtors_to(cx_block* blk) = 0;
00073
00074 protected:
00075 explicit cx_expr(ct_type const* t) : m_rtt(t) {}
00076
00077 public:
00078
00079 ct_type const* type_of() const { return m_rtt; }
00080
00081
00082 void set_type(ct_type const* rtt) { m_rtt = rtt; }
00083
00084 private:
00085 ct_type const* m_rtt;
00086 };
00087
00088
00089
00090
00091
00092
00093 struct cx_block : cx_expr
00094 {
00095 cx_block()
00096 : cx_expr(ct_type_of<void>()) {}
00097
00098
00099 cx_identifier* declare(ct_type const* rtt);
00100
00101
00102
00103
00104 cx_identifier* append_definition(cx_expr* e);
00105
00106
00107 void append_stmt(cx_expr* e);
00108
00109
00110
00111
00112 void prepend_stmt(cx_expr* e);
00113
00114
00115 void append_decls_from(cx_block const* blk);
00116
00117
00118 void append_stmts_from(cx_block const* blk);
00119
00120
00121 bool empty() const { return m_vars.empty() && m_stmts.empty(); }
00122
00123 virtual void print_as_expr(std::ostream& os) const;
00124 virtual void print_as_stmt(std::ostream& os) const;
00125 void print_as_stmt_seq(std::ostream& os) const;
00126 virtual void append_ctors_to(cx_block* blk);
00127 virtual void append_dtors_to(cx_block* blk);
00128
00129 private:
00130 #ifdef MORE_LANG_USE_GC
00131 typedef std::list< cx_identifier*, gen::gc_allocator<cx_identifier*> >
00132 var_container;
00133 typedef std::list< cx_expr*, gen::gc_allocator<cx_expr*> >
00134 stmt_container;
00135 #else
00136 typedef std::list<cx_identifier*> var_container;
00137 typedef std::list<cx_expr*> stmt_container;
00138 #endif
00139 var_container m_vars;
00140 stmt_container m_stmts;
00141 };
00142
00143 inline cx_block*
00144 make_block()
00145 {
00146 return new(UseGC) cx_block();
00147 }
00148
00149
00150
00151
00152
00153
00154
00155 struct cx_expr_with_ctor_dtor : cx_expr
00156 {
00157 cx_expr_with_ctor_dtor(ct_type const* t)
00158 : cx_expr(t), m_ctor_dtor(0) {}
00159
00160 virtual void append_ctors_to(cx_block* blk);
00161 virtual void append_dtors_to(cx_block* blk);
00162
00163 cx_identifier* ctor_declare(ct_type const* t);
00164 void ctor_append_stmt(cx_expr* e);
00165 void dtor_append_stmt(cx_expr* e);
00166
00167 private:
00168 struct ctor_dtor_type {
00169 cx_block m_ctors;
00170 cx_block m_dtors;
00171 };
00172 ctor_dtor_type* m_ctor_dtor;
00173 };
00174
00175
00176
00177
00178
00179
00180 struct cx_identifier : cx_expr_with_ctor_dtor
00181 {
00182
00183 explicit cx_identifier(ct_type const* r)
00184 : cx_expr_with_ctor_dtor(r) {}
00185
00186
00187 cx_identifier(ct_type const* r, identifier id)
00188 : cx_expr_with_ctor_dtor(r),
00189 m_id(id) {}
00190
00191 virtual ~cx_identifier();
00192
00193 identifier name() const { return m_id; }
00194
00195 void set_name(identifier idr) { m_id = idr; }
00196
00197 virtual void print_as_expr(std::ostream& os) const;
00198
00199 private:
00200 identifier m_id;
00201 };
00202
00203
00204
00205
00206
00207
00208
00209 template <typename T>
00210 struct cx_literal : cx_expr
00211 {
00212 explicit cx_literal(T const& value)
00213 : cx_expr(ct_type_of<T>()),
00214 m_value(value) {}
00215
00216 virtual void print_as_expr(std::ostream& os) const;
00217 virtual void append_ctors_to(cx_block* blk);
00218 virtual void append_dtors_to(cx_block* blk);
00219
00220 private:
00221 T m_value;
00222 };
00223
00224 template cx_literal<char>;
00225 template cx_literal<int>;
00226 template cx_literal<unsigned int>;
00227 template cx_literal<long>;
00228 template cx_literal<unsigned long>;
00229 #ifdef CXX_HAVE_LONG_LONG
00230 template cx_literal<long long>;
00231 template cx_literal<unsigned long long>;
00232 #endif
00233 template cx_literal<long double>;
00234 template cx_literal<char const*>;
00235
00236 inline cx_expr* make_literal(char x)
00237 { return new(UseGC) cx_literal<char>(x); }
00238 inline cx_expr* make_literal(int x)
00239 { return new(UseGC) cx_literal<int>(x); }
00240 inline cx_expr* make_literal(unsigned int x)
00241 { return new(UseGC) cx_literal<unsigned int>(x); }
00242 inline cx_expr* make_literal(long x)
00243 { return new(UseGC) cx_literal<long>(x); }
00244 inline cx_expr* make_literal(unsigned long x)
00245 { return new(UseGC) cx_literal<unsigned long>(x); }
00246 #ifdef CXX_HAVE_LONG_LONG
00247 inline cx_expr* make_literal(long long x)
00248 { return new(UseGC) cx_literal<long long>(x); }
00249 inline cx_expr* make_literal(unsigned long long x)
00250 { return new(UseGC) cx_literal<unsigned long long>(x); }
00251 #endif
00252 inline cx_expr* make_literal(long double x)
00253 { return new(UseGC) cx_literal<long double>(x); }
00254 inline cx_expr* make_literal(char const* x)
00255 { return new(UseGC) cx_literal<char const*>(x); }
00256
00257
00258
00259
00260
00261
00262 struct cx_call : cx_expr
00263 {
00264
00265
00266
00267 cx_call(cx_expr* fn, va_list va);
00268
00269
00270
00271
00272 cx_call(cx_expr* fn, cx_expr** args);
00273
00274 virtual void print_as_expr(std::ostream& os) const;
00275 virtual void append_ctors_to(cx_block* blk);
00276 virtual void append_dtors_to(cx_block* blk);
00277
00278 private:
00279 cx_expr* m_fn;
00280 cx_expr** m_args;
00281 };
00282
00283
00284
00285
00286 cx_call* make_call(cx_expr* fn, ...);
00287
00288
00289
00290
00291
00292 struct cx_switch : cx_block
00293 {
00294 explicit cx_switch(cx_expr* cond)
00295 : m_cond(cond) {}
00296
00297 virtual void print_as_stmt(std::ostream& os) const;
00298 virtual void append_ctors_to(cx_block* blk);
00299 virtual void append_dtors_to(cx_block* blk);
00300
00301 private:
00302 cx_expr* m_cond;
00303 };
00304
00305
00306
00307
00308
00309 struct cx_lambda : cx_expr
00310 {
00311
00312 typedef cx_identifier const* const* arg_const_iterator;
00313 typedef cx_identifier** arg_iterator;
00314
00315
00316 cx_lambda(ct_proto const* proto, identifier self);
00317
00318
00319 cx_lambda(ct_proto const* proto);
00320
00321
00322 ct_proto const* type_of() const
00323 {
00324 return static_cast<ct_proto const*>(cx_expr::type_of());
00325 }
00326
00327
00328 cx_identifier* arg(size_t i);
00329
00330
00331 arg_const_iterator arg_begin() const { return m_args; }
00332 arg_iterator arg_begin() { return m_args; }
00333
00334
00335 arg_const_iterator arg_end() const { return m_args+arg_count(); }
00336 arg_iterator arg_end() { return m_args + arg_count(); }
00337
00338
00339 size_t arg_count() const
00340 {
00341 return static_cast<ct_proto const*>(type_of())->arg_count();
00342 }
00343
00344
00345
00346 cx_block* body() { return &m_body; }
00347
00348
00349 cx_block const* body() const { return &m_body; }
00350
00351 virtual void append_ctors_to(cx_block* blk);
00352 virtual void append_dtors_to(cx_block* blk);
00353 virtual void print_as_expr(std::ostream& os) const;
00354
00355 private:
00356 cx_identifier** m_args;
00357 cx_block m_body;
00358 unsigned int m_inline_p : 1;
00359 };
00360
00361
00362
00363
00364
00365 struct cx_cond : cx_expr
00366 {
00367 cx_cond() : cx_expr(ct_type_of<void>()), m_counter_conseq(0) {}
00368
00369
00370
00371
00372
00373
00374 void append(cx_expr* condition, cx_expr* consequence);
00375
00376 virtual void print_as_expr(std::ostream& os) const;
00377 virtual void print_as_stmt(std::ostream& os) const;
00378 virtual void append_ctors_to(cx_block* blk);
00379 virtual void append_dtors_to(cx_block* blk);
00380
00381 private:
00382 struct cond_conseq
00383 {
00384 cx_expr* m_cond;
00385 cx_expr* m_conseq;
00386 cx_block* m_cond_ctors;
00387 cx_block* m_cond_dtors;
00388 };
00389 #ifdef MORE_LANG_USE_GC
00390 typedef std::list<cond_conseq, gen::gc_allocator<cond_conseq> >
00391 case_container;
00392 #else
00393 typedef std::list<cond_conseq> case_container;
00394 #endif
00395
00396 mutable case_container m_cases;
00397 cx_expr* m_counter_conseq;
00398 };
00399
00400
00401
00402
00403
00404
00405
00406 struct cx_pattern : cx_expr
00407 {
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420 cx_pattern(char const* pattern, ct_type const* result_type, va_list va);
00421
00422 virtual void print_as_expr(std::ostream& os) const;
00423 virtual void append_ctors_to(cx_block* blk);
00424 virtual void append_dtors_to(cx_block* blk);
00425
00426 private:
00427 void init(va_list va);
00428
00429 char const* m_pattern;
00430 int m_arity;
00431 cx_expr** m_args;
00432 };
00433
00434
00435
00436 struct cx_and : cx_expr
00437 {
00438 cx_and(size_t n_arg, cx_expr** arg_arr);
00439 virtual void print_as_expr(std::ostream& os) const;
00440 virtual void append_ctors_to(cx_block* blk);
00441 virtual void append_dtors_to(cx_block* blk);
00442
00443 private:
00444 size_t m_n;
00445 cx_expr** m_argv;
00446 };
00447
00448
00449
00450
00451
00452 cx_expr* make_expr(char const* pattern, ct_type const*, ...);
00453
00454
00455
00456 cx_expr* make_stmt(char const* pattern, ...);
00457
00458 cx_expr* make_deref_expr(cx_expr*);
00459 cx_expr* make_element_expr(cx_expr*, cx_expr*);
00460 cx_expr* make_cast(ct_type const*, cx_expr*);
00461
00462 cx_expr* make_not(cx_expr*);
00463 cx_expr* make_equal(cx_expr*, cx_expr*);
00464 cx_expr* make_less(cx_expr*, cx_expr*);
00465 cx_expr* make_lesseq(cx_expr*, cx_expr*);
00466
00467 cx_expr* make_assignment(cx_expr* dst, cx_expr* src);
00468
00469 cx_expr* make_negation(cx_expr* x);
00470 cx_expr* make_sum(cx_expr* x, cx_expr* y);
00471 cx_expr* make_difference(cx_expr* x, cx_expr* y);
00472 cx_expr* make_product(cx_expr* x, cx_expr* y);
00473 cx_expr* make_quotient(cx_expr* x, cx_expr* y);
00474 cx_expr* make_modulus(cx_expr* x, cx_expr* y);
00475
00476 cx_expr* make_bitnot(cx_expr*);
00477 cx_expr* make_bitand(cx_expr*, cx_expr*);
00478 cx_expr* make_bitor(cx_expr*, cx_expr*);
00479 cx_expr* make_bitxor(cx_expr*, cx_expr*);
00480 cx_expr* make_bitlsh(cx_expr*, cx_expr*);
00481 cx_expr* make_bitrsh(cx_expr*, cx_expr*);
00482
00483 cx_expr* make_return(cx_expr* x);
00484 cx_expr* make_return();
00485 cx_expr* make_break();
00486 cx_expr* make_while(cx_expr* cond, cx_expr* blk);
00487 cx_expr* make_do_while(cx_expr* blk, cx_expr* cond);
00488
00489 }}
00490
00491 #endif