void c_typecheck_baset::typecheck_switch_case(code_switch_caset &code) { if(code.operands().size()!=2) { err_location(code); error() << "switch_case expected to have two operands" << eom; throw 0; } typecheck_code(code.code()); if(code.is_default()) { if(!case_is_allowed) { err_location(code); error() << "did not expect default label here" << eom; throw 0; } } else { if(!case_is_allowed) { err_location(code); error() << "did not expect `case' here" << eom; throw 0; } exprt &case_expr=code.case_op(); typecheck_expr(case_expr); implicit_typecast(case_expr, switch_op_type); } }
void cpp_typecheckt::typecheck_enum_body(symbolt &enum_symbol) { c_enum_typet &c_enum_type=to_c_enum_type(enum_symbol.type); exprt &body=static_cast<exprt &>(c_enum_type.add(ID_body)); irept::subt &components=body.get_sub(); c_enum_tag_typet enum_tag_type(enum_symbol.name); mp_integer i=0; Forall_irep(it, components) { const irep_idt &name=it->get(ID_name); if(it->find(ID_value).is_not_nil()) { exprt &value=static_cast<exprt &>(it->add(ID_value)); typecheck_expr(value); implicit_typecast(value, c_enum_type.subtype()); make_constant(value); if(to_integer(value, i)) throw "failed to produce integer for enum constant"; } exprt value_expr=from_integer(i, c_enum_type.subtype()); value_expr.type()=enum_tag_type; // override type symbolt symbol; symbol.name=id2string(enum_symbol.name)+"::"+id2string(name); symbol.base_name=name; symbol.value=value_expr; symbol.location=static_cast<const source_locationt &>(it->find(ID_C_source_location)); symbol.mode=ID_cpp; symbol.module=module; symbol.type=enum_tag_type; symbol.is_type=false; symbol.is_macro=true; symbolt *new_symbol; if(symbol_table.move(symbol, new_symbol)) throw "cpp_typecheckt::typecheck_enum_body: symbol_table.move() failed"; cpp_idt &scope_identifier= cpp_scopes.put_into_scope(*new_symbol); scope_identifier.id_class=cpp_idt::SYMBOL; ++i; } }
void c_typecheck_baset::typecheck_gcc_switch_case_range(codet &code) { if(code.operands().size()!=3) { err_location(code); error() << "gcc_switch_case_range expected to have three operands" << eom; throw 0; } typecheck_code(to_code(code.op2())); if(!case_is_allowed) { err_location(code); error() << "did not expect `case' here" << eom; throw 0; } typecheck_expr(code.op0()); typecheck_expr(code.op1()); implicit_typecast(code.op0(), switch_op_type); implicit_typecast(code.op1(), switch_op_type); }
void c_typecheck_baset::typecheck_assign(codet &code) { if(code.operands().size()!=2) { err_location(code); error() << "assignment statement expected to have two operands" << eom; throw 0; } typecheck_expr(code.op0()); typecheck_expr(code.op1()); implicit_typecast(code.op1(), code.op0().type()); }
void c_typecheck_baset::typecheck_return(codet &code) { if(code.operands().empty()) { if(follow(return_type).id()!=ID_empty) { // gcc doesn't actually complain, it just warns! // We'll put a zero here, which is dubious. exprt zero=zero_initializer(return_type, code.source_location(), *this, get_message_handler()); code.copy_to_operands(zero); } } else if(code.operands().size()==1) { typecheck_expr(code.op0()); if(follow(return_type).id()==ID_empty) { // gcc doesn't actually complain, it just warns! if(follow(code.op0().type()).id()!=ID_empty) { warning().source_location=code.source_location(); warning() << "function has return void "; warning() << "but a return statement returning "; warning() << to_string(follow(code.op0().type())); warning() << eom; code.op0().make_typecast(return_type); } } else implicit_typecast(code.op0(), return_type); } else { err_location(code); error() << "return expected to have 0 or 1 operands" << eom; throw 0; } }
exprt c_typecheck_baset::do_initializer_rec( const exprt &value, const typet &type, bool force_constant) { const typet &full_type=follow(type); if(full_type.id()==ID_incomplete_struct) { err_location(value); error() << "type `" << to_string(full_type) << "' is still incomplete -- cannot initialize" << eom; throw 0; } if(value.id()==ID_initializer_list) return do_initializer_list(value, type, force_constant); if(value.id()==ID_array && value.get_bool(ID_C_string_constant) && full_type.id()==ID_array && (full_type.subtype().id()==ID_signedbv || full_type.subtype().id()==ID_unsignedbv) && full_type.subtype().get(ID_width)==value.type().subtype().get(ID_width)) { exprt tmp=value; // adjust char type tmp.type().subtype()=full_type.subtype(); Forall_operands(it, tmp) it->type()=full_type.subtype(); if(full_type.id()==ID_array && to_array_type(full_type).is_complete()) { // check size mp_integer array_size; if(to_integer(to_array_type(full_type).size(), array_size)) { err_location(value); error() << "array size needs to be constant, got " << to_string(to_array_type(full_type).size()) << eom; throw 0; } if(array_size<0) { err_location(value); error() << "array size must not be negative" << eom; throw 0; } if(mp_integer(tmp.operands().size())>array_size) { // cut off long strings. gcc does a warning for this tmp.operands().resize(integer2size_t(array_size)); tmp.type()=type; } else if(mp_integer(tmp.operands().size())<array_size) { // fill up tmp.type()=type; exprt zero= zero_initializer( full_type.subtype(), value.source_location(), *this, get_message_handler()); tmp.operands().resize(integer2size_t(array_size), zero); } } return tmp; } if(value.id()==ID_string_constant && full_type.id()==ID_array && (full_type.subtype().id()==ID_signedbv || full_type.subtype().id()==ID_unsignedbv) && full_type.subtype().get(ID_width)==char_type().get(ID_width)) { // will go away, to be replaced by the above block string_constantt tmp1=to_string_constant(value); // adjust char type tmp1.type().subtype()=full_type.subtype(); exprt tmp2=tmp1.to_array_expr(); if(full_type.id()==ID_array && to_array_type(full_type).is_complete()) { // check size mp_integer array_size; if(to_integer(to_array_type(full_type).size(), array_size)) { err_location(value); error() << "array size needs to be constant, got " << to_string(to_array_type(full_type).size()) << eom; throw 0; } if(array_size<0) { err_location(value); error() << "array size must not be negative" << eom; throw 0; } if(mp_integer(tmp2.operands().size())>array_size) { // cut off long strings. gcc does a warning for this tmp2.operands().resize(integer2size_t(array_size)); tmp2.type()=type; } else if(mp_integer(tmp2.operands().size())<array_size) { // fill up tmp2.type()=type; exprt zero= zero_initializer( full_type.subtype(), value.source_location(), *this, get_message_handler()); tmp2.operands().resize(integer2size_t(array_size), zero); } } return tmp2; } if(full_type.id()==ID_array && to_array_type(full_type).size().is_nil()) { err_location(value); error() << "type `" << to_string(full_type) << "' cannot be initialized with `" << to_string(value) << "'" << eom; throw 0; } if(value.id()==ID_designated_initializer) { err_location(value); error() << "type `" << to_string(full_type) << "' cannot be initialized with designated initializer" << eom; throw 0; } exprt result=value; implicit_typecast(result, type); return result; }
void cpp_typecheckt::typecheck_type(typet &type) { assert(!type.id().empty()); assert(type.is_not_nil()); try { cpp_convert_plain_type(type); } catch(const char *err) { error().source_location=type.source_location(); error() << err << eom; throw 0; } catch(const std::string &err) { error().source_location=type.source_location(); error() << err << eom; throw 0; } if(type.id()==ID_cpp_name) { c_qualifierst qualifiers(type); cpp_namet cpp_name; cpp_name.swap(type); exprt symbol_expr=resolve( cpp_name, cpp_typecheck_resolvet::wantt::TYPE, cpp_typecheck_fargst()); if(symbol_expr.id()!=ID_type) { error().source_location=type.source_location(); error() << "error: expected type" << eom; throw 0; } type=symbol_expr.type(); assert(type.is_not_nil()); if(type.get_bool(ID_C_constant)) qualifiers.is_constant = true; qualifiers.write(type); } else if(type.id()==ID_struct || type.id()==ID_union) { typecheck_compound_type(to_struct_union_type(type)); } else if(type.id()==ID_pointer) { // the pointer might have a qualifier, but do subtype first typecheck_type(type.subtype()); // Check if it is a pointer-to-member if(type.find("to-member").is_not_nil()) { // these can point either to data members or member functions // of a class typet &class_object=static_cast<typet &>(type.add("to-member")); if(class_object.id()==ID_cpp_name) { assert(class_object.get_sub().back().id()=="::"); class_object.get_sub().pop_back(); } typecheck_type(class_object); // there may be parameters if this is a pointer to member function if(type.subtype().id()==ID_code) { irept::subt ¶meters=type.subtype().add(ID_parameters).get_sub(); if(parameters.empty() || parameters.front().get(ID_C_base_name)!=ID_this) { // Add 'this' to the parameters exprt a0(ID_parameter); a0.set(ID_C_base_name, ID_this); a0.type().id(ID_pointer); a0.type().subtype() = class_object; parameters.insert(parameters.begin(), a0); } } } } else if(type.id()==ID_array) { exprt &size_expr=to_array_type(type).size(); if(size_expr.is_not_nil()) { typecheck_expr(size_expr); simplify(size_expr, *this); } typecheck_type(type.subtype()); if(type.subtype().get_bool(ID_C_constant)) type.set(ID_C_constant, true); if(type.subtype().get_bool(ID_C_volatile)) type.set(ID_C_volatile, true); } else if(type.id()==ID_code) { code_typet &code_type=to_code_type(type); typecheck_type(code_type.return_type()); code_typet::parameterst ¶meters=code_type.parameters(); for(auto ¶m : parameters) { typecheck_type(param.type()); // see if there is a default value if(param.has_default_value()) { typecheck_expr(param.default_value()); implicit_typecast(param.default_value(), param.type()); } } } else if(type.id()==ID_template) { typecheck_type(type.subtype()); } else if(type.id()==ID_c_enum) { typecheck_enum_type(type); } else if(type.id()==ID_c_enum_tag) { } else if(type.id()==ID_c_bit_field) { typecheck_c_bit_field_type(to_c_bit_field_type(type)); } else if(type.id()==ID_unsignedbv || type.id()==ID_signedbv || type.id()==ID_bool || type.id()==ID_floatbv || type.id()==ID_fixedbv || type.id()==ID_empty) { } else if(type.id()==ID_symbol) { } else if(type.id()==ID_constructor || type.id()==ID_destructor) { } else if(type.id()=="cpp-cast-operator") { } else if(type.id()=="cpp-template-type") { } else if(type.id()==ID_typeof) { exprt e=static_cast<const exprt &>(type.find(ID_expr_arg)); if(e.is_nil()) { typet tmp_type= static_cast<const typet &>(type.find(ID_type_arg)); if(tmp_type.id()==ID_cpp_name) { // this may be ambiguous -- it can be either a type or // an expression cpp_typecheck_fargst fargs; exprt symbol_expr=resolve( to_cpp_name(static_cast<const irept &>(tmp_type)), cpp_typecheck_resolvet::wantt::BOTH, fargs); type=symbol_expr.type(); } else { typecheck_type(tmp_type); type=tmp_type; } } else { typecheck_expr(e); type=e.type(); } } else if(type.id()==ID_decltype) { exprt e=static_cast<const exprt &>(type.find(ID_expr_arg)); typecheck_expr(e); type=e.type(); } else if(type.id()==ID_unassigned) { // ignore, for template parameter guessing } else if(type.id()==ID_template_class_instance) { // ok (internally generated) } else if(type.id()==ID_block_pointer) { // This is an Apple extension for lambda-like constructs. // http://thirdcog.eu/pwcblocks/ } else if(type.id()==ID_nullptr) { } else { error().source_location=type.source_location(); error() << "unexpected cpp type: " << type.pretty() << eom; throw 0; } assert(type.is_not_nil()); }
void cpp_typecheckt::typecheck_type(typet &type) { assert(type.id() != ""); assert(type.is_not_nil()); try { cpp_convert_plain_type(type); } catch(const char *error) { err_location(type); str << error; throw 0; } catch(const std::string &error) { err_location(type); str << error; throw 0; } if(type.id() == "cpp-name") { c_qualifierst qualifiers(type); cpp_namet cpp_name; cpp_name.swap(type); exprt symbol_expr = resolve(cpp_name, cpp_typecheck_resolvet::TYPE, cpp_typecheck_fargst()); if(symbol_expr.id() != "type") { err_location(type); str << "error: expected type"; throw 0; } type = symbol_expr.type(); assert(type.is_not_nil()); if(type.cmt_constant()) qualifiers.is_constant = true; qualifiers.write(type); } else if(type.id() == "struct" || type.id() == "union") { typecheck_compound_type(type); } else if(type.id() == "pointer") { // the pointer might have a qualifier, but do subtype first typecheck_type(type.subtype()); // Check if it is a pointer-to-member if(type.find("to-member").is_not_nil()) { // these can point either to data members or member functions // of a class typet &class_object = static_cast<typet &>(type.add("to-member")); if(class_object.id() == "cpp-name") { assert(class_object.get_sub().back().id() == "::"); class_object.get_sub().pop_back(); } typecheck_type(class_object); // there may be arguments if this is a pointer to member function if(type.subtype().id() == "code") { irept::subt &args = type.subtype().add("arguments").get_sub(); if(args.empty() || args.front().cmt_base_name() != "this") { // Add 'this' to the arguments exprt a0("argument"); a0.cmt_base_name("this"); a0.type().id("pointer"); a0.type().subtype() = class_object; args.insert(args.begin(), a0); } } } // now do qualifier if(type.find("#qualifier").is_not_nil()) { typet &t = static_cast<typet &>(type.add("#qualifier")); cpp_convert_plain_type(t); c_qualifierst q(t); q.write(type); } type.remove("#qualifier"); } else if(type.id() == "array") { exprt &size_expr = to_array_type(type).size(); if(size_expr.is_nil()) type.id("incomplete_array"); else typecheck_expr(size_expr); // TODO: If is a incomplete_array, it should always // have initializers, except for catch declaration typecheck_type(type.subtype()); if(type.subtype().cmt_constant()) type.cmt_constant(true); if(type.subtype().cmt_volatile()) type.set("#volatile", true); } else if(type.id() == "code") { code_typet &code_type = to_code_type(type); typecheck_type(code_type.return_type()); code_typet::argumentst &arguments = code_type.arguments(); for(auto &argument : arguments) { typecheck_type(argument.type()); // see if there is a default value if(argument.has_default_value()) { typecheck_expr(argument.default_value()); implicit_typecast(argument.default_value(), argument.type()); } } } else if(type.id() == "template") { typecheck_type(type.subtype()); } else if(type.id() == "c_enum") { typecheck_enum_type(type); } else if( type.id() == "unsignedbv" || type.id() == "signedbv" || type.id() == "bool" || type.id() == "floatbv" || type.id() == "fixedbv" || type.id() == "empty") { } else if(type.id() == "symbol") { } else if(type.id() == "constructor" || type.id() == "destructor") { } else if(type.id() == "cpp-cast-operator") { } else if(type.id() == "cpp-template-type") { } else if(type.id() == "typeof") { exprt e = static_cast<const exprt &>(type.find("expr")); if(e.is_nil()) { typet tmp_type = static_cast<const typet &>(type.find("sizeof-type")); if(tmp_type.id() == "cpp-name") { // this may be ambiguous -- it can be either a type or // an expression cpp_typecheck_fargst fargs; exprt symbol_expr = resolve( to_cpp_name(static_cast<const irept &>(tmp_type)), cpp_typecheck_resolvet::BOTH, fargs); type = symbol_expr.type(); } else { typecheck_type(tmp_type); type = tmp_type; } } else { typecheck_expr(e); type = e.type(); } } else if(type.id() == "decltype") { exprt e = static_cast<const exprt &>(type.find("expr_arg")); typecheck_expr(e); type = e.type(); } else if(type.id() == "unassigned") { // ignore, for template argument guessing } else if(type.id() == "ellipsis") { } else { err_location(type); str << "unexpected type: " << type.pretty(); throw 0; } assert(type.is_not_nil()); }
cpp_template_args_tct cpp_typecheckt::typecheck_template_args( const source_locationt &source_location, const symbolt &template_symbol, const cpp_template_args_non_tct &template_args) { // old stuff assert(template_args.id()!=ID_already_typechecked); assert(template_symbol.type.get_bool(ID_is_template)); const template_typet &template_type= to_cpp_declaration(template_symbol.type).template_type(); // bad re-cast, but better than copying the args one by one cpp_template_args_tct result= (const cpp_template_args_tct &)(template_args); cpp_template_args_tct::argumentst &args= result.arguments(); const template_typet::template_parameterst ¶meters= template_type.template_parameters(); if(parameters.size()<args.size()) { err_location(source_location); str << "too many template arguments (expected " << parameters.size() << ", but got " << args.size() << ")"; throw 0; } // we will modify the template map template_mapt old_template_map; old_template_map=template_map; // check for default arguments for(unsigned i=0; i<parameters.size(); i++) { const template_parametert ¶meter=parameters[i]; cpp_save_scopet cpp_saved_scope(cpp_scopes); if(i>=args.size()) { // Check for default argument for the parameter. // These may depend on previous arguments. if(!parameter.has_default_argument()) { err_location(source_location); str << "not enough template arguments (expected " << parameters.size() << ", but got " << args.size() << ")"; throw 0; } args.push_back(parameter.default_argument()); // these need to be typechecked in the scope of the template, // not in the current scope! cpp_idt *template_scope=cpp_scopes.id_map[template_symbol.name]; assert(template_scope!=NULL); cpp_scopes.go_to(*template_scope); } assert(i<args.size()); exprt &arg=args[i]; if(parameter.id()==ID_type) { if(arg.id()==ID_type) { typecheck_type(arg.type()); } else if(arg.id()=="ambiguous") { typecheck_type(arg.type()); typet t=arg.type(); arg=exprt(ID_type, t); } else { err_location(arg); str << "expected type, but got expression"; throw 0; } } else // expression { if(arg.id()==ID_type) { err_location(arg); str << "expected expression, but got type"; throw 0; } else if(arg.id()=="ambiguous") { exprt e; e.swap(arg.type()); arg.swap(e); } typet type=parameter.type(); // First check the parameter type (might have ealier // type parameters in it). Needs to be checked in scope // of template. { cpp_save_scopet cpp_saved_scope(cpp_scopes); cpp_idt *template_scope=cpp_scopes.id_map[template_symbol.name]; assert(template_scope!=NULL); cpp_scopes.go_to(*template_scope); typecheck_type(type); } // Now check the argument to match that. typecheck_expr(arg); simplify(arg, *this); implicit_typecast(arg, type); } // Set right away -- this is for the benefit of default // arguments and later parameters whose type might // depend on an earlier parameter. template_map.set(parameter, arg); } // restore template map template_map.swap(old_template_map); // now the numbers should match assert(args.size()==parameters.size()); return result; }