Skip to content

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

Merged
merged 1 commit into from
Aug 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/ansi-c/ansi_c_declaration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ void ansi_c_declaratort::build(irept &src)
{
typet *p=static_cast<typet *>(&src);

// walk down subtype until we hit symbol or "abstract"
// walk down subtype until we hit typedef_type, symbol or "abstract"
while(true)
{
typet &t=*p;

if(t.id()==ID_symbol)
if(t.id() == ID_typedef_type || t.id() == ID_symbol)
Copy link
Contributor

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>

{
set_base_name(t.get(ID_C_base_name));
add_source_location()=t.source_location();
Expand Down
3 changes: 2 additions & 1 deletion src/ansi-c/c_typecheck_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ class c_typecheck_baset:
virtual void typecheck_c_enum_type(typet &type);
virtual void typecheck_c_enum_tag_type(c_enum_tag_typet &type);
virtual void typecheck_code_type(code_typet &type);
virtual void typecheck_symbol_type(typet &type);
virtual void typecheck_symbol_type(symbol_typet &type);
virtual void typecheck_typedef_type(typet &type);
virtual void typecheck_c_bit_field_type(c_bit_field_typet &type);
virtual void typecheck_typeof_type(typet &type);
virtual void typecheck_array_type(array_typet &type);
Expand Down
58 changes: 41 additions & 17 deletions src/ansi-c/c_typecheck_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per coding standards use can_cast_typet<typedef_typet>(type) to hide direct id access.

typecheck_typedef_type(type);
else if(type.id()==ID_vector)
typecheck_vector_type(to_vector_type(type));
else if(type.id()==ID_custom_unsignedbv ||
Expand Down Expand Up @@ -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);
Expand All @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not take a typedef_typet to avoid the immediate cast (makes more consistent with typecheck_symbol_type as well).

Copy link
Member Author

Choose a reason for hiding this comment

The 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")
{
Expand Down
7 changes: 6 additions & 1 deletion src/ansi-c/scanner.l
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,20 @@ int make_identifier()

PARSER.tag_following=false;

stack(yyansi_clval).id(ID_symbol);
stack(yyansi_clval).set(ID_C_base_name, base_name);
stack(yyansi_clval).set(ID_identifier, identifier);
stack(yyansi_clval).set(ID_C_id_class, static_cast<int>(result));

if(result==ansi_c_id_classt::ANSI_C_TYPEDEF)
{
stack(yyansi_clval).id(ID_typedef_type);
return TOK_TYPEDEFNAME;
}
else
{
stack(yyansi_clval).id(ID_symbol);
return TOK_IDENTIFIER;
}
}
}

Expand Down
66 changes: 66 additions & 0 deletions src/ansi-c/typedef_type.h
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we introduce the method above the to_typedef_type methods we can de-duplicate the check (id() == ID_typedef_type) by replacing the preconditions with:

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
1 change: 1 addition & 0 deletions src/util/irep_ids.def
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ IREP_ID_ONE(concatenation)
IREP_ID_ONE(infinity)
IREP_ID_ONE(return_type)
IREP_ID_ONE(typedef)
IREP_ID_ONE(typedef_type)
IREP_ID_TWO(C_typedef, #typedef)
IREP_ID_ONE(extern)
IREP_ID_ONE(static)
Expand Down