ClangASTContext.h revision ced18cd66f0f32ec1d2226005044dd1cd44710c6
1//===-- ClangASTContext.h ---------------------------------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9 10#ifndef liblldb_ClangASTContext_h_ 11#define liblldb_ClangASTContext_h_ 12 13// C Includes 14// C++ Includes 15#include <string> 16#include <vector> 17#include <memory> 18#include <stdint.h> 19 20// Other libraries and framework includes 21// Project includes 22#include "lldb/lldb-enumerations.h" 23#include "lldb/Core/ClangForward.h" 24 25 26namespace lldb_private { 27 28class Declaration; 29 30class ClangASTContext 31{ 32public: 33 //------------------------------------------------------------------ 34 // Constructors and Destructors 35 //------------------------------------------------------------------ 36// ClangASTContext(Module *module); 37 38 ClangASTContext(const char *target_triple); 39 40// ClangASTContext(const ConstString &target_triple); 41 42 ~ClangASTContext(); 43 44 clang::ASTContext * 45 getASTContext(); 46 47 clang::Builtin::Context * 48 getBuiltinContext(); 49 50 clang::IdentifierTable * 51 getIdentifierTable(); 52 53 clang::LangOptions * 54 getLanguageOptions(); 55 56 clang::SelectorTable * 57 getSelectorTable(); 58 59 clang::SourceManager * 60 getSourceManager(); 61 62 clang::Diagnostic * 63 getDiagnostic(); 64 65 clang::TargetOptions * 66 getTargetOptions(); 67 68 clang::TargetInfo * 69 getTargetInfo(); 70 71 void 72 Clear(); 73 74 const char * 75 GetTargetTriple (); 76 77 void 78 SetTargetTriple (const char *target_triple); 79 80 //------------------------------------------------------------------ 81 // Basic Types 82 //------------------------------------------------------------------ 83 84 void * 85 GetBuiltinTypeForEncodingAndBitSize (lldb::Encoding encoding, 86 uint32_t bit_size); 87 88 static void * 89 GetBuiltinTypeForEncodingAndBitSize (clang::ASTContext *ast_context, 90 lldb::Encoding encoding, 91 uint32_t bit_size); 92 93 void * 94 GetBuiltinTypeForDWARFEncodingAndBitSize ( 95 const char *type_name, 96 uint32_t dw_ate, 97 uint32_t bit_size); 98 99 void * 100 GetVoidBuiltInType(); 101 102 void * 103 GetCStringType(bool is_const); 104 105 void * 106 GetVoidPtrType(bool is_const); 107 108 static void * 109 GetVoidPtrType(clang::ASTContext *ast_context, bool is_const); 110 111 static void * 112 CopyType(clang::ASTContext *dest_context, 113 clang::ASTContext *source_context, 114 void * clang_type); 115 116 //------------------------------------------------------------------ 117 // CVR modifiers 118 //------------------------------------------------------------------ 119 120 static void * 121 AddConstModifier (void * clang_type); 122 123 static void * 124 AddRestrictModifier (void * clang_type); 125 126 static void * 127 AddVolatileModifier (void * clang_type); 128 129 //------------------------------------------------------------------ 130 // Structure, Unions, Classes 131 //------------------------------------------------------------------ 132 133 void * 134 CreateRecordType ( 135 const char *name, 136 int kind, 137 clang::DeclContext *decl_ctx); 138 139 bool 140 AddFieldToRecordType ( 141 void * record_qual_type, 142 const char *name, 143 void * field_type, 144 int access, 145 uint32_t bitfield_bit_size); 146 147 bool 148 FieldIsBitfield ( 149 clang::FieldDecl* field, 150 uint32_t& bitfield_bit_size); 151 152 static bool 153 FieldIsBitfield ( 154 clang::ASTContext *ast_context, 155 clang::FieldDecl* field, 156 uint32_t& bitfield_bit_size); 157 158 static bool 159 RecordHasFields (const clang::RecordDecl *record_decl); 160 161 void 162 SetDefaultAccessForRecordFields ( 163 void * clang_qual_type, 164 int default_accessibility, 165 int *assigned_accessibilities, 166 size_t num_assigned_accessibilities); 167 168 //------------------------------------------------------------------ 169 // Aggregate Types 170 //------------------------------------------------------------------ 171 static bool 172 IsAggregateType (void * clang_type); 173 174 static uint32_t 175 GetNumChildren ( 176 void * clang_type, 177 bool omit_empty_base_classes); 178 179 void * 180 GetChildClangTypeAtIndex ( 181 const char *parent_name, 182 void * parent_clang_type, 183 uint32_t idx, 184 bool transparent_pointers, 185 bool omit_empty_base_classes, 186 std::string& child_name, 187 uint32_t &child_byte_size, 188 int32_t &child_byte_offset, 189 uint32_t &child_bitfield_bit_size, 190 uint32_t &child_bitfield_bit_offset); 191 192 static void * 193 GetChildClangTypeAtIndex ( 194 clang::ASTContext *ast_context, 195 const char *parent_name, 196 void * parent_clang_type, 197 uint32_t idx, 198 bool transparent_pointers, 199 bool omit_empty_base_classes, 200 std::string& child_name, 201 uint32_t &child_byte_size, 202 int32_t &child_byte_offset, 203 uint32_t &child_bitfield_bit_size, 204 uint32_t &child_bitfield_bit_offset); 205 206 // Lookup a child given a name. This function will match base class names 207 // and member member names in "clang_type" only, not descendants. 208 static uint32_t 209 GetIndexOfChildWithName (clang::ASTContext *ast_context, 210 void *clang_type, 211 const char *name, 212 bool omit_empty_base_classes); 213 214 // Lookup a child member given a name. This function will match member names 215 // only and will descend into "clang_type" children in search for the first 216 // member in this class, or any base class that matches "name". 217 // TODO: Return all matches for a given name by returning a vector<vector<uint32_t>> 218 // so we catch all names that match a given child name, not just the first. 219 static size_t 220 GetIndexOfChildMemberWithName (clang::ASTContext *ast_context, 221 void *clang_type, 222 const char *name, 223 bool omit_empty_base_classes, 224 std::vector<uint32_t>& child_indexes); 225 226 //------------------------------------------------------------------ 227 // clang::TagType 228 //------------------------------------------------------------------ 229 230 bool 231 SetTagTypeKind ( 232 void * tag_qual_type, 233 int kind); 234 235 //------------------------------------------------------------------ 236 // C++ Base Classes 237 //------------------------------------------------------------------ 238 239 clang::CXXBaseSpecifier * 240 CreateBaseClassSpecifier ( 241 void * base_class_type, 242 int access, 243 bool is_virtual, 244 bool base_of_class); 245 246 bool 247 SetBaseClassesForClassType ( 248 void * class_clang_type, 249 clang::CXXBaseSpecifier const * const *base_classes, 250 unsigned num_base_classes); 251 252 //------------------------------------------------------------------ 253 // DeclContext Functions 254 //------------------------------------------------------------------ 255 256 static clang::DeclContext * 257 GetDeclContextForType (void * qual_type); 258 259 //------------------------------------------------------------------ 260 // Namespace Declarations 261 //------------------------------------------------------------------ 262 263 clang::NamespaceDecl * 264 GetUniqueNamespaceDeclaration ( 265 const char *name, 266 const Declaration &decl, 267 clang::DeclContext *decl_ctx); 268 269 //------------------------------------------------------------------ 270 // Function Types 271 //------------------------------------------------------------------ 272 273 clang::FunctionDecl * 274 CreateFunctionDeclaration ( 275 const char *name, 276 void * function_Type, 277 int storage, 278 bool is_inline); 279 280 void * 281 CreateFunctionType ( 282 void * result_type, 283 void **args, 284 unsigned num_args, 285 bool isVariadic, 286 unsigned TypeQuals); 287 288 clang::ParmVarDecl * 289 CreateParmeterDeclaration ( 290 const char *name, 291 void * return_type, 292 int storage); 293 294 void 295 SetFunctionParameters ( 296 clang::FunctionDecl *function_decl, 297 clang::ParmVarDecl **params, 298 unsigned num_params); 299 300 //------------------------------------------------------------------ 301 // Array Types 302 //------------------------------------------------------------------ 303 304 void * 305 CreateArrayType ( 306 void * element_type, 307 size_t element_count, 308 uint32_t bit_stride); 309 310 //------------------------------------------------------------------ 311 // Tag Declarations 312 //------------------------------------------------------------------ 313 bool 314 StartTagDeclarationDefinition (void * qual_type); 315 316 bool 317 CompleteTagDeclarationDefinition (void * qual_type); 318 319 //------------------------------------------------------------------ 320 // Enumeration Types 321 //------------------------------------------------------------------ 322 void * 323 CreateEnumerationType (const Declaration &decl, const char *name); 324 325 bool 326 AddEnumerationValueToEnumerationType ( 327 void * enum_qual_type, 328 void * enumerator_qual_type, 329 const Declaration &decl, 330 const char *name, 331 int64_t enum_value, 332 uint32_t enum_value_bit_size); 333 334 //------------------------------------------------------------------ 335 // Pointers & References 336 //------------------------------------------------------------------ 337 void * 338 CreatePointerType (void * clang_type); 339 340 void * 341 CreateLValueReferenceType (void * clang_type); 342 343 void * 344 CreateRValueReferenceType (void * clang_type); 345 346 size_t 347 GetPointerBitSize (); 348 349 static size_t 350 GetTypeBitSize (clang::ASTContext *ast_context, void * clang_type); 351 352 static size_t 353 GetTypeBitAlign (clang::ASTContext *ast_context, void * clang_type); 354 355 static bool 356 IsIntegerType (void * clang_type, bool &is_signed); 357 358 static bool 359 IsPointerType (void * clang_type, void **target_type = NULL); 360 361 static bool 362 IsPointerOrReferenceType (void * clang_type, void **target_type = NULL); 363 364 static bool 365 IsCStringType (void * clang_type, uint32_t &length); 366 367 static bool 368 IsArrayType (void * clang_type, void **member_type = NULL, uint64_t *size = NULL); 369 370 //------------------------------------------------------------------ 371 // Typedefs 372 //------------------------------------------------------------------ 373 void * 374 CreateTypedefType ( 375 const char *name, 376 void * clang_type, 377 clang::DeclContext *decl_ctx); 378 379 //------------------------------------------------------------------ 380 // Type names 381 //------------------------------------------------------------------ 382 static std::string 383 GetTypeName(void *clang_type); 384 385 static bool 386 IsFloatingPointType (void * clang_type, uint32_t &count, bool &is_complex); 387 388 //static bool 389 //ConvertFloatValueToString (clang::ASTContext *ast_context, void * clang_type, const uint8_t* bytes, size_t byte_size, int apint_byte_order, std::string &float_str); 390 391 static size_t 392 ConvertStringToFloatValue (clang::ASTContext *ast_context, void * clang_type, const char *s, uint8_t *dst, size_t dst_size); 393 394protected: 395 //------------------------------------------------------------------ 396 // Classes that inherit from ClangASTContext can see and modify these 397 //------------------------------------------------------------------ 398 std::string m_target_triple; 399 std::auto_ptr<clang::ASTContext> m_ast_context_ap; 400 std::auto_ptr<clang::LangOptions> m_language_options_ap; 401 std::auto_ptr<clang::SourceManager> m_source_manager_ap; 402 std::auto_ptr<clang::Diagnostic> m_diagnostic_ap; 403 std::auto_ptr<clang::TargetOptions> m_target_options_ap; 404 std::auto_ptr<clang::TargetInfo> m_target_info_ap; 405 std::auto_ptr<clang::IdentifierTable> m_identifier_table_ap; 406 std::auto_ptr<clang::SelectorTable> m_selector_table_ap; 407 std::auto_ptr<clang::Builtin::Context> m_builtins_ap; 408 409private: 410 //------------------------------------------------------------------ 411 // For ClangASTContext only 412 //------------------------------------------------------------------ 413 ClangASTContext(const ClangASTContext&); 414 const ClangASTContext& operator=(const ClangASTContext&); 415}; 416 417} // namespace lldb_private 418 419#endif // liblldb_ClangASTContext_h_ 420