blob: 7f79a05b46d16b9de8a84a38041ecaed75ed5a67 [file] [log] [blame]
Chad Rosier571c5e92012-08-17 21:27:25 +00001//===--- SemaStmtAsm.cpp - Semantic Analysis for Asm Statements -----------===//
Chad Rosier0731aff2012-08-17 21:19:40 +00002//
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 file implements semantic analysis for inline asm statements.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Sema/SemaInternal.h"
Chad Rosier5c563642012-10-25 21:49:22 +000015#include "clang/AST/RecordLayout.h"
Chad Rosier0731aff2012-08-17 21:19:40 +000016#include "clang/AST/TypeLoc.h"
Chad Rosier0731aff2012-08-17 21:19:40 +000017#include "clang/Basic/TargetInfo.h"
Ehsan Akhgari31097582014-09-22 02:21:54 +000018#include "clang/Lex/Preprocessor.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "clang/Sema/Initialization.h"
20#include "clang/Sema/Lookup.h"
21#include "clang/Sema/Scope.h"
22#include "clang/Sema/ScopeInfo.h"
Chad Rosier0731aff2012-08-17 21:19:40 +000023#include "llvm/ADT/ArrayRef.h"
24#include "llvm/ADT/BitVector.h"
Alp Toker10399272014-06-08 05:11:37 +000025#include "llvm/MC/MCParser/MCAsmParser.h"
Chad Rosier0731aff2012-08-17 21:19:40 +000026using namespace clang;
27using namespace sema;
28
29/// CheckAsmLValue - GNU C has an extremely ugly extension whereby they silently
30/// ignore "noop" casts in places where an lvalue is required by an inline asm.
31/// We emulate this behavior when -fheinous-gnu-extensions is specified, but
32/// provide a strong guidance to not use it.
33///
34/// This method checks to see if the argument is an acceptable l-value and
35/// returns false if it is a case we can handle.
36static bool CheckAsmLValue(const Expr *E, Sema &S) {
37 // Type dependent expressions will be checked during instantiation.
38 if (E->isTypeDependent())
39 return false;
40
41 if (E->isLValue())
42 return false; // Cool, this is an lvalue.
43
44 // Okay, this is not an lvalue, but perhaps it is the result of a cast that we
45 // are supposed to allow.
46 const Expr *E2 = E->IgnoreParenNoopCasts(S.Context);
47 if (E != E2 && E2->isLValue()) {
48 if (!S.getLangOpts().HeinousExtensions)
49 S.Diag(E2->getLocStart(), diag::err_invalid_asm_cast_lvalue)
50 << E->getSourceRange();
51 else
52 S.Diag(E2->getLocStart(), diag::warn_invalid_asm_cast_lvalue)
53 << E->getSourceRange();
54 // Accept, even if we emitted an error diagnostic.
55 return false;
56 }
57
58 // None of the above, just randomly invalid non-lvalue.
59 return true;
60}
61
62/// isOperandMentioned - Return true if the specified operand # is mentioned
63/// anywhere in the decomposed asm string.
64static bool isOperandMentioned(unsigned OpNo,
Chad Rosierde70e0e2012-08-25 00:11:56 +000065 ArrayRef<GCCAsmStmt::AsmStringPiece> AsmStrPieces) {
Chad Rosier0731aff2012-08-17 21:19:40 +000066 for (unsigned p = 0, e = AsmStrPieces.size(); p != e; ++p) {
Chad Rosierde70e0e2012-08-25 00:11:56 +000067 const GCCAsmStmt::AsmStringPiece &Piece = AsmStrPieces[p];
Chad Rosier0731aff2012-08-17 21:19:40 +000068 if (!Piece.isOperand()) continue;
69
70 // If this is a reference to the input and if the input was the smaller
71 // one, then we have to reject this asm.
72 if (Piece.getOperandNo() == OpNo)
73 return true;
74 }
75 return false;
76}
77
Hans Wennborge9d240a2014-10-08 01:58:02 +000078static bool CheckNakedParmReference(Expr *E, Sema &S) {
79 FunctionDecl *Func = dyn_cast<FunctionDecl>(S.CurContext);
80 if (!Func)
81 return false;
82 if (!Func->hasAttr<NakedAttr>())
83 return false;
84
85 SmallVector<Expr*, 4> WorkList;
86 WorkList.push_back(E);
87 while (WorkList.size()) {
88 Expr *E = WorkList.pop_back_val();
89 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
90 if (isa<ParmVarDecl>(DRE->getDecl())) {
91 S.Diag(DRE->getLocStart(), diag::err_asm_naked_parm_ref);
92 S.Diag(Func->getAttr<NakedAttr>()->getLocation(), diag::note_attribute);
93 return true;
94 }
95 }
96 for (Stmt *Child : E->children()) {
97 if (Expr *E = dyn_cast_or_null<Expr>(Child))
98 WorkList.push_back(E);
99 }
100 }
101 return false;
102}
103
Chad Rosierde70e0e2012-08-25 00:11:56 +0000104StmtResult Sema::ActOnGCCAsmStmt(SourceLocation AsmLoc, bool IsSimple,
105 bool IsVolatile, unsigned NumOutputs,
106 unsigned NumInputs, IdentifierInfo **Names,
Dmitri Gribenkoea2d5f82013-05-10 01:14:26 +0000107 MultiExprArg constraints, MultiExprArg Exprs,
Chad Rosierde70e0e2012-08-25 00:11:56 +0000108 Expr *asmString, MultiExprArg clobbers,
109 SourceLocation RParenLoc) {
Chad Rosier0731aff2012-08-17 21:19:40 +0000110 unsigned NumClobbers = clobbers.size();
111 StringLiteral **Constraints =
Benjamin Kramercc4c49d2012-08-23 23:38:35 +0000112 reinterpret_cast<StringLiteral**>(constraints.data());
Chad Rosier0731aff2012-08-17 21:19:40 +0000113 StringLiteral *AsmString = cast<StringLiteral>(asmString);
Benjamin Kramercc4c49d2012-08-23 23:38:35 +0000114 StringLiteral **Clobbers = reinterpret_cast<StringLiteral**>(clobbers.data());
Chad Rosier0731aff2012-08-17 21:19:40 +0000115
116 SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos;
117
118 // The parser verifies that there is a string literal here.
119 if (!AsmString->isAscii())
120 return StmtError(Diag(AsmString->getLocStart(),diag::err_asm_wide_character)
121 << AsmString->getSourceRange());
122
123 for (unsigned i = 0; i != NumOutputs; i++) {
124 StringLiteral *Literal = Constraints[i];
125 if (!Literal->isAscii())
126 return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
127 << Literal->getSourceRange());
128
129 StringRef OutputName;
130 if (Names[i])
131 OutputName = Names[i]->getName();
132
133 TargetInfo::ConstraintInfo Info(Literal->getString(), OutputName);
134 if (!Context.getTargetInfo().validateOutputConstraint(Info))
135 return StmtError(Diag(Literal->getLocStart(),
136 diag::err_asm_invalid_output_constraint)
137 << Info.getConstraintStr());
138
139 // Check that the output exprs are valid lvalues.
140 Expr *OutputExpr = Exprs[i];
Bill Wendlingc4fc3a22013-03-25 21:09:49 +0000141 if (CheckAsmLValue(OutputExpr, *this))
Chad Rosier0731aff2012-08-17 21:19:40 +0000142 return StmtError(Diag(OutputExpr->getLocStart(),
Bill Wendlingc4fc3a22013-03-25 21:09:49 +0000143 diag::err_asm_invalid_lvalue_in_output)
144 << OutputExpr->getSourceRange());
145
Hans Wennborge9d240a2014-10-08 01:58:02 +0000146 // Referring to parameters is not allowed in naked functions.
147 if (CheckNakedParmReference(OutputExpr, *this))
148 return StmtError();
149
Bill Wendlingb68b7572013-03-27 06:06:26 +0000150 if (RequireCompleteType(OutputExpr->getLocStart(), Exprs[i]->getType(),
151 diag::err_dereference_incomplete_type))
152 return StmtError();
Chad Rosier0731aff2012-08-17 21:19:40 +0000153
154 OutputConstraintInfos.push_back(Info);
Akira Hatanaka974131e2014-09-18 18:17:18 +0000155
156 const Type *Ty = OutputExpr->getType().getTypePtr();
157
158 // If this is a dependent type, just continue. We don't know the size of a
159 // dependent type.
160 if (Ty->isDependentType())
161 continue;
162
163 unsigned Size = Context.getTypeSize(Ty);
164 if (!Context.getTargetInfo().validateOutputSize(Literal->getString(),
165 Size))
166 return StmtError(Diag(OutputExpr->getLocStart(),
167 diag::err_asm_invalid_output_size)
168 << Info.getConstraintStr());
Chad Rosier0731aff2012-08-17 21:19:40 +0000169 }
170
171 SmallVector<TargetInfo::ConstraintInfo, 4> InputConstraintInfos;
172
173 for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) {
174 StringLiteral *Literal = Constraints[i];
175 if (!Literal->isAscii())
176 return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
177 << Literal->getSourceRange());
178
179 StringRef InputName;
180 if (Names[i])
181 InputName = Names[i]->getName();
182
183 TargetInfo::ConstraintInfo Info(Literal->getString(), InputName);
184 if (!Context.getTargetInfo().validateInputConstraint(OutputConstraintInfos.data(),
185 NumOutputs, Info)) {
186 return StmtError(Diag(Literal->getLocStart(),
187 diag::err_asm_invalid_input_constraint)
188 << Info.getConstraintStr());
189 }
190
191 Expr *InputExpr = Exprs[i];
192
Hans Wennborge9d240a2014-10-08 01:58:02 +0000193 // Referring to parameters is not allowed in naked functions.
194 if (CheckNakedParmReference(InputExpr, *this))
195 return StmtError();
196
Chad Rosier0731aff2012-08-17 21:19:40 +0000197 // Only allow void types for memory constraints.
198 if (Info.allowsMemory() && !Info.allowsRegister()) {
199 if (CheckAsmLValue(InputExpr, *this))
200 return StmtError(Diag(InputExpr->getLocStart(),
201 diag::err_asm_invalid_lvalue_in_input)
202 << Info.getConstraintStr()
203 << InputExpr->getSourceRange());
David Majnemerade4bee2014-07-14 16:27:53 +0000204 } else {
205 ExprResult Result = DefaultFunctionArrayLvalueConversion(Exprs[i]);
206 if (Result.isInvalid())
207 return StmtError();
208
209 Exprs[i] = Result.get();
Chad Rosier0731aff2012-08-17 21:19:40 +0000210 }
211
212 if (Info.allowsRegister()) {
213 if (InputExpr->getType()->isVoidType()) {
214 return StmtError(Diag(InputExpr->getLocStart(),
215 diag::err_asm_invalid_type_in_input)
216 << InputExpr->getType() << Info.getConstraintStr()
217 << InputExpr->getSourceRange());
218 }
219 }
220
Chad Rosier0731aff2012-08-17 21:19:40 +0000221 InputConstraintInfos.push_back(Info);
Bill Wendling887b4852012-11-12 06:42:51 +0000222
223 const Type *Ty = Exprs[i]->getType().getTypePtr();
Bill Wendlingc4fc3a22013-03-25 21:09:49 +0000224 if (Ty->isDependentType())
Eric Christopherd41010a2012-11-12 23:13:34 +0000225 continue;
226
Bill Wendlingc4fc3a22013-03-25 21:09:49 +0000227 if (!Ty->isVoidType() || !Info.allowsMemory())
Bill Wendlingb68b7572013-03-27 06:06:26 +0000228 if (RequireCompleteType(InputExpr->getLocStart(), Exprs[i]->getType(),
229 diag::err_dereference_incomplete_type))
230 return StmtError();
Bill Wendlingc4fc3a22013-03-25 21:09:49 +0000231
Bill Wendling887b4852012-11-12 06:42:51 +0000232 unsigned Size = Context.getTypeSize(Ty);
233 if (!Context.getTargetInfo().validateInputSize(Literal->getString(),
234 Size))
235 return StmtError(Diag(InputExpr->getLocStart(),
236 diag::err_asm_invalid_input_size)
237 << Info.getConstraintStr());
Chad Rosier0731aff2012-08-17 21:19:40 +0000238 }
239
240 // Check that the clobbers are valid.
241 for (unsigned i = 0; i != NumClobbers; i++) {
242 StringLiteral *Literal = Clobbers[i];
243 if (!Literal->isAscii())
244 return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
245 << Literal->getSourceRange());
246
247 StringRef Clobber = Literal->getString();
248
249 if (!Context.getTargetInfo().isValidClobber(Clobber))
250 return StmtError(Diag(Literal->getLocStart(),
251 diag::err_asm_unknown_register_name) << Clobber);
252 }
253
Chad Rosierde70e0e2012-08-25 00:11:56 +0000254 GCCAsmStmt *NS =
255 new (Context) GCCAsmStmt(Context, AsmLoc, IsSimple, IsVolatile, NumOutputs,
Dmitri Gribenkoea2d5f82013-05-10 01:14:26 +0000256 NumInputs, Names, Constraints, Exprs.data(),
257 AsmString, NumClobbers, Clobbers, RParenLoc);
Chad Rosier0731aff2012-08-17 21:19:40 +0000258 // Validate the asm string, ensuring it makes sense given the operands we
259 // have.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000260 SmallVector<GCCAsmStmt::AsmStringPiece, 8> Pieces;
Chad Rosier0731aff2012-08-17 21:19:40 +0000261 unsigned DiagOffs;
262 if (unsigned DiagID = NS->AnalyzeAsmString(Pieces, Context, DiagOffs)) {
263 Diag(getLocationOfStringLiteralByte(AsmString, DiagOffs), DiagID)
264 << AsmString->getSourceRange();
265 return StmtError();
266 }
267
Bill Wendling9d1ee112012-10-25 23:28:48 +0000268 // Validate constraints and modifiers.
269 for (unsigned i = 0, e = Pieces.size(); i != e; ++i) {
270 GCCAsmStmt::AsmStringPiece &Piece = Pieces[i];
271 if (!Piece.isOperand()) continue;
272
273 // Look for the correct constraint index.
274 unsigned Idx = 0;
275 unsigned ConstraintIdx = 0;
276 for (unsigned i = 0, e = NS->getNumOutputs(); i != e; ++i, ++ConstraintIdx) {
277 TargetInfo::ConstraintInfo &Info = OutputConstraintInfos[i];
278 if (Idx == Piece.getOperandNo())
279 break;
280 ++Idx;
281
282 if (Info.isReadWrite()) {
283 if (Idx == Piece.getOperandNo())
284 break;
285 ++Idx;
286 }
287 }
288
289 for (unsigned i = 0, e = NS->getNumInputs(); i != e; ++i, ++ConstraintIdx) {
290 TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i];
291 if (Idx == Piece.getOperandNo())
292 break;
293 ++Idx;
294
295 if (Info.isReadWrite()) {
296 if (Idx == Piece.getOperandNo())
297 break;
298 ++Idx;
299 }
300 }
301
302 // Now that we have the right indexes go ahead and check.
303 StringLiteral *Literal = Constraints[ConstraintIdx];
304 const Type *Ty = Exprs[ConstraintIdx]->getType().getTypePtr();
305 if (Ty->isDependentType() || Ty->isIncompleteType())
306 continue;
307
308 unsigned Size = Context.getTypeSize(Ty);
Akira Hatanaka987f1862014-08-22 06:05:21 +0000309 std::string SuggestedModifier;
310 if (!Context.getTargetInfo().validateConstraintModifier(
311 Literal->getString(), Piece.getModifier(), Size,
312 SuggestedModifier)) {
Bill Wendling9d1ee112012-10-25 23:28:48 +0000313 Diag(Exprs[ConstraintIdx]->getLocStart(),
314 diag::warn_asm_mismatched_size_modifier);
Akira Hatanaka987f1862014-08-22 06:05:21 +0000315
316 if (!SuggestedModifier.empty()) {
317 auto B = Diag(Piece.getRange().getBegin(),
318 diag::note_asm_missing_constraint_modifier)
319 << SuggestedModifier;
320 SuggestedModifier = "%" + SuggestedModifier + Piece.getString();
321 B.AddFixItHint(FixItHint::CreateReplacement(Piece.getRange(),
322 SuggestedModifier));
323 }
324 }
Bill Wendling9d1ee112012-10-25 23:28:48 +0000325 }
326
Chad Rosier0731aff2012-08-17 21:19:40 +0000327 // Validate tied input operands for type mismatches.
328 for (unsigned i = 0, e = InputConstraintInfos.size(); i != e; ++i) {
329 TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i];
330
331 // If this is a tied constraint, verify that the output and input have
332 // either exactly the same type, or that they are int/ptr operands with the
333 // same size (int/long, int*/long, are ok etc).
334 if (!Info.hasTiedOperand()) continue;
335
336 unsigned TiedTo = Info.getTiedOperand();
337 unsigned InputOpNo = i+NumOutputs;
338 Expr *OutputExpr = Exprs[TiedTo];
339 Expr *InputExpr = Exprs[InputOpNo];
340
341 if (OutputExpr->isTypeDependent() || InputExpr->isTypeDependent())
342 continue;
343
344 QualType InTy = InputExpr->getType();
345 QualType OutTy = OutputExpr->getType();
346 if (Context.hasSameType(InTy, OutTy))
347 continue; // All types can be tied to themselves.
348
349 // Decide if the input and output are in the same domain (integer/ptr or
350 // floating point.
351 enum AsmDomain {
352 AD_Int, AD_FP, AD_Other
353 } InputDomain, OutputDomain;
354
355 if (InTy->isIntegerType() || InTy->isPointerType())
356 InputDomain = AD_Int;
357 else if (InTy->isRealFloatingType())
358 InputDomain = AD_FP;
359 else
360 InputDomain = AD_Other;
361
362 if (OutTy->isIntegerType() || OutTy->isPointerType())
363 OutputDomain = AD_Int;
364 else if (OutTy->isRealFloatingType())
365 OutputDomain = AD_FP;
366 else
367 OutputDomain = AD_Other;
368
369 // They are ok if they are the same size and in the same domain. This
370 // allows tying things like:
371 // void* to int*
372 // void* to int if they are the same size.
373 // double to long double if they are the same size.
374 //
375 uint64_t OutSize = Context.getTypeSize(OutTy);
376 uint64_t InSize = Context.getTypeSize(InTy);
377 if (OutSize == InSize && InputDomain == OutputDomain &&
378 InputDomain != AD_Other)
379 continue;
380
381 // If the smaller input/output operand is not mentioned in the asm string,
382 // then we can promote the smaller one to a larger input and the asm string
383 // won't notice.
384 bool SmallerValueMentioned = false;
385
386 // If this is a reference to the input and if the input was the smaller
387 // one, then we have to reject this asm.
388 if (isOperandMentioned(InputOpNo, Pieces)) {
389 // This is a use in the asm string of the smaller operand. Since we
390 // codegen this by promoting to a wider value, the asm will get printed
391 // "wrong".
392 SmallerValueMentioned |= InSize < OutSize;
393 }
394 if (isOperandMentioned(TiedTo, Pieces)) {
395 // If this is a reference to the output, and if the output is the larger
396 // value, then it's ok because we'll promote the input to the larger type.
397 SmallerValueMentioned |= OutSize < InSize;
398 }
399
400 // If the smaller value wasn't mentioned in the asm string, and if the
401 // output was a register, just extend the shorter one to the size of the
402 // larger one.
403 if (!SmallerValueMentioned && InputDomain != AD_Other &&
404 OutputConstraintInfos[TiedTo].allowsRegister())
405 continue;
406
407 // Either both of the operands were mentioned or the smaller one was
408 // mentioned. One more special case that we'll allow: if the tied input is
409 // integer, unmentioned, and is a constant, then we'll allow truncating it
410 // down to the size of the destination.
411 if (InputDomain == AD_Int && OutputDomain == AD_Int &&
412 !isOperandMentioned(InputOpNo, Pieces) &&
413 InputExpr->isEvaluatable(Context)) {
414 CastKind castKind =
415 (OutTy->isBooleanType() ? CK_IntegralToBoolean : CK_IntegralCast);
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000416 InputExpr = ImpCastExprToType(InputExpr, OutTy, castKind).get();
Chad Rosier0731aff2012-08-17 21:19:40 +0000417 Exprs[InputOpNo] = InputExpr;
418 NS->setInputExpr(i, InputExpr);
419 continue;
420 }
421
422 Diag(InputExpr->getLocStart(),
423 diag::err_asm_tying_incompatible_types)
424 << InTy << OutTy << OutputExpr->getSourceRange()
425 << InputExpr->getSourceRange();
426 return StmtError();
427 }
428
Nikola Smiljanic03ff2592014-05-29 14:05:12 +0000429 return NS;
Chad Rosier0731aff2012-08-17 21:19:40 +0000430}
431
John McCallf413f5e2013-05-03 00:10:13 +0000432ExprResult Sema::LookupInlineAsmIdentifier(CXXScopeSpec &SS,
433 SourceLocation TemplateKWLoc,
434 UnqualifiedId &Id,
Alp Toker10399272014-06-08 05:11:37 +0000435 llvm::InlineAsmIdentifierInfo &Info,
John McCallf413f5e2013-05-03 00:10:13 +0000436 bool IsUnevaluatedContext) {
Chad Rosierb18a2852013-04-22 17:01:37 +0000437 Info.clear();
Chad Rosierce2bcbf2012-10-18 15:49:40 +0000438
John McCallf413f5e2013-05-03 00:10:13 +0000439 if (IsUnevaluatedContext)
440 PushExpressionEvaluationContext(UnevaluatedAbstract,
441 ReuseLambdaContextDecl);
442
443 ExprResult Result = ActOnIdExpression(getCurScope(), SS, TemplateKWLoc, Id,
444 /*trailing lparen*/ false,
Chad Rosierb9aff1e2013-05-24 18:32:55 +0000445 /*is & operand*/ false,
Craig Topperc3ec1492014-05-26 06:22:03 +0000446 /*CorrectionCandidateCallback=*/nullptr,
Chad Rosierb9aff1e2013-05-24 18:32:55 +0000447 /*IsInlineAsmIdentifier=*/ true);
John McCallf413f5e2013-05-03 00:10:13 +0000448
449 if (IsUnevaluatedContext)
450 PopExpressionEvaluationContext();
451
452 if (!Result.isUsable()) return Result;
453
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000454 Result = CheckPlaceholderExpr(Result.get());
John McCallf413f5e2013-05-03 00:10:13 +0000455 if (!Result.isUsable()) return Result;
456
Hans Wennborg93dbeae2014-09-04 22:16:48 +0000457 // Referring to parameters is not allowed in naked functions.
Hans Wennborge9d240a2014-10-08 01:58:02 +0000458 if (CheckNakedParmReference(Result.get(), *this))
459 return ExprError();
Hans Wennborg93dbeae2014-09-04 22:16:48 +0000460
John McCallf413f5e2013-05-03 00:10:13 +0000461 QualType T = Result.get()->getType();
462
463 // For now, reject dependent types.
464 if (T->isDependentType()) {
465 Diag(Id.getLocStart(), diag::err_asm_incomplete_type) << T;
466 return ExprError();
Chad Rosierce2bcbf2012-10-18 15:49:40 +0000467 }
468
John McCallf413f5e2013-05-03 00:10:13 +0000469 // Any sort of function type is fine.
470 if (T->isFunctionType()) {
471 return Result;
Chad Rosierce2bcbf2012-10-18 15:49:40 +0000472 }
473
John McCallf413f5e2013-05-03 00:10:13 +0000474 // Otherwise, it needs to be a complete type.
475 if (RequireCompleteExprType(Result.get(), diag::err_asm_incomplete_type)) {
476 return ExprError();
477 }
478
479 // Compute the type size (and array length if applicable?).
480 Info.Type = Info.Size = Context.getTypeSizeInChars(T).getQuantity();
481 if (T->isArrayType()) {
482 const ArrayType *ATy = Context.getAsArrayType(T);
483 Info.Type = Context.getTypeSizeInChars(ATy->getElementType()).getQuantity();
484 Info.Length = Info.Size / Info.Type;
485 }
486
487 // We can work with the expression as long as it's not an r-value.
488 if (!Result.get()->isRValue())
Chad Rosierb18a2852013-04-22 17:01:37 +0000489 Info.IsVarDecl = true;
Chad Rosierce2bcbf2012-10-18 15:49:40 +0000490
John McCallf413f5e2013-05-03 00:10:13 +0000491 return Result;
Chad Rosier4a0054f2012-10-15 19:56:10 +0000492}
Chad Rosierd997bd12012-08-22 19:18:30 +0000493
Chad Rosier5c563642012-10-25 21:49:22 +0000494bool Sema::LookupInlineAsmField(StringRef Base, StringRef Member,
495 unsigned &Offset, SourceLocation AsmLoc) {
496 Offset = 0;
497 LookupResult BaseResult(*this, &Context.Idents.get(Base), SourceLocation(),
498 LookupOrdinaryName);
499
500 if (!LookupName(BaseResult, getCurScope()))
501 return true;
502
503 if (!BaseResult.isSingleResult())
504 return true;
505
Craig Topperc3ec1492014-05-26 06:22:03 +0000506 const RecordType *RT = nullptr;
Chad Rosier10230d42013-04-01 17:58:03 +0000507 NamedDecl *FoundDecl = BaseResult.getFoundDecl();
508 if (VarDecl *VD = dyn_cast<VarDecl>(FoundDecl))
Chad Rosier5c563642012-10-25 21:49:22 +0000509 RT = VD->getType()->getAs<RecordType>();
Nico Weber72889432014-09-06 01:25:55 +0000510 else if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(FoundDecl)) {
511 MarkAnyDeclReferenced(TD->getLocation(), TD, /*OdrUse=*/false);
Chad Rosier5c563642012-10-25 21:49:22 +0000512 RT = TD->getUnderlyingType()->getAs<RecordType>();
Nico Weber72889432014-09-06 01:25:55 +0000513 } else if (TypeDecl *TD = dyn_cast<TypeDecl>(FoundDecl))
Nico Weber9ef9ca42014-05-06 03:13:27 +0000514 RT = TD->getTypeForDecl()->getAs<RecordType>();
Chad Rosier5c563642012-10-25 21:49:22 +0000515 if (!RT)
516 return true;
517
518 if (RequireCompleteType(AsmLoc, QualType(RT, 0), 0))
519 return true;
520
521 LookupResult FieldResult(*this, &Context.Idents.get(Member), SourceLocation(),
522 LookupMemberName);
523
524 if (!LookupQualifiedName(FieldResult, RT->getDecl()))
525 return true;
526
527 // FIXME: Handle IndirectFieldDecl?
528 FieldDecl *FD = dyn_cast<FieldDecl>(FieldResult.getFoundDecl());
529 if (!FD)
530 return true;
531
532 const ASTRecordLayout &RL = Context.getASTRecordLayout(RT->getDecl());
533 unsigned i = FD->getFieldIndex();
534 CharUnits Result = Context.toCharUnitsFromBits(RL.getFieldOffset(i));
535 Offset = (unsigned)Result.getQuantity();
536
537 return false;
538}
539
Chad Rosierb261a502012-09-13 00:06:55 +0000540StmtResult Sema::ActOnMSAsmStmt(SourceLocation AsmLoc, SourceLocation LBraceLoc,
John McCallf413f5e2013-05-03 00:10:13 +0000541 ArrayRef<Token> AsmToks,
542 StringRef AsmString,
543 unsigned NumOutputs, unsigned NumInputs,
544 ArrayRef<StringRef> Constraints,
545 ArrayRef<StringRef> Clobbers,
546 ArrayRef<Expr*> Exprs,
547 SourceLocation EndLoc) {
548 bool IsSimple = (NumOutputs != 0 || NumInputs != 0);
Ehsan Akhgari31097582014-09-22 02:21:54 +0000549 getCurFunction()->setHasBranchProtectedScope();
Chad Rosier0731aff2012-08-17 21:19:40 +0000550 MSAsmStmt *NS =
Chad Rosierce2bcbf2012-10-18 15:49:40 +0000551 new (Context) MSAsmStmt(Context, AsmLoc, LBraceLoc, IsSimple,
552 /*IsVolatile*/ true, AsmToks, NumOutputs, NumInputs,
John McCallf413f5e2013-05-03 00:10:13 +0000553 Constraints, Exprs, AsmString,
554 Clobbers, EndLoc);
Nikola Smiljanic03ff2592014-05-29 14:05:12 +0000555 return NS;
Chad Rosier0731aff2012-08-17 21:19:40 +0000556}
Ehsan Akhgari31097582014-09-22 02:21:54 +0000557
558LabelDecl *Sema::GetOrCreateMSAsmLabel(StringRef ExternalLabelName,
559 SourceLocation Location,
560 bool AlwaysCreate) {
561 LabelDecl* Label = LookupOrCreateLabel(PP.getIdentifierInfo(ExternalLabelName),
562 Location);
563
564 if (!Label->isMSAsmLabel()) {
565 // Otherwise, insert it, but only resolve it if we have seen the label itself.
566 std::string InternalName;
567 llvm::raw_string_ostream OS(InternalName);
568 // Create an internal name for the label. The name should not be a valid mangled
569 // name, and should be unique. We use a dot to make the name an invalid mangled
570 // name.
571 OS << "__MSASMLABEL_." << MSAsmLabelNameCounter++ << "__" << ExternalLabelName;
572 Label->setMSAsmLabel(OS.str());
573 }
574 if (AlwaysCreate) {
575 // The label might have been created implicitly from a previously encountered
576 // goto statement. So, for both newly created and looked up labels, we mark
577 // them as resolved.
578 Label->setMSAsmLabelResolved();
579 }
580 // Adjust their location for being able to generate accurate diagnostics.
581 Label->setLocation(Location);
582
583 return Label;
584}