1 //===--- CodeGenTypes.cpp - Type translation for LLVM CodeGen -------------===//
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 is the code that handles AST -> LLVM type lowering.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "CodeGenTypes.h"
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/DeclObjC.h"
17 #include "clang/AST/DeclCXX.h"
18 #include "clang/AST/Expr.h"
19 #include "clang/AST/RecordLayout.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/Module.h"
22 #include "llvm/Target/TargetData.h"
23 
24 #include "CGCall.h"
25 #include "CGRecordLayoutBuilder.h"
26 
27 using namespace clang;
28 using namespace CodeGen;
29 
30 CodeGenTypes::CodeGenTypes(ASTContext &Ctx, llvm::Module& M,
31                            const llvm::TargetData &TD, const ABIInfo &Info)
32   : Context(Ctx), Target(Ctx.Target), TheModule(M), TheTargetData(TD),
33     TheABIInfo(Info) {
34 }
35 
36 CodeGenTypes::~CodeGenTypes() {
37   for (llvm::DenseMap<const Type *, CGRecordLayout *>::iterator
38          I = CGRecordLayouts.begin(), E = CGRecordLayouts.end();
39       I != E; ++I)
40     delete I->second;
41 
42   for (llvm::FoldingSet<CGFunctionInfo>::iterator
43        I = FunctionInfos.begin(), E = FunctionInfos.end(); I != E; )
44     delete &*I++;
45 }
46 
47 /// ConvertType - Convert the specified type to its LLVM form.
48 const llvm::Type *CodeGenTypes::ConvertType(QualType T) {
49   llvm::PATypeHolder Result = ConvertTypeRecursive(T);
50 
51   // Any pointers that were converted defered evaluation of their pointee type,
52   // creating an opaque type instead.  This is in order to avoid problems with
53   // circular types.  Loop through all these defered pointees, if any, and
54   // resolve them now.
55   while (!PointersToResolve.empty()) {
56     std::pair<QualType, llvm::OpaqueType*> P = PointersToResolve.pop_back_val();
57 
58     // We can handle bare pointers here because we know that the only pointers
59     // to the Opaque type are P.second and from other types.  Refining the
60     // opqaue type away will invalidate P.second, but we don't mind :).
61     const llvm::Type *NT = ConvertTypeForMemRecursive(P.first);
62     P.second->refineAbstractTypeTo(NT);
63   }
64 
65   return Result;
66 }
67 
68 const llvm::Type *CodeGenTypes::ConvertTypeRecursive(QualType T) {
69   T = Context.getCanonicalType(T);
70 
71   // See if type is already cached.
72   llvm::DenseMap<Type *, llvm::PATypeHolder>::iterator
73     I = TypeCache.find(T.getTypePtr());
74   // If type is found in map and this is not a definition for a opaque
75   // place holder type then use it. Otherwise, convert type T.
76   if (I != TypeCache.end())
77     return I->second.get();
78 
79   const llvm::Type *ResultType = ConvertNewType(T);
80   TypeCache.insert(std::make_pair(T.getTypePtr(),
81                                   llvm::PATypeHolder(ResultType)));
82   return ResultType;
83 }
84 
85 const llvm::Type *CodeGenTypes::ConvertTypeForMemRecursive(QualType T) {
86   const llvm::Type *ResultType = ConvertTypeRecursive(T);
87   if (ResultType->isInteger(1))
88     return llvm::IntegerType::get(getLLVMContext(),
89                                   (unsigned)Context.getTypeSize(T));
90   // FIXME: Should assert that the llvm type and AST type has the same size.
91   return ResultType;
92 }
93 
94 /// ConvertTypeForMem - Convert type T into a llvm::Type.  This differs from
95 /// ConvertType in that it is used to convert to the memory representation for
96 /// a type.  For example, the scalar representation for _Bool is i1, but the
97 /// memory representation is usually i8 or i32, depending on the target.
98 const llvm::Type *CodeGenTypes::ConvertTypeForMem(QualType T) {
99   const llvm::Type *R = ConvertType(T);
100 
101   // If this is a non-bool type, don't map it.
102   if (!R->isInteger(1))
103     return R;
104 
105   // Otherwise, return an integer of the target-specified size.
106   return llvm::IntegerType::get(getLLVMContext(),
107                                 (unsigned)Context.getTypeSize(T));
108 
109 }
110 
111 // Code to verify a given function type is complete, i.e. the return type
112 // and all of the argument types are complete.
113 static const TagType *VerifyFuncTypeComplete(const Type* T) {
114   const FunctionType *FT = cast<FunctionType>(T);
115   if (const TagType* TT = FT->getResultType()->getAs<TagType>())
116     if (!TT->getDecl()->isDefinition())
117       return TT;
118   if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(T))
119     for (unsigned i = 0; i < FPT->getNumArgs(); i++)
120       if (const TagType* TT = FPT->getArgType(i)->getAs<TagType>())
121         if (!TT->getDecl()->isDefinition())
122           return TT;
123   return 0;
124 }
125 
126 /// UpdateCompletedType - When we find the full definition for a TagDecl,
127 /// replace the 'opaque' type we previously made for it if applicable.
128 void CodeGenTypes::UpdateCompletedType(const TagDecl *TD) {
129   const Type *Key = Context.getTagDeclType(TD).getTypePtr();
130   llvm::DenseMap<const Type*, llvm::PATypeHolder>::iterator TDTI =
131     TagDeclTypes.find(Key);
132   if (TDTI == TagDeclTypes.end()) return;
133 
134   // Remember the opaque LLVM type for this tagdecl.
135   llvm::PATypeHolder OpaqueHolder = TDTI->second;
136   assert(isa<llvm::OpaqueType>(OpaqueHolder.get()) &&
137          "Updating compilation of an already non-opaque type?");
138 
139   // Remove it from TagDeclTypes so that it will be regenerated.
140   TagDeclTypes.erase(TDTI);
141 
142   // Generate the new type.
143   const llvm::Type *NT = ConvertTagDeclType(TD);
144 
145   // Refine the old opaque type to its new definition.
146   cast<llvm::OpaqueType>(OpaqueHolder.get())->refineAbstractTypeTo(NT);
147 
148   // Since we just completed a tag type, check to see if any function types
149   // were completed along with the tag type.
150   // FIXME: This is very inefficient; if we track which function types depend
151   // on which tag types, though, it should be reasonably efficient.
152   llvm::DenseMap<const Type*, llvm::PATypeHolder>::iterator i;
153   for (i = FunctionTypes.begin(); i != FunctionTypes.end(); ++i) {
154     if (const TagType* TT = VerifyFuncTypeComplete(i->first)) {
155       // This function type still depends on an incomplete tag type; make sure
156       // that tag type has an associated opaque type.
157       ConvertTagDeclType(TT->getDecl());
158     } else {
159       // This function no longer depends on an incomplete tag type; create the
160       // function type, and refine the opaque type to the new function type.
161       llvm::PATypeHolder OpaqueHolder = i->second;
162       const llvm::Type *NFT = ConvertNewType(QualType(i->first, 0));
163       cast<llvm::OpaqueType>(OpaqueHolder.get())->refineAbstractTypeTo(NFT);
164       FunctionTypes.erase(i);
165     }
166   }
167 }
168 
169 static const llvm::Type* getTypeForFormat(llvm::LLVMContext &VMContext,
170                                           const llvm::fltSemantics &format) {
171   if (&format == &llvm::APFloat::IEEEsingle)
172     return llvm::Type::getFloatTy(VMContext);
173   if (&format == &llvm::APFloat::IEEEdouble)
174     return llvm::Type::getDoubleTy(VMContext);
175   if (&format == &llvm::APFloat::IEEEquad)
176     return llvm::Type::getFP128Ty(VMContext);
177   if (&format == &llvm::APFloat::PPCDoubleDouble)
178     return llvm::Type::getPPC_FP128Ty(VMContext);
179   if (&format == &llvm::APFloat::x87DoubleExtended)
180     return llvm::Type::getX86_FP80Ty(VMContext);
181   assert(0 && "Unknown float format!");
182   return 0;
183 }
184 
185 const llvm::Type *CodeGenTypes::ConvertNewType(QualType T) {
186   const clang::Type &Ty = *Context.getCanonicalType(T).getTypePtr();
187 
188   switch (Ty.getTypeClass()) {
189 #define TYPE(Class, Base)
190 #define ABSTRACT_TYPE(Class, Base)
191 #define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
192 #define DEPENDENT_TYPE(Class, Base) case Type::Class:
193 #include "clang/AST/TypeNodes.def"
194     assert(false && "Non-canonical or dependent types aren't possible.");
195     break;
196 
197   case Type::Builtin: {
198     switch (cast<BuiltinType>(Ty).getKind()) {
199     case BuiltinType::Void:
200     case BuiltinType::ObjCId:
201     case BuiltinType::ObjCClass:
202     case BuiltinType::ObjCSel:
203       // LLVM void type can only be used as the result of a function call.  Just
204       // map to the same as char.
205       return llvm::IntegerType::get(getLLVMContext(), 8);
206 
207     case BuiltinType::Bool:
208       // Note that we always return bool as i1 for use as a scalar type.
209       return llvm::Type::getInt1Ty(getLLVMContext());
210 
211     case BuiltinType::Char_S:
212     case BuiltinType::Char_U:
213     case BuiltinType::SChar:
214     case BuiltinType::UChar:
215     case BuiltinType::Short:
216     case BuiltinType::UShort:
217     case BuiltinType::Int:
218     case BuiltinType::UInt:
219     case BuiltinType::Long:
220     case BuiltinType::ULong:
221     case BuiltinType::LongLong:
222     case BuiltinType::ULongLong:
223     case BuiltinType::WChar:
224     case BuiltinType::Char16:
225     case BuiltinType::Char32:
226       return llvm::IntegerType::get(getLLVMContext(),
227         static_cast<unsigned>(Context.getTypeSize(T)));
228 
229     case BuiltinType::Float:
230     case BuiltinType::Double:
231     case BuiltinType::LongDouble:
232       return getTypeForFormat(getLLVMContext(),
233                               Context.getFloatTypeSemantics(T));
234 
235     case BuiltinType::NullPtr: {
236       // Model std::nullptr_t as i8*
237       const llvm::Type *Ty = llvm::IntegerType::get(getLLVMContext(), 8);
238       return llvm::PointerType::getUnqual(Ty);
239     }
240 
241     case BuiltinType::UInt128:
242     case BuiltinType::Int128:
243       return llvm::IntegerType::get(getLLVMContext(), 128);
244 
245     case BuiltinType::Overload:
246     case BuiltinType::Dependent:
247     case BuiltinType::UndeducedAuto:
248       assert(0 && "Unexpected builtin type!");
249       break;
250     }
251     assert(0 && "Unknown builtin type!");
252     break;
253   }
254   case Type::Complex: {
255     const llvm::Type *EltTy =
256       ConvertTypeRecursive(cast<ComplexType>(Ty).getElementType());
257     return llvm::StructType::get(TheModule.getContext(), EltTy, EltTy, NULL);
258   }
259   case Type::LValueReference:
260   case Type::RValueReference: {
261     const ReferenceType &RTy = cast<ReferenceType>(Ty);
262     QualType ETy = RTy.getPointeeType();
263     llvm::OpaqueType *PointeeType = llvm::OpaqueType::get(getLLVMContext());
264     PointersToResolve.push_back(std::make_pair(ETy, PointeeType));
265     return llvm::PointerType::get(PointeeType, ETy.getAddressSpace());
266   }
267   case Type::Pointer: {
268     const PointerType &PTy = cast<PointerType>(Ty);
269     QualType ETy = PTy.getPointeeType();
270     llvm::OpaqueType *PointeeType = llvm::OpaqueType::get(getLLVMContext());
271     PointersToResolve.push_back(std::make_pair(ETy, PointeeType));
272     return llvm::PointerType::get(PointeeType, ETy.getAddressSpace());
273   }
274 
275   case Type::VariableArray: {
276     const VariableArrayType &A = cast<VariableArrayType>(Ty);
277     assert(A.getIndexTypeCVRQualifiers() == 0 &&
278            "FIXME: We only handle trivial array types so far!");
279     // VLAs resolve to the innermost element type; this matches
280     // the return of alloca, and there isn't any obviously better choice.
281     return ConvertTypeForMemRecursive(A.getElementType());
282   }
283   case Type::IncompleteArray: {
284     const IncompleteArrayType &A = cast<IncompleteArrayType>(Ty);
285     assert(A.getIndexTypeCVRQualifiers() == 0 &&
286            "FIXME: We only handle trivial array types so far!");
287     // int X[] -> [0 x int]
288     return llvm::ArrayType::get(ConvertTypeForMemRecursive(A.getElementType()), 0);
289   }
290   case Type::ConstantArray: {
291     const ConstantArrayType &A = cast<ConstantArrayType>(Ty);
292     const llvm::Type *EltTy = ConvertTypeForMemRecursive(A.getElementType());
293     return llvm::ArrayType::get(EltTy, A.getSize().getZExtValue());
294   }
295   case Type::ExtVector:
296   case Type::Vector: {
297     const VectorType &VT = cast<VectorType>(Ty);
298     return llvm::VectorType::get(ConvertTypeRecursive(VT.getElementType()),
299                                  VT.getNumElements());
300   }
301   case Type::FunctionNoProto:
302   case Type::FunctionProto: {
303     // First, check whether we can build the full function type.
304     if (const TagType* TT = VerifyFuncTypeComplete(&Ty)) {
305       // This function's type depends on an incomplete tag type; make sure
306       // we have an opaque type corresponding to the tag type.
307       ConvertTagDeclType(TT->getDecl());
308       // Create an opaque type for this function type, save it, and return it.
309       llvm::Type *ResultType = llvm::OpaqueType::get(getLLVMContext());
310       FunctionTypes.insert(std::make_pair(&Ty, ResultType));
311       return ResultType;
312     }
313     // The function type can be built; call the appropriate routines to
314     // build it.
315     if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(&Ty))
316       return GetFunctionType(getFunctionInfo(FPT), FPT->isVariadic());
317 
318     const FunctionNoProtoType *FNPT = cast<FunctionNoProtoType>(&Ty);
319     return GetFunctionType(getFunctionInfo(FNPT), true);
320   }
321 
322   case Type::ObjCInterface: {
323     // Objective-C interfaces are always opaque (outside of the
324     // runtime, which can do whatever it likes); we never refine
325     // these.
326     const llvm::Type *&T = InterfaceTypes[cast<ObjCInterfaceType>(&Ty)];
327     if (!T)
328         T = llvm::OpaqueType::get(getLLVMContext());
329     return T;
330   }
331 
332   case Type::ObjCObjectPointer: {
333     // Protocol qualifications do not influence the LLVM type, we just return a
334     // pointer to the underlying interface type. We don't need to worry about
335     // recursive conversion.
336     const llvm::Type *T =
337       ConvertTypeRecursive(cast<ObjCObjectPointerType>(Ty).getPointeeType());
338     return llvm::PointerType::getUnqual(T);
339   }
340 
341   case Type::Record:
342   case Type::Enum: {
343     const TagDecl *TD = cast<TagType>(Ty).getDecl();
344     const llvm::Type *Res = ConvertTagDeclType(TD);
345 
346     std::string TypeName(TD->getKindName());
347     TypeName += '.';
348 
349     // Name the codegen type after the typedef name
350     // if there is no tag type name available
351     if (TD->getIdentifier())
352       // FIXME: We should not have to check for a null decl context here.
353       // Right now we do it because the implicit Obj-C decls don't have one.
354       TypeName += TD->getDeclContext() ? TD->getQualifiedNameAsString() :
355         TD->getNameAsString();
356     else if (const TypedefType *TdT = dyn_cast<TypedefType>(T))
357       // FIXME: We should not have to check for a null decl context here.
358       // Right now we do it because the implicit Obj-C decls don't have one.
359       TypeName += TdT->getDecl()->getDeclContext() ?
360         TdT->getDecl()->getQualifiedNameAsString() :
361         TdT->getDecl()->getNameAsString();
362     else
363       TypeName += "anon";
364 
365     TheModule.addTypeName(TypeName, Res);
366     return Res;
367   }
368 
369   case Type::BlockPointer: {
370     const QualType FTy = cast<BlockPointerType>(Ty).getPointeeType();
371     llvm::OpaqueType *PointeeType = llvm::OpaqueType::get(getLLVMContext());
372     PointersToResolve.push_back(std::make_pair(FTy, PointeeType));
373     return llvm::PointerType::get(PointeeType, FTy.getAddressSpace());
374   }
375 
376   case Type::MemberPointer: {
377     // FIXME: This is ABI dependent. We use the Itanium C++ ABI.
378     // http://www.codesourcery.com/public/cxx-abi/abi.html#member-pointers
379     // If we ever want to support other ABIs this needs to be abstracted.
380 
381     QualType ETy = cast<MemberPointerType>(Ty).getPointeeType();
382     const llvm::Type *PtrDiffTy =
383         ConvertTypeRecursive(Context.getPointerDiffType());
384     if (ETy->isFunctionType())
385       return llvm::StructType::get(TheModule.getContext(), PtrDiffTy, PtrDiffTy,
386                                    NULL);
387     return PtrDiffTy;
388   }
389 
390   case Type::TemplateSpecialization:
391     assert(false && "Dependent types can't get here");
392   }
393 
394   // FIXME: implement.
395   return llvm::OpaqueType::get(getLLVMContext());
396 }
397 
398 /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or
399 /// enum.
400 const llvm::Type *CodeGenTypes::ConvertTagDeclType(const TagDecl *TD) {
401 
402   // FIXME. This may have to move to a better place.
403   if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(TD)) {
404     for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
405          e = RD->bases_end(); i != e; ++i) {
406       if (!i->isVirtual()) {
407         const CXXRecordDecl *Base =
408           cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
409         ConvertTagDeclType(Base);
410       }
411     }
412   }
413 
414   // TagDecl's are not necessarily unique, instead use the (clang)
415   // type connected to the decl.
416   const Type *Key =
417     Context.getTagDeclType(TD).getTypePtr();
418   llvm::DenseMap<const Type*, llvm::PATypeHolder>::iterator TDTI =
419     TagDeclTypes.find(Key);
420 
421   // If we've already compiled this tag type, use the previous definition.
422   if (TDTI != TagDeclTypes.end())
423     return TDTI->second;
424 
425   // If this is still a forward definition, just define an opaque type to use
426   // for this tagged decl.
427   if (!TD->isDefinition()) {
428     llvm::Type *ResultType = llvm::OpaqueType::get(getLLVMContext());
429     TagDeclTypes.insert(std::make_pair(Key, ResultType));
430     return ResultType;
431   }
432 
433   // Okay, this is a definition of a type.  Compile the implementation now.
434 
435   if (TD->isEnum())  // Don't bother storing enums in TagDeclTypes.
436     return ConvertTypeRecursive(cast<EnumDecl>(TD)->getIntegerType());
437 
438   // This decl could well be recursive.  In this case, insert an opaque
439   // definition of this type, which the recursive uses will get.  We will then
440   // refine this opaque version later.
441 
442   // Create new OpaqueType now for later use in case this is a recursive
443   // type.  This will later be refined to the actual type.
444   llvm::PATypeHolder ResultHolder = llvm::OpaqueType::get(getLLVMContext());
445   TagDeclTypes.insert(std::make_pair(Key, ResultHolder));
446 
447   const RecordDecl *RD = cast<const RecordDecl>(TD);
448 
449   // Layout fields.
450   CGRecordLayout *Layout = CGRecordLayoutBuilder::ComputeLayout(*this, RD);
451 
452   CGRecordLayouts[Key] = Layout;
453   const llvm::Type *ResultType = Layout->getLLVMType();
454 
455   // Refine our Opaque type to ResultType.  This can invalidate ResultType, so
456   // make sure to read the result out of the holder.
457   cast<llvm::OpaqueType>(ResultHolder.get())
458     ->refineAbstractTypeTo(ResultType);
459 
460   return ResultHolder.get();
461 }
462 
463 /// getLLVMFieldNo - Return llvm::StructType element number
464 /// that corresponds to the field FD.
465 unsigned CodeGenTypes::getLLVMFieldNo(const FieldDecl *FD) {
466   assert(!FD->isBitField() && "Don't use getLLVMFieldNo on bit fields!");
467 
468   llvm::DenseMap<const FieldDecl*, unsigned>::iterator I = FieldInfo.find(FD);
469   assert (I != FieldInfo.end()  && "Unable to find field info");
470   return I->second;
471 }
472 
473 /// addFieldInfo - Assign field number to field FD.
474 void CodeGenTypes::addFieldInfo(const FieldDecl *FD, unsigned No) {
475   FieldInfo[FD] = No;
476 }
477 
478 /// getBitFieldInfo - Return the BitFieldInfo  that corresponds to the field FD.
479 CodeGenTypes::BitFieldInfo CodeGenTypes::getBitFieldInfo(const FieldDecl *FD) {
480   llvm::DenseMap<const FieldDecl *, BitFieldInfo>::iterator
481     I = BitFields.find(FD);
482   assert (I != BitFields.end()  && "Unable to find bitfield info");
483   return I->second;
484 }
485 
486 /// addBitFieldInfo - Assign a start bit and a size to field FD.
487 void CodeGenTypes::addBitFieldInfo(const FieldDecl *FD, unsigned FieldNo,
488                                    unsigned Start, unsigned Size) {
489   BitFields.insert(std::make_pair(FD, BitFieldInfo(FieldNo, Start, Size)));
490 }
491 
492 /// getCGRecordLayout - Return record layout info for the given llvm::Type.
493 const CGRecordLayout &
494 CodeGenTypes::getCGRecordLayout(const TagDecl *TD) const {
495   const Type *Key = Context.getTagDeclType(TD).getTypePtr();
496   llvm::DenseMap<const Type*, CGRecordLayout *>::const_iterator I
497     = CGRecordLayouts.find(Key);
498   assert (I != CGRecordLayouts.end()
499           && "Unable to find record layout information for type");
500   return *I->second;
501 }
502