xref: /minix3/external/bsd/llvm/dist/clang/lib/Frontend/LayoutOverrideSource.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc //===--- LayoutOverrideSource.cpp --Override Record Layouts ---------------===//
2*f4a2713aSLionel Sambuc //
3*f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4*f4a2713aSLionel Sambuc //
5*f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6*f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7*f4a2713aSLionel Sambuc //
8*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9*f4a2713aSLionel Sambuc #include "clang/Frontend/LayoutOverrideSource.h"
10*f4a2713aSLionel Sambuc #include "clang/AST/Decl.h"
11*f4a2713aSLionel Sambuc #include "clang/Basic/CharInfo.h"
12*f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
13*f4a2713aSLionel Sambuc #include <fstream>
14*f4a2713aSLionel Sambuc #include <string>
15*f4a2713aSLionel Sambuc 
16*f4a2713aSLionel Sambuc using namespace clang;
17*f4a2713aSLionel Sambuc 
18*f4a2713aSLionel Sambuc /// \brief Parse a simple identifier.
parseName(StringRef S)19*f4a2713aSLionel Sambuc static std::string parseName(StringRef S) {
20*f4a2713aSLionel Sambuc   if (S.empty() || !isIdentifierHead(S[0]))
21*f4a2713aSLionel Sambuc     return "";
22*f4a2713aSLionel Sambuc 
23*f4a2713aSLionel Sambuc   unsigned Offset = 1;
24*f4a2713aSLionel Sambuc   while (Offset < S.size() && isIdentifierBody(S[Offset]))
25*f4a2713aSLionel Sambuc     ++Offset;
26*f4a2713aSLionel Sambuc 
27*f4a2713aSLionel Sambuc   return S.substr(0, Offset).str();
28*f4a2713aSLionel Sambuc }
29*f4a2713aSLionel Sambuc 
LayoutOverrideSource(StringRef Filename)30*f4a2713aSLionel Sambuc LayoutOverrideSource::LayoutOverrideSource(StringRef Filename) {
31*f4a2713aSLionel Sambuc   std::ifstream Input(Filename.str().c_str());
32*f4a2713aSLionel Sambuc   if (!Input.is_open())
33*f4a2713aSLionel Sambuc     return;
34*f4a2713aSLionel Sambuc 
35*f4a2713aSLionel Sambuc   // Parse the output of -fdump-record-layouts.
36*f4a2713aSLionel Sambuc   std::string CurrentType;
37*f4a2713aSLionel Sambuc   Layout CurrentLayout;
38*f4a2713aSLionel Sambuc   bool ExpectingType = false;
39*f4a2713aSLionel Sambuc 
40*f4a2713aSLionel Sambuc   while (Input.good()) {
41*f4a2713aSLionel Sambuc     std::string Line;
42*f4a2713aSLionel Sambuc     getline(Input, Line);
43*f4a2713aSLionel Sambuc 
44*f4a2713aSLionel Sambuc     StringRef LineStr(Line);
45*f4a2713aSLionel Sambuc 
46*f4a2713aSLionel Sambuc     // Determine whether the following line will start a
47*f4a2713aSLionel Sambuc     if (LineStr.find("*** Dumping AST Record Layout") != StringRef::npos)  {
48*f4a2713aSLionel Sambuc       // Flush the last type/layout, if there is one.
49*f4a2713aSLionel Sambuc       if (!CurrentType.empty())
50*f4a2713aSLionel Sambuc         Layouts[CurrentType] = CurrentLayout;
51*f4a2713aSLionel Sambuc       CurrentLayout = Layout();
52*f4a2713aSLionel Sambuc 
53*f4a2713aSLionel Sambuc       ExpectingType = true;
54*f4a2713aSLionel Sambuc       continue;
55*f4a2713aSLionel Sambuc     }
56*f4a2713aSLionel Sambuc 
57*f4a2713aSLionel Sambuc     // If we're expecting a type, grab it.
58*f4a2713aSLionel Sambuc     if (ExpectingType) {
59*f4a2713aSLionel Sambuc       ExpectingType = false;
60*f4a2713aSLionel Sambuc 
61*f4a2713aSLionel Sambuc       StringRef::size_type Pos;
62*f4a2713aSLionel Sambuc       if ((Pos = LineStr.find("struct ")) != StringRef::npos)
63*f4a2713aSLionel Sambuc         LineStr = LineStr.substr(Pos + strlen("struct "));
64*f4a2713aSLionel Sambuc       else if ((Pos = LineStr.find("class ")) != StringRef::npos)
65*f4a2713aSLionel Sambuc         LineStr = LineStr.substr(Pos + strlen("class "));
66*f4a2713aSLionel Sambuc       else if ((Pos = LineStr.find("union ")) != StringRef::npos)
67*f4a2713aSLionel Sambuc         LineStr = LineStr.substr(Pos + strlen("union "));
68*f4a2713aSLionel Sambuc       else
69*f4a2713aSLionel Sambuc         continue;
70*f4a2713aSLionel Sambuc 
71*f4a2713aSLionel Sambuc       // Find the name of the type.
72*f4a2713aSLionel Sambuc       CurrentType = parseName(LineStr);
73*f4a2713aSLionel Sambuc       CurrentLayout = Layout();
74*f4a2713aSLionel Sambuc       continue;
75*f4a2713aSLionel Sambuc     }
76*f4a2713aSLionel Sambuc 
77*f4a2713aSLionel Sambuc     // Check for the size of the type.
78*f4a2713aSLionel Sambuc     StringRef::size_type Pos = LineStr.find(" Size:");
79*f4a2713aSLionel Sambuc     if (Pos != StringRef::npos) {
80*f4a2713aSLionel Sambuc       // Skip past the " Size:" prefix.
81*f4a2713aSLionel Sambuc       LineStr = LineStr.substr(Pos + strlen(" Size:"));
82*f4a2713aSLionel Sambuc 
83*f4a2713aSLionel Sambuc       unsigned long long Size = 0;
84*f4a2713aSLionel Sambuc       (void)LineStr.getAsInteger(10, Size);
85*f4a2713aSLionel Sambuc       CurrentLayout.Size = Size;
86*f4a2713aSLionel Sambuc       continue;
87*f4a2713aSLionel Sambuc     }
88*f4a2713aSLionel Sambuc 
89*f4a2713aSLionel Sambuc     // Check for the alignment of the type.
90*f4a2713aSLionel Sambuc     Pos = LineStr.find("Alignment:");
91*f4a2713aSLionel Sambuc     if (Pos != StringRef::npos) {
92*f4a2713aSLionel Sambuc       // Skip past the "Alignment:" prefix.
93*f4a2713aSLionel Sambuc       LineStr = LineStr.substr(Pos + strlen("Alignment:"));
94*f4a2713aSLionel Sambuc 
95*f4a2713aSLionel Sambuc       unsigned long long Alignment = 0;
96*f4a2713aSLionel Sambuc       (void)LineStr.getAsInteger(10, Alignment);
97*f4a2713aSLionel Sambuc       CurrentLayout.Align = Alignment;
98*f4a2713aSLionel Sambuc       continue;
99*f4a2713aSLionel Sambuc     }
100*f4a2713aSLionel Sambuc 
101*f4a2713aSLionel Sambuc     // Check for the size/alignment of the type.
102*f4a2713aSLionel Sambuc     Pos = LineStr.find("sizeof=");
103*f4a2713aSLionel Sambuc     if (Pos != StringRef::npos) {
104*f4a2713aSLionel Sambuc       /* Skip past the sizeof= prefix. */
105*f4a2713aSLionel Sambuc       LineStr = LineStr.substr(Pos + strlen("sizeof="));
106*f4a2713aSLionel Sambuc 
107*f4a2713aSLionel Sambuc       // Parse size.
108*f4a2713aSLionel Sambuc       unsigned long long Size = 0;
109*f4a2713aSLionel Sambuc       (void)LineStr.getAsInteger(10, Size);
110*f4a2713aSLionel Sambuc       CurrentLayout.Size = Size;
111*f4a2713aSLionel Sambuc 
112*f4a2713aSLionel Sambuc       Pos = LineStr.find("align=");
113*f4a2713aSLionel Sambuc       if (Pos != StringRef::npos) {
114*f4a2713aSLionel Sambuc         /* Skip past the align= prefix. */
115*f4a2713aSLionel Sambuc         LineStr = LineStr.substr(Pos + strlen("align="));
116*f4a2713aSLionel Sambuc 
117*f4a2713aSLionel Sambuc         // Parse alignment.
118*f4a2713aSLionel Sambuc         unsigned long long Alignment = 0;
119*f4a2713aSLionel Sambuc         (void)LineStr.getAsInteger(10, Alignment);
120*f4a2713aSLionel Sambuc         CurrentLayout.Align = Alignment;
121*f4a2713aSLionel Sambuc       }
122*f4a2713aSLionel Sambuc 
123*f4a2713aSLionel Sambuc       continue;
124*f4a2713aSLionel Sambuc     }
125*f4a2713aSLionel Sambuc 
126*f4a2713aSLionel Sambuc     // Check for the field offsets of the type.
127*f4a2713aSLionel Sambuc     Pos = LineStr.find("FieldOffsets: [");
128*f4a2713aSLionel Sambuc     if (Pos == StringRef::npos)
129*f4a2713aSLionel Sambuc       continue;
130*f4a2713aSLionel Sambuc 
131*f4a2713aSLionel Sambuc     LineStr = LineStr.substr(Pos + strlen("FieldOffsets: ["));
132*f4a2713aSLionel Sambuc     while (!LineStr.empty() && isDigit(LineStr[0])) {
133*f4a2713aSLionel Sambuc       // Parse this offset.
134*f4a2713aSLionel Sambuc       unsigned Idx = 1;
135*f4a2713aSLionel Sambuc       while (Idx < LineStr.size() && isDigit(LineStr[Idx]))
136*f4a2713aSLionel Sambuc         ++Idx;
137*f4a2713aSLionel Sambuc 
138*f4a2713aSLionel Sambuc       unsigned long long Offset = 0;
139*f4a2713aSLionel Sambuc       (void)LineStr.substr(0, Idx).getAsInteger(10, Offset);
140*f4a2713aSLionel Sambuc 
141*f4a2713aSLionel Sambuc       CurrentLayout.FieldOffsets.push_back(Offset);
142*f4a2713aSLionel Sambuc 
143*f4a2713aSLionel Sambuc       // Skip over this offset, the following comma, and any spaces.
144*f4a2713aSLionel Sambuc       LineStr = LineStr.substr(Idx + 1);
145*f4a2713aSLionel Sambuc       while (!LineStr.empty() && isWhitespace(LineStr[0]))
146*f4a2713aSLionel Sambuc         LineStr = LineStr.substr(1);
147*f4a2713aSLionel Sambuc     }
148*f4a2713aSLionel Sambuc   }
149*f4a2713aSLionel Sambuc 
150*f4a2713aSLionel Sambuc   // Flush the last type/layout, if there is one.
151*f4a2713aSLionel Sambuc   if (!CurrentType.empty())
152*f4a2713aSLionel Sambuc     Layouts[CurrentType] = CurrentLayout;
153*f4a2713aSLionel Sambuc }
154*f4a2713aSLionel Sambuc 
155*f4a2713aSLionel Sambuc bool
layoutRecordType(const RecordDecl * Record,uint64_t & Size,uint64_t & Alignment,llvm::DenseMap<const FieldDecl *,uint64_t> & FieldOffsets,llvm::DenseMap<const CXXRecordDecl *,CharUnits> & BaseOffsets,llvm::DenseMap<const CXXRecordDecl *,CharUnits> & VirtualBaseOffsets)156*f4a2713aSLionel Sambuc LayoutOverrideSource::layoutRecordType(const RecordDecl *Record,
157*f4a2713aSLionel Sambuc   uint64_t &Size, uint64_t &Alignment,
158*f4a2713aSLionel Sambuc   llvm::DenseMap<const FieldDecl *, uint64_t> &FieldOffsets,
159*f4a2713aSLionel Sambuc   llvm::DenseMap<const CXXRecordDecl *, CharUnits> &BaseOffsets,
160*f4a2713aSLionel Sambuc   llvm::DenseMap<const CXXRecordDecl *, CharUnits> &VirtualBaseOffsets)
161*f4a2713aSLionel Sambuc {
162*f4a2713aSLionel Sambuc   // We can't override unnamed declarations.
163*f4a2713aSLionel Sambuc   if (!Record->getIdentifier())
164*f4a2713aSLionel Sambuc     return false;
165*f4a2713aSLionel Sambuc 
166*f4a2713aSLionel Sambuc   // Check whether we have a layout for this record.
167*f4a2713aSLionel Sambuc   llvm::StringMap<Layout>::iterator Known = Layouts.find(Record->getName());
168*f4a2713aSLionel Sambuc   if (Known == Layouts.end())
169*f4a2713aSLionel Sambuc     return false;
170*f4a2713aSLionel Sambuc 
171*f4a2713aSLionel Sambuc   // Provide field layouts.
172*f4a2713aSLionel Sambuc   unsigned NumFields = 0;
173*f4a2713aSLionel Sambuc   for (RecordDecl::field_iterator F = Record->field_begin(),
174*f4a2713aSLionel Sambuc                                FEnd = Record->field_end();
175*f4a2713aSLionel Sambuc        F != FEnd; ++F, ++NumFields) {
176*f4a2713aSLionel Sambuc     if (NumFields >= Known->second.FieldOffsets.size())
177*f4a2713aSLionel Sambuc       continue;
178*f4a2713aSLionel Sambuc 
179*f4a2713aSLionel Sambuc     FieldOffsets[*F] = Known->second.FieldOffsets[NumFields];
180*f4a2713aSLionel Sambuc   }
181*f4a2713aSLionel Sambuc 
182*f4a2713aSLionel Sambuc   // Wrong number of fields.
183*f4a2713aSLionel Sambuc   if (NumFields != Known->second.FieldOffsets.size())
184*f4a2713aSLionel Sambuc     return false;
185*f4a2713aSLionel Sambuc 
186*f4a2713aSLionel Sambuc   Size = Known->second.Size;
187*f4a2713aSLionel Sambuc   Alignment = Known->second.Align;
188*f4a2713aSLionel Sambuc   return true;
189*f4a2713aSLionel Sambuc }
190*f4a2713aSLionel Sambuc 
dump()191*f4a2713aSLionel Sambuc void LayoutOverrideSource::dump() {
192*f4a2713aSLionel Sambuc   raw_ostream &OS = llvm::errs();
193*f4a2713aSLionel Sambuc   for (llvm::StringMap<Layout>::iterator L = Layouts.begin(),
194*f4a2713aSLionel Sambuc                                       LEnd = Layouts.end();
195*f4a2713aSLionel Sambuc        L != LEnd; ++L) {
196*f4a2713aSLionel Sambuc     OS << "Type: blah " << L->first() << '\n';
197*f4a2713aSLionel Sambuc     OS << "  Size:" << L->second.Size << '\n';
198*f4a2713aSLionel Sambuc     OS << "  Alignment:" << L->second.Align << '\n';
199*f4a2713aSLionel Sambuc     OS << "  FieldOffsets: [";
200*f4a2713aSLionel Sambuc     for (unsigned I = 0, N = L->second.FieldOffsets.size(); I != N; ++I) {
201*f4a2713aSLionel Sambuc       if (I)
202*f4a2713aSLionel Sambuc         OS << ", ";
203*f4a2713aSLionel Sambuc       OS << L->second.FieldOffsets[I];
204*f4a2713aSLionel Sambuc     }
205*f4a2713aSLionel Sambuc     OS << "]\n";
206*f4a2713aSLionel Sambuc   }
207*f4a2713aSLionel Sambuc }
208*f4a2713aSLionel Sambuc 
209