DebugInfo: Introduce the notion of "form classes"
[oota-llvm.git] / include / llvm / DebugInfo / DWARFFormValue.h
1 //===-- DWARFFormValue.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 LLVM_DEBUGINFO_DWARFFORMVALUE_H
11 #define LLVM_DEBUGINFO_DWARFFORMVALUE_H
12
13 #include "llvm/ADT/Optional.h"
14 #include "llvm/Support/DataExtractor.h"
15
16 namespace llvm {
17
18 class DWARFUnit;
19 class raw_ostream;
20
21 class DWARFFormValue {
22 public:
23   enum FormClass {
24     FC_Unknown,
25     FC_Address,
26     FC_Block,
27     FC_Constant,
28     FC_String,
29     FC_Flag,
30     FC_Reference,
31     FC_Indirect,
32     FC_SectionOffset,
33     FC_Exprloc
34   };
35
36 private:
37   struct ValueType {
38     ValueType() : data(NULL) {
39       uval = 0;
40     }
41
42     union {
43       uint64_t uval;
44       int64_t sval;
45       const char* cstr;
46     };
47     const uint8_t* data;
48   };
49
50   uint16_t Form;   // Form for this value.
51   ValueType Value; // Contains all data for the form.
52
53 public:
54   DWARFFormValue(uint16_t Form = 0) : Form(Form) {}
55   uint16_t getForm() const { return Form; }
56   bool isFormClass(FormClass FC) const;
57
58   void dump(raw_ostream &OS, const DWARFUnit *U) const;
59   bool extractValue(DataExtractor data, uint32_t *offset_ptr,
60                     const DWARFUnit *u);
61   bool isInlinedCStr() const {
62     return Value.data != NULL && Value.data == (const uint8_t*)Value.cstr;
63   }
64
65   /// getAsFoo functions below return the extracted value as Foo if only
66   /// DWARFFormValue has form class is suitable for representing Foo.
67   Optional<uint64_t> getAsReference(const DWARFUnit *U) const;
68   Optional<uint64_t> getAsUnsignedConstant() const;
69   Optional<const char *> getAsCString(const DWARFUnit *U) const;
70   Optional<uint64_t> getAsAddress(const DWARFUnit *U) const;
71   Optional<uint64_t> getAsSectionOffset() const;
72
73   bool skipValue(DataExtractor debug_info_data, uint32_t *offset_ptr,
74                  const DWARFUnit *u) const;
75   static bool skipValue(uint16_t form, DataExtractor debug_info_data,
76                         uint32_t *offset_ptr, const DWARFUnit *u);
77
78   static const uint8_t *getFixedFormSizes(uint8_t AddrSize, uint16_t Version);
79 };
80
81 }
82
83 #endif