00001
00002
00003 #include "expr_annotator.h"
00004 #include "expr_annotator_i.h"
00005
00006
00007 #line 43 "expr_annotator.cpp"
00008
00009
00011 static bool
00012 has_type(Expr_result* l, const Type& t);
00013
00014 #line 52 "expr_annotator.cpp"
00015
00018 static Type
00019 get_merged_cv_type(Type l, Type r);
00020
00021 #line 73 "expr_annotator.cpp"
00022
00023 static Type
00024 get_composite_pointer_type(Expr_result* l, Expr_result* r);
00025
00026 #line 121 "expr_annotator.cpp"
00027
00028 static Type
00029 get_composite_mptr_type(Expr_result* l, Expr_result* r);
00030
00031 #line 157 "expr_annotator.cpp"
00032
00034 static void
00035 do_pointer_arith_conversions(Expr_result* l, Expr_result* r);
00036
00037 #line 170 "expr_annotator.cpp"
00038
00040 static void
00041 do_mptr_arith_conversions(Expr_result* l, Expr_result* r);
00042
00043 #line 183 "expr_annotator.cpp"
00044
00045 static void
00046 do_usual_conversions(Expr_result* l, Expr_result* r);
00047
00048 #line 267 "expr_annotator.cpp"
00049
00050 static Annotated<PtreeUnaryExpr>*
00051 make_this_star(Type t);
00052
00053 #line 276 "expr_annotator.cpp"
00054
00055 static Annotated<PtreeInfixExpr>*
00056 make_infix_expr(Type rt, Ptree* lhs, Ptree* op, Ptree* rhs);
00057
00058 #line 283 "expr_annotator.cpp"
00059
00060 static Annotated<PtreeUnaryExpr>*
00061 make_prefix_expr(Type rt, Ptree* op, Ptree* arg);
00062
00063 #line 290 "expr_annotator.cpp"
00064
00065
00066
00067 static void
00068 check_ptr_arith(Type t);
00069
00070 #line 817 "expr_annotator.cpp"
00071
00072
00075 static Type
00076 unary_deref_predicate(Type t);
00077
00078 #line 832 "expr_annotator.cpp"
00079
00081 static Type
00082 unary_plus_predicate(Type t);
00083
00084 #line 846 "expr_annotator.cpp"
00085
00087 static Type
00088 unary_minus_predicate(Type t);
00089
00090 #line 858 "expr_annotator.cpp"
00091
00093 static Type
00094 unary_bitnot_predicate(Type t);
00095
00096 #line 1111 "expr_annotator.cpp"
00097
00098
00102 static Type
00103 unary_delete_or_arrow_predicate(Type t);
00104
00105 #line 1330 "expr_annotator.cpp"
00106
00107
00109 static Expr_result
00110 make_sizeof_expr(Type t);
00111
00112 #line 1658 "expr_annotator.cpp"
00113
00114
00116 static Annotated<PtreeCondExpr>*
00117 make_cond_expr(Type ty, Ptree* i, Ptree* t, Ptree* e);
00118
00119 #line 1671 "expr_annotator.cpp"
00120 static Implicit_conversion*
00121 generate_ics_for_ternary(Expr_result e1, Expr_result e2);
00122
00123 #line 2618 "expr_annotator.cpp"
00124
00125
00128 static Type
00129 index_predicate(Type l, Type r);
00130
00131 #line 42 "expr_annotator.cpp"
00132
00133
00134
00136 static inline bool
00137 has_type(Expr_result* l, const Type& t)
00138 {
00139 return l->get_type().is_same_unqualified_type(t);
00140 }
00141
00142 #line 51 "expr_annotator.cpp"
00143
00147 static Type
00148 get_merged_cv_type(Type l, Type r)
00149 {
00150 if (l.get_kind() == Type::k_Pointer && r.get_kind() == Type::k_Pointer) {
00151 Type sub = get_merged_cv_type(l.get_basis_type(), r.get_basis_type());
00152 return sub.make_pointer_type().with_qualifiers(l).with_qualifiers(r);
00153 } else if (l.get_kind() == Type::k_Member && r.get_kind() == Type::k_Member) {
00154 if (!l.get_class_type().is_same_unqualified_type(r.get_class_type()))
00155 compile_error("pointer types are not similar");
00156 Type sub = get_merged_cv_type(l.get_member_type(), r.get_member_type());
00157 return l.get_class_type().with_qualifiers(r.get_class_type()).make_member_type(sub);
00158 } else {
00159 if (!l.is_same_unqualified_type(r))
00160 compile_error("pointer types are not similar");
00161 return l.with_qualifiers(r);
00162 }
00163 }
00164
00165 #line 72 "expr_annotator.cpp"
00166
00168 static Type
00169 get_composite_pointer_type(Expr_result* l, Expr_result* r)
00170 {
00171 if (l->get_type().is_same_unqualified_type(r->get_type()))
00172 return l->get_type();
00173
00174
00175
00176 if (l->is_npc())
00177 return r->get_type();
00178 if (r->is_npc())
00179 return l->get_type();
00180
00181 assert(l->get_type().get_kind() == Type::k_Pointer);
00182 assert(r->get_type().get_kind() == Type::k_Pointer);
00183
00184 Type lbase = l->get_type().get_basis_type();
00185 Type rbase = r->get_type().get_basis_type();
00186
00187
00188 if (lbase.get_kind() == Type::k_Function || rbase.get_kind() == Type::k_Function)
00189 compile_error("comparing function pointer with incompatible pointer");
00190
00191
00192 if (lbase.is_same_unqualified_type(rbase))
00193 return lbase.with_qualifiers(rbase).make_pointer_type();
00194 if (lbase.is_void())
00195 return lbase.with_qualifiers(rbase).make_pointer_type();
00196 if (rbase.is_void())
00197 return rbase.with_qualifiers(lbase).make_pointer_type();
00198
00199
00200
00201 if (lbase.is_class_type() && rbase.is_class_type()) {
00202 Class_symbol* lclass = downcast<Class_symbol*>(lbase.get_type_symbol());
00203 Class_symbol* rclass = downcast<Class_symbol*>(rbase.get_type_symbol());
00204 if (lclass->is_unique_base_class_of(rclass))
00205 return lbase.with_qualifiers(rbase).make_pointer_type();
00206 if (rclass->is_unique_base_class_of(lclass))
00207 return rbase.with_qualifiers(lbase).make_pointer_type();
00208 compile_error("comparing distinct pointer types `* " + lclass->get_name()
00209 + "' and `* " + rclass->get_name() + "'");
00210 }
00211
00212 return get_merged_cv_type(lbase, rbase).make_pointer_type();
00213 }
00214
00215 #line 120 "expr_annotator.cpp"
00216
00218 static Type
00219 get_composite_mptr_type(Expr_result* l, Expr_result* r)
00220 {
00221 if (l->get_type().is_same_unqualified_type(r->get_type()))
00222 return l->get_type();
00223
00224
00225
00226 if (l->is_npc())
00227 return r->get_type();
00228 if (r->is_npc())
00229 return l->get_type();
00230
00231 assert(l->get_type().get_kind() == Type::k_Member);
00232 assert(r->get_type().get_kind() == Type::k_Member);
00233
00234 Type lbase = l->get_type().get_class_type();
00235 Type rbase = r->get_type().get_class_type();
00236 Type lmem = l->get_type().get_member_type();
00237 Type rmem = r->get_type().get_member_type();
00238 Class_symbol* lclass = downcast<Class_symbol*>(lbase.get_type_symbol());
00239 Class_symbol* rclass = downcast<Class_symbol*>(rbase.get_type_symbol());
00240
00241
00242 Type base;
00243 if (lclass == rclass || lclass->is_base_class_of(rclass))
00244 base = rbase.with_qualifiers(lbase);
00245 else if (rclass->is_base_class_of(lclass))
00246 base = lbase.with_qualifiers(rbase);
00247 else
00248 compile_error("comparing member pointers of distinct types");
00249
00250 return base.make_member_type(get_merged_cv_type(lmem, rmem));
00251 }
00252
00253 #line 156 "expr_annotator.cpp"
00254
00257 static void
00258 do_pointer_arith_conversions(Expr_result* l, Expr_result* r)
00259 {
00260 assert(l->get_type().get_kind() == Type::k_Pointer || l->is_npc());
00261 assert(r->get_type().get_kind() == Type::k_Pointer || r->is_npc());
00262
00263 Type common = get_composite_pointer_type(l, r);
00264 l->convert_to(common);
00265 r->convert_to(common);
00266 }
00267
00268 #line 169 "expr_annotator.cpp"
00269
00272 static void
00273 do_mptr_arith_conversions(Expr_result* l, Expr_result* r)
00274 {
00275 assert(l->get_type().get_kind() == Type::k_Member || l->is_npc());
00276 assert(r->get_type().get_kind() == Type::k_Member || r->is_npc());
00277
00278 Type common = get_composite_mptr_type(l, r);
00279 l->convert_to(common);
00280 r->convert_to(common);
00281 }
00282
00283 #line 182 "expr_annotator.cpp"
00284
00286 static void
00287 do_usual_conversions(Expr_result* l, Expr_result* r)
00288 {
00289 assert(l->get_type().is_arithmetic_type() || l->get_type().is_enum_type());
00290 assert(r->get_type().is_arithmetic_type() || r->get_type().is_enum_type());
00291
00292
00293
00294 if (has_type(l, ldouble_type))
00295 r->convert_to(ldouble_type);
00296 else if (has_type(r, ldouble_type))
00297 l->convert_to(ldouble_type);
00298
00299
00300 else if (has_type(l, double_type))
00301 r->convert_to(double_type);
00302 else if (has_type(r, double_type))
00303 l->convert_to(double_type);
00304
00305
00306
00307 else if (has_type(l, float_type))
00308 r->convert_to(float_type);
00309 else if (has_type(r, float_type))
00310 l->convert_to(float_type);
00311
00312
00313 else {
00314 r->do_integral_promotions();
00315 l->do_integral_promotions();
00316
00317 if (has_type(r, ullong_type))
00318 l->convert_to(ullong_type);
00319 else if (has_type(l, ullong_type))
00320 r->convert_to(ullong_type);
00321 else if (has_type(r, llong_type))
00322 l->convert_to(llong_type);
00323 else if (has_type(l, llong_type))
00324 r->convert_to(llong_type);
00325
00326
00327 else if (has_type(r, ulong_type))
00328 l->convert_to(ulong_type);
00329 else if (has_type(l, ulong_type))
00330 r->convert_to(ulong_type);
00331
00332
00333
00334
00335
00336
00337 else if ((has_type(r, long_type) && has_type(l, uint_type))
00338 || (has_type(l, uint_type) && has_type(r, long_type))) {
00339 r->convert_to(ulong_type);
00340 l->convert_to(ulong_type);
00341 }
00342
00343
00344 else if (has_type(r, long_type))
00345 l->convert_to(long_type);
00346 else if (has_type(l, long_type))
00347 r->convert_to(long_type);
00348
00349
00350 else if (has_type(r, uint_type))
00351 l->convert_to(uint_type);
00352 else if (has_type(l, uint_type))
00353 r->convert_to(uint_type);
00354
00355
00356 else
00357 assert(has_type(r, int_type) && has_type(l, int_type));
00358 }
00359 }
00360
00361 #line 258 "expr_annotator.cpp"
00362
00363
00364
00365
00366 Expr_annotator::Expr_annotator(Abstract_scope* scope, Source* source)
00367 : Paranoid_visitor<Expr_result>(source),
00368 Ptree_visitor<Expr_result>(source), current_scope(scope)
00369 { }
00370
00371 #line 266 "expr_annotator.cpp"
00372
00374 static Annotated<PtreeUnaryExpr>*
00375 make_this_star(Type t)
00376 {
00377 return new Annotated<PtreeUnaryExpr>(t, 0,
00378 make_static_leaf("*"),
00379 Ptree::List(make_this(t)));
00380 }
00381
00382 #line 275 "expr_annotator.cpp"
00383
00385 static Annotated<PtreeInfixExpr>*
00386 make_infix_expr(Type rt, Ptree* lhs, Ptree* op, Ptree* rhs)
00387 {
00388 return new Annotated<PtreeInfixExpr>(rt, 0, lhs, Ptree::List(op, rhs));
00389 }
00390
00391 #line 282 "expr_annotator.cpp"
00392
00394 static Annotated<PtreeUnaryExpr>*
00395 make_prefix_expr(Type rt, Ptree* op, Ptree* arg)
00396 {
00397 return new Annotated<PtreeUnaryExpr>(rt, 0, op, Ptree::List(arg));
00398 }
00399
00400 #line 289 "expr_annotator.cpp"
00401
00402
00403
00404
00405 static void
00406 check_ptr_arith(Type t)
00407 {
00408 Type b = t.get_basis_type();
00409 if (!b.is_complete())
00410 compile_error("pointer arithmetic with incomplete type");
00411 if (!b.is_object_type())
00412 compile_error("arithmetic with non-object type");
00413 }
00414
00415 #line 302 "expr_annotator.cpp"
00416
00420 Expr_result
00421 Expr_annotator::visit_comma(PtreeCommaExpr* e)
00422 {
00423 expect_ptree(e->Second(), ',');
00424
00425 Expr_result lhs = visit(e->First());
00426 Expr_result rhs = visit(e->Third());
00427
00428
00429 if (!lhs.get_type().is_valid())
00430 compile_error("no type information for left operand in `,'");
00431 if (!rhs.is_value() && !rhs.is_function())
00432 compile_error("invalid right operand to `,'");
00433
00434 if (lhs.get_type().get_kind() == Type::k_Userdef || rhs.get_type().get_kind() == Type::k_Userdef) {
00435
00436 Overload_resolver resolver(true);
00437 resolver.add_arg(lhs);
00438 resolver.add_arg(rhs);
00439 if (Class_symbol* csym = dynamic_cast<Class_symbol*>(lhs.get_type().get_type_symbol())) {
00440 if (Function_symbol* fsym = dynamic_cast<Function_symbol*>(csym->get_scope()->lookup_here(Symbol_name::COMMA_OPERATOR_NAME, false).untag))
00441 resolver.add_function(fsym, false);
00442 }
00443 if (Function_symbol* fsym = dynamic_cast<Function_symbol*>(current_scope->lookup_unqualified(Symbol_name::COMMA_OPERATOR_NAME).untag))
00444 resolver.add_function(fsym, false);
00445
00446
00447 bool is_ambig;
00448 if (Overload_candidate* cand = resolver.get_best(&is_ambig)) {
00449 Annotated_funcall_maker afm(make_name(cand->fsig),
00450 cand->fsig->get_return_type());
00451 afm.add_arg(resolver.get_arg(0).get_tree());
00452 afm.add_arg(resolver.get_arg(1).get_tree());
00453 Expr_result result;
00454 result.set_value(afm.make_funcall(),
00455 cand->fsig->get_return_type());
00456 return result;
00457 }
00458 if (is_ambig)
00459 compile_error("invocation of comma operator is ambigous");
00460 }
00461
00462 rhs.set_tree(make_comma_expr(lhs.get_tree(), rhs.get_tree()));
00463 return rhs;
00464 }
00465
00466 #line 351 "expr_annotator.cpp"
00467
00469 Expr_result
00470 Expr_annotator::process_binary_operator(Ptree* oper, Expr_result l, Expr_result r)
00471 {
00472 if (oper->Eq("||")) {
00473
00474 l.convert_to_bool();
00475 r.convert_to_bool();
00476 return Expr_result(make_infix_expr(bool_type, l.get_tree(), oper, r.get_tree()),
00477 Expr_result::k_RValue);
00478 } else if (oper->Eq("&&")) {
00479
00480 l.convert_to_bool();
00481 r.convert_to_bool();
00482 return Expr_result(make_infix_expr(bool_type, l.get_tree(), oper, r.get_tree()),
00483 Expr_result::k_RValue);
00484 } else if (oper->Eq('|')) {
00485
00486 l.do_std_conversions();
00487 r.do_std_conversions();
00488 do_usual_conversions(&l, &r);
00489 if (!l.get_type().is_int() || !r.get_type().is_int())
00490 compile_error("operands to binary `|' must be integers");
00491 return Expr_result(make_infix_expr(l.get_type(), l.get_tree(), oper, r.get_tree()),
00492 Expr_result::k_RValue);
00493 } else if (oper->Eq('^')) {
00494
00495 l.do_std_conversions();
00496 r.do_std_conversions();
00497 do_usual_conversions(&l, &r);
00498 if (!l.get_type().is_int() || !r.get_type().is_int())
00499 compile_error("operands to binary `^' must be integers");
00500 return Expr_result(make_infix_expr(l.get_type(), l.get_tree(), oper, r.get_tree()),
00501 Expr_result::k_RValue);
00502 } else if (oper->Eq('&')) {
00503
00504 l.do_std_conversions();
00505 r.do_std_conversions();
00506 do_usual_conversions(&l, &r);
00507 if (!l.get_type().is_int() || !r.get_type().is_int())
00508 compile_error("operands to binary `&' must be integers");
00509 return Expr_result(make_infix_expr(l.get_type(), l.get_tree(), oper, r.get_tree()),
00510 Expr_result::k_RValue);
00511 } else if (oper->Eq("==") || oper->Eq("!=")) {
00512
00513 l.do_std_conversions();
00514 r.do_std_conversions();
00515 bool two_npc = l.is_npc() && r.is_npc();
00516 if (!two_npc
00517 && (l.get_type().get_kind() == Type::k_Pointer || l.is_npc())
00518 && (r.get_type().get_kind() == Type::k_Pointer || r.is_npc()))
00519 {
00520
00521 do_pointer_arith_conversions(&l, &r);
00522 } else if (!two_npc
00523 && (l.get_type().get_kind() == Type::k_Member || l.is_npc())
00524 && (r.get_type().get_kind() == Type::k_Member || r.is_npc()))
00525 {
00526
00527 do_mptr_arith_conversions(&l, &r);
00528 } else {
00529 do_usual_conversions(&l, &r);
00530 if (!l.get_type().is_arithmetic_type() || !r.get_type().is_arithmetic_type())
00531 compile_error("invalid operands to relational operator");
00532 }
00533 assert(l.get_type().is_same_unqualified_type(r.get_type()));
00534 return Expr_result(make_infix_expr(bool_type, l.get_tree(), oper, r.get_tree()),
00535 Expr_result::k_RValue);
00536 } else if (oper->Eq('<') || oper->Eq('>') || oper->Eq("<=") || oper->Eq(">=")) {
00537
00538 l.do_std_conversions();
00539 r.do_std_conversions();
00540 bool two_npc = l.is_npc() && r.is_npc();
00541 if (!two_npc
00542 && (l.get_type().get_kind() == Type::k_Pointer || l.is_npc())
00543 && (r.get_type().get_kind() == Type::k_Pointer || r.is_npc()))
00544 {
00545 do_pointer_arith_conversions(&l, &r);
00546
00547 } else {
00548 do_usual_conversions(&l, &r);
00549 if (!l.get_type().is_arithmetic_type() || !r.get_type().is_arithmetic_type())
00550 compile_error("invalid operands to relational operator");
00551 }
00552 assert(l.get_type().is_same_unqualified_type(r.get_type()));
00553 return Expr_result(make_infix_expr(bool_type, l.get_tree(), oper, r.get_tree()),
00554 Expr_result::k_RValue);
00555 } else if (oper->Eq("<<")) {
00556
00557 l.do_std_conversions();
00558 r.do_std_conversions();
00559 l.do_integral_promotions();
00560 r.do_integral_promotions();
00561 if (!l.get_type().is_int() || !r.get_type().is_int())
00562 compile_error("operands to binary `<<' must be integers");
00563 return Expr_result(make_infix_expr(l.get_type(), l.get_tree(), oper, r.get_tree()),
00564 Expr_result::k_RValue);
00565 } else if (oper->Eq(">>")) {
00566
00567 l.do_std_conversions();
00568 r.do_std_conversions();
00569 l.do_integral_promotions();
00570 r.do_integral_promotions();
00571 if (!l.get_type().is_int() || !r.get_type().is_int())
00572 compile_error("operands to binary `>>' must be integers");
00573 return Expr_result(make_infix_expr(l.get_type(), l.get_tree(), oper, r.get_tree()),
00574 Expr_result::k_RValue);
00575 } else if (oper->Eq('+')) {
00576
00577
00578
00579
00580 l.do_std_conversions();
00581 r.do_std_conversions();
00582 l.do_integral_promotions();
00583 r.do_integral_promotions();
00584 if (l.get_type().get_kind() == Type::k_Pointer && r.get_type().is_int()) {
00585
00586 check_ptr_arith(l.get_type());
00587 return Expr_result(make_infix_expr(l.get_type(), l.get_tree(), oper, r.get_tree()),
00588 Expr_result::k_RValue);
00589 } else if (r.get_type().get_kind() == Type::k_Pointer && l.get_type().is_int()) {
00590
00591 check_ptr_arith(r.get_type());
00592 compile_warning("swapping sides of pointer addition", oper);
00593 return Expr_result(make_infix_expr(r.get_type(), r.get_tree(), oper, l.get_tree()),
00594 Expr_result::k_RValue);
00595 } else if (r.get_type().is_arithmetic_type() && l.get_type().is_arithmetic_type()) {
00596
00597 do_usual_conversions(&l, &r);
00598 assert((l.get_type().is_int() && r.get_type().is_int())
00599 || (l.get_type().is_float() && r.get_type().is_float()));
00600 return Expr_result(make_infix_expr(l.get_type(), l.get_tree(), oper, r.get_tree()),
00601 Expr_result::k_RValue);
00602 } else
00603 compile_error("invalid arguments to binary `+'");
00604 } else if (oper->Eq('-')) {
00605 l.do_std_conversions();
00606 r.do_std_conversions();
00607 l.do_integral_promotions();
00608 r.do_integral_promotions();
00609 if (l.get_type().get_kind() == Type::k_Pointer && r.get_type().get_kind() == Type::k_Pointer) {
00610
00611 do_pointer_arith_conversions(&l, &r);
00612 check_ptr_arith(l.get_type());
00613 return Expr_result(make_infix_expr(ptrdiff_type, l.get_tree(), oper, r.get_tree()),
00614 Expr_result::k_RValue);
00615 } else if (l.get_type().get_kind() == Type::k_Pointer && r.get_type().is_int()) {
00616
00617 check_ptr_arith(l.get_type());
00618 return Expr_result(make_infix_expr(l.get_type(), l.get_tree(), oper, r.get_tree()),
00619 Expr_result::k_RValue);
00620 } else if (r.get_type().is_arithmetic_type() && l.get_type().is_arithmetic_type()) {
00621
00622 do_usual_conversions(&l, &r);
00623 assert((l.get_type().is_int() && r.get_type().is_int())
00624 || (l.get_type().is_float() && r.get_type().is_float()));
00625 return Expr_result(make_infix_expr(l.get_type(), l.get_tree(), oper, r.get_tree()),
00626 Expr_result::k_RValue);
00627 } else
00628 compile_error("invalid arguments to binary `-'");
00629 } else if (oper->Eq('*')) {
00630
00631
00632 do_usual_conversions(&l, &r);
00633 if ((l.get_type().is_int() && r.get_type().is_int())
00634 || (l.get_type().is_float() && r.get_type().is_float()))
00635 return Expr_result(make_infix_expr(l.get_type(), l.get_tree(), oper, r.get_tree()),
00636 Expr_result::k_RValue);
00637 else
00638 compile_error("invalid arguments to binary `*'");
00639 } else if (oper->Eq('/')) {
00640
00641 do_usual_conversions(&l, &r);
00642 if ((l.get_type().is_int() && r.get_type().is_int())
00643 || (l.get_type().is_float() && r.get_type().is_float()))
00644 return Expr_result(make_infix_expr(l.get_type(), l.get_tree(), oper, r.get_tree()),
00645 Expr_result::k_RValue);
00646 else
00647 compile_error("invalid arguments to binary `/'");
00648 } else if (oper->Eq('%')) {
00649
00650 do_usual_conversions(&l, &r);
00651 if (l.get_type().is_int() && r.get_type().is_int())
00652 return Expr_result(make_infix_expr(l.get_type(), l.get_tree(), oper, r.get_tree()),
00653 Expr_result::k_RValue);
00654 else
00655 compile_error("invalid arguments to binary `%'");
00656 } else
00657 bogus_ptree_error("den Operator kenn ich noch nicht", oper);
00658 }
00659
00660 #line 543 "expr_annotator.cpp"
00661
00664 Expr_result
00665 Expr_annotator::visit_infix(PtreeInfixExpr *p)
00666 {
00667 Expr_result l = visit(p->First());
00668 Expr_result r = visit(p->Third());
00669
00670
00671 if ((l.get_type().get_kind() != Type::k_Userdef || l.get_type().is_enum_type())
00672 && (r.get_type().get_kind() != Type::k_Userdef || r.get_type().is_enum_type()))
00673 return process_binary_operator(p->Second(), l, r);
00674 else
00675 compile_error("FIXME: user-defined operator geht noch nicht");
00676 return Expr_result();
00677 }
00678
00679 #line 560 "expr_annotator.cpp"
00680
00681
00682
00684 Expr_result
00685 Expr_annotator::visit_leaf(Leaf* l)
00686 {
00687 using namespace std;
00688
00689
00690 const char* c = l->ToString();
00691 Type t;
00692 if ((*c >= '0' && *c <= '9') || *c == '.') {
00693
00694 if ((c[0] == '0' && (c[1] == 'x' || c[1] == 'X')) || strpbrk(c, ".eE") == 0) {
00695
00696
00697 bool is_uns = strpbrk(c, "uU") || (c[0] == '0' && c[strspn(c, "0")] != 0);
00698 bool is_long = strpbrk(c, "lL");
00699 t = is_uns ? is_long ? ulong_type : uint_type : is_long ? long_type : int_type;
00700 } else {
00701
00702 if (strpbrk(c, "fF"))
00703 t = float_type;
00704 else if (strpbrk(c, "lL"))
00705 t = ldouble_type;
00706 else
00707 t = double_type;
00708 }
00709 } else if (*c == '\'') {
00710
00711 t = char_type;
00712 } else if (*c == '\"') {
00713
00714
00715 t = char_type.make_array_type(0);
00716 } else if (*c == 'l' || *c == 'L') {
00717
00718 if (c[1] == '\'')
00719 t = wchar_type;
00720 else if (c[1] == '"')
00721
00722 t = wchar_type.make_array_type(0);
00723 else
00724 bogus_ptree_error("strange long thing", l);
00725
00726 } else {
00727 bogus_ptree_error("unknown leaf type", l);
00728
00729 }
00730
00731 return Expr_result(new Annotated<Leaf>(t, 0, *l), Expr_result::k_RValue);
00732 }
00733
00734 #line 613 "expr_annotator.cpp"
00735
00738 Expr_result
00739 Expr_annotator::visit_name(Ptree* p)
00740 {
00741 if (p->IsLeaf() && (p->Eq("true") || p->Eq("false")))
00742 return Expr_result(new Annotated<Leaf>(bool_type, 0, *downcast<Leaf*>(p)),
00743 Expr_result::k_RValue);
00744
00745
00746 Symbol_pair sp = Symbol_name(p, current_scope, false).lookup_for_use(false);
00747 if (!sp)
00748 compile_error("unknown symbol `" + std::string(p->ToString()) + "'");
00749
00750 if (sp.untag->get_kind() == Symbol::k_Variable) {
00751
00752 Variable_symbol* vsym = downcast<Variable_symbol*>(sp.untag);
00753 Type t = vsym->get_type().sans_reference();
00754 Expr_result::Kind k = (vsym->has_address()
00755 ? Expr_result::k_LValue
00756 : Expr_result::k_RValue);
00757 Ptree* name_tree = make_name(vsym);
00758 if (vsym->is_member_variable()) {
00759
00760 Type this_type = current_scope->get_this_type();
00761 if (!this_type.is_valid())
00762 compile_error("attempt to access member variable without an object");
00763
00764 Class_symbol* this_class = downcast<Class_symbol*>(this_type.get_type_symbol());
00765 if (vsym->get_class() != this_class && !vsym->get_class()->is_unique_base_class_of(this_class))
00766 compile_error("attempt to access member variable of different class");
00767 if (this_type.is_qualified(Type::q_Const) && (vsym->get_storage_class() != s_Mutable))
00768 t.add_qualifier(Type::q_Const);
00769 return Expr_result(new Annotated<PtreeDotMemberExpr>(t, vsym,
00770 make_this_star(this_type),
00771 Ptree::List(make_static_leaf("."),
00772 name_tree)),
00773 k);
00774 } else {
00775 return Expr_result(name_tree, t, k);
00776 }
00777 } else if (sp.untag->get_kind() == Symbol::k_Function) {
00778
00779
00780
00781
00782
00783 Function_symbol* fs = downcast<Function_symbol*>(sp.untag);
00784 Function_signature* fsig = 0;
00785 if (!fs->is_overloaded())
00786 fsig = fs->get_nonoverloaded_signature();
00787 if (fsig && fsig->get_storage_specifier() != s_Member) {
00788
00789 return Expr_result(make_name(fsig), Expr_result::k_RValue);
00790 } else {
00791
00792 if (p->IsLeaf())
00793 return Expr_result(new Annotated<LeafName>(Type(), sp.untag, *downcast<LeafName*>(p)),
00794 fs);
00795 else
00796 return Expr_result(new Annotated<PtreeName>(Type(), sp.untag, *downcast<PtreeName*>(p)),
00797 fs);
00798 }
00799 } else {
00800 compile_error("I don't understand this symbol `" + std::string(p->ToString()) + "' at this place");
00801 }
00802 }
00803
00804 #line 681 "expr_annotator.cpp"
00805
00807 Expr_result
00808 Expr_annotator::visit_this(LeafThis* p)
00809 {
00810 expect_ptree(p, "this");
00811 Type t = current_scope->get_this_type();
00812 if (!t.is_valid())
00813 compile_error("`this' is not valid here");
00814
00815 return Expr_result(make_this(t), Expr_result::k_RValue);
00816 }
00817
00818 #line 693 "expr_annotator.cpp"
00819
00820
00821
00824 Expr_result
00825 Expr_annotator::visit_paren(PtreeParenExpr* e)
00826 {
00827 expect_ptree(e->First(), '(');
00828 expect_ptree(e->Third(), ')');
00829 return visit(e->Second());
00830 }
00831
00832 #line 705 "expr_annotator.cpp"
00833
00834
00835
00840 Expr_result
00841 Expr_annotator::visit_postfix(PtreePostfixExpr* e)
00842 {
00843 Ptree* oper_tree = e->Second();
00844 Expr_result arg = visit(e->First());
00845
00846 if (arg.get_type().get_kind() == Type::k_Userdef) {
00847
00848 Leaf zero_leaf((char*)"0", 1);
00849 Overload_resolver resolver(true );
00850 resolver.add_arg(arg);
00851 resolver.add_arg(Expr_result(new Annotated<Leaf>(int_type, 0, zero_leaf),
00852 Expr_result::k_RValue));
00853
00854 Class_symbol* csym = dynamic_cast<Class_symbol*>(arg.get_type().get_type_symbol());
00855 const char* opname = Symbol_name::get_operator_name(oper_tree);
00856 assert(opname);
00857
00858
00859 if(csym) {
00860 Symbol_pair p = csym->get_scope()->lookup_here(opname, false);
00861 Function_symbol* fsym = dynamic_cast<Function_symbol*>(p.untag);
00862 if (fsym)
00863 resolver.add_function(fsym, false);
00864 }
00865
00866
00867 Symbol_pair p = current_scope->lookup_unqualified(opname);
00868 if (Function_symbol* fsym = dynamic_cast<Function_symbol*>(p.untag))
00869 resolver.add_function(fsym, false);
00870
00871
00872
00873
00874
00875 if (oper_tree->Eq("++")) {
00876 resolver.add_builtin_incdec(true);
00877 } else if (oper_tree->Eq("--")) {
00878 resolver.add_builtin_incdec(false);
00879 } else {
00880 bogus_ptree_error("expected ++ or --", e);
00881 }
00882
00883 bool is_ambig;
00884 Overload_candidate* cand = resolver.get_best(&is_ambig);
00885 if (is_ambig)
00886 compile_error("call to postfix operator `" + std::string(oper_tree->ToString()) + "' is ambiguous");
00887 if (cand) {
00888 if (!cand->fsig->is_builtin()) {
00889
00890 Expr_result result;
00891 Annotated_funcall_maker afm(make_name(cand->fsig),
00892 cand->fsig->get_return_type());
00893 afm.add_arg(resolver.get_arg(0).get_tree());
00894 afm.add_arg(resolver.get_arg(1).get_tree());
00895 result.set_value(afm.make_funcall(),
00896 cand->fsig->get_return_type());
00897 return result;
00898 } else {
00899 arg = resolver.get_arg(0);
00900 }
00901 }
00902 }
00903
00904
00905 if (oper_tree->Eq("++")) {
00906
00907
00908 if (arg.get_type().is_arithmetic_type()) {
00909
00910 } else if (arg.get_type().get_kind() == Type::k_Pointer) {
00911 if (!arg.get_type().get_basis_type().is_complete())
00912 compile_error("postfix `++' applied to incomplete type");
00913 } else
00914 compile_error("postfix `++' applied to invalid type");
00915 if (!arg.is_modifyable_lvalue())
00916 compile_error("postfix `++' needs modifyabla lvalue");
00917 return Expr_result(new Annotated<PtreePostfixExpr>(arg.get_type().get_unqualified_type(),
00918 0 ,
00919 arg.get_tree(),
00920 Ptree::List(oper_tree)),
00921 Expr_result::k_RValue);
00922 } else if (oper_tree->Eq("--")) {
00923
00924
00925 if (arg.get_type().is_arithmetic_type()) {
00926 if (arg.get_type().is_same_unqualified_type(bool_type))
00927 compile_error("postfix `--' applied to `bool'");
00928 } else if (arg.get_type().get_kind() == Type::k_Pointer) {
00929 if (!arg.get_type().get_basis_type().is_complete())
00930 compile_error("postfix `--' applied to incomplete type");
00931 } else
00932 compile_error("postfix `--' applied to invalid type");
00933 if (!arg.is_modifyable_lvalue())
00934 compile_error("postfix `--' needs modifyabla lvalue");
00935 return Expr_result(new Annotated<PtreePostfixExpr>(arg.get_type().get_unqualified_type(),
00936 0 ,
00937 arg.get_tree(),
00938 Ptree::List(oper_tree)),
00939 Expr_result::k_RValue);
00940 } else {
00941 bogus_ptree_error("bogus postfix operator", e);
00942 }
00943 }
00944
00945 #line 816 "expr_annotator.cpp"
00946
00947
00948
00951 static Type
00952 unary_deref_predicate(Type t)
00953 {
00954 if (t.get_kind() == Type::k_Pointer) {
00955 Type b = t.get_basis_type();
00956 if (b.is_object_type() || b.get_kind() == Type::k_Function)
00957 return make_unary_function_type(t, b.make_reference_type());
00958 }
00959 return Type();
00960 }
00961
00962 #line 831 "expr_annotator.cpp"
00963
00966 static Type
00967 unary_plus_predicate(Type t)
00968 {
00969 if (t.get_kind() == Type::k_Pointer) {
00970 return make_unary_function_type(t, t);
00971 } else if (t.is_arithmetic_type() || t.is_enum_type()) {
00972 Type p = t.get_promoted_integer();
00973 return make_unary_function_type(p, p);
00974 }
00975 return Type();
00976 }
00977
00978 #line 845 "expr_annotator.cpp"
00979
00982 static Type
00983 unary_minus_predicate(Type t)
00984 {
00985 if (t.is_arithmetic_type() || t.is_enum_type()) {
00986 Type p = t.get_promoted_integer();
00987 return make_unary_function_type(p, p);
00988 }
00989 return Type();
00990 }
00991
00992 #line 857 "expr_annotator.cpp"
00993
00996 static Type
00997 unary_bitnot_predicate(Type t)
00998 {
00999 if (t.is_int() || t.is_enum_type()) {
01000 Type p = t.get_promoted_integer();
01001 return make_unary_function_type(p, p);
01002 }
01003 return Type();
01004 }
01005
01006 #line 869 "expr_annotator.cpp"
01007
01008
01009
01010
01011
01012
01013
01014
01015
01019 Expr_result
01020 Expr_annotator::process_unary_operator(Ptree* oper, Expr_result arg)
01021 {
01022
01023 if (oper->Eq('*')) {
01024 arg.do_std_conversions();
01025 if (arg.get_type().get_kind() != Type::k_Pointer)
01026 compile_error("operand to `*' must be pointer");
01027 Type r = arg.get_type().get_basis_type();
01028 if (r.is_void())
01029 compile_error("can't dereference a `void*'");
01030 return Expr_result(make_prefix_expr(r, oper, arg.get_tree()), Expr_result::k_LValue);
01031 } else if (oper->Eq('&')) {
01032
01033
01034 if (!arg.is_value()) {
01035
01036
01037
01038 if (!arg.is_function())
01039 compile_error("invalid argument to `&'");
01040 return arg;
01041 }
01042
01043 if (arg.get_type().get_kind() == Type::k_Function || arg.is_lvalue()) {
01044 return Expr_result(make_prefix_expr(arg.get_type().make_pointer_type(),
01045 oper, arg.get_tree()),
01046 Expr_result::k_RValue);
01047 } else {
01048 compile_error("invalid argument to unary `&'");
01049 }
01050 } else if (oper->Eq('+')) {
01051
01052 arg.do_std_conversions();
01053 arg.do_integral_promotions();
01054 if (arg.get_type().get_kind() == Type::k_Pointer || arg.get_type().is_arithmetic_type())
01055 return Expr_result(make_prefix_expr(arg.get_type(), oper, arg.get_tree()),
01056 Expr_result::k_RValue);
01057 else
01058 compile_error("invalid argument to unary `+'");
01059 } else if (oper->Eq('-')) {
01060
01061 arg.do_std_conversions();
01062 arg.do_integral_promotions();
01063 if (arg.get_type().is_int() || arg.get_type().is_float())
01064 return Expr_result(make_prefix_expr(arg.get_type(), oper, arg.get_tree()),
01065 Expr_result::k_RValue);
01066 else
01067 compile_error("invalid operand to unary `-'");
01068 } else if (oper->Eq('!')) {
01069
01070 arg.convert_to_bool();
01071 return Expr_result(make_prefix_expr(bool_type, oper, arg.get_tree()),
01072 Expr_result::k_RValue);
01073 } else if (oper->Eq('~')) {
01074
01075 arg.do_std_conversions();
01076 arg.do_integral_promotions();
01077 if (!arg.get_type().is_int())
01078 compile_error("invalid operand to unary `~'");
01079 return Expr_result(make_prefix_expr(arg.get_type(), oper, arg.get_tree()),
01080 Expr_result::k_RValue);
01081 } else if (oper->Eq("++")) {
01082
01083
01084 if (arg.get_type().get_kind() == Type::k_Pointer) {
01085 if (!arg.get_type().get_basis_type().is_complete())
01086 compile_error("`++' applied to pointer to incomplete type");
01087 } else if (!arg.get_type().is_arithmetic_type())
01088 compile_error("`++' applied to wrong type");
01089 if (arg.is_modifyable_lvalue())
01090 return Expr_result(make_prefix_expr(arg.get_type(), oper, arg.get_tree()),
01091 Expr_result::k_LValue);
01092 else
01093 compile_error("`++' only works for modifyable lvalues");
01094 } else if (oper->Eq("--")) {
01095
01096
01097 if (arg.get_type().get_kind() == Type::k_Pointer) {
01098 if (!arg.get_type().get_basis_type().is_complete())
01099 compile_error("`--' applied to pointer to incomplete type");
01100 } else if (!arg.get_type().is_arithmetic_type() || arg.get_type().is_same_unqualified_type(bool_type))
01101 compile_error("`--' applied to wrong type");
01102 if (arg.is_modifyable_lvalue())
01103 return Expr_result(make_prefix_expr(arg.get_type(), oper, arg.get_tree()),
01104 Expr_result::k_LValue);
01105 else
01106 compile_error("`--' only works for modifyable lvalues");
01107 } else {
01108 bogus_ptree_error("unknown operator", oper);
01109 }
01110 }
01111
01112 #line 973 "expr_annotator.cpp"
01113
01115 Expr_result
01116 Expr_annotator::visit_unary(PtreeUnaryExpr* e)
01117 {
01118 Ptree* oper_tree = e->First();
01119 Ptree* arg_tree = e->Second();
01120
01121
01122
01123 if (oper_tree->Eq('&') && ptree_is_name(arg_tree)) {
01124 Symbol_name name(arg_tree, current_scope, false);
01125 if (name.is_qualified()) {
01126
01127 assert(!arg_tree->IsLeaf());
01128
01129 Symbol* p = name.lookup_for_use(false).untag;
01130 if (!p)
01131 compile_error("unknown identifier `" + std::string(arg_tree->ToString()) + "'");
01132
01133
01134
01135
01136
01137 if (Variable_symbol* var = dynamic_cast<Variable_symbol*>(p)) {
01138 if (var->is_member_variable())
01139 return Expr_result(make_prefix_expr(var->get_class()->get_type().make_member_type(var->get_type()),
01140 e->First(),
01141 make_name(var)),
01142 Expr_result::k_RValue);
01143 } else if (Function_symbol* fsym = dynamic_cast<Function_symbol*>(p)) {
01144 if (fsym->is_overloaded()) {
01145 return Expr_result(new Annotated<PtreeName>(Type(),
01146 fsym,
01147 *downcast<PtreeName*>(arg_tree)),
01148 fsym);
01149 } else {
01150 Function_signature* fsig = fsym->get_nonoverloaded_signature();
01151 return Expr_result(new Annotated<PtreeName>(fsig->get_pointer_type(),
01152 fsig,
01153 *downcast<PtreeName*>(arg_tree)),
01154 Expr_result::k_RValue);
01155 }
01156 }
01157 }
01158 }
01159
01160
01161 Expr_result arg = visit(arg_tree);
01162
01163 if (arg.get_type().get_kind() == Type::k_Userdef) {
01164
01165
01166 Ptree* oper = e->First();
01167 Class_symbol* csym = dynamic_cast<Class_symbol*>(arg.get_type().get_type_symbol());
01168
01169 Overload_resolver resolver(true);
01170 resolver.add_arg(arg);
01171
01172 const char* opname = Symbol_name::get_operator_name(oper);
01173 assert(opname);
01174
01175
01176
01177
01178
01179
01180
01181 if(csym) {
01182 Symbol_pair p = csym->get_scope()->lookup_here(opname, false);
01183 Function_symbol* fsym = dynamic_cast<Function_symbol*>(p.untag);
01184 if (fsym)
01185 resolver.add_function(fsym, false);
01186 }
01187
01188
01189
01190
01191
01192
01193
01194
01195
01196
01197
01198
01199
01200 Symbol_pair p = current_scope->lookup_unqualified(opname);
01201 if (Function_symbol* fsym = dynamic_cast<Function_symbol*>(p.untag))
01202 resolver.add_function(fsym, false);
01203
01204
01205
01206
01207
01208 if (oper->Eq("++")) {
01209 resolver.add_builtin_incdec(true);
01210 } else if (oper->Eq("--")) {
01211 resolver.add_builtin_incdec(false);
01212 } else if (oper->Eq('*')) {
01213 resolver.add_builtin_unary_ops(unary_deref_predicate);
01214 } else if (oper->Eq('&')) {
01215
01216 } else if (oper->Eq('+')) {
01217 resolver.add_builtin_unary_ops(unary_plus_predicate);
01218 } else if (oper->Eq('-')) {
01219 resolver.add_builtin_unary_ops(unary_minus_predicate);
01220 } else if (oper->Eq('!')) {
01221
01222 resolver.add_signature(Function_signature::make_builtin(make_unary_function_type(bool_type, bool_type)));
01223 } else if (oper->Eq('~')) {
01224 resolver.add_builtin_unary_ops(unary_bitnot_predicate);
01225 } else {
01226 assert(0);
01227 }
01228
01229 bool is_ambig;
01230 Overload_candidate* cand = resolver.get_best(&is_ambig);
01231 if (is_ambig)
01232 compile_error("call to operator `" + std::string(oper->ToString()) + "' is ambiguous");
01233 if (cand) {
01234 if (!cand->fsig->is_builtin()) {
01235
01236 Expr_result result;
01237 result.set_value(make_unary_funcall(make_name(cand->fsig),
01238 cand->fsig->get_return_type(),
01239 resolver.get_arg(0).get_tree()),
01240 cand->fsig->get_return_type());
01241 return result;
01242 } else {
01243 arg = resolver.get_arg(0);
01244 }
01245 }
01246 }
01247
01248 return process_unary_operator(e->First(), arg);
01249 }
01250
01251 #line 1110 "expr_annotator.cpp"
01252
01253
01254
01258 static Type
01259 unary_delete_or_arrow_predicate(Type t)
01260 {
01261 if (t.get_kind() == Type::k_Pointer && t.get_basis_type().is_object_type())
01262 return make_unary_function_type(t, void_type);
01263 return Type();
01264 }
01265
01266 #line 1123 "expr_annotator.cpp"
01267
01269 Expr_result
01270 Expr_annotator::visit_delete(PtreeDeleteExpr* pde)
01271 {
01272
01273
01274
01275
01276
01277 Ptree* expr;
01278 Ptree* p = pde;
01279 bool is_global = false, is_array = false;
01280 if (p->First()->Eq("::")) {
01281 is_global = true;
01282 p = p->Cdr();
01283 }
01284
01285 expect_ptree(p->First(), "delete");
01286 if (p->Second()->Eq('[')) {
01287 expect_ptree(p->Third(), ']');
01288 expr = p->Nth(3);
01289 is_array = true;
01290 } else {
01291 expr = p->Second();
01292 }
01293
01294
01295 Expr_result arg = visit(expr);
01296
01297
01298 if (arg.get_type().is_class_type()) {
01299
01300
01301
01302
01303
01304
01305
01306
01307
01308
01309
01310 Overload_resolver resolver(true);
01311 resolver.add_arg(arg);
01312 resolver.add_builtin_unary_ops(unary_delete_or_arrow_predicate);
01313
01314 bool is_ambig;
01315 Overload_candidate* cand = resolver.get_best(&is_ambig);
01316 if (is_ambig)
01317 compile_error("call to operator delete is ambiguous");
01318 if (!cand)
01319 compile_error("no possible conversion to pointer for operator delete");
01320
01321
01322 arg = resolver.get_arg(0);
01323 }
01324
01325
01326 if (arg.get_type().get_kind() != Type::k_Pointer || !arg.get_type().get_basis_type().is_object_type())
01327 compile_error("invalid argument to operator delete");
01328
01329
01330 Symbol* delete_sym = 0;
01331
01332
01333 if (is_array) {
01334
01335
01336
01337
01338
01339 Annotated<PtreeDeleteExpr>* tree =
01340 new Annotated<PtreeDeleteExpr>(void_type,
01341 delete_sym,
01342 p->First(),
01343 Ptree::List(p->Second(),
01344 p->Third(),
01345 arg.get_tree()));
01346 return Expr_result(tree, Expr_result::k_RValue);
01347 } else {
01348
01349
01350
01351 return Expr_result(new Annotated<PtreeDeleteExpr>(void_type,
01352 delete_sym,
01353 p->First(),
01354 Ptree::List(arg.get_tree())),
01355 Expr_result::k_RValue);
01356 }
01357 }
01358
01359 #line 1214 "expr_annotator.cpp"
01360
01361
01362
01364 Expr_result
01365 Expr_annotator::visit_new(PtreeNewExpr* p_)
01366 {
01367 Ptree* p = p_;
01368
01369
01370
01371
01372
01373
01374 bool global = false;
01375 if (p->First()->Eq("::")) {
01376 p = p->Cdr();
01377 global = true;
01378 }
01379
01380 expect_ptree(p->First(), "new");
01381
01382
01383 Ptree* type_tree = p->Third();
01384 Type t = parse_type(type_tree->First(), current_scope, 0, false);
01385 if (!parse_abstract_declarator(type_tree->Second(), current_scope, &t))
01386 compile_error("invalid type for `new'");
01387
01388
01389 Type basis_type = t.sans_array();
01390 if (!t.is_object_type())
01391 compile_error("non-object type for `new'");
01392 const char* op_name = (t.get_kind() == Type::k_Array ? Symbol_name::ANEW_OPERATOR_NAME : Symbol_name::NEW_OPERATOR_NAME);
01393
01394 Overload_resolver resolver(false);
01395 Overload_candidate* cand = 0;
01396
01397 resolver.add_arg(make_sizeof_expr(basis_type));
01398 if (p->Second()) {
01399 Ptree* args = p->Second();
01400 expect_ptree(args->Car(), '(');
01401 expect_ptree(args->Third(), ')');
01402 add_parms_from_arglist(&resolver, args->Second());
01403 }
01404 if (!global && basis_type.is_class_type()) {
01405
01406 Function_symbol* fsym = dynamic_cast<Function_symbol*>(downcast<Class_symbol*>(basis_type.get_type_symbol())->get_scope()->lookup_here(op_name, false).untag);
01407 if (fsym) {
01408 resolver.add_function(fsym, true);
01409
01410 bool is_ambig;
01411 cand = resolver.get_best(&is_ambig);
01412 if (is_ambig)
01413 compile_error ("ambiguous call to operator new for type " + basis_type.get_human_readable_type());
01414 }
01415 }
01416
01417 if (!cand) {
01418 Function_symbol* fsym = dynamic_cast<Function_symbol*>(current_scope->get_global_scope()->lookup_here(op_name, false).untag);
01419 if (!fsym)
01420 compile_error("huh? no operator new declared");
01421 resolver.add_function(fsym, true);
01422
01423 bool is_ambig;
01424 cand = resolver.get_best(&is_ambig);
01425 if (is_ambig)
01426 compile_error("ambiguous call to ::operator new for type " + basis_type.get_human_readable_type());
01427 }
01428
01429 if (!cand)
01430 compile_error("no matching function to call for this operator new expression");
01431
01432
01433
01434
01435
01436
01437
01438
01439
01440
01441
01442
01443
01444 Ptree* init = 0;
01445 if (p->Length() > 3)
01446 init = Init_handler(current_scope, p->Nth(3), false).process_initializer(t);
01447
01448 Ptree* call = 0;
01449 if (resolver.get_arg_count() > 1) {
01450 Ptree* list = 0;
01451 for (unsigned i = 1; i < resolver.get_arg_count(); ++i) {
01452 if (list) {
01453 Ptree::Snoc(list, make_static_leaf(","));
01454 Ptree::Snoc(list, resolver.get_arg(i).get_tree());
01455 } else {
01456 list = Ptree::List(resolver.get_arg(i).get_tree());
01457 }
01458 }
01459 call = Ptree::List(make_static_leaf("("), list, make_static_leaf(")"));
01460 }
01461
01462 Encoding tmp_enc;
01463 Ptree* tail = Ptree::List(call,
01464 Ptree::List(make_name(t.get_encoded_type(), t, 0),
01465 new PtreeDeclarator(Ptree::List(0), tmp_enc)));
01466 if (init)
01467 Ptree::Snoc(tail, init);
01468
01469 return Expr_result(new Annotated<PtreeNewExpr>(t.get_kind() == Type::k_Array ? t.get_basis_type().make_pointer_type() : t.make_pointer_type(),
01470 cand->fsig,
01471 p->First() ,
01472 tail),
01473 Expr_result::k_RValue);
01474 }
01475
01476 #line 1329 "expr_annotator.cpp"
01477
01478
01479
01481 static Expr_result
01482 make_sizeof_expr(Type t)
01483 {
01484 if (!t.is_valid() || !t.is_complete() || t.get_kind() == Type::k_Function)
01485 compile_error("invalid type argument to `sizeof'");
01486
01487 Annotated<PtreeSizeofExpr>* ex =
01488 new Annotated<PtreeSizeofExpr>(size_type, 0,
01489 make_static_leaf("sizeof"),
01490 Ptree::List(make_static_leaf("("),
01491 make_name(t.get_encoded_type(), t, 0),
01492 make_static_leaf(")")));
01493 return Expr_result(ex, Expr_result::k_RValue);
01494 }
01495
01496 #line 1347 "expr_annotator.cpp"
01497
01499 Expr_result
01500 Expr_annotator::visit_sizeof(PtreeSizeofExpr* e)
01501 {
01502
01503
01504
01505
01506
01507
01508
01509
01510
01511
01512
01513
01514
01515
01516
01517
01518
01519
01520
01521
01522
01523 expect_ptree(e->First(), "sizeof");
01524 if (e->Length() == 4) {
01525
01526 expect_ptree(e->Second(), '(');
01527 expect_ptree(e->Nth(3), ')');
01528
01529 Ptree* arg = e->Third();
01530
01531
01532 if (!arg->Second() || (!arg->Second()->IsLeaf() && !arg->Second()->First())) {
01533 try {
01534 return make_sizeof_expr(parse_type(arg->First(), current_scope, 0, false));
01535 }
01536 catch(...) { }
01537
01538
01539 Symbol* sym = Symbol_name(arg->First(), current_scope, false).lookup_for_use(false).untag;
01540 if (Type_symbol* tsym = dynamic_cast<Type_symbol*>(sym))
01541 return make_sizeof_expr(tsym->get_type());
01542 else if (Variable_symbol* vsym = dynamic_cast<Variable_symbol*>(sym))
01543 return make_sizeof_expr(visit_name(arg->First()).get_type());
01544 else
01545 compile_error("invalid argument to sizeof");
01546 }
01547
01548
01549 Type t = parse_type(arg->First(), current_scope, 0, false);
01550 if (parse_abstract_declarator(arg->Second(), current_scope, &t)) {
01551 return make_sizeof_expr(t);
01552 } else {
01553 compile_error("invalid type for sizeof");
01554 }
01555 } else if (e->Length() == 2) {
01556
01557 return make_sizeof_expr(visit(e->Second()).get_type());
01558 } else {
01559 bogus_ptree_error("don't understand this sizeof", e);
01560 }
01561 }
01562
01563 #line 1412 "expr_annotator.cpp"
01564
01566 Expr_result
01567 Expr_annotator::visit_throw(PtreeThrowExpr* p)
01568 {
01569 expect_ptree(p->First(), "throw");
01570
01571 Ptree* ex_tree;
01572 if (!p->Second()) {
01573
01574 ex_tree = 0;
01575 } else {
01576
01577 Expr_result r = visit(p->Second());
01578 if (r.get_type().is_void())
01579 compile_error("throw void is not allowed");
01580 r.convert_to_pointer();
01581 r.convert_to_fpointer();
01582 r.convert_to_qual(r.get_type().get_unqualified_type());
01583 ex_tree = r.get_tree();
01584 }
01585
01586 return Expr_result(new Annotated<PtreeThrowExpr>(void_type, 0,
01587 p->First(),
01588 Ptree::List(ex_tree)),
01589 Expr_result::k_RValue);
01590 }
01591
01592 #line 1439 "expr_annotator.cpp"
01593
01594
01595
01597 Expr_result
01598 Expr_annotator::process_member_access(Expr_result lhs, Ptree* rhs_tree)
01599 {
01600 Class_symbol* csym = downcast<Class_symbol*>(lhs.get_type().get_type_symbol());
01601
01602 Symbol_name rhs(rhs_tree, csym->get_scope(), false);
01603 Symbol* p = rhs.lookup_for_use(true).untag;
01604 if (!p)
01605 compile_error("unknown name `" + rhs.get_name() + "'");
01606
01607
01608
01609
01610 Class_scope* look_scope = dynamic_cast<Class_scope*>(rhs.get_scope());
01611 if (!look_scope)
01612 compile_error("invalid qualified name on the RHS of `.' or `->'");
01613 Class_symbol* look_class = look_scope->get_class_symbol();
01614 if (look_class != csym) {
01615
01616
01617 if (!look_class->is_unique_base_class_of(csym))
01618 compile_error("`" + look_class->get_name() + "' is not a unique base class of `" + csym->get_name() + "'");
01619 lhs = Expr_result(make_derived_to_base_cast(lhs.get_tree(),
01620 look_class->get_type().with_qualifiers(lhs.get_type())),
01621 lhs.get_kind());
01622 }
01623
01624 if (Function_symbol* fsym = dynamic_cast<Function_symbol*>(p)) {
01625
01626 Annotated<LeafName>* name = make_name(fsym->get_name(), Type(), fsym);
01627 if (rhs.is_qualified())
01628 name->add_flag(Annotation::af_DirectCall);
01629 return Expr_result(lhs.get_tree(), name, fsym);
01630 } else if (Variable_symbol* vsym = dynamic_cast<Variable_symbol*>(p)) {
01631
01632 if (vsym->is_member_variable()) {
01633
01634
01635
01636
01637
01638
01639
01640 if (vsym->get_class() != look_class && !vsym->get_class()->is_unique_base_class_of(look_class))
01641 compile_error("attempt to access member variable of `" + vsym->get_class()->get_name()
01642 + "' which is not a unique base class of `" + look_class->get_name() + "'");
01643 Type vt = vsym->get_type();
01644 if (lhs.get_type().is_qualified(Type::q_Const) && (vsym->get_storage_class() != s_Mutable))
01645 vt.add_qualifier(Type::q_Const);
01646 return Expr_result(new Annotated<PtreeDotMemberExpr>(vt, vsym,
01647 lhs.get_tree(),
01648 Ptree::List(make_static_leaf("."),
01649 make_name(vsym))),
01650 lhs.get_kind());
01651 } else {
01652
01653
01654
01655
01656
01657
01658
01659 compile_warning("using a static variable with object", rhs_tree);
01660 return Expr_result(make_comma_expr(lhs.get_tree(), make_name(vsym)), lhs.get_kind());
01661 }
01662 } else {
01663 compile_error("name `" + p->get_name() + "' not allowed at RHS of `.' or `->'");
01664 }
01665 }
01666
01667 #line 1512 "expr_annotator.cpp"
01668
01670 Expr_result
01671 Expr_annotator::visit_dotmember(PtreeDotMemberExpr* e)
01672 {
01673 expect_ptree(e->Second(), '.');
01674
01675 Expr_result lhs = visit(e->First());
01676 if (!lhs.get_type().is_class_type())
01677 compile_error("left-hand side of `.' is not of class type");
01678
01679 return process_member_access(lhs, e->Third());
01680 }
01681
01682 #line 1525 "expr_annotator.cpp"
01683
01686 Expr_result
01687 Expr_annotator::visit_arrowmember(PtreeArrowMemberExpr* e)
01688 {
01689 expect_ptree(e->Second(), "->");
01690
01691 Expr_result lhs = visit(e->First());
01692
01693 int loop_count = 0;
01694 while (lhs.get_type().is_class_type()) {
01695
01696 if (++loop_count > PTR_DELEGATION_LIMIT) {
01697 compile_warning("Whatever you drank, it didn't contribute to your health:", e);
01698 compile_error("Pointer delegation takes too long to resolve, bailing out.");
01699 }
01700
01701 Class_symbol* csym = downcast<Class_symbol*>(lhs.get_type().get_type_symbol());
01702 Class_scope* cscope = csym->get_scope();
01703
01704
01705 Function_symbol* fsym = dynamic_cast<Function_symbol*>(cscope->lookup_here(Symbol_name::PTR_OPERATOR_NAME, false).untag);
01706 if (fsym) {
01707 Overload_resolver resolver(true);
01708 resolver.add_arg(lhs);
01709 resolver.add_function(fsym, true);
01710
01711 bool is_ambig;
01712 Overload_candidate* cand = resolver.get_best(&is_ambig);
01713 if (is_ambig)
01714 compile_error("operator -> is ambiguous");
01715 if (cand) {
01716
01717 lhs.set_value(make_unary_funcall(make_name(cand->fsig),
01718 cand->fsig->get_return_type(),
01719 resolver.get_arg(0).get_tree()),
01720 cand->fsig->get_return_type());
01721 continue;
01722 }
01723 }
01724
01725
01726 Overload_resolver resolver(true);
01727 resolver.add_arg(lhs);
01728 resolver.add_builtin_unary_ops(unary_delete_or_arrow_predicate);
01729
01730 bool is_ambig;
01731 Overload_candidate* cand = resolver.get_best(&is_ambig);
01732 if (is_ambig)
01733 compile_error("conversion to pointer for -> is ambiguous");
01734 if (!cand)
01735 compile_error("no possible conversion to pointer for operator ->");
01736
01737 lhs = resolver.get_arg(0);
01738 }
01739
01740 if (!lhs.get_type().get_kind() == Type::k_Pointer
01741 || !lhs.get_type().get_basis_type().is_class_type())
01742 compile_error("left-hand-side of operator -> must be pointer to class type");
01743
01744 return process_member_access(Expr_result(make_prefix_expr(lhs.get_type().get_basis_type(),
01745 make_static_leaf("*"),
01746 lhs.get_tree()),
01747 Expr_result::k_LValue),
01748 e->Third());
01749 }
01750
01751 #line 1592 "expr_annotator.cpp"
01752
01754 Expr_result
01755 Expr_annotator::process_pm_expr(Expr_result lhs, const Expr_result& rhs)
01756 {
01757 if (!lhs.get_type().is_class_type())
01758 compile_error("left side of `.*' must be class");
01759 if (rhs.get_type().get_kind() != Type::k_Member)
01760 compile_error("right side of `.*' must be member pointer");
01761
01762 Type ltype = lhs.get_type();
01763 Type rtype = rhs.get_type().get_class_type().with_qualifiers(rhs.get_type().get_member_type());
01764 Class_symbol* lclass = downcast<Class_symbol*>(ltype.get_type_symbol());
01765 Class_symbol* rclass = downcast<Class_symbol*>(rtype.get_type_symbol());
01766
01767 if (rclass != lclass) {
01768
01769 if (!rclass->is_unique_base_class_of(lclass))
01770 compile_error("cannot use a pointer-to-member-of-" + rclass->get_name()
01771 + " with an object of type " + lclass->get_name());
01772 lhs = Expr_result(make_derived_to_base_cast(lhs.get_tree(), rtype),
01773 lhs.get_kind());
01774 }
01775
01776
01777 Type memtype = rhs.get_type().get_member_type();
01778 if (memtype.get_kind() == Type::k_Function) {
01779
01780
01781 if (ltype.is_more_qualified_than(rtype))
01782 compile_error("class is more qualified than member pointer");
01783 return Expr_result(lhs.get_tree(),
01784 rhs.get_tree(),
01785 memtype);
01786 } else {
01787
01788 return Expr_result(new Annotated<PtreePmExpr>(memtype.with_qualifiers(ltype),
01789 0,
01790 lhs.get_tree(),
01791 Ptree::List(make_static_leaf(".*"),
01792 rhs.get_tree())),
01793 lhs.get_kind());
01794 }
01795 }
01796
01797 #line 1636 "expr_annotator.cpp"
01798
01800 Expr_result
01801 Expr_annotator::visit_pm(PtreePmExpr* e)
01802 {
01803 if (e->Second()->Eq("->*")) {
01804
01805 Expr_result lhs = visit(e->First());
01806 if (lhs.get_type().get_kind() != Type::k_Pointer && !lhs.get_type().get_basis_type().is_object_type())
01807 compile_error("operator ->* needs a pointer on the LHS");
01808 return process_pm_expr(Expr_result(make_prefix_expr(lhs.get_type().get_basis_type(),
01809 make_static_leaf("*"),
01810 lhs.get_tree()),
01811 Expr_result::k_LValue),
01812 visit(e->Third()));
01813 } else if (e->Second()->Eq(".*")) {
01814 return process_pm_expr(visit(e->First()),
01815 visit(e->Third()));
01816 } else
01817 bogus_ptree_error("expected .* or ->*", e);
01818 }
01819
01820 #line 1657 "expr_annotator.cpp"
01821
01822
01823
01825 static Annotated<PtreeCondExpr>*
01826 make_cond_expr(Type ty, Ptree* i, Ptree* t, Ptree* e)
01827 {
01828 return new Annotated<PtreeCondExpr>(ty, 0, i,
01829 Ptree::List(make_static_leaf("?"),
01830 t,
01831 make_static_leaf(":"),
01832 e));
01833 }
01834
01835 #line 1670 "expr_annotator.cpp"
01836
01837 static Implicit_conversion*
01838 generate_ics_for_ternary(Expr_result e1, Expr_result e2)
01839 {
01840
01841
01842
01843 if (e2.is_lvalue()) {
01844 Implicit_conversion* ics = generate_implicit_conversion(e1,
01845 e2.get_type().make_reference_type(),
01846 0,
01847 true ,
01848 false ,
01849 false );
01850 if (!ics->uses_temporary())
01851 return ics;
01852 }
01853
01854 e1.convert_to_rvalue();
01855 e2.convert_to_rvalue();
01856
01857
01858 return generate_implicit_conversion(e1, e2.get_type(), 0, true, false, false);
01859 }
01860
01861 #line 1694 "expr_annotator.cpp"
01862
01864 Expr_result
01865 Expr_annotator::visit_cond(PtreeCondExpr* p)
01866 {
01867 expect_ptree(p->Second(), '?');
01868 expect_ptree(p->Nth(3), ':');
01869
01870 Expr_result i = visit(p->First());
01871 Expr_result t = visit(p->Third());
01872 Expr_result e = visit(p->Nth(4));
01873
01874 Implicit_conversion* ics = generate_implicit_conversion(i, bool_type, 0,
01875 true, true, false);
01876 if (!ics)
01877 compile_error("unable to convert first arg of ?: to bool");
01878 i = ics->make_tree(i);
01879
01880 if (t.get_type().is_void() || e.get_type().is_void()) {
01881
01882
01883
01884 t.do_std_conversions();
01885 e.do_std_conversions();
01886 Type ty = (dynamic_cast<PtreeThrowExpr*>(t.get_tree())
01887 ? e.get_type()
01888 : dynamic_cast<PtreeThrowExpr*>(e.get_tree())
01889 ? t.get_type()
01890 : void_type);
01891 return Expr_result(make_cond_expr(ty, i.get_tree(),
01892 t.get_tree(), e.get_tree()),
01893 Expr_result::k_RValue);
01894 }
01895
01896 if (!t.get_type().is_same_unqualified_type(e.get_type())
01897 && (t.get_type().is_class_type() || e.get_type().is_class_type()))
01898 {
01899
01900
01901 Implicit_conversion* ics_cvt_t = generate_ics_for_ternary(t, e);
01902 Implicit_conversion* ics_cvt_e = generate_ics_for_ternary(e, t);
01903 if (ics_cvt_e && ics_cvt_t)
01904 compile_error("ternary operator application is ambiguous");
01905 if (ics_cvt_t)
01906 t = ics_cvt_t->make_tree(t);
01907 if (ics_cvt_e)
01908 e = ics_cvt_e->make_tree(e);
01909 }
01910
01911 if (t.is_lvalue() && e.is_lvalue() && t.get_type().is_same_unqualified_type(e.get_type())) {
01912 return Expr_result(make_cond_expr(t.get_type().with_qualifiers(e.get_type()),
01913 i.get_tree(), t.get_tree(), e.get_tree()),
01914 Expr_result::k_LValue);
01915 }
01916
01917
01918
01919 t.do_std_conversions();
01920 e.do_std_conversions();
01921 bool two_npc = t.is_npc() && e.is_npc();
01922 if (t.get_type().is_same_unqualified_type(e.get_type()))
01923 ;
01924 else if ((t.get_type().is_arithmetic_type() || t.get_type().is_enum_type())
01925 && (e.get_type().is_arithmetic_type() || e.get_type().is_enum_type()))
01926 do_usual_conversions(&t, &e);
01927 else if (!two_npc
01928 && (t.get_type().get_kind() == Type::k_Pointer || t.is_npc())
01929 && (e.get_type().get_kind() == Type::k_Pointer || e.is_npc()))
01930 {
01931
01932 do_pointer_arith_conversions(&t, &e);
01933 } else if (!two_npc
01934 && (t.get_type().get_kind() == Type::k_Member || t.is_npc())
01935 && (e.get_type().get_kind() == Type::k_Member || e.is_npc()))
01936 {
01937
01938 do_mptr_arith_conversions(&t, &e);
01939 } else {
01940 compile_error("invalid arguments to ternary operator");
01941 }
01942
01943 assert(t.get_type().is_same_unqualified_type(e.get_type()));
01944
01945 return Expr_result(make_cond_expr(t.get_type().with_qualifiers(e.get_type()),
01946 i.get_tree(), t.get_tree(), e.get_tree()),
01947 Expr_result::k_RValue);
01948 }
01949
01950 #line 1781 "expr_annotator.cpp"
01951
01952
01953
01954
01955
01956
01957
01958
01959
01960
01961
01962
01963
01964
01965
01966
01967
01968
01969
01970
01971
01972
01973
01974
01975
01976
01977
01978
01979
01980
01981
01982
01983
01985 Type
01986 Expr_annotator::get_cast_type(Ptree* type_args)
01987 {
01988
01989
01990 expect_ptree(type_args->First(), '<');
01991 expect_ptree(type_args->Third(), '>');
01992
01993 Ptree* inside = type_args->Second();
01994 if (inside->IsLeaf() || inside->Length() != 1 || inside->First()->IsLeaf() || inside->First()->Length() != 2)
01995 compile_error("syntax error in C++ cast");
01996
01997 Type t = parse_type(inside->First()->First(), current_scope, 0, false);
01998 if (!parse_abstract_declarator(inside->First()->Second(), current_scope, &t))
01999 compile_error("bogus declarator in C++ cast");
02000 return t;
02001 }
02002
02003 #line 1832 "expr_annotator.cpp"
02004
02006 Expr_result
02007 Expr_annotator::get_cast_expr(Ptree* tree)
02008 {
02009 if (tree->IsLeaf() || tree->Length() != 1)
02010 compile_error("C++ cast expects exactly one argument");
02011 return visit(tree->First());
02012 }
02013
02014 #line 1841 "expr_annotator.cpp"
02015
02017 bool
02018 Expr_annotator::try_static_cast(Type target, bool enforce_const, Expr_result* res)
02019 {
02020
02021 if (target.is_void()) {
02022 res->convert_to(void_type);
02023 return true;
02024 }
02025
02026
02027 if (res->get_type().is_void())
02028 return false;
02029
02030
02031 if (Implicit_conversion* ics = generate_implicit_conversion(*res, target, 0,
02032 true, false, false))
02033 {
02034 *res = ics->make_tree(*res);
02035 return true;
02036 }
02037
02038
02039 if (!res->is_value())
02040 return false;
02041
02042
02043 if (target.is_enum_type() && (res->get_type().is_int() || res->get_type().is_enum_type())) {
02044 res->convert_to(target);
02045 return true;
02046 }
02047
02048
02049 if (target.get_kind() == Type::k_Pointer && res->get_type().get_kind() == Type::k_Pointer) {
02050 Type hbase = res->get_type().get_basis_type();
02051 Type wbase = target.get_basis_type();
02052 if (enforce_const && hbase.is_more_qualified_than(wbase))
02053 return false;
02054 if (hbase.get_kind() == Type::k_Function || wbase.get_kind() == Type::k_Function)
02055 return false;
02056 if (hbase.is_void() || wbase.is_void()
02057 || (hbase.is_class_type() && wbase.is_class_type()
02058 && downcast<Class_symbol*>(hbase.get_type_symbol())->is_unique_base_class_of(dynamic_cast<Class_symbol*>(wbase.get_type_symbol()))))
02059 {
02060 res->convert_to(target);
02061 return true;
02062 }
02063 }
02064
02065
02066 if (target.get_kind() == Type::k_Member && res->get_type().get_kind() == Type::k_Member) {
02067 Type hclass = res->get_type().get_class_type();
02068 Type wclass = target.get_class_type();
02069 if (enforce_const && hclass.is_more_qualified_than(wclass))
02070 return false;
02071 if (res->get_type().get_member_type().is_same_unqualified_type(target.get_member_type())
02072 && downcast<Class_symbol*>(wclass.get_type_symbol())->is_unique_base_class_of(downcast<Class_symbol*>(hclass.get_type_symbol())))
02073 {
02074 res->convert_to(target);
02075 return true;
02076 }
02077 }
02078
02079 return false;
02080 }
02081
02082 #line 1907 "expr_annotator.cpp"
02083
02085 bool
02086 try_reinterpret_cast(Type t, bool enforce_const, Expr_result* res)
02087 {
02088 Type ht = res->get_type();
02089 if (!ht.is_valid())
02090 return false;
02091
02092 if (t.get_kind() == Type::k_Pointer) {
02093
02094 if (ht.is_int() || ht.is_enum_type()) {
02095 res->convert_to(t);
02096 return true;
02097 }
02098
02099 if (t.get_basis_type().get_kind() == Type::k_Function) {
02100
02101 if (ht.get_kind() == Type::k_Function ||
02102 (ht.get_kind() == Type::k_Pointer &&
02103 ht.get_basis_type().get_kind() == Type::k_Function))
02104 {
02105 res->convert_to_pointer();
02106 res->convert_to(t);
02107 return true;
02108 }
02109 } else {
02110 if (ht.get_kind() == Type::k_Array ||
02111 (ht.get_kind() == Type::k_Pointer &&
02112 ht.get_basis_type().get_kind() != Type::k_Function))
02113 {
02114
02115 res->convert_to_fpointer();
02116 res->convert_to(t);
02117 return true;
02118 }
02119 }
02120 } else if (t.get_kind() == Type::k_Reference) {
02121
02122 if (!res->is_lvalue())
02123 return false;
02124 if (enforce_const && ht.is_more_qualified_than(t.get_basis_type()))
02125 return false;
02126 res->convert_to(t);
02127 return true;
02128 } else if (t.is_int() && ht.get_kind() == Type::k_Pointer) {
02129
02130 res->convert_to(t);
02131 return true;
02132 } else if (t.get_kind() == Type::k_Member && ht.get_kind() == Type::k_Member) {
02133
02134 if (enforce_const && ht.get_class_type().is_more_qualified_than(t.get_class_type()))
02135 return false;
02136 if ((ht.get_member_type().get_kind() == Type::k_Function)
02137 + (t.get_member_type().get_kind() == Type::k_Function) == 1)
02138 return false;
02139 res->convert_to(t);
02140 return true;
02141 }
02142 return false;
02143 }
02144
02145 #line 1968 "expr_annotator.cpp"
02146
02147
02148
02149 Expr_result
02150 Expr_annotator::do_dynamic_cast(Ptree* type_args, Ptree* cast_args)
02151 {
02152 Type t = get_cast_type(type_args);
02153 Expr_result arg = get_cast_expr(cast_args);
02154 compile_error("dynamic_cast geht noch nicht");
02155 }
02156
02157 #line 1978 "expr_annotator.cpp"
02158
02159 Expr_result
02160 Expr_annotator::do_reinterpret_cast(Ptree* type_args, Ptree* cast_args)
02161 {
02162 Type t = get_cast_type(type_args);
02163 Expr_result arg = get_cast_expr(cast_args);
02164 if (!try_reinterpret_cast(t, true, &arg))
02165 compile_error("invalid reinterpret_cast");
02166 return arg;
02167 }
02168
02169 #line 1988 "expr_annotator.cpp"
02170
02171 Expr_result
02172 Expr_annotator::do_static_cast(Ptree* type_args, Ptree* cast_args)
02173 {
02174 Type t = get_cast_type(type_args);
02175 Expr_result arg = get_cast_expr(cast_args);
02176 if (!try_static_cast(t, true, &arg))
02177 compile_error("invalid static_cast");
02178 return arg;
02179 }
02180
02181 #line 1998 "expr_annotator.cpp"
02182
02183 Expr_result
02184 Expr_annotator::do_const_cast(Ptree* type_args, Ptree* cast_args)
02185 {
02186 Type t = get_cast_type(type_args);
02187 Expr_result arg = get_cast_expr(cast_args);
02188 compile_error("const_cast geht noch nicht");
02189 }
02190
02191 #line 2006 "expr_annotator.cpp"
02192
02196 Expr_result
02197 Expr_annotator::do_ccast(Type t, Ptree* arg)
02198 {
02199 Expr_result res = visit(arg);
02200 if (try_static_cast(t, false, &res) || try_reinterpret_cast(t, false, &res))
02201 return res;
02202 else
02203 compile_error("can't do this type conversion");
02204 }
02205
02206 #line 2019 "expr_annotator.cpp"
02207
02211 Expr_result
02212 Expr_annotator::do_fstyle_cast(Type target, Ptree* arglist)
02213 {
02214 if (arglist && arglist->Length() == 1) {
02215
02216
02217
02218 return do_ccast(target, arglist->Car());
02219 } else {
02220 Expr_result res;
02221 res.set_value(Init_handler(current_scope, 0, false).make_constructor_call(target, arglist), target);
02222 return res;
02223 }
02224 }
02225
02226 #line 2037 "expr_annotator.cpp"
02227
02228 void
02229 Expr_annotator::add_parms_from_arglist(Overload_resolver* resolver, Ptree* p)
02230 {
02231
02232 if (!p)
02233 return;
02234
02235 while (p) {
02236 resolver->add_arg(visit(p->Car()));
02237 p = p->Cdr();
02238 if (!p)
02239 break;
02240 expect_ptree(p->Car(), ',');
02241 p = p->Cdr();
02242 }
02243 }
02244
02245 #line 2054 "expr_annotator.cpp"
02246
02251 Expr_result
02252 Expr_annotator::do_funcall(Expr_result lhs, Ptree* arglist)
02253 {
02254 if (lhs.is_value()) {
02255
02256
02257 if (lhs.get_type().is_class_type()) {
02258
02259 Class_symbol* csym = downcast<Class_symbol*>(lhs.get_type().get_type_symbol());
02260
02261 Overload_resolver resolver(true);
02262 resolver.add_arg(lhs);
02263 add_parms_from_arglist(&resolver, arglist);
02264
02265 if (Function_symbol* fsym = dynamic_cast<Function_symbol*>(csym->get_scope()->lookup_here(Symbol_name::CALL_OPERATOR_NAME, false).untag))
02266 resolver.add_function(fsym, true);
02267
02268
02269 {
02270 Conversion_op_map op_map;
02271 enumerate_conversion_ops(lhs, &op_map);
02272 for (Conversion_op_map::iterator i = op_map.begin(); i != op_map.end(); ++i) {
02273 Type cvt_target = i->first->get_return_type().sans_reference();
02274 if (cvt_target.get_kind() == Type::k_Pointer)
02275 cvt_target = cvt_target.get_basis_type();
02276 if (cvt_target.get_kind() == Type::k_Function) {
02277
02278
02279
02280 resolver.add_signature(Function_signature::make_builtin(cvt_target.with_first_arg(cvt_target.make_pointer_type())));
02281 }
02282 }
02283 }
02284
02285 bool is_ambig;
02286 Overload_candidate* best = resolver.get_best(&is_ambig);
02287 if (is_ambig)
02288 compile_error("call to object is ambiguous");
02289 if (!best)
02290 compile_error("no matching function call operator");
02291
02292 if (best->fsig->is_builtin()) {
02293
02294
02295 assert(resolver.get_arg(0).get_type().get_kind() == Type::k_Pointer);
02296 assert(resolver.get_arg(0).get_type().get_basis_type().get_kind() == Type::k_Function);
02297
02298 Annotated_funcall_maker afm(resolver.get_arg(0).get_tree(),
02299 best->fsig->get_return_type());
02300 for (unsigned i = 1; i < resolver.get_arg_count(); ++i)
02301 afm.add_arg(resolver.get_arg(i).get_tree());
02302
02303 Expr_result ex;
02304 ex.set_value(afm.make_funcall(), best->fsig->get_return_type());
02305 return ex;
02306 } else {
02307
02308 Annotated_funcall_maker afm(make_name(best->fsig),
02309 best->fsig->get_return_type());
02310 for (unsigned i = 0; i < resolver.get_arg_count(); ++i)
02311 afm.add_arg(resolver.get_arg(i).get_tree());
02312
02313 Expr_result ex;
02314 ex.set_value(afm.make_funcall(), best->fsig->get_return_type());
02315 return ex;
02316 }
02317 }
02318
02319
02320 Type ftype = lhs.get_type();
02321 if (ftype.get_kind() == Type::k_Pointer)
02322 ftype = ftype.get_basis_type();
02323 if (ftype.get_kind() != Type::k_Function)
02324 compile_error("cannot call object of type `" + lhs.get_type().get_human_readable_type() + "'");
02325
02326 Overload_resolver resolver(false);
02327 add_parms_from_arglist(&resolver, arglist);
02328 Function_signature tmp_fsig(ftype, Type(), ftype, s_Static, 0, 0);
02329 resolver.add_signature(&tmp_fsig);
02330
02331
02332 if (!resolver.get_best(0))
02333 compile_error("invalid argument list for call to function pointer");
02334
02335
02336 Annotated_funcall_maker afm(lhs.get_tree(), ftype.get_return_type());
02337 for (unsigned i = 0; i < resolver.get_arg_count(); ++i)
02338 afm.add_arg(resolver.get_arg(i).get_tree());
02339
02340 Expr_result ex;
02341 ex.set_value(afm.make_funcall(), ftype.get_return_type());
02342 return ex;
02343 } else if (lhs.is_bound_pmf()) {
02344
02345
02346
02347
02348
02349
02350
02351 Overload_resolver resolver(false);
02352 Annotation* oanno = downcast<Annotation*>(lhs.get_object());
02353
02354 resolver.set_object(Expr_result(lhs.get_object(),
02355 oanno->get_type(),
02356 Expr_result::k_RValue));
02357 add_parms_from_arglist(&resolver, arglist);
02358
02359 Function_signature tmp_fsig(lhs.get_type(), oanno->get_type(),
02360 lhs.get_type(), s_Member, 0, 0);
02361 resolver.add_signature(&tmp_fsig);
02362
02363
02364 if (!resolver.get_best(0))
02365 compile_error("invalid parameter list for call to bound member function");
02366
02367
02368 Annotated_funcall_maker afm(lhs.get_tree(), lhs.get_type().get_return_type());
02369 for (unsigned i = 0; i < resolver.get_arg_count(); ++i)
02370 afm.add_arg(resolver.get_arg(i).get_tree());
02371
02372 Expr_result ex;
02373 ex.set_value(afm.make_funcall(), lhs.get_type().get_return_type());
02374 return ex;
02375 } else if (lhs.is_function() || lhs.is_bound_member()) {
02376 bool invented_this_ptr = false;
02377 Overload_resolver resolver(false);
02378 if (lhs.is_bound_member()) {
02379
02380 Annotation* oanno = downcast<Annotation*>(lhs.get_object());
02381 resolver.set_object(Expr_result(lhs.get_object(),
02382 oanno->get_type(),
02383 Expr_result::k_RValue));
02384 } else {
02385
02386
02387 Type t = current_scope->get_this_type();
02388 if (t.is_valid()) {
02389 resolver.set_object(Expr_result(make_this_star(t), Expr_result::k_LValue));
02390 invented_this_ptr = true;
02391 }
02392 }
02393 add_parms_from_arglist(&resolver, arglist);
02394 assert(lhs.get_function());
02395 resolver.add_function(lhs.get_function(), true);
02396
02397 bool is_ambig;
02398 Overload_candidate* best = resolver.get_best(&is_ambig);
02399 if (is_ambig)
02400 compile_error("call to `" + lhs.get_function()->get_name() + "' is ambiguous");
02401 if (!best)
02402 compile_error("no matching function for call to `" + lhs.get_function()->get_name() + "'");
02403
02404
02405 fill_in_overload_annotation(lhs.get_tree(), best->fsig, best->fsig->get_call_type());
02406 Annotated_funcall_maker afm(lhs.get_tree(), best->fsig->get_return_type());
02407 if (best->fsig->get_storage_specifier() == s_Member) {
02408 assert(resolver.is_object());
02409 for (unsigned i = 0; i < resolver.get_arg_count(); ++i)
02410 afm.add_arg(resolver.get_arg(i).get_tree());
02411
02412 Expr_result ex;
02413 ex.set_value(afm.make_funcall(), best->fsig->get_return_type());
02414 return ex;
02415 } else if (resolver.is_object()) {
02416 for (unsigned i = 1; i < resolver.get_arg_count(); ++i)
02417 afm.add_arg(resolver.get_arg(i).get_tree());
02418
02419 Expr_result ex;
02420 if (invented_this_ptr) {
02421
02422
02423 ex.set_value(afm.make_funcall(), best->fsig->get_return_type());
02424 } else {
02425 compile_warning("calling a static function with object", arglist);
02426 ex.set_value(make_comma_expr(resolver.get_arg(0).get_tree(),
02427 afm.make_funcall()),
02428 best->fsig->get_return_type());
02429 }
02430 return ex;
02431 } else {
02432 for (unsigned i = 0; i < resolver.get_arg_count(); ++i)
02433 afm.add_arg(resolver.get_arg(i).get_tree());
02434
02435 Expr_result ex;
02436 ex.set_value(afm.make_funcall(), best->fsig->get_return_type());
02437 return ex;
02438 }
02439 } else
02440 assert(0);
02441 }
02442
02443 #line 2250 "expr_annotator.cpp"
02444
02446 Expr_result
02447 Expr_annotator::visit_fstylecastexpr(PtreeFstyleCastExpr* e)
02448 {
02449
02450 expect_ptree(e->Second(), '(');
02451 expect_ptree(e->Nth(3), ')');
02452 return do_fstyle_cast(parse_type(e->First(), current_scope, 0, false),
02453 e->Third());
02454 }
02455
02456 #line 2261 "expr_annotator.cpp"
02457
02459 Expr_result
02460 Expr_annotator::visit_funcall(PtreeFuncallExpr* e)
02461 {
02462
02463
02464
02465
02466
02467
02468
02469
02470
02471
02472
02473
02474
02475 expect_ptree(e->Second(), '(');
02476 expect_ptree(e->Nth(3), ')');
02477 Ptree* arglist = e->Third();
02478
02479 if (ptree_is_name(e->First())) {
02480
02481 if (!e->First()->IsLeaf()) {
02482 Ptree* first_name = e->First()->First();
02483 if (first_name->Eq("dynamic_cast"))
02484 return do_dynamic_cast(e->First()->Second(), arglist);
02485 if (first_name->Eq("reinterpret_cast"))
02486 return do_reinterpret_cast(e->First()->Second(), arglist);
02487 if (first_name->Eq("const_cast"))
02488 return do_const_cast(e->First()->Second(), arglist);
02489 if (first_name->Eq("static_cast"))
02490 return do_static_cast(e->First()->Second(), arglist);
02491 }
02492
02493
02494 Symbol_name sname(e->First(), current_scope, false);
02495 Symbol* sym = sname.lookup_for_use(false).untag;
02496
02497 if (!sym) {
02498 compile_error("unknown symbol `" + std::string(e->First()->ToString()) + "' in function call");
02499 } else if (Typedef_symbol* tdsym = dynamic_cast<Typedef_symbol*>(sym)) {
02500 return do_fstyle_cast(tdsym->get_type(), arglist);
02501 } else if (Type_symbol* tysym = dynamic_cast<Type_symbol*>(sym)) {
02502 return do_fstyle_cast(tysym->get_type(), arglist);
02503 } else if (Function_symbol* fsym = dynamic_cast<Function_symbol*>(sym)) {
02504
02505 return do_funcall(visit(e->First()), arglist);
02506 } else {
02507 return do_funcall(visit(e->First()), arglist);
02508 }
02509 } else
02510 return do_funcall(visit(e->First()), arglist);
02511 }
02512
02513 #line 2316 "expr_annotator.cpp"
02514
02516 Expr_result
02517 Expr_annotator::visit_cast(PtreeCastExpr* p)
02518 {
02519 expect_ptree(p->First(), '(');
02520 expect_ptree(p->Third(), ')');
02521
02522 Ptree* typ = p->Second();
02523 Ptree* arg = p->Nth(3);
02524 Type t;
02525
02526 try {
02527 t = parse_type(typ->First(), current_scope, 0, false);
02528 if (!parse_abstract_declarator(typ->Second(), current_scope, &t))
02529 compile_error("you'll never see this");
02530 }
02531 catch(...) {
02532 if (dynamic_cast<PtreeParenExpr*>(arg)) {
02533
02534 return do_funcall(visit(convert_decl_to_expr(typ->First(),
02535 typ->Second())),
02536 convert_comma_expr_to_list(arg->Second()));
02537 } else if (dynamic_cast<PtreeUnaryExpr*>(arg)) {
02538
02539 Ptree* oper = arg->First();
02540 if (oper->Eq('&') || oper->Eq('+') || oper->Eq('-') || oper->Eq('*')) {
02541 compile_warning("expression of style `(expr) " +
02542 std::string(oper->ToString()) +
02543 " expr' might be translated wrong", oper);
02544 return visit_infix(new PtreeInfixExpr(convert_decl_to_expr(typ->First(),
02545 typ->Second()),
02546 arg));
02547 }
02548 }
02549 compile_error("invalid C-style cast");
02550 }
02551
02552 return do_ccast(t, arg);
02553 }
02554
02555 #line 2356 "expr_annotator.cpp"
02556
02557
02558 Expr_result
02559 Expr_annotator::visit_brace(PtreeBrace* b)
02560 {
02561 compile_error("brace expression not valid here");
02562 }
02563
02564 #line 2363 "expr_annotator.cpp"
02565
02566
02567
02570 Type
02571 binary_multeq_predicate(Type l, Type r)
02572 {
02573 if (l.get_kind() != Type::k_Reference)
02574 return Type();
02575
02576 r = r.sans_reference();
02577 if (l.get_basis_type().is_arithmetic_type()) {
02578 if (r.is_int() || r.is_enum_type())
02579 return make_binary_function_type(l, r.get_promoted_integer(), l);
02580 else if (r.is_same_unqualified_type(float_type))
02581 return make_binary_function_type(l, double_type, l);
02582 else if (r.is_float())
02583 return make_binary_function_type(l, r, l);
02584 }
02585 return Type();
02586 }
02587
02588 #line 2385 "expr_annotator.cpp"
02589
02593 Type
02594 binary_addeq_predicate(Type l, Type r)
02595 {
02596 if (l.get_kind() != Type::k_Reference)
02597 return Type();
02598
02599 r = r.sans_reference();
02600 if (l.get_basis_type().is_arithmetic_type()) {
02601 if (r.is_int() || r.is_enum_type())
02602 return make_binary_function_type(l, r.get_promoted_integer(), l);
02603 } else if (l.get_basis_type().get_kind() == Type::k_Pointer
02604 && l.get_basis_type().get_basis_type().is_object_type()) {
02605
02606
02607 return make_binary_function_type(l, ptrdiff_type, l);
02608 }
02609 return Type();
02610 }
02611
02612 #line 2407 "expr_annotator.cpp"
02613
02616 Type
02617 binary_inteq_predicate(Type l, Type r)
02618 {
02619
02620
02621
02622
02623
02624
02625
02626 r = r.sans_reference();
02627 if (l.get_kind() == Type::k_Reference && l.get_basis_type().is_int()
02628 && (r.is_int() || r.is_enum_type()))
02629 return make_binary_function_type(l, r.get_promoted_integer(), l);
02630 else
02631 return Type();
02632 }
02633
02634 #line 2427 "expr_annotator.cpp"
02635
02637 Expr_result
02638 Expr_annotator::visit_assign(PtreeAssignExpr* p)
02639 {
02640 Expr_result lhs = visit(p->First());
02641 Expr_result rhs = visit(p->Third());
02642
02643 if (p->Second()->Eq('=')) {
02644
02645
02646
02647
02648
02649
02650
02651
02652
02653
02654
02655 if (lhs.get_type().is_class_type()) {
02656 Class_symbol* csym = downcast<Class_symbol*>(lhs.get_type().get_type_symbol());
02657 Function_symbol* fsym = dynamic_cast<Function_symbol*>(csym->lookup_helper(Symbol_name::ASSIGNMENT_OPERATOR_NAME).untag);
02658 if (!fsym)
02659 compile_error("unable to assign to an object of type `" + csym->get_name() + "'");
02660
02661 Overload_resolver resolver(false);
02662 resolver.set_object(lhs);
02663 resolver.add_arg(rhs);
02664 resolver.add_function(fsym, true);
02665
02666 bool is_ambig;
02667 Overload_candidate* cand = resolver.get_best(&is_ambig);
02668 if (is_ambig)
02669 compile_error("assignment to `" + csym->get_name() + "' is ambiguous");
02670 if (!cand)
02671 compile_error("no matching `operator=' in assignment to `" + csym->get_name() + "'");
02672
02673 Annotated_funcall_maker afm(make_name(cand->fsig),
02674 cand->fsig->get_return_type());
02675 afm.add_arg(resolver.get_arg(0).get_tree());
02676 afm.add_arg(resolver.get_arg(1).get_tree());
02677
02678 Expr_result res;
02679 res.set_value(afm.make_funcall(), cand->fsig->get_return_type());
02680 return res;
02681 }
02682
02683
02684 if (!lhs.is_modifyable_lvalue() || !lhs.is_value() || lhs.get_type().is_void())
02685 compile_error("LHS of assignment is not modifyable lvalue");
02686
02687 Implicit_conversion* ics = generate_implicit_conversion(rhs,
02688 lhs.get_type(),
02689 0,
02690 true ,
02691 true ,
02692 false );
02693 if (!ics)
02694 compile_error("unable to convert `" + rhs.get_type().get_human_readable_type()
02695 + "' into `" + lhs.get_type().get_human_readable_type() + "'");
02696
02697 return Expr_result(new Annotated<PtreeAssignExpr>(lhs.get_type(),
02698 0,
02699 lhs.get_tree(),
02700 Ptree::List(p->Second(),
02701 ics->make_tree(rhs).get_tree())),
02702 Expr_result::k_LValue);
02703 }
02704
02705
02706
02707
02708
02709
02710
02711
02712
02713 Ptree* oper_tree = p->Second();
02714 if (lhs.get_type().get_kind() == Type::k_Userdef || rhs.get_type().get_kind() == Type::k_Userdef) {
02715
02716 const char* operator_name = Symbol_name::get_operator_name(oper_tree);
02717
02718
02719 Overload_resolver resolver(true);
02720 resolver.add_arg(lhs);
02721 resolver.add_arg(rhs);
02722
02723
02724 Class_symbol* csym;
02725 if (lhs.get_type().get_kind() == Type::k_Userdef && (csym = dynamic_cast<Class_symbol*>(lhs.get_type().get_type_symbol()))) {
02726 Function_symbol* fsym = dynamic_cast<Function_symbol*>(csym->get_scope()->lookup_here(operator_name, false).untag);
02727 if (fsym)
02728 resolver.add_function(fsym, false);
02729 }
02730
02731
02732 if (Function_symbol* fsym = dynamic_cast<Function_symbol*>(current_scope->lookup_unqualified(operator_name).untag))
02733 resolver.add_function(fsym, false);
02734
02735 if (oper_tree->Eq("*=") || oper_tree->Eq("/="))
02736 resolver.add_builtin_binary_ops(binary_multeq_predicate);
02737 else if (oper_tree->Eq("+=") || oper_tree->Eq("-="))
02738 resolver.add_builtin_binary_ops(binary_addeq_predicate);
02739 else if (oper_tree->Eq("%=") || oper_tree->Eq("<<=") || oper_tree->Eq(">>=")
02740 || oper_tree->Eq("&=") || oper_tree->Eq("^=") || oper_tree->Eq("|="))
02741 resolver.add_builtin_binary_ops(binary_inteq_predicate);
02742 else
02743 bogus_ptree_error("huh? strange operator", oper_tree);
02744
02745 bool is_ambig;
02746 Overload_candidate* cand = resolver.get_best(&is_ambig);
02747 if (is_ambig)
02748 compile_error("call to operator `" + std::string(oper_tree->ToString()) + "' is ambiguous");
02749 if (cand) {
02750 if (!cand->fsig->is_builtin()) {
02751
02752 Annotated_funcall_maker afm(make_name(cand->fsig), cand->fsig->get_return_type());
02753 afm.add_arg(resolver.get_arg(0).get_tree());
02754 afm.add_arg(resolver.get_arg(1).get_tree());
02755
02756 Expr_result res;
02757 res.set_value(afm.make_funcall(), cand->fsig->get_return_type());
02758 return res;
02759 }
02760
02761 lhs = resolver.get_arg(0);
02762 rhs = resolver.get_arg(1);
02763 }
02764 }
02765
02766
02767 if (!lhs.is_modifyable_lvalue())
02768 compile_error("need modifyable lvalue on the lhs of a compound assignment");
02769 if (!lhs.is_value() || !rhs.is_value())
02770 compile_error("invalid operand to compound assignment");
02771
02772 if (oper_tree->Eq("*=") || oper_tree->Eq("/=")) {
02773
02774 if (rhs.get_type().is_enum_type())
02775 rhs.convert_to(rhs.get_type().get_promoted_integer());
02776 if (lhs.get_type().is_arithmetic_type() && rhs.get_type().is_arithmetic_type())
02777 return Expr_result(new Annotated<PtreeAssignExpr>(lhs.get_type(), 0,
02778 lhs.get_tree(),
02779 Ptree::List(oper_tree,
02780 rhs.get_tree())),
02781 Expr_result::k_LValue);
02782 compile_error("invalid operand to *= or /=");
02783 } else if (oper_tree->Eq("+=") || oper_tree->Eq("-=")) {
02784
02785 if (lhs.get_type().get_kind() == Type::k_Pointer) {
02786 check_ptr_arith(lhs.get_type());
02787 if (rhs.get_type().is_int() || rhs.get_type().is_enum_type())
02788 rhs.convert_to(ptrdiff_type);
02789 else
02790 compile_error("need integer on rhs of pointer += / -=");
02791 } else if (lhs.get_type().is_arithmetic_type()) {
02792 if (rhs.get_type().is_arithmetic_type())
02793 ;
02794 else if (rhs.get_type().is_enum_type())
02795 rhs.convert_to(rhs.get_type().get_promoted_integer());
02796 else
02797 compile_error("invalid operand to += or -=");
02798 } else
02799 compile_error("invalid left operand to += or -=");
02800 return Expr_result(new Annotated<PtreeAssignExpr>(lhs.get_type(), 0,
02801 lhs.get_tree(),
02802 Ptree::List(oper_tree,
02803 rhs.get_tree())),
02804 Expr_result::k_LValue);
02805 } else if (oper_tree->Eq("%=") || oper_tree->Eq("<<=") || oper_tree->Eq(">>=")
02806 || oper_tree->Eq("&=") || oper_tree->Eq("^=") || oper_tree->Eq("|="))
02807 {
02808
02809 if (!lhs.get_type().is_int())
02810 compile_error(std::string(oper_tree->ToString()) + " needs integer on the lhs");
02811 if (rhs.get_type().is_enum_type())
02812 rhs.convert_to(rhs.get_type().get_promoted_integer());
02813 else if (rhs.get_type().is_int())
02814 ;
02815 else
02816 compile_error(std::string(oper_tree->ToString()) + " needs integer or enum on the rhs");
02817 return Expr_result(new Annotated<PtreeAssignExpr>(lhs.get_type(), 0,
02818 lhs.get_tree(),
02819 Ptree::List(oper_tree,
02820 rhs.get_tree())),
02821 Expr_result::k_LValue);
02822 } else
02823 bogus_ptree_error("strange operator", oper_tree);
02824 }
02825
02826 #line 2617 "expr_annotator.cpp"
02827
02828
02829
02832 static Type
02833 index_predicate(Type l, Type r)
02834 {
02835 l = l.sans_reference();
02836 r = r.sans_reference();
02837 if (l.get_kind() == Type::k_Pointer || l.get_kind() == Type::k_Array) {
02838 if (r.is_arithmetic_type() || r.is_enum_type())
02839 return make_binary_function_type(l.get_basis_type().make_pointer_type(),
02840 ptrdiff_type,
02841 void_type );
02842 } else if (r.get_kind() == Type::k_Pointer || r.get_kind() == Type::k_Array) {
02843 if (l.is_arithmetic_type() || l.is_enum_type())
02844 return make_binary_function_type(ptrdiff_type,
02845 l.get_basis_type().make_pointer_type(),
02846 void_type );
02847 }
02848 return Type();
02849 }
02850
02851 #line 2640 "expr_annotator.cpp"
02852
02853 Expr_result
02854 Expr_annotator::visit_array(PtreeArrayExpr* p)
02855 {
02856 expect_ptree(p->Second(), '[');
02857 expect_ptree(p->Nth(3), ']');
02858
02859 Expr_result lhs = visit(p->First());
02860 Expr_result rhs = visit(p->Third());
02861
02862 if (lhs.get_type().get_kind() == Type::k_Userdef || rhs.get_type().get_kind() == Type::k_Userdef) {
02863 Overload_resolver resolver(true);
02864 resolver.add_arg(lhs);
02865 resolver.add_arg(rhs);
02866
02867
02868 Class_symbol* csym;
02869 if (lhs.get_type().get_kind() == Type::k_Userdef && (csym = dynamic_cast<Class_symbol*>(lhs.get_type().get_type_symbol()))) {
02870 Function_symbol* fsym = dynamic_cast<Function_symbol*>(csym->get_scope()->lookup_here(Symbol_name::INDEX_OPERATOR_NAME, false).untag);
02871 if (fsym)
02872 resolver.add_function(fsym, false);
02873 }
02874
02875
02876 if (Function_symbol* fsym = dynamic_cast<Function_symbol*>(current_scope->lookup_unqualified(Symbol_name::INDEX_OPERATOR_NAME).untag))
02877 resolver.add_function(fsym, false);
02878
02879 resolver.add_builtin_binary_ops(index_predicate);
02880
02881 bool is_ambig;
02882 Overload_candidate* cand = resolver.get_best(&is_ambig);
02883 if (is_ambig)
02884 compile_error("call to operator[] is ambiguous");
02885 if (cand) {
02886 if (!cand->fsig->is_builtin()) {
02887 Annotated_funcall_maker afm(make_name(cand->fsig),
02888 cand->fsig->get_return_type());
02889 afm.add_arg(resolver.get_arg(0).get_tree());
02890 afm.add_arg(resolver.get_arg(1).get_tree());
02891
02892 Expr_result res;
02893 res.set_value(afm.make_funcall(), cand->fsig->get_return_type());
02894 return res;
02895 }
02896
02897 lhs = resolver.get_arg(0);
02898 rhs = resolver.get_arg(1);
02899 }
02900 }
02901
02902
02903 lhs.do_std_conversions();
02904 rhs.do_std_conversions();
02905
02906 if (lhs.get_type().get_kind() == Type::k_Pointer) {
02907 check_ptr_arith(lhs.get_type());
02908 if (rhs.get_type().is_int() || rhs.get_type().is_enum_type())
02909 rhs.convert_to(ptrdiff_type);
02910 else
02911 compile_error("invalid type `" + rhs.get_type().get_human_readable_type() + "' for array subscript");
02912
02913 return Expr_result(new Annotated<PtreeArrayExpr>(lhs.get_type().get_basis_type(), 0,
02914 lhs.get_tree(),
02915 Ptree::List(p->Second(),
02916 rhs.get_tree(),
02917 p->Nth(3))),
02918 Expr_result::k_LValue);
02919 } else if (rhs.get_type().get_kind() == Type::k_Pointer) {
02920 check_ptr_arith(rhs.get_type());
02921 if (lhs.get_type().is_int() || lhs.get_type().is_enum_type())
02922 lhs.convert_to(ptrdiff_type);
02923 else
02924 compile_error("invalid type `" + lhs.get_type().get_human_readable_type() + "' for array subscript");
02925
02926 compile_warning("swapping sides of array subscript expression", p);
02927 return Expr_result(new Annotated<PtreeArrayExpr>(rhs.get_type().get_basis_type(), 0,
02928 rhs.get_tree(),
02929 Ptree::List(p->Second(),
02930 lhs.get_tree(),
02931 p->Nth(3))),
02932 Expr_result::k_LValue);
02933 }
02934 compile_error("invalid types for array subscript");
02935 }