1//===--- ASTDiagnostic.cpp - Diagnostic Printing Hooks for AST Nodes ------===//
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//===----------------------------------------------------------------------===//
--- 984 unchanged lines hidden (view full) ---
993 InitializeNonTypeDiffVariables(Context, ToIter, ToDefaultNonTypeDecl,
994 HasToInt, HasToValueDecl, ToNullPtr,
995 ToExpr, ToInt, ToValueDecl);
996
997 assert(((!HasFromInt && !HasToInt) ||
998 (!HasFromValueDecl && !HasToValueDecl)) &&
999 "Template argument cannot be both integer and declaration");
1000
1001 if (!HasFromInt && !HasToInt && !HasFromValueDecl && !HasToValueDecl) {
1002 Tree.SetNode(FromExpr, ToExpr);
1003 Tree.SetDefault(FromIter.isEnd() && FromExpr, ToIter.isEnd() && ToExpr);
1004 if (FromDefaultNonTypeDecl->getType()->isIntegralOrEnumerationType()) {
1005 if (FromExpr)
1006 HasFromInt = GetInt(Context, FromIter, FromExpr, FromInt,
1007 FromDefaultNonTypeDecl->getType());
1008 if (ToExpr)
1009 HasToInt = GetInt(Context, ToIter, ToExpr, ToInt,
1010 ToDefaultNonTypeDecl->getType());
1011 }
1012 if (HasFromInt && HasToInt) {
1013 Tree.SetNode(FromInt, ToInt, HasFromInt, HasToInt);
1014 Tree.SetSame(FromInt == ToInt);
1015 Tree.SetKind(DiffTree::Integer);
1016 } else if (HasFromInt || HasToInt) {
1017 Tree.SetNode(FromInt, ToInt, HasFromInt, HasToInt);
1018 Tree.SetSame(false);
1019 Tree.SetKind(DiffTree::Integer);
1020 } else {
1021 Tree.SetSame(IsEqualExpr(Context, FromExpr, ToExpr) ||
1022 (FromNullPtr && ToNullPtr));
1023 Tree.SetNullPtr(FromNullPtr, ToNullPtr);
1024 Tree.SetKind(DiffTree::Expression);
1025 }
1026 return;
1027 }
1028
1029 if (HasFromInt || HasToInt) {
1030 if (!HasFromInt && FromExpr)
1031 HasFromInt = GetInt(Context, FromIter, FromExpr, FromInt,
1032 FromDefaultNonTypeDecl->getType());
1033 if (!HasToInt && ToExpr)
1034 HasToInt = GetInt(Context, ToIter, ToExpr, ToInt,
1035 ToDefaultNonTypeDecl->getType());
1036 Tree.SetNode(FromInt, ToInt, HasFromInt, HasToInt);
1037 if (HasFromInt && HasToInt) {
1038 Tree.SetSame(FromInt == ToInt);
1039 } else {
1040 Tree.SetSame(false);
1041 }
1042 Tree.SetDefault(FromIter.isEnd() && HasFromInt,
1043 ToIter.isEnd() && HasToInt);
1044 Tree.SetKind(DiffTree::Integer);
1045 return;
1046 }
1047
1048 if (!HasFromValueDecl && FromExpr)
1049 FromValueDecl = GetValueDecl(FromIter, FromExpr);
--- 159 unchanged lines hidden (view full) ---
1209 while (SubstNonTypeTemplateParmExpr *SNTTPE =
1210 dyn_cast<SubstNonTypeTemplateParmExpr>(ArgExpr))
1211 ArgExpr = SNTTPE->getReplacement();
1212
1213 return ArgExpr;
1214 }
1215
1216 /// GetInt - Retrieves the template integer argument, including evaluating
1217 /// default arguments. If the value comes from an expression, extend the
1218 /// APSInt to size of IntegerType to match the behavior in
1219 /// Sema::CheckTemplateArgument
1220 static bool GetInt(ASTContext &Context, const TSTiterator &Iter,
1221 Expr *ArgExpr, llvm::APSInt &Int, QualType IntegerType) {
1222 // Default, value-depenedent expressions require fetching
1223 // from the desugared TemplateArgument, otherwise expression needs to
1224 // be evaluatable.
1225 if (Iter.isEnd() && ArgExpr->isValueDependent()) {
1226 switch (Iter.getDesugar().getKind()) {
1227 case TemplateArgument::Integral:
1228 Int = Iter.getDesugar().getAsIntegral();
1229 return true;
1230 case TemplateArgument::Expression:
1231 ArgExpr = Iter.getDesugar().getAsExpr();
1232 Int = ArgExpr->EvaluateKnownConstInt(Context);
1233 Int = Int.extOrTrunc(Context.getTypeSize(IntegerType));
1234 return true;
1235 default:
1236 llvm_unreachable("Unexpected template argument kind");
1237 }
1238 } else if (ArgExpr->isEvaluatable(Context)) {
1239 Int = ArgExpr->EvaluateKnownConstInt(Context);
1240 Int = Int.extOrTrunc(Context.getTypeSize(IntegerType));
1241 return true;
1242 }
1243
1244 return false;
1245 }
1246
1247 /// GetValueDecl - Retrieves the template Decl argument, including
1248 /// default expression argument.
--- 56 unchanged lines hidden (view full) ---
1305 if (!Iter.isEnd())
1306 return Iter->getAsTemplate().getAsTemplateDecl();
1307 if (!isVariadic)
1308 return DefaultTD;
1309
1310 return nullptr;
1311 }
1312
1313 /// IsEqualExpr - Returns true if the expressions evaluate to the same value.
1314 static bool IsEqualExpr(ASTContext &Context, Expr *FromExpr, Expr *ToExpr) {
1315 if (FromExpr == ToExpr)
1316 return true;
1317
1318 if (!FromExpr || !ToExpr)
1319 return false;
1320
1321 DeclRefExpr *FromDRE = dyn_cast<DeclRefExpr>(FromExpr->IgnoreParens()),
1322 *ToDRE = dyn_cast<DeclRefExpr>(ToExpr->IgnoreParens());
--- 15 unchanged lines hidden (view full) ---
1338
1339 APValue &FromVal = FromResult.Val;
1340 APValue &ToVal = ToResult.Val;
1341
1342 if (FromVal.getKind() != ToVal.getKind()) return false;
1343
1344 switch (FromVal.getKind()) {
1345 case APValue::Int:
1346 return FromVal.getInt() == ToVal.getInt();
1347 case APValue::LValue: {
1348 APValue::LValueBase FromBase = FromVal.getLValueBase();
1349 APValue::LValueBase ToBase = ToVal.getLValueBase();
1350 if (FromBase.isNull() && ToBase.isNull())
1351 return true;
1352 if (FromBase.isNull() || ToBase.isNull())
1353 return false;
1354 return FromBase.get<const ValueDecl*>() ==
--- 293 unchanged lines hidden (view full) ---
1648 OS << Val.toString(10);
1649 } else if (E) {
1650 PrintExpr(E);
1651 } else {
1652 OS << "(no argument)";
1653 }
1654 Unbold();
1655 }
1656
1657 /// HasExtraInfo - Returns true if E is not an integer literal or the
1658 /// negation of an integer literal
1659 bool HasExtraInfo(Expr *E) {
1660 if (!E) return false;
1661
1662 E = E->IgnoreImpCasts();
1663
1664 if (isa<IntegerLiteral>(E)) return false;
1665
1666 if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E))
1667 if (UO->getOpcode() == UO_Minus)
1668 if (isa<IntegerLiteral>(UO->getSubExpr()))
1669 return false;
1670
1671 return true;
--- 202 unchanged lines hidden ---
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//===----------------------------------------------------------------------===//
--- 984 unchanged lines hidden (view full) ---
993 InitializeNonTypeDiffVariables(Context, ToIter, ToDefaultNonTypeDecl,
994 HasToInt, HasToValueDecl, ToNullPtr,
995 ToExpr, ToInt, ToValueDecl);
996
997 assert(((!HasFromInt && !HasToInt) ||
998 (!HasFromValueDecl && !HasToValueDecl)) &&
999 "Template argument cannot be both integer and declaration");
1000
1001 if (!HasFromInt && !HasToInt && !HasFromValueDecl && !HasToValueDecl) {
1002 Tree.SetNode(FromExpr, ToExpr);
1003 Tree.SetDefault(FromIter.isEnd() && FromExpr, ToIter.isEnd() && ToExpr);
1004 if (FromDefaultNonTypeDecl->getType()->isIntegralOrEnumerationType()) {
1005 if (FromExpr)
1006 HasFromInt = GetInt(Context, FromIter, FromExpr, FromInt,
1007 FromDefaultNonTypeDecl->getType());
1008 if (ToExpr)
1009 HasToInt = GetInt(Context, ToIter, ToExpr, ToInt,
1010 ToDefaultNonTypeDecl->getType());
1011 }
1012 if (HasFromInt && HasToInt) {
1013 Tree.SetNode(FromInt, ToInt, HasFromInt, HasToInt);
1014 Tree.SetSame(FromInt == ToInt);
1015 Tree.SetKind(DiffTree::Integer);
1016 } else if (HasFromInt || HasToInt) {
1017 Tree.SetNode(FromInt, ToInt, HasFromInt, HasToInt);
1018 Tree.SetSame(false);
1019 Tree.SetKind(DiffTree::Integer);
1020 } else {
1021 Tree.SetSame(IsEqualExpr(Context, FromExpr, ToExpr) ||
1022 (FromNullPtr && ToNullPtr));
1023 Tree.SetNullPtr(FromNullPtr, ToNullPtr);
1024 Tree.SetKind(DiffTree::Expression);
1025 }
1026 return;
1027 }
1028
1029 if (HasFromInt || HasToInt) {
1030 if (!HasFromInt && FromExpr)
1031 HasFromInt = GetInt(Context, FromIter, FromExpr, FromInt,
1032 FromDefaultNonTypeDecl->getType());
1033 if (!HasToInt && ToExpr)
1034 HasToInt = GetInt(Context, ToIter, ToExpr, ToInt,
1035 ToDefaultNonTypeDecl->getType());
1036 Tree.SetNode(FromInt, ToInt, HasFromInt, HasToInt);
1037 if (HasFromInt && HasToInt) {
1038 Tree.SetSame(FromInt == ToInt);
1039 } else {
1040 Tree.SetSame(false);
1041 }
1042 Tree.SetDefault(FromIter.isEnd() && HasFromInt,
1043 ToIter.isEnd() && HasToInt);
1044 Tree.SetKind(DiffTree::Integer);
1045 return;
1046 }
1047
1048 if (!HasFromValueDecl && FromExpr)
1049 FromValueDecl = GetValueDecl(FromIter, FromExpr);
--- 159 unchanged lines hidden (view full) ---
1209 while (SubstNonTypeTemplateParmExpr *SNTTPE =
1210 dyn_cast<SubstNonTypeTemplateParmExpr>(ArgExpr))
1211 ArgExpr = SNTTPE->getReplacement();
1212
1213 return ArgExpr;
1214 }
1215
1216 /// GetInt - Retrieves the template integer argument, including evaluating
1217 /// default arguments. If the value comes from an expression, extend the
1218 /// APSInt to size of IntegerType to match the behavior in
1219 /// Sema::CheckTemplateArgument
1220 static bool GetInt(ASTContext &Context, const TSTiterator &Iter,
1221 Expr *ArgExpr, llvm::APSInt &Int, QualType IntegerType) {
1222 // Default, value-depenedent expressions require fetching
1223 // from the desugared TemplateArgument, otherwise expression needs to
1224 // be evaluatable.
1225 if (Iter.isEnd() && ArgExpr->isValueDependent()) {
1226 switch (Iter.getDesugar().getKind()) {
1227 case TemplateArgument::Integral:
1228 Int = Iter.getDesugar().getAsIntegral();
1229 return true;
1230 case TemplateArgument::Expression:
1231 ArgExpr = Iter.getDesugar().getAsExpr();
1232 Int = ArgExpr->EvaluateKnownConstInt(Context);
1233 Int = Int.extOrTrunc(Context.getTypeSize(IntegerType));
1234 return true;
1235 default:
1236 llvm_unreachable("Unexpected template argument kind");
1237 }
1238 } else if (ArgExpr->isEvaluatable(Context)) {
1239 Int = ArgExpr->EvaluateKnownConstInt(Context);
1240 Int = Int.extOrTrunc(Context.getTypeSize(IntegerType));
1241 return true;
1242 }
1243
1244 return false;
1245 }
1246
1247 /// GetValueDecl - Retrieves the template Decl argument, including
1248 /// default expression argument.
--- 56 unchanged lines hidden (view full) ---
1305 if (!Iter.isEnd())
1306 return Iter->getAsTemplate().getAsTemplateDecl();
1307 if (!isVariadic)
1308 return DefaultTD;
1309
1310 return nullptr;
1311 }
1312
1313 /// IsEqualExpr - Returns true if the expressions evaluate to the same value.
1314 static bool IsEqualExpr(ASTContext &Context, Expr *FromExpr, Expr *ToExpr) {
1315 if (FromExpr == ToExpr)
1316 return true;
1317
1318 if (!FromExpr || !ToExpr)
1319 return false;
1320
1321 DeclRefExpr *FromDRE = dyn_cast<DeclRefExpr>(FromExpr->IgnoreParens()),
1322 *ToDRE = dyn_cast<DeclRefExpr>(ToExpr->IgnoreParens());
--- 15 unchanged lines hidden (view full) ---
1338
1339 APValue &FromVal = FromResult.Val;
1340 APValue &ToVal = ToResult.Val;
1341
1342 if (FromVal.getKind() != ToVal.getKind()) return false;
1343
1344 switch (FromVal.getKind()) {
1345 case APValue::Int:
1346 return FromVal.getInt() == ToVal.getInt();
1347 case APValue::LValue: {
1348 APValue::LValueBase FromBase = FromVal.getLValueBase();
1349 APValue::LValueBase ToBase = ToVal.getLValueBase();
1350 if (FromBase.isNull() && ToBase.isNull())
1351 return true;
1352 if (FromBase.isNull() || ToBase.isNull())
1353 return false;
1354 return FromBase.get<const ValueDecl*>() ==
--- 293 unchanged lines hidden (view full) ---
1648 OS << Val.toString(10);
1649 } else if (E) {
1650 PrintExpr(E);
1651 } else {
1652 OS << "(no argument)";
1653 }
1654 Unbold();
1655 }
1656
1657 /// HasExtraInfo - Returns true if E is not an integer literal or the
1658 /// negation of an integer literal
1659 bool HasExtraInfo(Expr *E) {
1660 if (!E) return false;
1661
1662 E = E->IgnoreImpCasts();
1663
1664 if (isa<IntegerLiteral>(E)) return false;
1665
1666 if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E))
1667 if (UO->getOpcode() == UO_Minus)
1668 if (isa<IntegerLiteral>(UO->getSubExpr()))
1669 return false;
1670
1671 return true;
--- 202 unchanged lines hidden ---