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  cpp98(false), cpp11(false),
33  for_has_scope(false)
34  {
35  }
36 
37  virtual bool parse() override
38  {
39  return yyansi_cparse()!=0;
40  }
41 
42  virtual void clear() override
43  {
45  parse_tree.clear();
46 
47  // scanner state
48  tag_following=false;
49  asm_block_following=false;
51  string_literal.clear();
52  pragma_pack.clear();
53 
54  // set up global scope
55  scopes.clear();
56  scopes.push_back(scopet());
57  }
58 
59  // internal state of the scanner
63  std::string string_literal;
64  std::list<exprt> pragma_pack;
65 
67  modet mode;
68 
69  // recognize C++98 and C++11 keywords
70  bool cpp98, cpp11;
71 
72  // in C99 and upwards, for(;;) has a scope
74 
77 
78  typedef std::list<scopet> scopest;
79  scopest scopes;
80 
81  scopet &root_scope()
82  {
83  return scopes.front();
84  }
85 
86  const scopet &root_scope() const
87  {
88  return scopes.front();
89  }
90 
91  void pop_scope()
92  {
93  scopes.pop_back();
94  }
95 
96  scopet &current_scope()
97  {
98  assert(!scopes.empty());
99  return scopes.back();
100  }
101 
102  enum class decl_typet { TAG, MEMBER, PARAMETER, OTHER };
103 
104  // convert a declarator and then add it to existing an declaration
105  void add_declarator(exprt &declaration, irept &declarator);
106 
107  // adds a tag to the current scope
108  void add_tag_with_body(irept &tag);
109 
110  void copy_item(const ansi_c_declarationt &declaration)
111  {
112  assert(declaration.id()==ID_declaration);
113  parse_tree.items.push_back(declaration);
114  }
115 
116  void new_scope(const std::string &prefix)
117  {
118  const scopet &current=current_scope();
119  scopes.push_back(scopet());
120  scopes.back().prefix=current.prefix+prefix;
121  }
122 
124  const irep_idt &base_name, // in
125  irep_idt &identifier, // out
126  bool tag,
127  bool label);
128 
129  static ansi_c_id_classt get_class(const typet &type);
130 
131  irep_idt lookup_label(const irep_idt base_name)
132  {
133  irep_idt identifier;
134  lookup(base_name, identifier, false, true);
135  return identifier;
136  }
137 };
138 
140 
141 int yyansi_cerror(const std::string &error);
142 void ansi_c_scanner_init();
143 
144 #endif // CPROVER_ANSI_C_ANSI_C_PARSER_H
The type of an expression.
Definition: type.h:20
static ansi_c_id_classt get_class(const typet &type)
ansi_c_parse_treet parse_tree
Definition: ansi_c_parser.h:29
scopet & current_scope()
Definition: ansi_c_parser.h:96
std::string string_literal
Definition: ansi_c_parser.h:63
void add_tag_with_body(irept &tag)
Definition: parser.h:23
ansi_c_identifiert identifiert
Definition: ansi_c_parser.h:75
Parser utilities.
const scopet & root_scope() const
Definition: ansi_c_parser.h:86
const irep_idt & id() const
Definition: irep.h:189
ansi_c_id_classt
Definition: ansi_c_scope.h:15
int yyansi_cparse()
virtual bool parse() override
Definition: ansi_c_parser.h:37
void add_declarator(exprt &declaration, irept &declarator)
Base class for tree-like data structures with sharing.
Definition: irep.h:87
scopet & root_scope()
Definition: ansi_c_parser.h:81
void copy_item(const ansi_c_declarationt &declaration)
ansi_c_scopet scopet
Definition: ansi_c_parser.h:76
void new_scope(const std::string &prefix)
virtual void clear()
Definition: parser.h:32
Base class for all expressions.
Definition: expr.h:46
std::list< exprt > pragma_pack
Definition: ansi_c_parser.h:64
unsigned parenthesis_counter
Definition: ansi_c_parser.h:62
configt::ansi_ct::flavourt modet
Definition: ansi_c_parser.h:66
mstreamt & error()
Definition: message.h:223
string hashing
irep_idt lookup_label(const irep_idt base_name)
std::string prefix
Definition: ansi_c_scope.h:46
std::list< scopet > scopest
Definition: ansi_c_parser.h:78
bool asm_block_following
Definition: ansi_c_parser.h:61
ansi_c_parsert ansi_c_parser
virtual void clear() override
Definition: ansi_c_parser.h:42
void ansi_c_scanner_init()
int yyansi_cerror(const std::string &error)
ansi_c_id_classt lookup(const irep_idt &base_name, irep_idt &identifier, bool tag, bool label)