CBMC
ansi_c_parser.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
9 
10 #ifndef CPROVER_ANSI_C_ANSI_C_PARSER_H
11 #define CPROVER_ANSI_C_ANSI_C_PARSER_H
12 
13 #include <map>
14 
15 #include <util/parser.h>
16 #include <util/config.h>
17 
18 #include "ansi_c_parse_tree.h"
19 #include "ansi_c_scope.h"
20 
21 class ansi_c_parsert;
23 
24 class ansi_c_parsert:public parsert
25 {
26 public:
28 
29  explicit ansi_c_parsert(message_handlert &message_handler)
30  : parsert(message_handler),
31  tag_following(false),
32  asm_block_following(false),
34  mode(modet::NONE),
35  cpp98(false),
36  cpp11(false),
37  for_has_scope(false),
39  float16_type(false),
40  bf16_type(false)
41  {
42  // set up global scope
43  scopes.clear();
44  scopes.push_back(scopet());
45  }
46 
47  bool parse() override
48  {
49  return yyansi_cparse(*this) != 0;
50  }
51 
52  // internal state of the scanner
56  std::string string_literal;
57  std::list<exprt> pragma_pack;
58 
61 
62  // recognize C++98 and C++11 keywords
63  bool cpp98, cpp11;
64 
65  // in C99 and upwards, for(;;) has a scope
67 
68  // ISO/IEC TS 18661-3:2015
71  bool bf16_type;
72 
75 
76  typedef std::list<scopet> scopest;
78 
80  {
81  return scopes.front();
82  }
83 
84  const scopet &root_scope() const
85  {
86  return scopes.front();
87  }
88 
89  void pop_scope()
90  {
91  scopes.pop_back();
92  }
93 
95  {
96  PRECONDITION(!scopes.empty());
97  return scopes.back();
98  }
99 
100  enum class decl_typet { TAG, MEMBER, PARAMETER, OTHER };
101 
102  // convert a declarator and then add it to existing an declaration
103  void add_declarator(exprt &declaration, irept &declarator);
104 
105  // adds a tag to the current scope
106  void add_tag_with_body(irept &tag);
107 
108  void copy_item(const ansi_c_declarationt &declaration)
109  {
110  PRECONDITION(declaration.id() == ID_declaration);
111  parse_tree.items.push_back(declaration);
112  }
113 
114  void new_scope(const std::string &prefix)
115  {
116  const scopet &current=current_scope();
117  scopes.push_back(scopet());
118  scopes.back().prefix=current.prefix+prefix;
119  }
120 
122  const irep_idt &base_name, // in
123  irep_idt &identifier, // out
124  bool tag,
125  bool label);
126 
127  static ansi_c_id_classt get_class(const typet &type);
128 
129  irep_idt lookup_label(const irep_idt base_name)
130  {
131  irep_idt identifier;
132  lookup(base_name, identifier, false, true);
133  return identifier;
134  }
135 
137  bool pragma_cprover_empty();
138 
140  void pragma_cprover_push();
141 
143  void pragma_cprover_pop();
144 
146  void pragma_cprover_add_check(const irep_idt &name, bool enabled);
147 
150  bool pragma_cprover_clash(const irep_idt &name, bool enabled);
151 
154  void set_pragma_cprover();
155 
156 private:
157  std::list<std::map<const irep_idt, bool>> pragma_cprover_stack;
158 };
159 
161 
162 #endif // CPROVER_ANSI_C_ANSI_C_PARSER_H
int yyansi_cparse(ansi_c_parsert &)
void ansi_c_scanner_init(ansi_c_parsert &)
ansi_c_id_classt
Definition: ansi_c_scope.h:18
void pragma_cprover_pop()
Pops a level in the CPROVER pragma stack.
ansi_c_parsert(message_handlert &message_handler)
Definition: ansi_c_parser.h:29
void set_pragma_cprover()
Tags source_location with the current CPROVER pragma stack.
std::list< exprt > pragma_pack
Definition: ansi_c_parser.h:57
bool asm_block_following
Definition: ansi_c_parser.h:54
void new_scope(const std::string &prefix)
bool pragma_cprover_empty()
True iff the CPROVER pragma stack is empty.
bool ts_18661_3_Floatn_types
Definition: ansi_c_parser.h:69
ansi_c_identifiert identifiert
Definition: ansi_c_parser.h:73
void add_declarator(exprt &declaration, irept &declarator)
ansi_c_scopet scopet
Definition: ansi_c_parser.h:74
bool parse() override
Definition: ansi_c_parser.h:47
scopet & root_scope()
Definition: ansi_c_parser.h:79
ansi_c_parse_treet parse_tree
Definition: ansi_c_parser.h:27
std::list< std::map< const irep_idt, bool > > pragma_cprover_stack
unsigned parenthesis_counter
Definition: ansi_c_parser.h:55
configt::ansi_ct::flavourt modet
Definition: ansi_c_parser.h:59
void add_tag_with_body(irept &tag)
bool pragma_cprover_clash(const irep_idt &name, bool enabled)
Returns true iff the same check with polarity is already present at top of the stack.
std::list< scopet > scopest
Definition: ansi_c_parser.h:76
static ansi_c_id_classt get_class(const typet &type)
ansi_c_id_classt lookup(const irep_idt &base_name, irep_idt &identifier, bool tag, bool label)
void copy_item(const ansi_c_declarationt &declaration)
void pragma_cprover_push()
Pushes an empty level in the CPROVER pragma stack.
scopet & current_scope()
Definition: ansi_c_parser.h:94
std::string string_literal
Definition: ansi_c_parser.h:56
irep_idt lookup_label(const irep_idt base_name)
const scopet & root_scope() const
Definition: ansi_c_parser.h:84
void pragma_cprover_add_check(const irep_idt &name, bool enabled)
Adds a check to the CPROVER pragma stack.
std::string prefix
Definition: ansi_c_scope.h:47
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:38
Base class for all expressions.
Definition: expr.h:56
There are a large number of kinds of tree structured or tree-like data in CPROVER.
Definition: irep.h:360
const irep_idt & id() const
Definition: irep.h:384
Definition: parser.h:25
The type of an expression, extends irept.
Definition: type.h:29
@ NONE
Do not apply loop contracts.
Parser utilities.
#define PRECONDITION(CONDITION)
Definition: invariant.h:463