1//===------ SemaPPC.cpp ------ PowerPC target-specific routines -----------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements semantic analysis functions specific to PowerPC.
10//
11//===----------------------------------------------------------------------===//
12
13#include "clang/Sema/SemaPPC.h"
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/Attr.h"
16#include "clang/AST/CharUnits.h"
17#include "clang/AST/Decl.h"
18#include "clang/AST/Type.h"
19#include "clang/Basic/DiagnosticSema.h"
20#include "clang/Basic/SourceLocation.h"
21#include "clang/Basic/TargetBuiltins.h"
22#include "clang/Basic/TargetInfo.h"
23#include "clang/Sema/Sema.h"
24#include "llvm/ADT/APSInt.h"
25
26namespace clang {
27
28SemaPPC::SemaPPC(Sema &S) : SemaBase(S) {}
29
30void SemaPPC::checkAIXMemberAlignment(SourceLocation Loc, const Expr *Arg) {
31 const auto *ICE = dyn_cast<ImplicitCastExpr>(Val: Arg->IgnoreParens());
32 if (!ICE)
33 return;
34
35 const auto *DR = dyn_cast<DeclRefExpr>(Val: ICE->getSubExpr());
36 if (!DR)
37 return;
38
39 const auto *PD = dyn_cast<ParmVarDecl>(Val: DR->getDecl());
40 if (!PD || !PD->getType()->isRecordType())
41 return;
42
43 QualType ArgType = Arg->getType();
44 for (const FieldDecl *FD : ArgType->castAsRecordDecl()->fields()) {
45 if (const auto *AA = FD->getAttr<AlignedAttr>()) {
46 CharUnits Alignment = getASTContext().toCharUnitsFromBits(
47 BitSize: AA->getAlignment(Ctx&: getASTContext()));
48 if (Alignment.getQuantity() == 16) {
49 Diag(Loc: FD->getLocation(), DiagID: diag::warn_not_xl_compatible) << FD;
50 Diag(Loc, DiagID: diag::note_misaligned_member_used_here) << PD;
51 }
52 }
53 }
54}
55
56static bool isPPC_64Builtin(unsigned BuiltinID) {
57 // These builtins only work on PPC 64bit targets.
58 switch (BuiltinID) {
59 case PPC::BI__builtin_divde:
60 case PPC::BI__builtin_divdeu:
61 case PPC::BI__builtin_bpermd:
62 case PPC::BI__builtin_pdepd:
63 case PPC::BI__builtin_pextd:
64 case PPC::BI__builtin_ppc_cdtbcd:
65 case PPC::BI__builtin_ppc_cbcdtd:
66 case PPC::BI__builtin_ppc_addg6s:
67 case PPC::BI__builtin_ppc_ldarx:
68 case PPC::BI__builtin_ppc_stdcx:
69 case PPC::BI__builtin_ppc_tdw:
70 case PPC::BI__builtin_ppc_trapd:
71 case PPC::BI__builtin_ppc_cmpeqb:
72 case PPC::BI__builtin_ppc_setb:
73 case PPC::BI__builtin_ppc_mulhd:
74 case PPC::BI__builtin_ppc_mulhdu:
75 case PPC::BI__builtin_ppc_maddhd:
76 case PPC::BI__builtin_ppc_maddhdu:
77 case PPC::BI__builtin_ppc_maddld:
78 case PPC::BI__builtin_ppc_load8r:
79 case PPC::BI__builtin_ppc_store8r:
80 case PPC::BI__builtin_ppc_insert_exp:
81 case PPC::BI__builtin_ppc_extract_sig:
82 case PPC::BI__builtin_ppc_addex:
83 case PPC::BI__builtin_darn:
84 case PPC::BI__builtin_darn_raw:
85 case PPC::BI__builtin_ppc_compare_and_swaplp:
86 case PPC::BI__builtin_ppc_fetch_and_addlp:
87 case PPC::BI__builtin_ppc_fetch_and_andlp:
88 case PPC::BI__builtin_ppc_fetch_and_orlp:
89 case PPC::BI__builtin_ppc_fetch_and_swaplp:
90 case PPC::BI__builtin_amo_lwat:
91 case PPC::BI__builtin_amo_ldat:
92 case PPC::BI__builtin_amo_lwat_s:
93 case PPC::BI__builtin_amo_ldat_s:
94 case PPC::BI__builtin_amo_lwat_cond:
95 case PPC::BI__builtin_amo_ldat_cond:
96 case PPC::BI__builtin_amo_lwat_cond_s:
97 case PPC::BI__builtin_amo_ldat_cond_s:
98 case PPC::BI__builtin_amo_stwat:
99 case PPC::BI__builtin_amo_stdat:
100 case PPC::BI__builtin_amo_stwat_s:
101 case PPC::BI__builtin_amo_stdat_s:
102 return true;
103 }
104 return false;
105}
106
107bool SemaPPC::CheckPPCBuiltinFunctionCall(const TargetInfo &TI,
108 unsigned BuiltinID,
109 CallExpr *TheCall) {
110 ASTContext &Context = getASTContext();
111 bool IsTarget64Bit = TI.getTypeWidth(T: TI.getIntPtrType()) == 64;
112
113 if (isPPC_64Builtin(BuiltinID) && !IsTarget64Bit)
114 return Diag(Loc: TheCall->getBeginLoc(), DiagID: diag::err_64_bit_builtin_32_bit_tgt)
115 << TheCall->getSourceRange();
116
117 // Common BCD type-validation helpers
118 // Emit error diagnostics and return true on success
119 // - IsTypeVecUChar: enforces vector unsigned char
120 // - IsIntType: enforces any integer type
121 // Lambdas centralize type checks for BCD builtin handlers
122
123 // Lambda 1: verify vector unsigned char type
124 auto IsTypeVecUChar = [&](QualType ArgTy, unsigned ArgIndex) -> bool {
125 QualType VecType = Context.getVectorType(VectorType: Context.UnsignedCharTy, NumElts: 16,
126 VecKind: VectorKind::AltiVecVector);
127 if (Context.hasSameType(T1: ArgTy, T2: VecType))
128 return true;
129
130 Diag(Loc: TheCall->getArg(Arg: ArgIndex)->getBeginLoc(),
131 DiagID: diag::err_ppc_invalid_arg_type)
132 << ArgIndex << VecType << ArgTy;
133 return false;
134 };
135
136 // Lambda 2: verify integer type
137 auto IsIntType = [&](QualType ArgTy, unsigned ArgIndex) -> bool {
138 if (ArgTy->isIntegerType())
139 return true;
140
141 Diag(Loc: TheCall->getArg(Arg: ArgIndex)->getBeginLoc(),
142 DiagID: diag::err_ppc_invalid_arg_type)
143 << ArgIndex << "integer" << ArgTy;
144 return false;
145 };
146
147 switch (BuiltinID) {
148 default:
149 return false;
150 case PPC::BI__builtin_ppc_bcdsetsign: {
151 // Arg0 must be vector unsigned char
152 if (!IsTypeVecUChar(TheCall->getArg(Arg: 0)->getType(), 0))
153 return false;
154
155 // Restrict Arg1 constant range (0–1)
156 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 1, Low: 0, High: 1);
157 }
158 case PPC::BI__builtin_ppc_national2packed:
159 case PPC::BI__builtin_ppc_packed2zoned:
160 case PPC::BI__builtin_ppc_zoned2packed:
161 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 1, Low: 0, High: 1);
162 case PPC::BI__builtin_ppc_bcdshift:
163 case PPC::BI__builtin_ppc_bcdshiftround:
164 case PPC::BI__builtin_ppc_bcdtruncate: {
165
166 // Arg0 must be vector unsigned char
167 if (!IsTypeVecUChar(TheCall->getArg(Arg: 0)->getType(), 0))
168 return false;
169
170 // Arg1 must be integer type
171 if (!IsIntType(TheCall->getArg(Arg: 1)->getType(), 1))
172 return false;
173
174 // Restrict Arg2 constant range (0–1)
175 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 2, Low: 0, High: 1);
176 }
177 case PPC::BI__builtin_altivec_crypto_vshasigmaw:
178 case PPC::BI__builtin_altivec_crypto_vshasigmad:
179 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 1, Low: 0, High: 1) ||
180 SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 2, Low: 0, High: 15);
181 case PPC::BI__builtin_altivec_dss:
182 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 0, Low: 0, High: 3);
183 case PPC::BI__builtin_tbegin:
184 case PPC::BI__builtin_tend:
185 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 0, Low: 0, High: 1);
186 case PPC::BI__builtin_tsr:
187 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 0, Low: 0, High: 7);
188 case PPC::BI__builtin_tabortwc:
189 case PPC::BI__builtin_tabortdc:
190 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 0, Low: 0, High: 31);
191 case PPC::BI__builtin_tabortwci:
192 case PPC::BI__builtin_tabortdci:
193 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 0, Low: 0, High: 31) ||
194 SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 2, Low: 0, High: 31);
195 // According to GCC 'Basic PowerPC Built-in Functions Available on ISA 2.05',
196 // __builtin_(un)pack_longdouble are available only if long double uses IBM
197 // extended double representation.
198 case PPC::BI__builtin_unpack_longdouble:
199 if (SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 1, Low: 0, High: 1))
200 return true;
201 [[fallthrough]];
202 case PPC::BI__builtin_pack_longdouble:
203 if (&TI.getLongDoubleFormat() != &llvm::APFloat::PPCDoubleDouble())
204 return Diag(Loc: TheCall->getBeginLoc(), DiagID: diag::err_ppc_builtin_requires_abi)
205 << "ibmlongdouble";
206 return false;
207 case PPC::BI__builtin_altivec_dst:
208 case PPC::BI__builtin_altivec_dstt:
209 case PPC::BI__builtin_altivec_dstst:
210 case PPC::BI__builtin_altivec_dststt:
211 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 2, Low: 0, High: 3);
212 case PPC::BI__builtin_vsx_xxpermdi:
213 case PPC::BI__builtin_vsx_xxsldwi:
214 return BuiltinVSX(TheCall);
215 case PPC::BI__builtin_unpack_vector_int128:
216 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 1, Low: 0, High: 1);
217 case PPC::BI__builtin_altivec_vgnb:
218 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 1, Low: 2, High: 7);
219 case PPC::BI__builtin_vsx_xxeval:
220 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 3, Low: 0, High: 255);
221 case PPC::BI__builtin_altivec_vsldbi:
222 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 2, Low: 0, High: 7);
223 case PPC::BI__builtin_altivec_vsrdbi:
224 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 2, Low: 0, High: 7);
225 case PPC::BI__builtin_vsx_xxpermx:
226 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 3, Low: 0, High: 7);
227 case PPC::BI__builtin_ppc_tw:
228 case PPC::BI__builtin_ppc_tdw:
229 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 2, Low: 1, High: 31);
230 case PPC::BI__builtin_ppc_cmprb:
231 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 0, Low: 0, High: 1);
232 // For __rlwnm, __rlwimi and __rldimi, the last parameter mask must
233 // be a constant that represents a contiguous bit field.
234 case PPC::BI__builtin_ppc_rlwnm:
235 return SemaRef.ValueIsRunOfOnes(TheCall, ArgNum: 2);
236 case PPC::BI__builtin_ppc_rlwimi:
237 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 2, Low: 0, High: 31) ||
238 SemaRef.ValueIsRunOfOnes(TheCall, ArgNum: 3);
239 case PPC::BI__builtin_ppc_rldimi:
240 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 2, Low: 0, High: 63) ||
241 SemaRef.ValueIsRunOfOnes(TheCall, ArgNum: 3);
242 case PPC::BI__builtin_ppc_addex: {
243 if (SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 2, Low: 0, High: 3))
244 return true;
245 // Output warning for reserved values 1 to 3.
246 int ArgValue =
247 TheCall->getArg(Arg: 2)->getIntegerConstantExpr(Ctx: Context)->getSExtValue();
248 if (ArgValue != 0)
249 Diag(Loc: TheCall->getBeginLoc(), DiagID: diag::warn_argument_undefined_behaviour)
250 << ArgValue;
251 return false;
252 }
253 case PPC::BI__builtin_ppc_mtfsb0:
254 case PPC::BI__builtin_ppc_mtfsb1:
255 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 0, Low: 0, High: 31);
256 case PPC::BI__builtin_ppc_mtfsf:
257 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 0, Low: 0, High: 255);
258 case PPC::BI__builtin_ppc_mtfsfi:
259 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 0, Low: 0, High: 7) ||
260 SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 1, Low: 0, High: 15);
261 case PPC::BI__builtin_ppc_alignx:
262 return SemaRef.BuiltinConstantArgPower2(TheCall, ArgNum: 0);
263 case PPC::BI__builtin_ppc_rdlam:
264 return SemaRef.ValueIsRunOfOnes(TheCall, ArgNum: 2);
265 case PPC::BI__builtin_vsx_ldrmb:
266 case PPC::BI__builtin_vsx_strmb:
267 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 1, Low: 1, High: 16);
268 case PPC::BI__builtin_altivec_vcntmbb:
269 case PPC::BI__builtin_altivec_vcntmbh:
270 case PPC::BI__builtin_altivec_vcntmbw:
271 case PPC::BI__builtin_altivec_vcntmbd:
272 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 1, Low: 0, High: 1);
273 case PPC::BI__builtin_vsx_xxgenpcvbm:
274 case PPC::BI__builtin_vsx_xxgenpcvhm:
275 case PPC::BI__builtin_vsx_xxgenpcvwm:
276 case PPC::BI__builtin_vsx_xxgenpcvdm:
277 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 1, Low: 0, High: 3);
278 case PPC::BI__builtin_ppc_test_data_class: {
279 // Check if the first argument of the __builtin_ppc_test_data_class call is
280 // valid. The argument must be 'float' or 'double' or '__float128'.
281 QualType ArgType = TheCall->getArg(Arg: 0)->getType();
282 if (ArgType != QualType(Context.FloatTy) &&
283 ArgType != QualType(Context.DoubleTy) &&
284 ArgType != QualType(Context.Float128Ty))
285 return Diag(Loc: TheCall->getBeginLoc(),
286 DiagID: diag::err_ppc_invalid_test_data_class_type);
287 return SemaRef.BuiltinConstantArgRange(TheCall, ArgNum: 1, Low: 0, High: 127);
288 }
289 case PPC::BI__builtin_ppc_maxfe:
290 case PPC::BI__builtin_ppc_minfe:
291 case PPC::BI__builtin_ppc_maxfl:
292 case PPC::BI__builtin_ppc_minfl:
293 case PPC::BI__builtin_ppc_maxfs:
294 case PPC::BI__builtin_ppc_minfs: {
295 if (Context.getTargetInfo().getTriple().isOSAIX() &&
296 (BuiltinID == PPC::BI__builtin_ppc_maxfe ||
297 BuiltinID == PPC::BI__builtin_ppc_minfe))
298 return Diag(Loc: TheCall->getBeginLoc(), DiagID: diag::err_target_unsupported_type)
299 << "builtin" << true << 128 << QualType(Context.LongDoubleTy)
300 << false << Context.getTargetInfo().getTriple().str();
301 // Argument type should be exact.
302 QualType ArgType = QualType(Context.LongDoubleTy);
303 if (BuiltinID == PPC::BI__builtin_ppc_maxfl ||
304 BuiltinID == PPC::BI__builtin_ppc_minfl)
305 ArgType = QualType(Context.DoubleTy);
306 else if (BuiltinID == PPC::BI__builtin_ppc_maxfs ||
307 BuiltinID == PPC::BI__builtin_ppc_minfs)
308 ArgType = QualType(Context.FloatTy);
309 for (unsigned I = 0, E = TheCall->getNumArgs(); I < E; ++I)
310 if (TheCall->getArg(Arg: I)->getType() != ArgType)
311 return Diag(Loc: TheCall->getBeginLoc(),
312 DiagID: diag::err_typecheck_convert_incompatible)
313 << TheCall->getArg(Arg: I)->getType() << ArgType << 1 << 0 << 0;
314 return false;
315 }
316#define CUSTOM_BUILTIN(Name, Intr, Types, Acc, Feature) \
317 case PPC::BI__builtin_##Name: \
318 return BuiltinPPCMMACall(TheCall, BuiltinID, Types);
319#include "clang/Basic/BuiltinsPPC.def"
320 case PPC::BI__builtin_amo_lwat:
321 case PPC::BI__builtin_amo_ldat:
322 case PPC::BI__builtin_amo_lwat_s:
323 case PPC::BI__builtin_amo_ldat_s: {
324 llvm::APSInt Result;
325 if (SemaRef.BuiltinConstantArg(TheCall, ArgNum: 2, Result))
326 return true;
327 unsigned Val = Result.getZExtValue();
328
329 bool IsUnsigned = (BuiltinID == PPC::BI__builtin_amo_lwat ||
330 BuiltinID == PPC::BI__builtin_amo_ldat);
331
332 bool IsValid = IsUnsigned
333 ? llvm::is_contained(Set: {0u, 1u, 2u, 3u, 4u, 6u, 8u}, Element: Val)
334 : llvm::is_contained(Set: {0u, 5u, 7u, 8u}, Element: Val);
335
336 if (IsValid)
337 return false;
338
339 Expr *Arg = TheCall->getArg(Arg: 2);
340 return SemaRef.Diag(Loc: Arg->getBeginLoc(), DiagID: diag::err_argument_invalid_range)
341 << toString(I: Result, Radix: 10) << (IsUnsigned ? "0-4, 6" : "0, 5, 7") << "8"
342 << Arg->getSourceRange();
343 }
344 case PPC::BI__builtin_amo_lwat_cond:
345 case PPC::BI__builtin_amo_ldat_cond:
346 case PPC::BI__builtin_amo_lwat_cond_s:
347 case PPC::BI__builtin_amo_ldat_cond_s: {
348 llvm::APSInt Result;
349 if (SemaRef.BuiltinConstantArg(TheCall, ArgNum: 1, Result))
350 return true;
351 unsigned Val = Result.getZExtValue();
352 if (llvm::is_contained(Set: {24u, 25u, 28u}, Element: Val))
353 return false;
354
355 Expr *Arg = TheCall->getArg(Arg: 1);
356 return SemaRef.Diag(Loc: Arg->getBeginLoc(), DiagID: diag::err_argument_invalid_range)
357 << toString(I: Result, Radix: 10) << "24, 25" << "28" << Arg->getSourceRange();
358 }
359 case PPC::BI__builtin_amo_stwat:
360 case PPC::BI__builtin_amo_stdat:
361 case PPC::BI__builtin_amo_stwat_s:
362 case PPC::BI__builtin_amo_stdat_s: {
363 llvm::APSInt Result;
364 if (SemaRef.BuiltinConstantArg(TheCall, ArgNum: 2, Result))
365 return true;
366 unsigned Val = Result.getZExtValue();
367
368 bool IsUnsigned = (BuiltinID == PPC::BI__builtin_amo_stwat ||
369 BuiltinID == PPC::BI__builtin_amo_stdat);
370
371 bool IsValid = IsUnsigned
372 ? llvm::is_contained(Set: {0u, 1u, 2u, 3u, 4u, 6u, 24u}, Element: Val)
373 : llvm::is_contained(Set: {0u, 5u, 7u, 24u}, Element: Val);
374
375 if (IsValid)
376 return false;
377
378 Expr *Arg = TheCall->getArg(Arg: 2);
379 return SemaRef.Diag(Loc: Arg->getBeginLoc(), DiagID: diag::err_argument_invalid_range)
380 << toString(I: Result, Radix: 10) << (IsUnsigned ? "0-4, 6" : "0, 5, 7")
381 << "24" << Arg->getSourceRange();
382 }
383 }
384 llvm_unreachable("must return from switch");
385}
386
387// Check if the given type is a non-pointer PPC MMA type. This function is used
388// in Sema to prevent invalid uses of restricted PPC MMA types.
389bool SemaPPC::CheckPPCMMAType(QualType Type, SourceLocation TypeLoc) {
390 ASTContext &Context = getASTContext();
391 if (Type->isPointerType() || Type->isArrayType())
392 return false;
393
394 QualType CoreType = Type.getCanonicalType().getUnqualifiedType();
395#define PPC_VECTOR_TYPE(Name, Id, Size) || CoreType == Context.Id##Ty
396 if (false
397#include "clang/Basic/PPCTypes.def"
398 ) {
399 Diag(Loc: TypeLoc, DiagID: diag::err_ppc_invalid_use_mma_type);
400 return true;
401 }
402 return false;
403}
404
405/// DecodePPCMMATypeFromStr - This decodes one PPC MMA type descriptor from Str,
406/// advancing the pointer over the consumed characters. The decoded type is
407/// returned. If the decoded type represents a constant integer with a
408/// constraint on its value then Mask is set to that value. The type descriptors
409/// used in Str are specific to PPC MMA builtins and are documented in the file
410/// defining the PPC builtins.
411static QualType DecodePPCMMATypeFromStr(ASTContext &Context, const char *&Str,
412 unsigned &Mask) {
413 bool RequireICE = false;
414 ASTContext::GetBuiltinTypeError Error = ASTContext::GE_None;
415 switch (*Str++) {
416 case 'V':
417 return Context.getVectorType(VectorType: Context.UnsignedCharTy, NumElts: 16,
418 VecKind: VectorKind::AltiVecVector);
419 case 'i': {
420 char *End;
421 unsigned size = strtoul(nptr: Str, endptr: &End, base: 10);
422 assert(End != Str && "Missing constant parameter constraint");
423 Str = End;
424 Mask = size;
425 return Context.IntTy;
426 }
427 case 'W': {
428 char *End;
429 unsigned size = strtoul(nptr: Str, endptr: &End, base: 10);
430 assert(End != Str && "Missing PowerPC MMA type size");
431 Str = End;
432 QualType Type;
433 switch (size) {
434#define PPC_VECTOR_TYPE(typeName, Id, size) \
435 case size: \
436 Type = Context.Id##Ty; \
437 break;
438#include "clang/Basic/PPCTypes.def"
439 default:
440 llvm_unreachable("Invalid PowerPC MMA vector type");
441 }
442 bool CheckVectorArgs = false;
443 while (!CheckVectorArgs) {
444 switch (*Str++) {
445 case '*':
446 Type = Context.getPointerType(T: Type);
447 break;
448 case 'C':
449 Type = Type.withConst();
450 break;
451 default:
452 CheckVectorArgs = true;
453 --Str;
454 break;
455 }
456 }
457 return Type;
458 }
459 default:
460 return Context.DecodeTypeStr(Str&: --Str, Context, Error, RequireICE, AllowTypeModifiers: true);
461 }
462}
463
464bool SemaPPC::BuiltinPPCMMACall(CallExpr *TheCall, unsigned BuiltinID,
465 const char *TypeStr) {
466
467 assert((TypeStr[0] != '\0') &&
468 "Invalid types in PPC MMA builtin declaration");
469
470 ASTContext &Context = getASTContext();
471 unsigned Mask = 0;
472 unsigned ArgNum = 0;
473
474 // The first type in TypeStr is the type of the value returned by the
475 // builtin. So we first read that type and change the type of TheCall.
476 QualType type = DecodePPCMMATypeFromStr(Context, Str&: TypeStr, Mask);
477 TheCall->setType(type);
478
479 while (*TypeStr != '\0') {
480 Mask = 0;
481 QualType ExpectedType = DecodePPCMMATypeFromStr(Context, Str&: TypeStr, Mask);
482 if (ArgNum >= TheCall->getNumArgs()) {
483 ArgNum++;
484 break;
485 }
486
487 Expr *Arg = TheCall->getArg(Arg: ArgNum);
488 QualType PassedType = Arg->getType();
489 QualType StrippedRVType = PassedType.getCanonicalType();
490
491 // Strip Restrict/Volatile qualifiers.
492 if (StrippedRVType.isRestrictQualified() ||
493 StrippedRVType.isVolatileQualified())
494 StrippedRVType = StrippedRVType.getCanonicalType().getUnqualifiedType();
495
496 // The only case where the argument type and expected type are allowed to
497 // mismatch is if the argument type is a non-void pointer (or array) and
498 // expected type is a void pointer.
499 if (StrippedRVType != ExpectedType)
500 if (!(ExpectedType->isVoidPointerType() &&
501 (StrippedRVType->isPointerType() || StrippedRVType->isArrayType())))
502 return Diag(Loc: Arg->getBeginLoc(),
503 DiagID: diag::err_typecheck_convert_incompatible)
504 << PassedType << ExpectedType << 1 << 0 << 0;
505
506 // If the value of the Mask is not 0, we have a constraint in the size of
507 // the integer argument so here we ensure the argument is a constant that
508 // is in the valid range.
509 if (Mask != 0 &&
510 SemaRef.BuiltinConstantArgRange(TheCall, ArgNum, Low: 0, High: Mask, RangeIsError: true))
511 return true;
512
513 ArgNum++;
514 }
515
516 // In case we exited early from the previous loop, there are other types to
517 // read from TypeStr. So we need to read them all to ensure we have the right
518 // number of arguments in TheCall and if it is not the case, to display a
519 // better error message.
520 while (*TypeStr != '\0') {
521 (void)DecodePPCMMATypeFromStr(Context, Str&: TypeStr, Mask);
522 ArgNum++;
523 }
524 if (SemaRef.checkArgCount(Call: TheCall, DesiredArgCount: ArgNum))
525 return true;
526
527 return false;
528}
529
530bool SemaPPC::BuiltinVSX(CallExpr *TheCall) {
531 unsigned ExpectedNumArgs = 3;
532 if (SemaRef.checkArgCount(Call: TheCall, DesiredArgCount: ExpectedNumArgs))
533 return true;
534
535 // Check the third argument is a compile time constant
536 if (!TheCall->getArg(Arg: 2)->isIntegerConstantExpr(Ctx: getASTContext()))
537 return Diag(Loc: TheCall->getBeginLoc(),
538 DiagID: diag::err_vsx_builtin_nonconstant_argument)
539 << 3 /* argument index */ << TheCall->getDirectCallee()
540 << SourceRange(TheCall->getArg(Arg: 2)->getBeginLoc(),
541 TheCall->getArg(Arg: 2)->getEndLoc());
542
543 QualType Arg1Ty = TheCall->getArg(Arg: 0)->getType();
544 QualType Arg2Ty = TheCall->getArg(Arg: 1)->getType();
545
546 // Check the type of argument 1 and argument 2 are vectors.
547 SourceLocation BuiltinLoc = TheCall->getBeginLoc();
548 if ((!Arg1Ty->isVectorType() && !Arg1Ty->isDependentType()) ||
549 (!Arg2Ty->isVectorType() && !Arg2Ty->isDependentType())) {
550 return Diag(Loc: BuiltinLoc, DiagID: diag::err_vec_builtin_non_vector)
551 << TheCall->getDirectCallee() << /*isMorethantwoArgs*/ false
552 << SourceRange(TheCall->getArg(Arg: 0)->getBeginLoc(),
553 TheCall->getArg(Arg: 1)->getEndLoc());
554 }
555
556 // Check the first two arguments are the same type.
557 if (!getASTContext().hasSameUnqualifiedType(T1: Arg1Ty, T2: Arg2Ty)) {
558 return Diag(Loc: BuiltinLoc, DiagID: diag::err_vec_builtin_incompatible_vector)
559 << TheCall->getDirectCallee() << /*isMorethantwoArgs*/ false
560 << SourceRange(TheCall->getArg(Arg: 0)->getBeginLoc(),
561 TheCall->getArg(Arg: 1)->getEndLoc());
562 }
563
564 // When default clang type checking is turned off and the customized type
565 // checking is used, the returning type of the function must be explicitly
566 // set. Otherwise it is _Bool by default.
567 TheCall->setType(Arg1Ty);
568
569 return false;
570}
571
572} // namespace clang
573