ASTContext.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. //===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
  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 the ASTContext interface.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/AST/ASTContext.h"
  14. #include "clang/AST/Decl.h"
  15. #include "clang/Lex/Preprocessor.h"
  16. #include "clang/Basic/TargetInfo.h"
  17. #include "llvm/ADT/SmallVector.h"
  18. using namespace clang;
  19. enum FloatingRank {
  20. FloatRank, DoubleRank, LongDoubleRank
  21. };
  22. ASTContext::~ASTContext() {
  23. // Deallocate all the types.
  24. while (!Types.empty()) {
  25. if (FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(Types.back())) {
  26. // Destroy the object, but don't call delete. These are malloc'd.
  27. FT->~FunctionTypeProto();
  28. free(FT);
  29. } else {
  30. delete Types.back();
  31. }
  32. Types.pop_back();
  33. }
  34. }
  35. void ASTContext::PrintStats() const {
  36. fprintf(stderr, "*** AST Context Stats:\n");
  37. fprintf(stderr, " %d types total.\n", (int)Types.size());
  38. unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
  39. unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
  40. unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
  41. for (unsigned i = 0, e = Types.size(); i != e; ++i) {
  42. Type *T = Types[i];
  43. if (isa<BuiltinType>(T))
  44. ++NumBuiltin;
  45. else if (isa<PointerType>(T))
  46. ++NumPointer;
  47. else if (isa<ReferenceType>(T))
  48. ++NumReference;
  49. else if (isa<ArrayType>(T))
  50. ++NumArray;
  51. else if (isa<FunctionTypeNoProto>(T))
  52. ++NumFunctionNP;
  53. else if (isa<FunctionTypeProto>(T))
  54. ++NumFunctionP;
  55. else if (isa<TypedefType>(T))
  56. ++NumTypeName;
  57. else if (TagType *TT = dyn_cast<TagType>(T)) {
  58. ++NumTagged;
  59. switch (TT->getDecl()->getKind()) {
  60. default: assert(0 && "Unknown tagged type!");
  61. case Decl::Struct: ++NumTagStruct; break;
  62. case Decl::Union: ++NumTagUnion; break;
  63. case Decl::Class: ++NumTagClass; break;
  64. case Decl::Enum: ++NumTagEnum; break;
  65. }
  66. } else {
  67. assert(0 && "Unknown type!");
  68. }
  69. }
  70. fprintf(stderr, " %d builtin types\n", NumBuiltin);
  71. fprintf(stderr, " %d pointer types\n", NumPointer);
  72. fprintf(stderr, " %d reference types\n", NumReference);
  73. fprintf(stderr, " %d array types\n", NumArray);
  74. fprintf(stderr, " %d function types with proto\n", NumFunctionP);
  75. fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
  76. fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
  77. fprintf(stderr, " %d tagged types\n", NumTagged);
  78. fprintf(stderr, " %d struct types\n", NumTagStruct);
  79. fprintf(stderr, " %d union types\n", NumTagUnion);
  80. fprintf(stderr, " %d class types\n", NumTagClass);
  81. fprintf(stderr, " %d enum types\n", NumTagEnum);
  82. fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
  83. NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
  84. NumFunctionP*sizeof(FunctionTypeProto)+
  85. NumFunctionNP*sizeof(FunctionTypeNoProto)+
  86. NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)));
  87. }
  88. void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
  89. Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
  90. }
  91. void ASTContext::InitBuiltinTypes() {
  92. assert(VoidTy.isNull() && "Context reinitialized?");
  93. // C99 6.2.5p19.
  94. InitBuiltinType(VoidTy, BuiltinType::Void);
  95. // C99 6.2.5p2.
  96. InitBuiltinType(BoolTy, BuiltinType::Bool);
  97. // C99 6.2.5p3.
  98. if (Target.isCharSigned(SourceLocation()))
  99. InitBuiltinType(CharTy, BuiltinType::Char_S);
  100. else
  101. InitBuiltinType(CharTy, BuiltinType::Char_U);
  102. // C99 6.2.5p4.
  103. InitBuiltinType(SignedCharTy, BuiltinType::SChar);
  104. InitBuiltinType(ShortTy, BuiltinType::Short);
  105. InitBuiltinType(IntTy, BuiltinType::Int);
  106. InitBuiltinType(LongTy, BuiltinType::Long);
  107. InitBuiltinType(LongLongTy, BuiltinType::LongLong);
  108. // C99 6.2.5p6.
  109. InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
  110. InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
  111. InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
  112. InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
  113. InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
  114. // C99 6.2.5p10.
  115. InitBuiltinType(FloatTy, BuiltinType::Float);
  116. InitBuiltinType(DoubleTy, BuiltinType::Double);
  117. InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
  118. // C99 6.2.5p11.
  119. FloatComplexTy = getComplexType(FloatTy);
  120. DoubleComplexTy = getComplexType(DoubleTy);
  121. LongDoubleComplexTy = getComplexType(LongDoubleTy);
  122. }
  123. /// getComplexType - Return the uniqued reference to the type for a complex
  124. /// number with the specified element type.
  125. QualType ASTContext::getComplexType(QualType T) {
  126. // Unique pointers, to guarantee there is only one pointer of a particular
  127. // structure.
  128. llvm::FoldingSetNodeID ID;
  129. ComplexType::Profile(ID, T);
  130. void *InsertPos = 0;
  131. if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
  132. return QualType(CT, 0);
  133. // If the pointee type isn't canonical, this won't be a canonical type either,
  134. // so fill in the canonical type field.
  135. QualType Canonical;
  136. if (!T->isCanonical()) {
  137. Canonical = getComplexType(T.getCanonicalType());
  138. // Get the new insert position for the node we care about.
  139. ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
  140. assert(NewIP == 0 && "Shouldn't be in the map!");
  141. }
  142. ComplexType *New = new ComplexType(T, Canonical);
  143. Types.push_back(New);
  144. ComplexTypes.InsertNode(New, InsertPos);
  145. return QualType(New, 0);
  146. }
  147. /// getPointerType - Return the uniqued reference to the type for a pointer to
  148. /// the specified type.
  149. QualType ASTContext::getPointerType(QualType T) {
  150. // Unique pointers, to guarantee there is only one pointer of a particular
  151. // structure.
  152. llvm::FoldingSetNodeID ID;
  153. PointerType::Profile(ID, T);
  154. void *InsertPos = 0;
  155. if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
  156. return QualType(PT, 0);
  157. // If the pointee type isn't canonical, this won't be a canonical type either,
  158. // so fill in the canonical type field.
  159. QualType Canonical;
  160. if (!T->isCanonical()) {
  161. Canonical = getPointerType(T.getCanonicalType());
  162. // Get the new insert position for the node we care about.
  163. PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
  164. assert(NewIP == 0 && "Shouldn't be in the map!");
  165. }
  166. PointerType *New = new PointerType(T, Canonical);
  167. Types.push_back(New);
  168. PointerTypes.InsertNode(New, InsertPos);
  169. return QualType(New, 0);
  170. }
  171. /// getReferenceType - Return the uniqued reference to the type for a reference
  172. /// to the specified type.
  173. QualType ASTContext::getReferenceType(QualType T) {
  174. // Unique pointers, to guarantee there is only one pointer of a particular
  175. // structure.
  176. llvm::FoldingSetNodeID ID;
  177. ReferenceType::Profile(ID, T);
  178. void *InsertPos = 0;
  179. if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
  180. return QualType(RT, 0);
  181. // If the referencee type isn't canonical, this won't be a canonical type
  182. // either, so fill in the canonical type field.
  183. QualType Canonical;
  184. if (!T->isCanonical()) {
  185. Canonical = getReferenceType(T.getCanonicalType());
  186. // Get the new insert position for the node we care about.
  187. ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
  188. assert(NewIP == 0 && "Shouldn't be in the map!");
  189. }
  190. ReferenceType *New = new ReferenceType(T, Canonical);
  191. Types.push_back(New);
  192. ReferenceTypes.InsertNode(New, InsertPos);
  193. return QualType(New, 0);
  194. }
  195. /// getArrayType - Return the unique reference to the type for an array of the
  196. /// specified element type.
  197. QualType ASTContext::getArrayType(QualType EltTy,ArrayType::ArraySizeModifier ASM,
  198. unsigned EltTypeQuals, Expr *NumElts) {
  199. // Unique array types, to guarantee there is only one array of a particular
  200. // structure.
  201. llvm::FoldingSetNodeID ID;
  202. ArrayType::Profile(ID, ASM, EltTypeQuals, EltTy, NumElts);
  203. void *InsertPos = 0;
  204. if (ArrayType *ATP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
  205. return QualType(ATP, 0);
  206. // If the element type isn't canonical, this won't be a canonical type either,
  207. // so fill in the canonical type field.
  208. QualType Canonical;
  209. if (!EltTy->isCanonical()) {
  210. Canonical = getArrayType(EltTy.getCanonicalType(), ASM, EltTypeQuals,
  211. NumElts);
  212. // Get the new insert position for the node we care about.
  213. ArrayType *NewIP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
  214. assert(NewIP == 0 && "Shouldn't be in the map!");
  215. }
  216. ArrayType *New = new ArrayType(EltTy, ASM, EltTypeQuals, Canonical, NumElts);
  217. ArrayTypes.InsertNode(New, InsertPos);
  218. Types.push_back(New);
  219. return QualType(New, 0);
  220. }
  221. /// convertToVectorType - Return the unique reference to a vector type of
  222. /// the specified element type and size. VectorType can be a pointer, array,
  223. /// function, or built-in type (i.e. _Bool, integer, or float).
  224. QualType ASTContext::convertToVectorType(QualType vecType, unsigned NumElts) {
  225. BuiltinType *baseType;
  226. baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
  227. assert(baseType != 0 &&
  228. "convertToVectorType(): Complex vector types unimplemented");
  229. // Check if we've already instantiated a vector of this type.
  230. llvm::FoldingSetNodeID ID;
  231. VectorType::Profile(ID, vecType, NumElts);
  232. void *InsertPos = 0;
  233. if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
  234. return QualType(VTP, 0);
  235. // If the element type isn't canonical, this won't be a canonical type either,
  236. // so fill in the canonical type field.
  237. QualType Canonical;
  238. if (!vecType->isCanonical()) {
  239. Canonical = convertToVectorType(vecType.getCanonicalType(), NumElts);
  240. // Get the new insert position for the node we care about.
  241. VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
  242. assert(NewIP == 0 && "Shouldn't be in the map!");
  243. }
  244. VectorType *New = new VectorType(vecType, NumElts, Canonical);
  245. VectorTypes.InsertNode(New, InsertPos);
  246. Types.push_back(New);
  247. return QualType(New, 0);
  248. }
  249. /// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
  250. ///
  251. QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
  252. // Unique functions, to guarantee there is only one function of a particular
  253. // structure.
  254. llvm::FoldingSetNodeID ID;
  255. FunctionTypeNoProto::Profile(ID, ResultTy);
  256. void *InsertPos = 0;
  257. if (FunctionTypeNoProto *FT =
  258. FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
  259. return QualType(FT, 0);
  260. QualType Canonical;
  261. if (!ResultTy->isCanonical()) {
  262. Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
  263. // Get the new insert position for the node we care about.
  264. FunctionTypeNoProto *NewIP =
  265. FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
  266. assert(NewIP == 0 && "Shouldn't be in the map!");
  267. }
  268. FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
  269. Types.push_back(New);
  270. FunctionTypeProtos.InsertNode(New, InsertPos);
  271. return QualType(New, 0);
  272. }
  273. /// getFunctionType - Return a normal function type with a typed argument
  274. /// list. isVariadic indicates whether the argument list includes '...'.
  275. QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
  276. unsigned NumArgs, bool isVariadic) {
  277. // Unique functions, to guarantee there is only one function of a particular
  278. // structure.
  279. llvm::FoldingSetNodeID ID;
  280. FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
  281. void *InsertPos = 0;
  282. if (FunctionTypeProto *FTP =
  283. FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
  284. return QualType(FTP, 0);
  285. // Determine whether the type being created is already canonical or not.
  286. bool isCanonical = ResultTy->isCanonical();
  287. for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
  288. if (!ArgArray[i]->isCanonical())
  289. isCanonical = false;
  290. // If this type isn't canonical, get the canonical version of it.
  291. QualType Canonical;
  292. if (!isCanonical) {
  293. llvm::SmallVector<QualType, 16> CanonicalArgs;
  294. CanonicalArgs.reserve(NumArgs);
  295. for (unsigned i = 0; i != NumArgs; ++i)
  296. CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
  297. Canonical = getFunctionType(ResultTy.getCanonicalType(),
  298. &CanonicalArgs[0], NumArgs,
  299. isVariadic);
  300. // Get the new insert position for the node we care about.
  301. FunctionTypeProto *NewIP =
  302. FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
  303. assert(NewIP == 0 && "Shouldn't be in the map!");
  304. }
  305. // FunctionTypeProto objects are not allocated with new because they have a
  306. // variable size array (for parameter types) at the end of them.
  307. FunctionTypeProto *FTP =
  308. (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
  309. (NumArgs-1)*sizeof(QualType));
  310. new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
  311. Canonical);
  312. Types.push_back(FTP);
  313. FunctionTypeProtos.InsertNode(FTP, InsertPos);
  314. return QualType(FTP, 0);
  315. }
  316. /// getTypedefType - Return the unique reference to the type for the
  317. /// specified typename decl.
  318. QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
  319. if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
  320. QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
  321. Decl->TypeForDecl = new TypedefType(Decl, Canonical);
  322. Types.push_back(Decl->TypeForDecl);
  323. return QualType(Decl->TypeForDecl, 0);
  324. }
  325. /// getTagDeclType - Return the unique reference to the type for the
  326. /// specified TagDecl (struct/union/class/enum) decl.
  327. QualType ASTContext::getTagDeclType(TagDecl *Decl) {
  328. // The decl stores the type cache.
  329. if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
  330. Decl->TypeForDecl = new TagType(Decl, QualType());
  331. Types.push_back(Decl->TypeForDecl);
  332. return QualType(Decl->TypeForDecl, 0);
  333. }
  334. /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
  335. /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
  336. /// needs to agree with the definition in <stddef.h>.
  337. QualType ASTContext::getSizeType() const {
  338. // On Darwin, size_t is defined as a "long unsigned int".
  339. // FIXME: should derive from "Target".
  340. return UnsignedLongTy;
  341. }
  342. /// getIntegerBitwidth - Return the bitwidth of the specified integer type
  343. /// according to the target. 'Loc' specifies the source location that
  344. /// requires evaluation of this property.
  345. unsigned ASTContext::getIntegerBitwidth(QualType T, SourceLocation Loc) {
  346. if (const TagType *TT = dyn_cast<TagType>(T.getCanonicalType())) {
  347. assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
  348. assert(0 && "FIXME: getIntegerBitwidth(enum) unimplemented!");
  349. }
  350. const BuiltinType *BT = cast<BuiltinType>(T.getCanonicalType());
  351. switch (BT->getKind()) {
  352. default: assert(0 && "getIntegerBitwidth(): not a built-in integer");
  353. case BuiltinType::Bool: return Target.getBoolWidth(Loc);
  354. case BuiltinType::Char_S:
  355. case BuiltinType::Char_U:
  356. case BuiltinType::SChar:
  357. case BuiltinType::UChar: return Target.getCharWidth(Loc);
  358. case BuiltinType::Short:
  359. case BuiltinType::UShort: return Target.getShortWidth(Loc);
  360. case BuiltinType::Int:
  361. case BuiltinType::UInt: return Target.getIntWidth(Loc);
  362. case BuiltinType::Long:
  363. case BuiltinType::ULong: return Target.getLongWidth(Loc);
  364. case BuiltinType::LongLong:
  365. case BuiltinType::ULongLong: return Target.getLongLongWidth(Loc);
  366. }
  367. }
  368. /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
  369. /// routine will assert if passed a built-in type that isn't an integer or enum.
  370. static int getIntegerRank(QualType t) {
  371. if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
  372. assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
  373. return 4;
  374. }
  375. const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType());
  376. switch (BT->getKind()) {
  377. default:
  378. assert(0 && "getIntegerRank(): not a built-in integer");
  379. case BuiltinType::Bool:
  380. return 1;
  381. case BuiltinType::Char_S:
  382. case BuiltinType::Char_U:
  383. case BuiltinType::SChar:
  384. case BuiltinType::UChar:
  385. return 2;
  386. case BuiltinType::Short:
  387. case BuiltinType::UShort:
  388. return 3;
  389. case BuiltinType::Int:
  390. case BuiltinType::UInt:
  391. return 4;
  392. case BuiltinType::Long:
  393. case BuiltinType::ULong:
  394. return 5;
  395. case BuiltinType::LongLong:
  396. case BuiltinType::ULongLong:
  397. return 6;
  398. }
  399. }
  400. /// getFloatingRank - Return a relative rank for floating point types.
  401. /// This routine will assert if passed a built-in type that isn't a float.
  402. static int getFloatingRank(QualType T) {
  403. T = T.getCanonicalType();
  404. if (ComplexType *CT = dyn_cast<ComplexType>(T))
  405. return getFloatingRank(CT->getElementType());
  406. switch (cast<BuiltinType>(T)->getKind()) {
  407. default: assert(0 && "getFloatingPointRank(): not a floating type");
  408. case BuiltinType::Float: return FloatRank;
  409. case BuiltinType::Double: return DoubleRank;
  410. case BuiltinType::LongDouble: return LongDoubleRank;
  411. }
  412. }
  413. // maxComplexType - the following code handles 3 different combinations:
  414. // complex/complex, complex/float, float/complex.
  415. // When both operands are complex, the shorter operand is converted to the
  416. // type of the longer, and that is the type of the result. This corresponds
  417. // to what is done when combining two real floating-point operands.
  418. // The fun begins when size promotion occur across type domains. g
  419. // getFloatingRank & convertFloatingRankToComplexType handle this without
  420. // enumerating all permutations.
  421. // It also allows us to add new types without breakage.
  422. // From H&S 6.3.4: When one operand is complex and the other is a real
  423. // floating-point type, the less precise type is converted, within it's
  424. // real or complex domain, to the precision of the other type. For example,
  425. // when combining a "long double" with a "double _Complex", the
  426. // "double _Complex" is promoted to "long double _Complex".
  427. QualType ASTContext::maxComplexType(QualType lt, QualType rt) const {
  428. switch (std::max(getFloatingRank(lt), getFloatingRank(rt))) {
  429. default: assert(0 && "convertRankToComplex(): illegal value for rank");
  430. case FloatRank: return FloatComplexTy;
  431. case DoubleRank: return DoubleComplexTy;
  432. case LongDoubleRank: return LongDoubleComplexTy;
  433. }
  434. }
  435. // maxFloatingType - handles the simple case, both operands are floats.
  436. QualType ASTContext::maxFloatingType(QualType lt, QualType rt) {
  437. return getFloatingRank(lt) > getFloatingRank(rt) ? lt : rt;
  438. }
  439. // maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
  440. // unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
  441. QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
  442. if (lhs == rhs) return lhs;
  443. bool t1Unsigned = lhs->isUnsignedIntegerType();
  444. bool t2Unsigned = rhs->isUnsignedIntegerType();
  445. if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
  446. return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
  447. // We have two integer types with differing signs
  448. QualType unsignedType = t1Unsigned ? lhs : rhs;
  449. QualType signedType = t1Unsigned ? rhs : lhs;
  450. if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
  451. return unsignedType;
  452. else {
  453. // FIXME: Need to check if the signed type can represent all values of the
  454. // unsigned type. If it can, then the result is the signed type.
  455. // If it can't, then the result is the unsigned version of the signed type.
  456. // Should probably add a helper that returns a signed integer type from
  457. // an unsigned (and vice versa). C99 6.3.1.8.
  458. return signedType;
  459. }
  460. }