-
Notifications
You must be signed in to change notification settings - Fork 273
ansi-c: introduce typedef type #2641
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ Author: Daniel Kroening, [email protected] | |
#include "gcc_types.h" | ||
#include "padding.h" | ||
#include "type2name.h" | ||
#include "typedef_type.h" | ||
|
||
void c_typecheck_baset::typecheck_type(typet &type) | ||
{ | ||
|
@@ -89,7 +90,9 @@ void c_typecheck_baset::typecheck_type(typet &type) | |
else if(type.id()==ID_typeof) | ||
typecheck_typeof_type(type); | ||
else if(type.id()==ID_symbol) | ||
typecheck_symbol_type(type); | ||
typecheck_symbol_type(to_symbol_type(type)); | ||
else if(type.id() == ID_typedef_type) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Per coding standards use |
||
typecheck_typedef_type(type); | ||
else if(type.id()==ID_vector) | ||
typecheck_vector_type(to_vector_type(type)); | ||
else if(type.id()==ID_custom_unsignedbv || | ||
|
@@ -1425,10 +1428,10 @@ void c_typecheck_baset::typecheck_typeof_type(typet &type) | |
c_qualifiers.write(type); | ||
} | ||
|
||
void c_typecheck_baset::typecheck_symbol_type(typet &type) | ||
void c_typecheck_baset::typecheck_symbol_type(symbol_typet &type) | ||
{ | ||
const irep_idt &identifier= | ||
to_symbol_type(type).get_identifier(); | ||
// we do some consistency checking only | ||
const irep_idt &identifier = type.get_identifier(); | ||
|
||
symbol_tablet::symbolst::const_iterator s_it= | ||
symbol_table.symbols.find(identifier); | ||
|
@@ -1449,25 +1452,46 @@ void c_typecheck_baset::typecheck_symbol_type(typet &type) | |
error() << "expected type symbol" << eom; | ||
throw 0; | ||
} | ||
} | ||
|
||
if(symbol.is_macro) | ||
{ | ||
// overwrite, but preserve (add) any qualifiers and other flags | ||
void c_typecheck_baset::typecheck_typedef_type(typet &type) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not take a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The typedef_type is replaced by whatever the typedef points to; i.e., you would violate the type invariant upon returning from this method. |
||
{ | ||
const irep_idt &identifier = to_typedef_type(type).get_identifier(); | ||
|
||
c_qualifierst c_qualifiers(type); | ||
bool is_packed=type.get_bool(ID_C_packed); | ||
irept alignment=type.find(ID_C_alignment); | ||
symbol_tablet::symbolst::const_iterator s_it = | ||
symbol_table.symbols.find(identifier); | ||
|
||
c_qualifiers+=c_qualifierst(symbol.type); | ||
type=symbol.type; | ||
c_qualifiers.write(type); | ||
if(s_it == symbol_table.symbols.end()) | ||
{ | ||
error().source_location = type.source_location(); | ||
error() << "typedef symbol `" << identifier << "' not found" << eom; | ||
throw 0; | ||
} | ||
|
||
if(is_packed) | ||
type.set(ID_C_packed, true); | ||
if(alignment.is_not_nil()) | ||
type.set(ID_C_alignment, alignment); | ||
const symbolt &symbol = s_it->second; | ||
|
||
if(!symbol.is_type) | ||
{ | ||
error().source_location = type.source_location(); | ||
error() << "expected type symbol for typedef" << eom; | ||
throw 0; | ||
} | ||
|
||
// overwrite, but preserve (add) any qualifiers and other flags | ||
|
||
c_qualifierst c_qualifiers(type); | ||
bool is_packed = type.get_bool(ID_C_packed); | ||
irept alignment = type.find(ID_C_alignment); | ||
|
||
c_qualifiers += c_qualifierst(symbol.type); | ||
type = symbol.type; | ||
c_qualifiers.write(type); | ||
|
||
if(is_packed) | ||
type.set(ID_C_packed, true); | ||
if(alignment.is_not_nil()) | ||
type.set(ID_C_alignment, alignment); | ||
|
||
// CPROVER extensions | ||
if(symbol.base_name=="__CPROVER_rational") | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/*******************************************************************\ | ||
|
||
Module: | ||
|
||
Author: Daniel Kroening, [email protected] | ||
|
||
\*******************************************************************/ | ||
|
||
#ifndef CPROVER_ANSI_C_TYPEDEF_TYPE_H | ||
#define CPROVER_ANSI_C_TYPEDEF_TYPE_H | ||
|
||
#include <util/type.h> | ||
|
||
/*! \brief A typedef | ||
*/ | ||
class typedef_typet : public typet | ||
{ | ||
public: | ||
explicit typedef_typet(const irep_idt &identifier) : typet(ID_typedef_type) | ||
{ | ||
set_identifier(identifier); | ||
} | ||
|
||
void set_identifier(const irep_idt &identifier) | ||
{ | ||
set(ID_identifier, identifier); | ||
} | ||
|
||
const irep_idt &get_identifier() const | ||
{ | ||
return get(ID_identifier); | ||
} | ||
}; | ||
|
||
/*! \brief Cast a generic typet to a \ref typedef_typet | ||
* | ||
* This is an unchecked conversion. \a type must be known to be \ref | ||
* typedef_typet. | ||
* | ||
* \param type Source type | ||
* \return Object of type \ref typedef_typet | ||
* | ||
* \ingroup gr_std_types | ||
*/ | ||
inline const typedef_typet &to_typedef_type(const typet &type) | ||
{ | ||
PRECONDITION(type.id() == ID_typedef_type); | ||
return static_cast<const typedef_typet &>(type); | ||
} | ||
|
||
/*! \copydoc to_typedef_type(const typet &) | ||
* \ingroup gr_std_types | ||
*/ | ||
inline typedef_typet &to_typedef_type(typet &type) | ||
{ | ||
PRECONDITION(type.id() == ID_typedef_type); | ||
return static_cast<typedef_typet &>(type); | ||
} | ||
|
||
template <> | ||
inline bool can_cast_type<typedef_typet>(const typet &type) | ||
{ | ||
return type.id() == ID_typedef_type; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we introduce the method above the PRECONDITION(can_cast_type<typedef_typet>(type)); Which will make it easier to add more robust checks in future. |
||
} | ||
|
||
#endif // CPROVER_ANSI_C_TYPEDEF_TYPE_H |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per coding standards prefer using
can_cast_typet<typedef_typet>