cprover
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 <cassert>
14 
15 #include <util/parser.h>
16 #include <util/expr.h>
17 #include <util/string_hash.h>
18 #include <util/mp_arith.h>
19 #include <util/config.h>
20 
21 #include "ansi_c_parse_tree.h"
22 #include "ansi_c_scope.h"
23 
24 int yyansi_cparse();
25 
26 class ansi_c_parsert:public parsert
27 {
28 public:
30 
32  tag_following(false),
33  asm_block_following(false),
35  mode(modet::NONE),
36  cpp98(false),
37  cpp11(false),
38  for_has_scope(false),
40  Float128_type(false)
41  {
42  }
43 
44  virtual bool parse() override
45  {
46  return yyansi_cparse()!=0;
47  }
48 
49  virtual void clear() override
50  {
52  parse_tree.clear();
53 
54  // scanner state
55  tag_following=false;
56  asm_block_following=false;
58  string_literal.clear();
59  pragma_pack.clear();
60 
61  // set up global scope
62  scopes.clear();
63  scopes.push_back(scopet());
64  }
65 
66  // internal state of the scanner
70  std::string string_literal;
71  std::list<exprt> pragma_pack;
72 
75 
76  // recognize C++98 and C++11 keywords
77  bool cpp98, cpp11;
78 
79  // in C99 and upwards, for(;;) has a scope
81 
82  // ISO/IEC TS 18661-3:2015
84 
85  // Does the compiler version emulated provide _Float128?
87 
90 
91  typedef std::list<scopet> scopest;
93 
95  {
96  return scopes.front();
97  }
98 
99  const scopet &root_scope() const
100  {
101  return scopes.front();
102  }
103 
104  void pop_scope()
105  {
106  scopes.pop_back();
107  }
108 
110  {
111  assert(!scopes.empty());
112  return scopes.back();
113  }
114 
115  enum class decl_typet { TAG, MEMBER, PARAMETER, OTHER };
116 
117  // convert a declarator and then add it to existing an declaration
118  void add_declarator(exprt &declaration, irept &declarator);
119 
120  // adds a tag to the current scope
121  void add_tag_with_body(irept &tag);
122 
123  void copy_item(const ansi_c_declarationt &declaration)
124  {
125  assert(declaration.id()==ID_declaration);
126  parse_tree.items.push_back(declaration);
127  }
128 
129  void new_scope(const std::string &prefix)
130  {
131  const scopet &current=current_scope();
132  scopes.push_back(scopet());
133  scopes.back().prefix=current.prefix+prefix;
134  }
135 
137  const irep_idt &base_name, // in
138  irep_idt &identifier, // out
139  bool tag,
140  bool label);
141 
142  static ansi_c_id_classt get_class(const typet &type);
143 
144  irep_idt lookup_label(const irep_idt base_name)
145  {
146  irep_idt identifier;
147  lookup(base_name, identifier, false, true);
148  return identifier;
149  }
150 };
151 
153 
154 int yyansi_cerror(const std::string &error);
155 void ansi_c_scanner_init();
156 
157 #endif // CPROVER_ANSI_C_ANSI_C_PARSER_H
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:35
ansi_c_parsert::add_declarator
void add_declarator(exprt &declaration, irept &declarator)
Definition: ansi_c_parser.cpp:84
ansi_c_parsert::scopet
ansi_c_scopet scopet
Definition: ansi_c_parser.h:89
mp_arith.h
yyansi_cparse
int yyansi_cparse()
Definition: ansi_c_y.tab.cpp:3469
typet
The type of an expression, extends irept.
Definition: type.h:27
ansi_c_parse_tree.h
ansi_c_parsert::cpp98
bool cpp98
Definition: ansi_c_parser.h:77
ansi_c_id_classt
ansi_c_id_classt
Definition: ansi_c_scope.h:17
ansi_c_parsert::decl_typet
decl_typet
Definition: ansi_c_parser.h:115
ansi_c_parse_treet::items
itemst items
Definition: ansi_c_parse_tree.h:22
parsert::clear
virtual void clear()
Definition: parser.h:32
exprt
Base class for all expressions.
Definition: expr.h:54
ansi_c_parsert::modet
configt::ansi_ct::flavourt modet
Definition: ansi_c_parser.h:73
ansi_c_scopet::prefix
std::string prefix
Definition: ansi_c_scope.h:47
ansi_c_parsert::parenthesis_counter
unsigned parenthesis_counter
Definition: ansi_c_parser.h:69
ansi_c_parsert::copy_item
void copy_item(const ansi_c_declarationt &declaration)
Definition: ansi_c_parser.h:123
ansi_c_parsert::new_scope
void new_scope(const std::string &prefix)
Definition: ansi_c_parser.h:129
ansi_c_parsert::asm_block_following
bool asm_block_following
Definition: ansi_c_parser.h:68
ansi_c_parsert::lookup
ansi_c_id_classt lookup(const irep_idt &base_name, irep_idt &identifier, bool tag, bool label)
Definition: ansi_c_parser.cpp:15
expr.h
yyansi_cerror
int yyansi_cerror(const std::string &error)
Definition: ansi_c_parser.cpp:78
ansi_c_parsert::pragma_pack
std::list< exprt > pragma_pack
Definition: ansi_c_parser.h:71
ansi_c_parsert::ts_18661_3_Floatn_types
bool ts_18661_3_Floatn_types
Definition: ansi_c_parser.h:83
ansi_c_parsert::lookup_label
irep_idt lookup_label(const irep_idt base_name)
Definition: ansi_c_parser.h:144
ansi_c_parsert::decl_typet::OTHER
ansi_c_parsert::pop_scope
void pop_scope()
Definition: ansi_c_parser.h:104
ansi_c_parsert::get_class
static ansi_c_id_classt get_class(const typet &type)
Definition: ansi_c_parser.cpp:157
ansi_c_scopet
Definition: ansi_c_scope.h:39
ansi_c_parsert::scopest
std::list< scopet > scopest
Definition: ansi_c_parser.h:91
ansi_c_parsert::decl_typet::PARAMETER
ansi_c_parsert::ansi_c_parsert
ansi_c_parsert()
Definition: ansi_c_parser.h:31
ansi_c_parsert::clear
virtual void clear() override
Definition: ansi_c_parser.h:49
ansi_c_parser
ansi_c_parsert ansi_c_parser
Definition: ansi_c_parser.cpp:13
ansi_c_parsert
Definition: ansi_c_parser.h:26
string_hash.h
ansi_c_scanner_init
void ansi_c_scanner_init()
Definition: ansi_c_lex.yy.cpp:4391
ansi_c_parsert::string_literal
std::string string_literal
Definition: ansi_c_parser.h:70
ansi_c_parse_treet
Definition: ansi_c_parse_tree.h:17
irept::id
const irep_idt & id() const
Definition: irep.h:259
ansi_c_parsert::decl_typet::MEMBER
ansi_c_identifiert
Definition: ansi_c_scope.h:28
parsert
Definition: parser.h:23
ansi_c_parsert::tag_following
bool tag_following
Definition: ansi_c_parser.h:67
ansi_c_parsert::cpp11
bool cpp11
Definition: ansi_c_parser.h:77
ansi_c_parsert::mode
modet mode
Definition: ansi_c_parser.h:74
ansi_c_parsert::scopes
scopest scopes
Definition: ansi_c_parser.h:92
ansi_c_parsert::add_tag_with_body
void add_tag_with_body(irept &tag)
Definition: ansi_c_parser.cpp:58
ansi_c_declarationt
Definition: ansi_c_declaration.h:72
ansi_c_parsert::parse_tree
ansi_c_parse_treet parse_tree
Definition: ansi_c_parser.h:29
ansi_c_parsert::root_scope
const scopet & root_scope() const
Definition: ansi_c_parser.h:99
parser.h
ansi_c_parsert::Float128_type
bool Float128_type
Definition: ansi_c_parser.h:86
ansi_c_parsert::current_scope
scopet & current_scope()
Definition: ansi_c_parser.h:109
ansi_c_parsert::parse
virtual bool parse() override
Definition: ansi_c_parser.h:44
ansi_c_parsert::decl_typet::TAG
config.h
ansi_c_scope.h
irept
Base class for tree-like data structures with sharing.
Definition: irep.h:156
ansi_c_parsert::identifiert
ansi_c_identifiert identifiert
Definition: ansi_c_parser.h:88
configt::ansi_ct::flavourt
flavourt
Definition: config.h:105
ansi_c_parsert::root_scope
scopet & root_scope()
Definition: ansi_c_parser.h:94
ansi_c_parsert::for_has_scope
bool for_has_scope
Definition: ansi_c_parser.h:80
ansi_c_parse_treet::clear
void clear()
Definition: ansi_c_parse_tree.cpp:18