llvm-mc/Mach-O: Improve symbol table support:
[oota-llvm.git] / include / llvm / MC / MCSymbol.h
1 //===- MCSymbol.h - Machine Code Symbols ------------------------*- 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 // This file contains the declaration of the MCSymbol class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_MC_MCSYMBOL_H
15 #define LLVM_MC_MCSYMBOL_H
16
17 #include <string>
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Support/DataTypes.h"
20
21 namespace llvm {
22   class MCSection;
23   class MCContext;
24   class raw_ostream;
25
26   /// MCSymbol - Instances of this class represent a symbol name in the MC file,
27   /// and MCSymbols are created and unique'd by the MCContext class.
28   ///
29   /// If the symbol is defined/emitted into the current translation unit, the
30   /// Section member is set to indicate what section it lives in.  Otherwise, if
31   /// it is a reference to an external entity, it has a null section.  
32   /// 
33   class MCSymbol {
34     // Special sentinal value for the absolute pseudo section.
35     //
36     // FIXME: Use a PointerInt wrapper for this?
37     static const MCSection *AbsolutePseudoSection;
38
39     /// Name - The name of the symbol.
40     std::string Name;
41
42     /// Section - The section the symbol is defined in. This is null for
43     /// undefined symbols, and the special AbsolutePseudoSection value for
44     /// absolute symbols.
45     const MCSection *Section;
46
47     /// IsTemporary - True if this is an assembler temporary label, which
48     /// typically does not survive in the .o file's symbol table.  Usually
49     /// "Lfoo" or ".foo".
50     unsigned IsTemporary : 1;
51
52   private:  // MCContext creates and uniques these.
53     friend class MCContext;
54     MCSymbol(const StringRef &_Name, bool _IsTemporary) 
55       : Name(_Name), Section(0), IsTemporary(_IsTemporary) {}
56     
57     MCSymbol(const MCSymbol&);       // DO NOT IMPLEMENT
58     void operator=(const MCSymbol&); // DO NOT IMPLEMENT
59   public:
60     /// getName - Get the symbol name.
61     const std::string &getName() const { return Name; }
62
63     /// @name Symbol Type
64     /// @{
65
66     /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
67     bool isUndefined() const {
68       return Section == 0;
69     }
70
71     /// isAbsolute - Check if this this is an absolute symbol.
72     bool isAbsolute() const {
73       return Section == AbsolutePseudoSection;
74     }
75
76     /// getSection - Get the section associated with a defined, non-absolute
77     /// symbol.
78     const MCSection &getSection() const {
79       assert(!isUndefined() && !isAbsolute() && "Invalid accessor!");
80       return *Section;
81     }
82
83     /// setSection - Mark the symbol as defined in the section \arg S.
84     void setSection(const MCSection &S) { Section = &S; }
85
86     /// setUndefined - Mark the symbol as undefined.
87     void setUndefined() {
88       Section = 0;
89     }
90
91     /// setAbsolute - Mark the symbol as absolute.
92     void setAbsolute() { Section = AbsolutePseudoSection; }
93
94     /// @}
95
96     /// print - Print the value to the stream \arg OS.
97     void print(raw_ostream &OS) const;
98
99     /// dump - Print the value to stderr.
100     void dump() const;
101   };
102
103 } // end namespace llvm
104
105 #endif