clang API Documentation

SemaStmtAsm.cpp
Go to the documentation of this file.
00001 //===--- SemaStmtAsm.cpp - Semantic Analysis for Asm Statements -----------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file implements semantic analysis for inline asm statements.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "clang/Sema/SemaInternal.h"
00015 #include "clang/AST/RecordLayout.h"
00016 #include "clang/AST/TypeLoc.h"
00017 #include "clang/Basic/TargetInfo.h"
00018 #include "clang/Lex/Preprocessor.h"
00019 #include "clang/Sema/Initialization.h"
00020 #include "clang/Sema/Lookup.h"
00021 #include "clang/Sema/Scope.h"
00022 #include "clang/Sema/ScopeInfo.h"
00023 #include "llvm/ADT/ArrayRef.h"
00024 #include "llvm/ADT/BitVector.h"
00025 #include "llvm/MC/MCParser/MCAsmParser.h"
00026 using namespace clang;
00027 using namespace sema;
00028 
00029 /// CheckAsmLValue - GNU C has an extremely ugly extension whereby they silently
00030 /// ignore "noop" casts in places where an lvalue is required by an inline asm.
00031 /// We emulate this behavior when -fheinous-gnu-extensions is specified, but
00032 /// provide a strong guidance to not use it.
00033 ///
00034 /// This method checks to see if the argument is an acceptable l-value and
00035 /// returns false if it is a case we can handle.
00036 static bool CheckAsmLValue(const Expr *E, Sema &S) {
00037   // Type dependent expressions will be checked during instantiation.
00038   if (E->isTypeDependent())
00039     return false;
00040 
00041   if (E->isLValue())
00042     return false;  // Cool, this is an lvalue.
00043 
00044   // Okay, this is not an lvalue, but perhaps it is the result of a cast that we
00045   // are supposed to allow.
00046   const Expr *E2 = E->IgnoreParenNoopCasts(S.Context);
00047   if (E != E2 && E2->isLValue()) {
00048     if (!S.getLangOpts().HeinousExtensions)
00049       S.Diag(E2->getLocStart(), diag::err_invalid_asm_cast_lvalue)
00050         << E->getSourceRange();
00051     else
00052       S.Diag(E2->getLocStart(), diag::warn_invalid_asm_cast_lvalue)
00053         << E->getSourceRange();
00054     // Accept, even if we emitted an error diagnostic.
00055     return false;
00056   }
00057 
00058   // None of the above, just randomly invalid non-lvalue.
00059   return true;
00060 }
00061 
00062 /// isOperandMentioned - Return true if the specified operand # is mentioned
00063 /// anywhere in the decomposed asm string.
00064 static bool isOperandMentioned(unsigned OpNo,
00065                          ArrayRef<GCCAsmStmt::AsmStringPiece> AsmStrPieces) {
00066   for (unsigned p = 0, e = AsmStrPieces.size(); p != e; ++p) {
00067     const GCCAsmStmt::AsmStringPiece &Piece = AsmStrPieces[p];
00068     if (!Piece.isOperand()) continue;
00069 
00070     // If this is a reference to the input and if the input was the smaller
00071     // one, then we have to reject this asm.
00072     if (Piece.getOperandNo() == OpNo)
00073       return true;
00074   }
00075   return false;
00076 }
00077 
00078 static bool CheckNakedParmReference(Expr *E, Sema &S) {
00079   FunctionDecl *Func = dyn_cast<FunctionDecl>(S.CurContext);
00080   if (!Func)
00081     return false;
00082   if (!Func->hasAttr<NakedAttr>())
00083     return false;
00084 
00085   SmallVector<Expr*, 4> WorkList;
00086   WorkList.push_back(E);
00087   while (WorkList.size()) {
00088     Expr *E = WorkList.pop_back_val();
00089     if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
00090       if (isa<ParmVarDecl>(DRE->getDecl())) {
00091         S.Diag(DRE->getLocStart(), diag::err_asm_naked_parm_ref);
00092         S.Diag(Func->getAttr<NakedAttr>()->getLocation(), diag::note_attribute);
00093         return true;
00094       }
00095     }
00096     for (Stmt *Child : E->children()) {
00097       if (Expr *E = dyn_cast_or_null<Expr>(Child))
00098         WorkList.push_back(E);
00099     }
00100   }
00101   return false;
00102 }
00103 
00104 StmtResult Sema::ActOnGCCAsmStmt(SourceLocation AsmLoc, bool IsSimple,
00105                                  bool IsVolatile, unsigned NumOutputs,
00106                                  unsigned NumInputs, IdentifierInfo **Names,
00107                                  MultiExprArg constraints, MultiExprArg Exprs,
00108                                  Expr *asmString, MultiExprArg clobbers,
00109                                  SourceLocation RParenLoc) {
00110   unsigned NumClobbers = clobbers.size();
00111   StringLiteral **Constraints =
00112     reinterpret_cast<StringLiteral**>(constraints.data());
00113   StringLiteral *AsmString = cast<StringLiteral>(asmString);
00114   StringLiteral **Clobbers = reinterpret_cast<StringLiteral**>(clobbers.data());
00115 
00116   SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos;
00117 
00118   // The parser verifies that there is a string literal here.
00119   if (!AsmString->isAscii())
00120     return StmtError(Diag(AsmString->getLocStart(),diag::err_asm_wide_character)
00121       << AsmString->getSourceRange());
00122 
00123   for (unsigned i = 0; i != NumOutputs; i++) {
00124     StringLiteral *Literal = Constraints[i];
00125     if (!Literal->isAscii())
00126       return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
00127         << Literal->getSourceRange());
00128 
00129     StringRef OutputName;
00130     if (Names[i])
00131       OutputName = Names[i]->getName();
00132 
00133     TargetInfo::ConstraintInfo Info(Literal->getString(), OutputName);
00134     if (!Context.getTargetInfo().validateOutputConstraint(Info))
00135       return StmtError(Diag(Literal->getLocStart(),
00136                             diag::err_asm_invalid_output_constraint)
00137                        << Info.getConstraintStr());
00138 
00139     // Check that the output exprs are valid lvalues.
00140     Expr *OutputExpr = Exprs[i];
00141     if (CheckAsmLValue(OutputExpr, *this))
00142       return StmtError(Diag(OutputExpr->getLocStart(),
00143                             diag::err_asm_invalid_lvalue_in_output)
00144                        << OutputExpr->getSourceRange());
00145 
00146     // Referring to parameters is not allowed in naked functions.
00147     if (CheckNakedParmReference(OutputExpr, *this))
00148       return StmtError();
00149 
00150     if (RequireCompleteType(OutputExpr->getLocStart(), Exprs[i]->getType(),
00151                             diag::err_dereference_incomplete_type))
00152       return StmtError();
00153 
00154     OutputConstraintInfos.push_back(Info);
00155 
00156     const Type *Ty = OutputExpr->getType().getTypePtr();
00157 
00158     // If this is a dependent type, just continue. We don't know the size of a
00159     // dependent type.
00160     if (Ty->isDependentType())
00161       continue;
00162 
00163     unsigned Size = Context.getTypeSize(Ty);
00164     if (!Context.getTargetInfo().validateOutputSize(Literal->getString(),
00165                                                     Size))
00166       return StmtError(Diag(OutputExpr->getLocStart(),
00167                             diag::err_asm_invalid_output_size)
00168                        << Info.getConstraintStr());
00169   }
00170 
00171   SmallVector<TargetInfo::ConstraintInfo, 4> InputConstraintInfos;
00172 
00173   for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) {
00174     StringLiteral *Literal = Constraints[i];
00175     if (!Literal->isAscii())
00176       return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
00177         << Literal->getSourceRange());
00178 
00179     StringRef InputName;
00180     if (Names[i])
00181       InputName = Names[i]->getName();
00182 
00183     TargetInfo::ConstraintInfo Info(Literal->getString(), InputName);
00184     if (!Context.getTargetInfo().validateInputConstraint(OutputConstraintInfos.data(),
00185                                                 NumOutputs, Info)) {
00186       return StmtError(Diag(Literal->getLocStart(),
00187                             diag::err_asm_invalid_input_constraint)
00188                        << Info.getConstraintStr());
00189     }
00190 
00191     Expr *InputExpr = Exprs[i];
00192 
00193     // Referring to parameters is not allowed in naked functions.
00194     if (CheckNakedParmReference(InputExpr, *this))
00195       return StmtError();
00196 
00197     // Only allow void types for memory constraints.
00198     if (Info.allowsMemory() && !Info.allowsRegister()) {
00199       if (CheckAsmLValue(InputExpr, *this))
00200         return StmtError(Diag(InputExpr->getLocStart(),
00201                               diag::err_asm_invalid_lvalue_in_input)
00202                          << Info.getConstraintStr()
00203                          << InputExpr->getSourceRange());
00204     } else {
00205       ExprResult Result = DefaultFunctionArrayLvalueConversion(Exprs[i]);
00206       if (Result.isInvalid())
00207         return StmtError();
00208 
00209       Exprs[i] = Result.get();
00210     }
00211 
00212     if (Info.allowsRegister()) {
00213       if (InputExpr->getType()->isVoidType()) {
00214         return StmtError(Diag(InputExpr->getLocStart(),
00215                               diag::err_asm_invalid_type_in_input)
00216           << InputExpr->getType() << Info.getConstraintStr()
00217           << InputExpr->getSourceRange());
00218       }
00219     }
00220 
00221     InputConstraintInfos.push_back(Info);
00222 
00223     const Type *Ty = Exprs[i]->getType().getTypePtr();
00224     if (Ty->isDependentType())
00225       continue;
00226 
00227     if (!Ty->isVoidType() || !Info.allowsMemory())
00228       if (RequireCompleteType(InputExpr->getLocStart(), Exprs[i]->getType(),
00229                               diag::err_dereference_incomplete_type))
00230         return StmtError();
00231 
00232     unsigned Size = Context.getTypeSize(Ty);
00233     if (!Context.getTargetInfo().validateInputSize(Literal->getString(),
00234                                                    Size))
00235       return StmtError(Diag(InputExpr->getLocStart(),
00236                             diag::err_asm_invalid_input_size)
00237                        << Info.getConstraintStr());
00238   }
00239 
00240   // Check that the clobbers are valid.
00241   for (unsigned i = 0; i != NumClobbers; i++) {
00242     StringLiteral *Literal = Clobbers[i];
00243     if (!Literal->isAscii())
00244       return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
00245         << Literal->getSourceRange());
00246 
00247     StringRef Clobber = Literal->getString();
00248 
00249     if (!Context.getTargetInfo().isValidClobber(Clobber))
00250       return StmtError(Diag(Literal->getLocStart(),
00251                   diag::err_asm_unknown_register_name) << Clobber);
00252   }
00253 
00254   GCCAsmStmt *NS =
00255     new (Context) GCCAsmStmt(Context, AsmLoc, IsSimple, IsVolatile, NumOutputs,
00256                              NumInputs, Names, Constraints, Exprs.data(),
00257                              AsmString, NumClobbers, Clobbers, RParenLoc);
00258   // Validate the asm string, ensuring it makes sense given the operands we
00259   // have.
00260   SmallVector<GCCAsmStmt::AsmStringPiece, 8> Pieces;
00261   unsigned DiagOffs;
00262   if (unsigned DiagID = NS->AnalyzeAsmString(Pieces, Context, DiagOffs)) {
00263     Diag(getLocationOfStringLiteralByte(AsmString, DiagOffs), DiagID)
00264            << AsmString->getSourceRange();
00265     return StmtError();
00266   }
00267 
00268   // Validate constraints and modifiers.
00269   for (unsigned i = 0, e = Pieces.size(); i != e; ++i) {
00270     GCCAsmStmt::AsmStringPiece &Piece = Pieces[i];
00271     if (!Piece.isOperand()) continue;
00272 
00273     // Look for the correct constraint index.
00274     unsigned Idx = 0;
00275     unsigned ConstraintIdx = 0;
00276     for (unsigned i = 0, e = NS->getNumOutputs(); i != e; ++i, ++ConstraintIdx) {
00277       TargetInfo::ConstraintInfo &Info = OutputConstraintInfos[i];
00278       if (Idx == Piece.getOperandNo())
00279         break;
00280       ++Idx;
00281 
00282       if (Info.isReadWrite()) {
00283         if (Idx == Piece.getOperandNo())
00284           break;
00285         ++Idx;
00286       }
00287     }
00288 
00289     for (unsigned i = 0, e = NS->getNumInputs(); i != e; ++i, ++ConstraintIdx) {
00290       TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i];
00291       if (Idx == Piece.getOperandNo())
00292         break;
00293       ++Idx;
00294 
00295       if (Info.isReadWrite()) {
00296         if (Idx == Piece.getOperandNo())
00297           break;
00298         ++Idx;
00299       }
00300     }
00301 
00302     // Now that we have the right indexes go ahead and check.
00303     StringLiteral *Literal = Constraints[ConstraintIdx];
00304     const Type *Ty = Exprs[ConstraintIdx]->getType().getTypePtr();
00305     if (Ty->isDependentType() || Ty->isIncompleteType())
00306       continue;
00307 
00308     unsigned Size = Context.getTypeSize(Ty);
00309     std::string SuggestedModifier;
00310     if (!Context.getTargetInfo().validateConstraintModifier(
00311             Literal->getString(), Piece.getModifier(), Size,
00312             SuggestedModifier)) {
00313       Diag(Exprs[ConstraintIdx]->getLocStart(),
00314            diag::warn_asm_mismatched_size_modifier);
00315 
00316       if (!SuggestedModifier.empty()) {
00317         auto B = Diag(Piece.getRange().getBegin(),
00318                       diag::note_asm_missing_constraint_modifier)
00319                  << SuggestedModifier;
00320         SuggestedModifier = "%" + SuggestedModifier + Piece.getString();
00321         B.AddFixItHint(FixItHint::CreateReplacement(Piece.getRange(),
00322                                                     SuggestedModifier));
00323       }
00324     }
00325   }
00326 
00327   // Validate tied input operands for type mismatches.
00328   for (unsigned i = 0, e = InputConstraintInfos.size(); i != e; ++i) {
00329     TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i];
00330 
00331     // If this is a tied constraint, verify that the output and input have
00332     // either exactly the same type, or that they are int/ptr operands with the
00333     // same size (int/long, int*/long, are ok etc).
00334     if (!Info.hasTiedOperand()) continue;
00335 
00336     unsigned TiedTo = Info.getTiedOperand();
00337     unsigned InputOpNo = i+NumOutputs;
00338     Expr *OutputExpr = Exprs[TiedTo];
00339     Expr *InputExpr = Exprs[InputOpNo];
00340 
00341     if (OutputExpr->isTypeDependent() || InputExpr->isTypeDependent())
00342       continue;
00343 
00344     QualType InTy = InputExpr->getType();
00345     QualType OutTy = OutputExpr->getType();
00346     if (Context.hasSameType(InTy, OutTy))
00347       continue;  // All types can be tied to themselves.
00348 
00349     // Decide if the input and output are in the same domain (integer/ptr or
00350     // floating point.
00351     enum AsmDomain {
00352       AD_Int, AD_FP, AD_Other
00353     } InputDomain, OutputDomain;
00354 
00355     if (InTy->isIntegerType() || InTy->isPointerType())
00356       InputDomain = AD_Int;
00357     else if (InTy->isRealFloatingType())
00358       InputDomain = AD_FP;
00359     else
00360       InputDomain = AD_Other;
00361 
00362     if (OutTy->isIntegerType() || OutTy->isPointerType())
00363       OutputDomain = AD_Int;
00364     else if (OutTy->isRealFloatingType())
00365       OutputDomain = AD_FP;
00366     else
00367       OutputDomain = AD_Other;
00368 
00369     // They are ok if they are the same size and in the same domain.  This
00370     // allows tying things like:
00371     //   void* to int*
00372     //   void* to int            if they are the same size.
00373     //   double to long double   if they are the same size.
00374     //
00375     uint64_t OutSize = Context.getTypeSize(OutTy);
00376     uint64_t InSize = Context.getTypeSize(InTy);
00377     if (OutSize == InSize && InputDomain == OutputDomain &&
00378         InputDomain != AD_Other)
00379       continue;
00380 
00381     // If the smaller input/output operand is not mentioned in the asm string,
00382     // then we can promote the smaller one to a larger input and the asm string
00383     // won't notice.
00384     bool SmallerValueMentioned = false;
00385 
00386     // If this is a reference to the input and if the input was the smaller
00387     // one, then we have to reject this asm.
00388     if (isOperandMentioned(InputOpNo, Pieces)) {
00389       // This is a use in the asm string of the smaller operand.  Since we
00390       // codegen this by promoting to a wider value, the asm will get printed
00391       // "wrong".
00392       SmallerValueMentioned |= InSize < OutSize;
00393     }
00394     if (isOperandMentioned(TiedTo, Pieces)) {
00395       // If this is a reference to the output, and if the output is the larger
00396       // value, then it's ok because we'll promote the input to the larger type.
00397       SmallerValueMentioned |= OutSize < InSize;
00398     }
00399 
00400     // If the smaller value wasn't mentioned in the asm string, and if the
00401     // output was a register, just extend the shorter one to the size of the
00402     // larger one.
00403     if (!SmallerValueMentioned && InputDomain != AD_Other &&
00404         OutputConstraintInfos[TiedTo].allowsRegister())
00405       continue;
00406 
00407     // Either both of the operands were mentioned or the smaller one was
00408     // mentioned.  One more special case that we'll allow: if the tied input is
00409     // integer, unmentioned, and is a constant, then we'll allow truncating it
00410     // down to the size of the destination.
00411     if (InputDomain == AD_Int && OutputDomain == AD_Int &&
00412         !isOperandMentioned(InputOpNo, Pieces) &&
00413         InputExpr->isEvaluatable(Context)) {
00414       CastKind castKind =
00415         (OutTy->isBooleanType() ? CK_IntegralToBoolean : CK_IntegralCast);
00416       InputExpr = ImpCastExprToType(InputExpr, OutTy, castKind).get();
00417       Exprs[InputOpNo] = InputExpr;
00418       NS->setInputExpr(i, InputExpr);
00419       continue;
00420     }
00421 
00422     Diag(InputExpr->getLocStart(),
00423          diag::err_asm_tying_incompatible_types)
00424       << InTy << OutTy << OutputExpr->getSourceRange()
00425       << InputExpr->getSourceRange();
00426     return StmtError();
00427   }
00428 
00429   return NS;
00430 }
00431 
00432 ExprResult Sema::LookupInlineAsmIdentifier(CXXScopeSpec &SS,
00433                                            SourceLocation TemplateKWLoc,
00434                                            UnqualifiedId &Id,
00435                                            llvm::InlineAsmIdentifierInfo &Info,
00436                                            bool IsUnevaluatedContext) {
00437   Info.clear();
00438 
00439   if (IsUnevaluatedContext)
00440     PushExpressionEvaluationContext(UnevaluatedAbstract,
00441                                     ReuseLambdaContextDecl);
00442 
00443   ExprResult Result = ActOnIdExpression(getCurScope(), SS, TemplateKWLoc, Id,
00444                                         /*trailing lparen*/ false,
00445                                         /*is & operand*/ false,
00446                                         /*CorrectionCandidateCallback=*/nullptr,
00447                                         /*IsInlineAsmIdentifier=*/ true);
00448 
00449   if (IsUnevaluatedContext)
00450     PopExpressionEvaluationContext();
00451 
00452   if (!Result.isUsable()) return Result;
00453 
00454   Result = CheckPlaceholderExpr(Result.get());
00455   if (!Result.isUsable()) return Result;
00456 
00457   // Referring to parameters is not allowed in naked functions.
00458   if (CheckNakedParmReference(Result.get(), *this))
00459     return ExprError();
00460 
00461   QualType T = Result.get()->getType();
00462 
00463   // For now, reject dependent types.
00464   if (T->isDependentType()) {
00465     Diag(Id.getLocStart(), diag::err_asm_incomplete_type) << T;
00466     return ExprError();
00467   }
00468 
00469   // Any sort of function type is fine.
00470   if (T->isFunctionType()) {
00471     return Result;
00472   }
00473 
00474   // Otherwise, it needs to be a complete type.
00475   if (RequireCompleteExprType(Result.get(), diag::err_asm_incomplete_type)) {
00476     return ExprError();
00477   }
00478 
00479   // Compute the type size (and array length if applicable?).
00480   Info.Type = Info.Size = Context.getTypeSizeInChars(T).getQuantity();
00481   if (T->isArrayType()) {
00482     const ArrayType *ATy = Context.getAsArrayType(T);
00483     Info.Type = Context.getTypeSizeInChars(ATy->getElementType()).getQuantity();
00484     Info.Length = Info.Size / Info.Type;
00485   }
00486 
00487   // We can work with the expression as long as it's not an r-value.
00488   if (!Result.get()->isRValue())
00489     Info.IsVarDecl = true;
00490 
00491   return Result;
00492 }
00493 
00494 bool Sema::LookupInlineAsmField(StringRef Base, StringRef Member,
00495                                 unsigned &Offset, SourceLocation AsmLoc) {
00496   Offset = 0;
00497   LookupResult BaseResult(*this, &Context.Idents.get(Base), SourceLocation(),
00498                           LookupOrdinaryName);
00499 
00500   if (!LookupName(BaseResult, getCurScope()))
00501     return true;
00502 
00503   if (!BaseResult.isSingleResult())
00504     return true;
00505 
00506   const RecordType *RT = nullptr;
00507   NamedDecl *FoundDecl = BaseResult.getFoundDecl();
00508   if (VarDecl *VD = dyn_cast<VarDecl>(FoundDecl))
00509     RT = VD->getType()->getAs<RecordType>();
00510   else if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(FoundDecl)) {
00511     MarkAnyDeclReferenced(TD->getLocation(), TD, /*OdrUse=*/false);
00512     RT = TD->getUnderlyingType()->getAs<RecordType>();
00513   } else if (TypeDecl *TD = dyn_cast<TypeDecl>(FoundDecl))
00514     RT = TD->getTypeForDecl()->getAs<RecordType>();
00515   if (!RT)
00516     return true;
00517 
00518   if (RequireCompleteType(AsmLoc, QualType(RT, 0), 0))
00519     return true;
00520 
00521   LookupResult FieldResult(*this, &Context.Idents.get(Member), SourceLocation(),
00522                            LookupMemberName);
00523 
00524   if (!LookupQualifiedName(FieldResult, RT->getDecl()))
00525     return true;
00526 
00527   // FIXME: Handle IndirectFieldDecl?
00528   FieldDecl *FD = dyn_cast<FieldDecl>(FieldResult.getFoundDecl());
00529   if (!FD)
00530     return true;
00531 
00532   const ASTRecordLayout &RL = Context.getASTRecordLayout(RT->getDecl());
00533   unsigned i = FD->getFieldIndex();
00534   CharUnits Result = Context.toCharUnitsFromBits(RL.getFieldOffset(i));
00535   Offset = (unsigned)Result.getQuantity();
00536 
00537   return false;
00538 }
00539 
00540 StmtResult Sema::ActOnMSAsmStmt(SourceLocation AsmLoc, SourceLocation LBraceLoc,
00541                                 ArrayRef<Token> AsmToks,
00542                                 StringRef AsmString,
00543                                 unsigned NumOutputs, unsigned NumInputs,
00544                                 ArrayRef<StringRef> Constraints,
00545                                 ArrayRef<StringRef> Clobbers,
00546                                 ArrayRef<Expr*> Exprs,
00547                                 SourceLocation EndLoc) {
00548   bool IsSimple = (NumOutputs != 0 || NumInputs != 0);
00549   getCurFunction()->setHasBranchProtectedScope();
00550   MSAsmStmt *NS =
00551     new (Context) MSAsmStmt(Context, AsmLoc, LBraceLoc, IsSimple,
00552                             /*IsVolatile*/ true, AsmToks, NumOutputs, NumInputs,
00553                             Constraints, Exprs, AsmString,
00554                             Clobbers, EndLoc);
00555   return NS;
00556 }
00557 
00558 LabelDecl *Sema::GetOrCreateMSAsmLabel(StringRef ExternalLabelName,
00559                                        SourceLocation Location,
00560                                        bool AlwaysCreate) {
00561   LabelDecl* Label = LookupOrCreateLabel(PP.getIdentifierInfo(ExternalLabelName),
00562                                          Location);
00563 
00564   if (Label->isMSAsmLabel()) {
00565     // If we have previously created this label implicitly, mark it as used.
00566     Label->markUsed(Context);
00567   } else {
00568     // Otherwise, insert it, but only resolve it if we have seen the label itself.
00569     std::string InternalName;
00570     llvm::raw_string_ostream OS(InternalName);
00571     // Create an internal name for the label.  The name should not be a valid mangled
00572     // name, and should be unique.  We use a dot to make the name an invalid mangled
00573     // name.
00574     OS << "__MSASMLABEL_." << MSAsmLabelNameCounter++ << "__" << ExternalLabelName;
00575     Label->setMSAsmLabel(OS.str());
00576   }
00577   if (AlwaysCreate) {
00578     // The label might have been created implicitly from a previously encountered
00579     // goto statement.  So, for both newly created and looked up labels, we mark
00580     // them as resolved.
00581     Label->setMSAsmLabelResolved();
00582   }
00583   // Adjust their location for being able to generate accurate diagnostics.
00584   Label->setLocation(Location);
00585 
00586   return Label;
00587 }