]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/Sema/SemaStmtAsm.cpp
Merge ^/head r277327 through r277718.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / Sema / SemaStmtAsm.cpp
1 //===--- SemaStmtAsm.cpp - Semantic Analysis for Asm Statements -----------===//
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 //===----------------------------------------------------------------------===//
9 //
10 //  This file implements semantic analysis for inline asm statements.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "clang/Sema/SemaInternal.h"
15 #include "clang/AST/RecordLayout.h"
16 #include "clang/AST/TypeLoc.h"
17 #include "clang/Basic/TargetInfo.h"
18 #include "clang/Lex/Preprocessor.h"
19 #include "clang/Sema/Initialization.h"
20 #include "clang/Sema/Lookup.h"
21 #include "clang/Sema/Scope.h"
22 #include "clang/Sema/ScopeInfo.h"
23 #include "llvm/ADT/ArrayRef.h"
24 #include "llvm/ADT/BitVector.h"
25 #include "llvm/MC/MCParser/MCAsmParser.h"
26 using namespace clang;
27 using 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.
36 static 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.
64 static bool isOperandMentioned(unsigned OpNo,
65                          ArrayRef<GCCAsmStmt::AsmStringPiece> AsmStrPieces) {
66   for (unsigned p = 0, e = AsmStrPieces.size(); p != e; ++p) {
67     const GCCAsmStmt::AsmStringPiece &Piece = AsmStrPieces[p];
68     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
78 static 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
104 StmtResult Sema::ActOnGCCAsmStmt(SourceLocation AsmLoc, bool IsSimple,
105                                  bool IsVolatile, unsigned NumOutputs,
106                                  unsigned NumInputs, IdentifierInfo **Names,
107                                  MultiExprArg constraints, MultiExprArg Exprs,
108                                  Expr *asmString, MultiExprArg clobbers,
109                                  SourceLocation RParenLoc) {
110   unsigned NumClobbers = clobbers.size();
111   StringLiteral **Constraints =
112     reinterpret_cast<StringLiteral**>(constraints.data());
113   StringLiteral *AsmString = cast<StringLiteral>(asmString);
114   StringLiteral **Clobbers = reinterpret_cast<StringLiteral**>(clobbers.data());
115
116   SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos;
117
118   // The parser verifies that there is a string literal here.
119   assert(AsmString->isAscii());
120
121   for (unsigned i = 0; i != NumOutputs; i++) {
122     StringLiteral *Literal = Constraints[i];
123     assert(Literal->isAscii());
124
125     StringRef OutputName;
126     if (Names[i])
127       OutputName = Names[i]->getName();
128
129     TargetInfo::ConstraintInfo Info(Literal->getString(), OutputName);
130     if (!Context.getTargetInfo().validateOutputConstraint(Info))
131       return StmtError(Diag(Literal->getLocStart(),
132                             diag::err_asm_invalid_output_constraint)
133                        << Info.getConstraintStr());
134
135     ExprResult ER = CheckPlaceholderExpr(Exprs[i]);
136     if (ER.isInvalid())
137       return StmtError();
138     Exprs[i] = ER.get();
139
140     // Check that the output exprs are valid lvalues.
141     Expr *OutputExpr = Exprs[i];
142
143     // Referring to parameters is not allowed in naked functions.
144     if (CheckNakedParmReference(OutputExpr, *this))
145       return StmtError();
146
147     OutputConstraintInfos.push_back(Info);
148
149     // If this is dependent, just continue.
150     if (OutputExpr->isTypeDependent())
151       continue;
152
153     Expr::isModifiableLvalueResult IsLV =
154         OutputExpr->isModifiableLvalue(Context, /*Loc=*/nullptr);
155     switch (IsLV) {
156     case Expr::MLV_Valid:
157       // Cool, this is an lvalue.
158       break;
159     case Expr::MLV_ArrayType:
160       // This is OK too.
161       break;
162     case Expr::MLV_LValueCast: {
163       const Expr *LVal = OutputExpr->IgnoreParenNoopCasts(Context);
164       if (!getLangOpts().HeinousExtensions) {
165         Diag(LVal->getLocStart(), diag::err_invalid_asm_cast_lvalue)
166             << OutputExpr->getSourceRange();
167       } else {
168         Diag(LVal->getLocStart(), diag::warn_invalid_asm_cast_lvalue)
169             << OutputExpr->getSourceRange();
170       }
171       // Accept, even if we emitted an error diagnostic.
172       break;
173     }
174     case Expr::MLV_IncompleteType:
175     case Expr::MLV_IncompleteVoidType:
176       if (RequireCompleteType(OutputExpr->getLocStart(), Exprs[i]->getType(),
177                               diag::err_dereference_incomplete_type))
178         return StmtError();
179     default:
180       return StmtError(Diag(OutputExpr->getLocStart(),
181                             diag::err_asm_invalid_lvalue_in_output)
182                        << OutputExpr->getSourceRange());
183     }
184
185     unsigned Size = Context.getTypeSize(OutputExpr->getType());
186     if (!Context.getTargetInfo().validateOutputSize(Literal->getString(),
187                                                     Size))
188       return StmtError(Diag(OutputExpr->getLocStart(),
189                             diag::err_asm_invalid_output_size)
190                        << Info.getConstraintStr());
191   }
192
193   SmallVector<TargetInfo::ConstraintInfo, 4> InputConstraintInfos;
194
195   for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) {
196     StringLiteral *Literal = Constraints[i];
197     assert(Literal->isAscii());
198
199     StringRef InputName;
200     if (Names[i])
201       InputName = Names[i]->getName();
202
203     TargetInfo::ConstraintInfo Info(Literal->getString(), InputName);
204     if (!Context.getTargetInfo().validateInputConstraint(OutputConstraintInfos.data(),
205                                                 NumOutputs, Info)) {
206       return StmtError(Diag(Literal->getLocStart(),
207                             diag::err_asm_invalid_input_constraint)
208                        << Info.getConstraintStr());
209     }
210
211     ExprResult ER = CheckPlaceholderExpr(Exprs[i]);
212     if (ER.isInvalid())
213       return StmtError();
214     Exprs[i] = ER.get();
215
216     Expr *InputExpr = Exprs[i];
217
218     // Referring to parameters is not allowed in naked functions.
219     if (CheckNakedParmReference(InputExpr, *this))
220       return StmtError();
221
222     // Only allow void types for memory constraints.
223     if (Info.allowsMemory() && !Info.allowsRegister()) {
224       if (CheckAsmLValue(InputExpr, *this))
225         return StmtError(Diag(InputExpr->getLocStart(),
226                               diag::err_asm_invalid_lvalue_in_input)
227                          << Info.getConstraintStr()
228                          << InputExpr->getSourceRange());
229     } else if (Info.requiresImmediateConstant() && !Info.allowsRegister()) {
230       llvm::APSInt Result;
231       if (!InputExpr->EvaluateAsInt(Result, Context))
232         return StmtError(
233             Diag(InputExpr->getLocStart(), diag::err_asm_invalid_type_in_input)
234             << InputExpr->getType() << Info.getConstraintStr()
235             << InputExpr->getSourceRange());
236       if (Result.slt(Info.getImmConstantMin()) ||
237           Result.sgt(Info.getImmConstantMax()))
238         return StmtError(Diag(InputExpr->getLocStart(),
239                               diag::err_invalid_asm_value_for_constraint)
240                          << Result.toString(10) << Info.getConstraintStr()
241                          << InputExpr->getSourceRange());
242
243     } else {
244       ExprResult Result = DefaultFunctionArrayLvalueConversion(Exprs[i]);
245       if (Result.isInvalid())
246         return StmtError();
247
248       Exprs[i] = Result.get();
249     }
250
251     if (Info.allowsRegister()) {
252       if (InputExpr->getType()->isVoidType()) {
253         return StmtError(Diag(InputExpr->getLocStart(),
254                               diag::err_asm_invalid_type_in_input)
255           << InputExpr->getType() << Info.getConstraintStr()
256           << InputExpr->getSourceRange());
257       }
258     }
259
260     InputConstraintInfos.push_back(Info);
261
262     const Type *Ty = Exprs[i]->getType().getTypePtr();
263     if (Ty->isDependentType())
264       continue;
265
266     if (!Ty->isVoidType() || !Info.allowsMemory())
267       if (RequireCompleteType(InputExpr->getLocStart(), Exprs[i]->getType(),
268                               diag::err_dereference_incomplete_type))
269         return StmtError();
270
271     unsigned Size = Context.getTypeSize(Ty);
272     if (!Context.getTargetInfo().validateInputSize(Literal->getString(),
273                                                    Size))
274       return StmtError(Diag(InputExpr->getLocStart(),
275                             diag::err_asm_invalid_input_size)
276                        << Info.getConstraintStr());
277   }
278
279   // Check that the clobbers are valid.
280   for (unsigned i = 0; i != NumClobbers; i++) {
281     StringLiteral *Literal = Clobbers[i];
282     assert(Literal->isAscii());
283
284     StringRef Clobber = Literal->getString();
285
286     if (!Context.getTargetInfo().isValidClobber(Clobber))
287       return StmtError(Diag(Literal->getLocStart(),
288                   diag::err_asm_unknown_register_name) << Clobber);
289   }
290
291   GCCAsmStmt *NS =
292     new (Context) GCCAsmStmt(Context, AsmLoc, IsSimple, IsVolatile, NumOutputs,
293                              NumInputs, Names, Constraints, Exprs.data(),
294                              AsmString, NumClobbers, Clobbers, RParenLoc);
295   // Validate the asm string, ensuring it makes sense given the operands we
296   // have.
297   SmallVector<GCCAsmStmt::AsmStringPiece, 8> Pieces;
298   unsigned DiagOffs;
299   if (unsigned DiagID = NS->AnalyzeAsmString(Pieces, Context, DiagOffs)) {
300     Diag(getLocationOfStringLiteralByte(AsmString, DiagOffs), DiagID)
301            << AsmString->getSourceRange();
302     return StmtError();
303   }
304
305   // Validate constraints and modifiers.
306   for (unsigned i = 0, e = Pieces.size(); i != e; ++i) {
307     GCCAsmStmt::AsmStringPiece &Piece = Pieces[i];
308     if (!Piece.isOperand()) continue;
309
310     // Look for the correct constraint index.
311     unsigned Idx = 0;
312     unsigned ConstraintIdx = 0;
313     for (unsigned i = 0, e = NS->getNumOutputs(); i != e; ++i, ++ConstraintIdx) {
314       TargetInfo::ConstraintInfo &Info = OutputConstraintInfos[i];
315       if (Idx == Piece.getOperandNo())
316         break;
317       ++Idx;
318
319       if (Info.isReadWrite()) {
320         if (Idx == Piece.getOperandNo())
321           break;
322         ++Idx;
323       }
324     }
325
326     for (unsigned i = 0, e = NS->getNumInputs(); i != e; ++i, ++ConstraintIdx) {
327       TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i];
328       if (Idx == Piece.getOperandNo())
329         break;
330       ++Idx;
331
332       if (Info.isReadWrite()) {
333         if (Idx == Piece.getOperandNo())
334           break;
335         ++Idx;
336       }
337     }
338
339     // Now that we have the right indexes go ahead and check.
340     StringLiteral *Literal = Constraints[ConstraintIdx];
341     const Type *Ty = Exprs[ConstraintIdx]->getType().getTypePtr();
342     if (Ty->isDependentType() || Ty->isIncompleteType())
343       continue;
344
345     unsigned Size = Context.getTypeSize(Ty);
346     std::string SuggestedModifier;
347     if (!Context.getTargetInfo().validateConstraintModifier(
348             Literal->getString(), Piece.getModifier(), Size,
349             SuggestedModifier)) {
350       Diag(Exprs[ConstraintIdx]->getLocStart(),
351            diag::warn_asm_mismatched_size_modifier);
352
353       if (!SuggestedModifier.empty()) {
354         auto B = Diag(Piece.getRange().getBegin(),
355                       diag::note_asm_missing_constraint_modifier)
356                  << SuggestedModifier;
357         SuggestedModifier = "%" + SuggestedModifier + Piece.getString();
358         B.AddFixItHint(FixItHint::CreateReplacement(Piece.getRange(),
359                                                     SuggestedModifier));
360       }
361     }
362   }
363
364   // Validate tied input operands for type mismatches.
365   unsigned NumAlternatives = ~0U;
366   for (unsigned i = 0, e = OutputConstraintInfos.size(); i != e; ++i) {
367     TargetInfo::ConstraintInfo &Info = OutputConstraintInfos[i];
368     StringRef ConstraintStr = Info.getConstraintStr();
369     unsigned AltCount = ConstraintStr.count(',') + 1;
370     if (NumAlternatives == ~0U)
371       NumAlternatives = AltCount;
372     else if (NumAlternatives != AltCount)
373       return StmtError(Diag(NS->getOutputExpr(i)->getLocStart(),
374                             diag::err_asm_unexpected_constraint_alternatives)
375                        << NumAlternatives << AltCount);
376   }
377   for (unsigned i = 0, e = InputConstraintInfos.size(); i != e; ++i) {
378     TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i];
379     StringRef ConstraintStr = Info.getConstraintStr();
380     unsigned AltCount = ConstraintStr.count(',') + 1;
381     if (NumAlternatives == ~0U)
382       NumAlternatives = AltCount;
383     else if (NumAlternatives != AltCount)
384       return StmtError(Diag(NS->getInputExpr(i)->getLocStart(),
385                             diag::err_asm_unexpected_constraint_alternatives)
386                        << NumAlternatives << AltCount);
387
388     // If this is a tied constraint, verify that the output and input have
389     // either exactly the same type, or that they are int/ptr operands with the
390     // same size (int/long, int*/long, are ok etc).
391     if (!Info.hasTiedOperand()) continue;
392
393     unsigned TiedTo = Info.getTiedOperand();
394     unsigned InputOpNo = i+NumOutputs;
395     Expr *OutputExpr = Exprs[TiedTo];
396     Expr *InputExpr = Exprs[InputOpNo];
397
398     if (OutputExpr->isTypeDependent() || InputExpr->isTypeDependent())
399       continue;
400
401     QualType InTy = InputExpr->getType();
402     QualType OutTy = OutputExpr->getType();
403     if (Context.hasSameType(InTy, OutTy))
404       continue;  // All types can be tied to themselves.
405
406     // Decide if the input and output are in the same domain (integer/ptr or
407     // floating point.
408     enum AsmDomain {
409       AD_Int, AD_FP, AD_Other
410     } InputDomain, OutputDomain;
411
412     if (InTy->isIntegerType() || InTy->isPointerType())
413       InputDomain = AD_Int;
414     else if (InTy->isRealFloatingType())
415       InputDomain = AD_FP;
416     else
417       InputDomain = AD_Other;
418
419     if (OutTy->isIntegerType() || OutTy->isPointerType())
420       OutputDomain = AD_Int;
421     else if (OutTy->isRealFloatingType())
422       OutputDomain = AD_FP;
423     else
424       OutputDomain = AD_Other;
425
426     // They are ok if they are the same size and in the same domain.  This
427     // allows tying things like:
428     //   void* to int*
429     //   void* to int            if they are the same size.
430     //   double to long double   if they are the same size.
431     //
432     uint64_t OutSize = Context.getTypeSize(OutTy);
433     uint64_t InSize = Context.getTypeSize(InTy);
434     if (OutSize == InSize && InputDomain == OutputDomain &&
435         InputDomain != AD_Other)
436       continue;
437
438     // If the smaller input/output operand is not mentioned in the asm string,
439     // then we can promote the smaller one to a larger input and the asm string
440     // won't notice.
441     bool SmallerValueMentioned = false;
442
443     // If this is a reference to the input and if the input was the smaller
444     // one, then we have to reject this asm.
445     if (isOperandMentioned(InputOpNo, Pieces)) {
446       // This is a use in the asm string of the smaller operand.  Since we
447       // codegen this by promoting to a wider value, the asm will get printed
448       // "wrong".
449       SmallerValueMentioned |= InSize < OutSize;
450     }
451     if (isOperandMentioned(TiedTo, Pieces)) {
452       // If this is a reference to the output, and if the output is the larger
453       // value, then it's ok because we'll promote the input to the larger type.
454       SmallerValueMentioned |= OutSize < InSize;
455     }
456
457     // If the smaller value wasn't mentioned in the asm string, and if the
458     // output was a register, just extend the shorter one to the size of the
459     // larger one.
460     if (!SmallerValueMentioned && InputDomain != AD_Other &&
461         OutputConstraintInfos[TiedTo].allowsRegister())
462       continue;
463
464     // Either both of the operands were mentioned or the smaller one was
465     // mentioned.  One more special case that we'll allow: if the tied input is
466     // integer, unmentioned, and is a constant, then we'll allow truncating it
467     // down to the size of the destination.
468     if (InputDomain == AD_Int && OutputDomain == AD_Int &&
469         !isOperandMentioned(InputOpNo, Pieces) &&
470         InputExpr->isEvaluatable(Context)) {
471       CastKind castKind =
472         (OutTy->isBooleanType() ? CK_IntegralToBoolean : CK_IntegralCast);
473       InputExpr = ImpCastExprToType(InputExpr, OutTy, castKind).get();
474       Exprs[InputOpNo] = InputExpr;
475       NS->setInputExpr(i, InputExpr);
476       continue;
477     }
478
479     Diag(InputExpr->getLocStart(),
480          diag::err_asm_tying_incompatible_types)
481       << InTy << OutTy << OutputExpr->getSourceRange()
482       << InputExpr->getSourceRange();
483     return StmtError();
484   }
485
486   return NS;
487 }
488
489 ExprResult Sema::LookupInlineAsmIdentifier(CXXScopeSpec &SS,
490                                            SourceLocation TemplateKWLoc,
491                                            UnqualifiedId &Id,
492                                            llvm::InlineAsmIdentifierInfo &Info,
493                                            bool IsUnevaluatedContext) {
494   Info.clear();
495
496   if (IsUnevaluatedContext)
497     PushExpressionEvaluationContext(UnevaluatedAbstract,
498                                     ReuseLambdaContextDecl);
499
500   ExprResult Result = ActOnIdExpression(getCurScope(), SS, TemplateKWLoc, Id,
501                                         /*trailing lparen*/ false,
502                                         /*is & operand*/ false,
503                                         /*CorrectionCandidateCallback=*/nullptr,
504                                         /*IsInlineAsmIdentifier=*/ true);
505
506   if (IsUnevaluatedContext)
507     PopExpressionEvaluationContext();
508
509   if (!Result.isUsable()) return Result;
510
511   Result = CheckPlaceholderExpr(Result.get());
512   if (!Result.isUsable()) return Result;
513
514   // Referring to parameters is not allowed in naked functions.
515   if (CheckNakedParmReference(Result.get(), *this))
516     return ExprError();
517
518   QualType T = Result.get()->getType();
519
520   // For now, reject dependent types.
521   if (T->isDependentType()) {
522     Diag(Id.getLocStart(), diag::err_asm_incomplete_type) << T;
523     return ExprError();
524   }
525
526   // Any sort of function type is fine.
527   if (T->isFunctionType()) {
528     return Result;
529   }
530
531   // Otherwise, it needs to be a complete type.
532   if (RequireCompleteExprType(Result.get(), diag::err_asm_incomplete_type)) {
533     return ExprError();
534   }
535
536   // Compute the type size (and array length if applicable?).
537   Info.Type = Info.Size = Context.getTypeSizeInChars(T).getQuantity();
538   if (T->isArrayType()) {
539     const ArrayType *ATy = Context.getAsArrayType(T);
540     Info.Type = Context.getTypeSizeInChars(ATy->getElementType()).getQuantity();
541     Info.Length = Info.Size / Info.Type;
542   }
543
544   // We can work with the expression as long as it's not an r-value.
545   if (!Result.get()->isRValue())
546     Info.IsVarDecl = true;
547
548   return Result;
549 }
550
551 bool Sema::LookupInlineAsmField(StringRef Base, StringRef Member,
552                                 unsigned &Offset, SourceLocation AsmLoc) {
553   Offset = 0;
554   LookupResult BaseResult(*this, &Context.Idents.get(Base), SourceLocation(),
555                           LookupOrdinaryName);
556
557   if (!LookupName(BaseResult, getCurScope()))
558     return true;
559
560   if (!BaseResult.isSingleResult())
561     return true;
562
563   const RecordType *RT = nullptr;
564   NamedDecl *FoundDecl = BaseResult.getFoundDecl();
565   if (VarDecl *VD = dyn_cast<VarDecl>(FoundDecl))
566     RT = VD->getType()->getAs<RecordType>();
567   else if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(FoundDecl)) {
568     MarkAnyDeclReferenced(TD->getLocation(), TD, /*OdrUse=*/false);
569     RT = TD->getUnderlyingType()->getAs<RecordType>();
570   } else if (TypeDecl *TD = dyn_cast<TypeDecl>(FoundDecl))
571     RT = TD->getTypeForDecl()->getAs<RecordType>();
572   if (!RT)
573     return true;
574
575   if (RequireCompleteType(AsmLoc, QualType(RT, 0), 0))
576     return true;
577
578   LookupResult FieldResult(*this, &Context.Idents.get(Member), SourceLocation(),
579                            LookupMemberName);
580
581   if (!LookupQualifiedName(FieldResult, RT->getDecl()))
582     return true;
583
584   // FIXME: Handle IndirectFieldDecl?
585   FieldDecl *FD = dyn_cast<FieldDecl>(FieldResult.getFoundDecl());
586   if (!FD)
587     return true;
588
589   const ASTRecordLayout &RL = Context.getASTRecordLayout(RT->getDecl());
590   unsigned i = FD->getFieldIndex();
591   CharUnits Result = Context.toCharUnitsFromBits(RL.getFieldOffset(i));
592   Offset = (unsigned)Result.getQuantity();
593
594   return false;
595 }
596
597 StmtResult Sema::ActOnMSAsmStmt(SourceLocation AsmLoc, SourceLocation LBraceLoc,
598                                 ArrayRef<Token> AsmToks,
599                                 StringRef AsmString,
600                                 unsigned NumOutputs, unsigned NumInputs,
601                                 ArrayRef<StringRef> Constraints,
602                                 ArrayRef<StringRef> Clobbers,
603                                 ArrayRef<Expr*> Exprs,
604                                 SourceLocation EndLoc) {
605   bool IsSimple = (NumOutputs != 0 || NumInputs != 0);
606   getCurFunction()->setHasBranchProtectedScope();
607   MSAsmStmt *NS =
608     new (Context) MSAsmStmt(Context, AsmLoc, LBraceLoc, IsSimple,
609                             /*IsVolatile*/ true, AsmToks, NumOutputs, NumInputs,
610                             Constraints, Exprs, AsmString,
611                             Clobbers, EndLoc);
612   return NS;
613 }
614
615 LabelDecl *Sema::GetOrCreateMSAsmLabel(StringRef ExternalLabelName,
616                                        SourceLocation Location,
617                                        bool AlwaysCreate) {
618   LabelDecl* Label = LookupOrCreateLabel(PP.getIdentifierInfo(ExternalLabelName),
619                                          Location);
620
621   if (Label->isMSAsmLabel()) {
622     // If we have previously created this label implicitly, mark it as used.
623     Label->markUsed(Context);
624   } else {
625     // Otherwise, insert it, but only resolve it if we have seen the label itself.
626     std::string InternalName;
627     llvm::raw_string_ostream OS(InternalName);
628     // Create an internal name for the label.  The name should not be a valid mangled
629     // name, and should be unique.  We use a dot to make the name an invalid mangled
630     // name.
631     OS << "__MSASMLABEL_." << MSAsmLabelNameCounter++ << "__" << ExternalLabelName;
632     Label->setMSAsmLabel(OS.str());
633   }
634   if (AlwaysCreate) {
635     // The label might have been created implicitly from a previously encountered
636     // goto statement.  So, for both newly created and looked up labels, we mark
637     // them as resolved.
638     Label->setMSAsmLabelResolved();
639   }
640   // Adjust their location for being able to generate accurate diagnostics.
641   Label->setLocation(Location);
642
643   return Label;
644 }