Type.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. //===--- Type.cpp - Type representation and manipulation ------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file was developed by Chris Lattner and is distributed under
  6. // the University of Illinois Open Source License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements type-related functionality.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/Lex/IdentifierTable.h"
  14. #include "clang/AST/Type.h"
  15. #include "clang/AST/Decl.h"
  16. #include "clang/AST/Expr.h"
  17. #include "clang/Basic/TargetInfo.h"
  18. #include "llvm/Support/Streams.h"
  19. #include "llvm/ADT/StringExtras.h"
  20. using namespace clang;
  21. Type::~Type() {}
  22. /// isVoidType - Helper method to determine if this is the 'void' type.
  23. bool Type::isVoidType() const {
  24. if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
  25. return BT->getKind() == BuiltinType::Void;
  26. return false;
  27. }
  28. bool Type::isObjectType() const {
  29. if (isa<FunctionType>(CanonicalType))
  30. return false;
  31. else if (CanonicalType->isIncompleteType())
  32. return false;
  33. else
  34. return true;
  35. }
  36. bool Type::isDerivedType() const {
  37. switch (CanonicalType->getTypeClass()) {
  38. case Pointer:
  39. case Array:
  40. case FunctionProto:
  41. case FunctionNoProto:
  42. case Reference:
  43. return true;
  44. case Tagged: {
  45. const TagType *TT = cast<TagType>(CanonicalType);
  46. const Decl::Kind Kind = TT->getDecl()->getKind();
  47. return Kind == Decl::Struct || Kind == Decl::Union;
  48. }
  49. default:
  50. return false;
  51. }
  52. }
  53. bool Type::isFunctionType() const {
  54. return isa<FunctionType>(CanonicalType);
  55. }
  56. bool Type::isPointerType() const {
  57. return isa<PointerType>(CanonicalType);
  58. }
  59. bool Type::isReferenceType() const {
  60. return isa<ReferenceType>(CanonicalType);
  61. }
  62. bool Type::isArrayType() const {
  63. return isa<ArrayType>(CanonicalType);
  64. }
  65. bool Type::isStructureType() const {
  66. if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
  67. if (TT->getDecl()->getKind() == Decl::Struct)
  68. return true;
  69. }
  70. return false;
  71. }
  72. bool Type::isUnionType() const {
  73. if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
  74. if (TT->getDecl()->getKind() == Decl::Union)
  75. return true;
  76. }
  77. return false;
  78. }
  79. // C99 6.2.7p1: If both are complete types, then the following additional
  80. // requirements apply...FIXME (handle compatibility across source files).
  81. bool Type::tagTypesAreCompatible(QualType lhs, QualType rhs) {
  82. TagDecl *ldecl = cast<TagType>(lhs.getCanonicalType())->getDecl();
  83. TagDecl *rdecl = cast<TagType>(rhs.getCanonicalType())->getDecl();
  84. if (ldecl->getKind() == Decl::Struct && rdecl->getKind() == Decl::Struct) {
  85. if (ldecl->getIdentifier() == rdecl->getIdentifier())
  86. return true;
  87. }
  88. if (ldecl->getKind() == Decl::Union && rdecl->getKind() == Decl::Union) {
  89. if (ldecl->getIdentifier() == rdecl->getIdentifier())
  90. return true;
  91. }
  92. return false;
  93. }
  94. bool Type::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
  95. // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
  96. // identically qualified and both shall be pointers to compatible types.
  97. if (lhs.getQualifiers() != rhs.getQualifiers())
  98. return false;
  99. QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
  100. QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
  101. return typesAreCompatible(ltype, rtype);
  102. }
  103. // C++ 5.17p6: When the left opperand of an assignment operator denotes a
  104. // reference to T, the operation assigns to the object of type T denoted by the
  105. // reference.
  106. bool Type::referenceTypesAreCompatible(QualType lhs, QualType rhs) {
  107. QualType ltype = lhs;
  108. if (lhs->isReferenceType())
  109. ltype = cast<ReferenceType>(lhs.getCanonicalType())->getReferenceeType();
  110. QualType rtype = rhs;
  111. if (rhs->isReferenceType())
  112. rtype = cast<ReferenceType>(rhs.getCanonicalType())->getReferenceeType();
  113. return typesAreCompatible(ltype, rtype);
  114. }
  115. bool Type::functionTypesAreCompatible(QualType lhs, QualType rhs) {
  116. const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
  117. const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
  118. const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
  119. const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
  120. // first check the return types (common between C99 and K&R).
  121. if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
  122. return false;
  123. if (lproto && rproto) { // two C99 style function prototypes
  124. unsigned lproto_nargs = lproto->getNumArgs();
  125. unsigned rproto_nargs = rproto->getNumArgs();
  126. if (lproto_nargs != rproto_nargs)
  127. return false;
  128. // both prototypes have the same number of arguments.
  129. if ((lproto->isVariadic() && !rproto->isVariadic()) ||
  130. (rproto->isVariadic() && !lproto->isVariadic()))
  131. return false;
  132. // The use of ellipsis agree...now check the argument types.
  133. for (unsigned i = 0; i < lproto_nargs; i++)
  134. if (!typesAreCompatible(lproto->getArgType(i), rproto->getArgType(i)))
  135. return false;
  136. return true;
  137. }
  138. if (!lproto && !rproto) // two K&R style function decls, nothing to do.
  139. return true;
  140. // we have a mixture of K&R style with C99 prototypes
  141. const FunctionTypeProto *proto = lproto ? lproto : rproto;
  142. if (proto->isVariadic())
  143. return false;
  144. // FIXME: Each parameter type T in the prototype must be compatible with the
  145. // type resulting from applying the usual argument conversions to T.
  146. return true;
  147. }
  148. bool Type::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
  149. QualType ltype = cast<ArrayType>(lhs.getCanonicalType())->getElementType();
  150. QualType rtype = cast<ArrayType>(rhs.getCanonicalType())->getElementType();
  151. if (!typesAreCompatible(ltype, rtype))
  152. return false;
  153. // FIXME: If both types specify constant sizes, then the sizes must also be
  154. // the same. Even if the sizes are the same, GCC produces an error.
  155. return true;
  156. }
  157. /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
  158. /// both shall have the identically qualified version of a compatible type.
  159. /// C99 6.2.7p1: Two types have compatible types if their types are the
  160. /// same. See 6.7.[2,3,5] for additional rules.
  161. bool Type::typesAreCompatible(QualType lhs, QualType rhs) {
  162. QualType lcanon = lhs.getCanonicalType();
  163. QualType rcanon = rhs.getCanonicalType();
  164. // If two types are identical, they are are compatible
  165. if (lcanon == rcanon)
  166. return true;
  167. // If the canonical type classes don't match, they can't be compatible
  168. if (lcanon->getTypeClass() != rcanon->getTypeClass())
  169. return false;
  170. switch (lcanon->getTypeClass()) {
  171. case Type::Pointer:
  172. return pointerTypesAreCompatible(lcanon, rcanon);
  173. case Type::Reference:
  174. return referenceTypesAreCompatible(lcanon, rcanon);
  175. case Type::Array:
  176. return arrayTypesAreCompatible(lcanon, rcanon);
  177. case Type::FunctionNoProto:
  178. case Type::FunctionProto:
  179. return functionTypesAreCompatible(lcanon, rcanon);
  180. case Type::Tagged: // handle structures, unions
  181. return tagTypesAreCompatible(lcanon, rcanon);
  182. case Type::Builtin:
  183. return false;
  184. default:
  185. assert(0 && "unexpected type");
  186. }
  187. return true; // should never get here...
  188. }
  189. bool Type::isIntegerType() const {
  190. if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
  191. return BT->getKind() >= BuiltinType::Bool &&
  192. BT->getKind() <= BuiltinType::LongLong;
  193. if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
  194. if (TT->getDecl()->getKind() == Decl::Enum)
  195. return true;
  196. return false;
  197. }
  198. bool Type::isSignedIntegerType() const {
  199. if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
  200. return BT->getKind() >= BuiltinType::Char_S &&
  201. BT->getKind() <= BuiltinType::LongLong;
  202. }
  203. return false;
  204. }
  205. bool Type::isUnsignedIntegerType() const {
  206. if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
  207. return BT->getKind() >= BuiltinType::Bool &&
  208. BT->getKind() <= BuiltinType::ULongLong;
  209. }
  210. return false;
  211. }
  212. bool Type::isFloatingType() const {
  213. if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
  214. return BT->getKind() >= BuiltinType::Float &&
  215. BT->getKind() <= BuiltinType::LongDouble;
  216. if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
  217. return CT->isFloatingType();
  218. return false;
  219. }
  220. bool Type::isRealFloatingType() const {
  221. if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
  222. return BT->getKind() >= BuiltinType::Float &&
  223. BT->getKind() <= BuiltinType::LongDouble;
  224. return false;
  225. }
  226. bool Type::isRealType() const {
  227. if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
  228. return BT->getKind() >= BuiltinType::Bool &&
  229. BT->getKind() <= BuiltinType::LongDouble;
  230. if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
  231. return TT->getDecl()->getKind() == Decl::Enum;
  232. return false;
  233. }
  234. bool Type::isComplexType() const {
  235. return isa<ComplexType>(CanonicalType);
  236. }
  237. bool Type::isVectorType() const {
  238. return isa<VectorType>(CanonicalType);
  239. }
  240. bool Type::isArithmeticType() const {
  241. if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
  242. return BT->getKind() != BuiltinType::Void;
  243. if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
  244. if (TT->getDecl()->getKind() == Decl::Enum)
  245. return true;
  246. return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
  247. }
  248. bool Type::isScalarType() const {
  249. if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
  250. return BT->getKind() != BuiltinType::Void;
  251. if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
  252. if (TT->getDecl()->getKind() == Decl::Enum)
  253. return true;
  254. return false;
  255. }
  256. return isa<PointerType>(CanonicalType) || isa<ComplexType>(CanonicalType);
  257. }
  258. bool Type::isAggregateType() const {
  259. if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
  260. if (TT->getDecl()->getKind() == Decl::Struct)
  261. return true;
  262. return false;
  263. }
  264. return CanonicalType->getTypeClass() == Array;
  265. }
  266. // The only variable size types are auto arrays within a function. Structures
  267. // cannot contain a VLA member. They can have a flexible array member, however
  268. // the structure is still constant size (C99 6.7.2.1p16).
  269. bool Type::isConstantSizeType(SourceLocation *loc) const {
  270. if (const ArrayType *Ary = dyn_cast<ArrayType>(CanonicalType)) {
  271. assert(Ary->getSize() && "Incomplete types don't have a size at all!");
  272. return Ary->getSize()->isIntegerConstantExpr(loc); // Variable Length Array?
  273. }
  274. return true;
  275. }
  276. /// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
  277. /// - a type that can describe objects, but which lacks information needed to
  278. /// determine its size.
  279. bool Type::isIncompleteType() const {
  280. switch (CanonicalType->getTypeClass()) {
  281. default: return false;
  282. case Builtin:
  283. // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
  284. // be completed.
  285. return isVoidType();
  286. case Tagged:
  287. // A tagged type (struct/union/enum/class) is incomplete if the decl is a
  288. // forward declaration, but not a full definition (C99 6.2.5p22).
  289. return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
  290. case Array:
  291. // An array of unknown size is an incomplete type (C99 6.2.5p22).
  292. return cast<ArrayType>(CanonicalType)->getSize() == 0;
  293. }
  294. }
  295. bool Type::isPromotableIntegerType() const {
  296. const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
  297. if (!BT) return false;
  298. switch (BT->getKind()) {
  299. case BuiltinType::Bool:
  300. case BuiltinType::Char_S:
  301. case BuiltinType::Char_U:
  302. case BuiltinType::SChar:
  303. case BuiltinType::UChar:
  304. case BuiltinType::Short:
  305. case BuiltinType::UShort:
  306. return true;
  307. default:
  308. return false;
  309. }
  310. }
  311. const char *BuiltinType::getName() const {
  312. switch (getKind()) {
  313. default: assert(0 && "Unknown builtin type!");
  314. case Void: return "void";
  315. case Bool: return "_Bool";
  316. case Char_S: return "char";
  317. case Char_U: return "char";
  318. case SChar: return "signed char";
  319. case Short: return "short";
  320. case Int: return "int";
  321. case Long: return "long";
  322. case LongLong: return "long long";
  323. case UChar: return "unsigned char";
  324. case UShort: return "unsigned short";
  325. case UInt: return "unsigned int";
  326. case ULong: return "unsigned long";
  327. case ULongLong: return "unsigned long long";
  328. case Float: return "float";
  329. case Double: return "double";
  330. case LongDouble: return "long double";
  331. }
  332. }
  333. // FIXME: need to use TargetInfo to derive the target specific sizes. This
  334. // implementation will suffice for play with vector support.
  335. unsigned BuiltinType::getSize() const {
  336. switch (getKind()) {
  337. default: assert(0 && "Unknown builtin type!");
  338. case Void: return 0;
  339. case Bool:
  340. case Char_S:
  341. case Char_U: return sizeof(char) * 8;
  342. case SChar: return sizeof(signed char) * 8;
  343. case Short: return sizeof(short) * 8;
  344. case Int: return sizeof(int) * 8;
  345. case Long: return sizeof(long) * 8;
  346. case LongLong: return sizeof(long long) * 8;
  347. case UChar: return sizeof(unsigned char) * 8;
  348. case UShort: return sizeof(unsigned short) * 8;
  349. case UInt: return sizeof(unsigned int) * 8;
  350. case ULong: return sizeof(unsigned long) * 8;
  351. case ULongLong: return sizeof(unsigned long long) * 8;
  352. case Float: return sizeof(float) * 8;
  353. case Double: return sizeof(double) * 8;
  354. case LongDouble: return sizeof(long double) * 8;
  355. }
  356. }
  357. void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
  358. QualType* ArgTys,
  359. unsigned NumArgs, bool isVariadic) {
  360. ID.AddPointer(Result.getAsOpaquePtr());
  361. for (unsigned i = 0; i != NumArgs; ++i)
  362. ID.AddPointer(ArgTys[i].getAsOpaquePtr());
  363. ID.AddInteger(isVariadic);
  364. }
  365. void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
  366. Profile(ID, getResultType(), ArgInfo, NumArgs, isVariadic());
  367. }
  368. bool RecordType::classof(const Type *T) {
  369. if (const TagType *TT = dyn_cast<TagType>(T))
  370. return isa<RecordDecl>(TT->getDecl());
  371. return false;
  372. }
  373. //===----------------------------------------------------------------------===//
  374. // Type Printing
  375. //===----------------------------------------------------------------------===//
  376. void QualType::dump(const char *msg) const {
  377. std::string R = "foo";
  378. getAsStringInternal(R);
  379. if (msg)
  380. fprintf(stderr, "%s: %s\n", msg, R.c_str());
  381. else
  382. fprintf(stderr, "%s\n", R.c_str());
  383. }
  384. static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
  385. // Note: funkiness to ensure we get a space only between quals.
  386. bool NonePrinted = true;
  387. if (TypeQuals & QualType::Const)
  388. S += "const", NonePrinted = false;
  389. if (TypeQuals & QualType::Volatile)
  390. S += (NonePrinted+" volatile"), NonePrinted = false;
  391. if (TypeQuals & QualType::Restrict)
  392. S += (NonePrinted+" restrict"), NonePrinted = false;
  393. }
  394. void QualType::getAsStringInternal(std::string &S) const {
  395. if (isNull()) {
  396. S += "NULL TYPE\n";
  397. return;
  398. }
  399. // Print qualifiers as appropriate.
  400. if (unsigned TQ = getQualifiers()) {
  401. std::string TQS;
  402. AppendTypeQualList(TQS, TQ);
  403. if (!S.empty())
  404. S = TQS + ' ' + S;
  405. else
  406. S = TQS;
  407. }
  408. getTypePtr()->getAsStringInternal(S);
  409. }
  410. void BuiltinType::getAsStringInternal(std::string &S) const {
  411. if (S.empty()) {
  412. S = getName();
  413. } else {
  414. // Prefix the basic type, e.g. 'int X'.
  415. S = ' ' + S;
  416. S = getName() + S;
  417. }
  418. }
  419. void ComplexType::getAsStringInternal(std::string &S) const {
  420. ElementType->getAsStringInternal(S);
  421. S = "_Complex " + S;
  422. }
  423. void PointerType::getAsStringInternal(std::string &S) const {
  424. S = '*' + S;
  425. // Handle things like 'int (*A)[4];' correctly.
  426. // FIXME: this should include vectors, but vectors use attributes I guess.
  427. if (isa<ArrayType>(PointeeType.getTypePtr()))
  428. S = '(' + S + ')';
  429. PointeeType.getAsStringInternal(S);
  430. }
  431. void ReferenceType::getAsStringInternal(std::string &S) const {
  432. S = '&' + S;
  433. // Handle things like 'int (&A)[4];' correctly.
  434. // FIXME: this should include vectors, but vectors use attributes I guess.
  435. if (isa<ArrayType>(ReferenceeType.getTypePtr()))
  436. S = '(' + S + ')';
  437. ReferenceeType.getAsStringInternal(S);
  438. }
  439. void ArrayType::getAsStringInternal(std::string &S) const {
  440. S += '[';
  441. if (IndexTypeQuals) {
  442. AppendTypeQualList(S, IndexTypeQuals);
  443. S += ' ';
  444. }
  445. if (SizeModifier == Static)
  446. S += "static";
  447. else if (SizeModifier == Star)
  448. S += '*';
  449. S += ']';
  450. ElementType.getAsStringInternal(S);
  451. }
  452. void VectorType::getAsStringInternal(std::string &S) const {
  453. S += " __attribute__(( vector_size(";
  454. // FIXME: handle types that are != 32 bits.
  455. S += llvm::utostr_32(NumElements*4); // convert back to bytes.
  456. S += ") ))";
  457. ElementType.getAsStringInternal(S);
  458. }
  459. void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
  460. // If needed for precedence reasons, wrap the inner part in grouping parens.
  461. if (!S.empty())
  462. S = "(" + S + ")";
  463. S += "()";
  464. getResultType().getAsStringInternal(S);
  465. }
  466. void FunctionTypeProto::getAsStringInternal(std::string &S) const {
  467. // If needed for precedence reasons, wrap the inner part in grouping parens.
  468. if (!S.empty())
  469. S = "(" + S + ")";
  470. S += "(";
  471. std::string Tmp;
  472. for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
  473. if (i) S += ", ";
  474. getArgType(i).getAsStringInternal(Tmp);
  475. S += Tmp;
  476. Tmp.clear();
  477. }
  478. if (isVariadic()) {
  479. if (getNumArgs())
  480. S += ", ";
  481. S += "...";
  482. } else if (getNumArgs() == 0) {
  483. // Do not emit int() if we have a proto, emit 'int(void)'.
  484. S += "void";
  485. }
  486. S += ")";
  487. getResultType().getAsStringInternal(S);
  488. }
  489. void TypedefType::getAsStringInternal(std::string &InnerString) const {
  490. if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
  491. InnerString = ' ' + InnerString;
  492. InnerString = getDecl()->getIdentifier()->getName() + InnerString;
  493. }
  494. void TagType::getAsStringInternal(std::string &InnerString) const {
  495. if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
  496. InnerString = ' ' + InnerString;
  497. const char *Kind = getDecl()->getKindName();
  498. const char *ID;
  499. if (const IdentifierInfo *II = getDecl()->getIdentifier())
  500. ID = II->getName();
  501. else
  502. ID = "<anonymous>";
  503. InnerString = std::string(Kind) + " " + ID + InnerString;
  504. }