CodeSonar C++ API
[For improved navigation, enable JavaScript.]
cs_ast.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023, an unpublished work by CodeSecure, Inc.
3  * ALL RIGHTS RESERVED
4  *
5  * Copyright (c) 2013-2023, an unpublished work by GrammaTech, Inc.
6  * ALL RIGHTS RESERVED
7  *
8  * This software is furnished under a license and may be used and
9  * copied only in accordance with the terms of such license and the
10  * inclusion of the above copyright notice. This software or any
11  * other copies thereof may not be provided or otherwise made
12  * available to any other person. Title to and ownership of the
13  * software is retained by CodeSecure, Inc.
14  */
15 
16 #ifndef CS_AST_HPP
17 #define CS_AST_HPP
18 
19 #include "cs_ast_decl.hpp"
20 
21 #ifndef CSFE_API
22 #include "cs_symbol_decl.hpp"
23 #include "cs_sfileinst_decl.hpp"
24 #else /* CSFE_API */
25 #include "csfe_sfileinst.hpp"
26 #endif /* CSFE_API */
27 
30 #include "cs_string64.h"
31 
33 namespace cs{
34 
35 #ifndef CSFE_API
36 
37  inline ast_field::ast_field(ast_ordinal ord, symbol val)
38  {
39  inner.ordinal = cglue<ast_ordinal>::unwrap(ord);
40  inner.type = csft_abs_loc;
41  inner._.abs_loc = cglue<symbol>::unwrap(val);
42  }
43 
45  {
46  if( inner.type != csft_abs_loc )
47  check(CS_ERROR_INVALID_TYPE);
48  return cglue<symbol>::wrap(inner._.abs_loc);
49  }
50 
51 #endif /* CSFE_API */
52 
53  inline ast_field::ast_field(ast_ordinal ord, sfileinst val)
54  {
55  inner.ordinal = cglue<ast_ordinal>::unwrap(ord);
56  inner.type = csft_sfid;
57  inner._.sfid =
58 #ifdef CSFE_API
59  csfe::
60 #endif /* CSFE_API */
61  cglue<sfileinst>::unwrap(val);
62  }
63 
65  {
66  if( inner.type != csft_sfid )
67  check(CS_ERROR_INVALID_TYPE);
68  return
69 #ifdef CSFE_API
70  csfe::
71 #endif /* CSFE_API */
72  cglue<sfileinst>::wrap(inner._.sfid);
73  }
74 
76  {
77  cs_ast_field rv;
78  cs_result r = cs_ast_get_field(inner, cglue<ast_ordinal>::unwrap(ord), &rv);
79  if( r == CS_ELEMENT_NOT_PRESENT )
80  {
81  ast_field_not_found_error nferr(*this, ord);
82 #if CS_CPP_NO_EXCEPTIONS
83  cs_fatal("%s", nferr.as_string().c_str());
84 #else
85  throw nferr;
86 #endif
87  }
88  check(r);
89  return ast_field::;
90  }
91 
92  inline ast_field ast::operator[](size_t ord) const
93  {
94  cs_ast_field rv;
95  cs_result r = cs_ast_get_field(inner, cglue<ast_ordinal>::unwrap(ord), &rv);
96  if( r == CS_ELEMENT_NOT_PRESENT )
97  {
98  ast_field_not_found_error nferr(*this, ord);
99 #if CS_CPP_NO_EXCEPTIONS
100  cs_fatal("%s", nferr.as_string().c_str());
101 #else
102  throw nferr;
103 #endif
104  }
105  check(r);
106  return ast_field::;
107  }
108 
109  inline bool ast::has_field(ast_ordinal ord) const
110  {
111  cs_ast_field rv;
112  cs_result r = cs_ast_get_field(inner, cglue<ast_ordinal>::unwrap(ord), &rv);
113  switch(r)
114  {
115  case CS_SUCCESS:
116  return true;
117  case CS_ELEMENT_NOT_PRESENT:
118  return false;
119  default:
120  check(r);
121  return false;
122  }
123  }
124 
125  inline bool ast::has_field(size_t ord) const
126  {
127  cs_ast_field rv;
128  cs_result r = cs_ast_get_field(inner, cglue<ast_ordinal>::unwrap(ord), &rv);
129  switch(r)
130  {
131  case CS_SUCCESS:
132  return true;
133  case CS_ELEMENT_NOT_PRESENT:
134  return false;
135  default:
136  check(r);
137  return false;
138  }
139  }
140 
141  inline size_t ast::num_fields() const
142  {
143  size_t bn;
144  cs_result r = cs_ast_fields(
145  inner, NULL, 0, &bn );
146  switch( r )
147  {
148  case CS_SUCCESS:
149  case CS_TRUNCATED:
150  return bn / sizeof(cs_ast_field);
151  default:
152  check(r);
153  return 0;
154  }
155  }
156 
157  inline std::vector<ast_field> ast::fields() const
158  {
159  typedef scratchpad<ast_field> sp;
160  sp::to_vector_functor1<ast, cs_ast_fields>
161  functor(inner);
162  return sp::to_vector(functor);
163  }
164 
165  inline std::vector<ast_field> ast::children() const
166  {
167  typedef scratchpad<ast_field> sp;
168  sp::to_vector_functor1<ast, cs_ast_children>
169  functor(inner);
170  return sp::to_vector(functor);
171  }
172 
173  inline std::vector<ast_field> ast::attributes() const
174  {
175  typedef scratchpad<ast_field> sp;
176  sp::to_vector_functor1<ast, cs_ast_attributes>
177  functor(inner);
178  return sp::to_vector(functor);
179  }
180 
181  inline std::string ast::pretty_print(size_t limit) const
182  {
183  string_scratchpad::
184  to_string_functor1_limit<ast, cs_ast_pretty_print>
185  functor(inner, limit);
186  return string_scratchpad::to_string(functor);
187  }
188 
189  inline std::string ast::dump(size_t attribute_depth) const
190  {
191  return cs_ast_dump( inner, attribute_depth );
192  }
193 
195  inline void ast::set_field(const ast_field &c)
196  {
197  cs_ast_field fld = cglue<ast_field>::unwrap(c);
198  check(cs_ast_set_field(inner, &fld));
199  }
202 #ifndef CSFE_API
204  {
205  return ast_iterator(
206  ast_iterator_policy_container(
207  inner, flags));
208  }
209 #endif /* CSFE_API */
210 
211  inline void ast_field::print( std::ostream &out ) const
212  {
213  if( inner.ordinal != csao_null )
214  {
215  out << ordinal().name();
216  out << ":";
217  }
218  switch( inner.type )
219  {
220  case csft_null:
221  out << "null";
222  break;
223  case csft_ast:
224  out << "[" << as_ast().get_class().name() << "] "
225  << as_ast().as_string();
226  break;
227  case csft_ast_class:
228  out << as_ast_class().as_string();
229  break;
230  case csft_boolean:
231  out << (as_boolean() ? "true" : "false");
232  break;
233  case csft_enumeration:
234  out << as_enum_value_string();
235  break;
236  case csft_int8:
237  out << (csint32)as_int8();
238  break;
239  case csft_uint8:
240  out << (csuint32)as_uint8();
241  break;
242  case csft_int16:
243  out << (csint32)as_int16();
244  break;
245  case csft_uint16:
246  out << (csuint32)as_uint16();
247  break;
248  case csft_int32:
249  out << as_int32();
250  break;
251  case csft_uint32:
252  out << as_uint32();
253  break;
254  case csft_int64:
255  out << as_int64();
256  break;
257  case csft_uint64:
258  out << as_uint64();
259  break;
260  case csft_int128:
261  out << "<int128 not implemented>";
262  break;
263  case csft_uint128:
264  out << "<uint128 not implemented>";
265  break;
266  case csft_flt32:
267  out << as_flt32();
268  break;
269  case csft_flt64:
270  out << as_flt64();
271  break;
272  case csft_flt96:
273  out << "<flt96 not implemented>";
274  break;
275  case csft_flt128:
276  out << "<flt128 not implemented>";
277  break;
278  case csft_bytes:
279  cglue<std_string_wrapper>::wrap(inner._.const_str64).print(out);
280  break;
281  case csft_const_str64:
282  out << cglue<std_string_wrapper>::wrap(inner._.const_str64).value;
283  break;
284  case csft_const_str:
285  out << inner._.const_str;
286  break;
287 #ifndef CSFE_API
288  case csft_abs_loc:
289  out << as_symbol();
290  break;
291 #endif /* CSFE_API */
292  case csft_sfid:
293  out << as_sfileinst();
294  break;
295  case csft_reserved:
296  out << inner._.const_str;
297  break;
298  default:
299  out << "<invalid ast_field>";
300  break;
301  }
302  }
303 
304 }
305 
306 #endif /* CS_AST_HPP */
Namespace for CodeSonar/CodeSurfer API.
Definition: cs_ast.hpp:33
std::vector< ast_field > children() const
Get all children of an ast.
Definition: cs_ast.hpp:165
csuint64 as_uint64() const
Get the value from an ast_field of type ast_field_type::UINT64.
Definition: cs_ast_decl.hpp:1406
std::string name() const
Get a string representation of an ast_ordinal.
Definition: cs_ast_decl.hpp:218
ast_iterator traverse(ast_traverse_flags flags=ast_traverse_flags::NONE) const
Get an iterator over an ast.
Definition: cs_ast.hpp:203
ast_class as_ast_class() const
Get the value from an ast_field of type ast_field_type::AST_CLASS.
Definition: cs_ast_decl.hpp:1239
double as_flt64() const
Get the value from an ast_field of type ast_field_type::FLT64.
Definition: cs_ast_decl.hpp:1436
A function or variable.
Definition: cs_symbol_decl.hpp:243
bool has_field(ast_ordinal ord) const
Check: is the designated field present in an ast?
Definition: cs_ast.hpp:109
Describe an error where an ast_field lookup failed.
Definition: cs_ast_decl.hpp:2485
Flag class: specifies what kind of traversal an ast_iterator will carry out.
Definition: cs_ast_decl.hpp:288
csint16 as_int16() const
Get the value from an ast_field of type ast_field_type::INT16.
Definition: cs_ast_decl.hpp:1331
csint8 as_int8() const
Get the value from an ast_field of type ast_field_type::INT8.
Definition: cs_ast_decl.hpp:1301
std::string as_enum_value_string() const
Get the string representation from an ast_field of type ast_field_type::ENUMERATION.
Definition: cs_ast_decl.hpp:1286
std::string dump(size_t attribute_depth=2) const
Get an ASCII art tree rendering of an ast.
Definition: cs_ast.hpp:189
std::string as_string() const
Get a simple string representation of a ast_class object.
Definition: cs_ast_decl.hpp:96
std::string pretty_print(size_t limit=SIZE_MAX) const
Get a pretty-printed version of an ast.
Definition: cs_ast.hpp:181
bool as_boolean() const
Get the value from an ast_field of type ast_field_type::BOOLEAN.
Definition: cs_ast_decl.hpp:1254
ast_field operator[](ast_ordinal ord) const
Get the designated field from an ast.
Definition: cs_ast.hpp:75
Enumeration class for AST ordinals: used to designate a field (ast_field) of an ast.
Definition: cs_ast_decl.hpp:159
std::string as_string() const
Get a simple string representation of a ast_field_not_found_error object.
Definition: cs_ast_decl.hpp:2512
sfileinst as_sfileinst() const
Get the value from an ast_field whose type is ast_field_type::SFID.
Definition: cs_ast.hpp:64
ast as_ast() const
Get the value from an ast_field of type ast_field_type::AST.
Definition: cs_ast_decl.hpp:1224
csuint8 as_uint8() const
Get the value from an ast_field of type ast_field_type::UINT8.
Definition: cs_ast_decl.hpp:1316
csuint16 as_uint16() const
Get the value from an ast_field of type ast_field_type::UINT16.
Definition: cs_ast_decl.hpp:1346
size_t num_fields() const
Get the number of fields in an ast (that is, the number of children plus the number of attributes)...
Definition: cs_ast.hpp:141
csint64 as_int64() const
Get the value from an ast_field of type ast_field_type::INT64.
Definition: cs_ast_decl.hpp:1391
csuint32 as_uint32() const
Get the value from an ast_field of type ast_field_type::UINT32.
Definition: cs_ast_decl.hpp:1376
std::string name() const
Get a string representation of an ast_class name.
Definition: cs_ast_decl.hpp:127
float as_flt32() const
Get the value from an ast_field of type ast_field_type::FLT32.
Definition: cs_ast_decl.hpp:1421
std::vector< ast_field > attributes() const
Get all attributes of an ast.
Definition: cs_ast.hpp:173
A source file instance.
Definition: cs_sfileinst_decl.hpp:302
std::string as_string() const
Get a simple string representation of a ast object.
Definition: cs_ast_decl.hpp:746
A field (child or attribute) of an Abstract Syntax Tree (class ast).
Definition: cs_ast_decl.hpp:898
ast_ordinal ordinal() const
Get the ast_ordinal associated with an ast_field.
Definition: cs_ast_decl.hpp:1196
ast_class get_class() const
Get ast_class to which an ast belongs.
Definition: cs_ast_decl.hpp:492
symbol as_symbol() const
Get the value from an ast_field whose type is ast_field_type::ABS_LOC.
Definition: cs_ast.hpp:44
Iterator over the Abstract Syntax Trees (ASTs, ast) in the tree rooted at a particular ast...
Definition: cs_tplt_instantiations.hpp:585
std::vector< ast_field > fields() const
Get all fields (that is, all children and all attributes) of an ast.
Definition: cs_ast.hpp:157
csint32 as_int32() const
Get the value from an ast_field of type ast_field_type::INT32.
Definition: cs_ast_decl.hpp:1361