12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034 |
- //===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
- //
- // The LLVM Compiler Infrastructure
- //
- // This file is distributed under the University of Illinois Open Source
- // License. See LICENSE.TXT for details.
- //
- //===----------------------------------------------------------------------===//
- //
- // This file implements the C++ Declaration portions of the Parser interfaces.
- //
- //===----------------------------------------------------------------------===//
- #include "clang/Parse/Parser.h"
- #include "RAIIObjectsForParser.h"
- #include "clang/AST/ASTContext.h"
- #include "clang/AST/DeclTemplate.h"
- #include "clang/Basic/Attributes.h"
- #include "clang/Basic/CharInfo.h"
- #include "clang/Basic/OperatorKinds.h"
- #include "clang/Basic/TargetInfo.h"
- #include "clang/Parse/ParseDiagnostic.h"
- #include "clang/Sema/DeclSpec.h"
- #include "clang/Sema/ParsedTemplate.h"
- #include "clang/Sema/PrettyDeclStackTrace.h"
- #include "clang/Sema/Scope.h"
- #include "clang/Sema/SemaDiagnostic.h"
- #include "llvm/ADT/SmallString.h"
- using namespace clang;
- /// ParseNamespace - We know that the current token is a namespace keyword. This
- /// may either be a top level namespace or a block-level namespace alias. If
- /// there was an inline keyword, it has already been parsed.
- ///
- /// namespace-definition: [C++ 7.3: basic.namespace]
- /// named-namespace-definition
- /// unnamed-namespace-definition
- ///
- /// unnamed-namespace-definition:
- /// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}'
- ///
- /// named-namespace-definition:
- /// original-namespace-definition
- /// extension-namespace-definition
- ///
- /// original-namespace-definition:
- /// 'inline'[opt] 'namespace' identifier attributes[opt]
- /// '{' namespace-body '}'
- ///
- /// extension-namespace-definition:
- /// 'inline'[opt] 'namespace' original-namespace-name
- /// '{' namespace-body '}'
- ///
- /// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
- /// 'namespace' identifier '=' qualified-namespace-specifier ';'
- ///
- Decl *Parser::ParseNamespace(unsigned Context,
- SourceLocation &DeclEnd,
- SourceLocation InlineLoc) {
- assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
- SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
- ObjCDeclContextSwitch ObjCDC(*this);
-
- if (Tok.is(tok::code_completion)) {
- Actions.CodeCompleteNamespaceDecl(getCurScope());
- cutOffParsing();
- return nullptr;
- }
- SourceLocation IdentLoc;
- IdentifierInfo *Ident = nullptr;
- std::vector<SourceLocation> ExtraIdentLoc;
- std::vector<IdentifierInfo*> ExtraIdent;
- std::vector<SourceLocation> ExtraNamespaceLoc;
- ParsedAttributesWithRange attrs(AttrFactory);
- SourceLocation attrLoc;
- if (getLangOpts().CPlusPlus11 && isCXX11AttributeSpecifier()) {
- if (!getLangOpts().CPlusPlus1z)
- Diag(Tok.getLocation(), diag::warn_cxx14_compat_attribute)
- << 0 /*namespace*/;
- attrLoc = Tok.getLocation();
- ParseCXX11Attributes(attrs);
- }
- if (Tok.is(tok::identifier)) {
- Ident = Tok.getIdentifierInfo();
- IdentLoc = ConsumeToken(); // eat the identifier.
- while (Tok.is(tok::coloncolon) && NextToken().is(tok::identifier)) {
- ExtraNamespaceLoc.push_back(ConsumeToken());
- ExtraIdent.push_back(Tok.getIdentifierInfo());
- ExtraIdentLoc.push_back(ConsumeToken());
- }
- }
- // HLSL Change Starts
- else if (getLangOpts().HLSL) {
- Diag(Tok, diag::err_expected) << tok::identifier;
- }
- // HLSL Change Ends
- // A nested namespace definition cannot have attributes.
- if (!ExtraNamespaceLoc.empty() && attrLoc.isValid())
- Diag(attrLoc, diag::err_unexpected_nested_namespace_attribute);
- // Read label attributes, if present.
- if (Tok.is(tok::kw___attribute)) {
- attrLoc = Tok.getLocation();
- ParseGNUAttributes(attrs);
- }
- if (Tok.is(tok::equal)) {
- if (!Ident || getLangOpts().HLSL) { // HLSL Change: HLSL disallows namespace aliases
- Diag(Tok, diag::err_expected) << tok::identifier;
- // Skip to end of the definition and eat the ';'.
- SkipUntil(tok::semi);
- return nullptr;
- }
- if (attrLoc.isValid())
- Diag(attrLoc, diag::err_unexpected_namespace_attributes_alias);
- if (InlineLoc.isValid())
- Diag(InlineLoc, diag::err_inline_namespace_alias)
- << FixItHint::CreateRemoval(InlineLoc);
- return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
- }
- BalancedDelimiterTracker T(*this, tok::l_brace);
- if (T.consumeOpen()) {
- if (Ident)
- Diag(Tok, diag::err_expected) << tok::l_brace;
- else
- Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace;
- return nullptr;
- }
- if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
- getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
- getCurScope()->getFnParent()) {
- Diag(T.getOpenLocation(), diag::err_namespace_nonnamespace_scope);
- SkipUntil(tok::r_brace);
- return nullptr;
- }
- if (ExtraIdent.empty()) {
- // Normal namespace definition, not a nested-namespace-definition.
- } else if (InlineLoc.isValid()) {
- Diag(InlineLoc, diag::err_inline_nested_namespace_definition);
- } else if (getLangOpts().CPlusPlus1z) {
- Diag(ExtraNamespaceLoc[0],
- diag::warn_cxx14_compat_nested_namespace_definition);
- } else {
- TentativeParsingAction TPA(*this);
- SkipUntil(tok::r_brace, StopBeforeMatch);
- Token rBraceToken = Tok;
- TPA.Revert();
- if (!rBraceToken.is(tok::r_brace)) {
- Diag(ExtraNamespaceLoc[0],
- getLangOpts().HLSL ? diag::err_hlsl_nested_namespace_definition : // HLSL Change
- diag::ext_nested_namespace_definition)
- << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
- } else {
- std::string NamespaceFix;
- for (std::vector<IdentifierInfo*>::iterator I = ExtraIdent.begin(),
- E = ExtraIdent.end(); I != E; ++I) {
- NamespaceFix += " { namespace ";
- NamespaceFix += (*I)->getName();
- }
- std::string RBraces;
- for (unsigned i = 0, e = ExtraIdent.size(); i != e; ++i)
- RBraces += "} ";
- Diag(ExtraNamespaceLoc[0],
- getLangOpts().HLSL ? diag::err_hlsl_nested_namespace_definition : // HLSL Change
- diag::ext_nested_namespace_definition)
- << FixItHint::CreateReplacement(SourceRange(ExtraNamespaceLoc.front(),
- ExtraIdentLoc.back()),
- NamespaceFix)
- << FixItHint::CreateInsertion(rBraceToken.getLocation(), RBraces);
- }
- }
- // If we're still good, complain about inline namespaces in non-C++0x now.
- if (InlineLoc.isValid())
- Diag(InlineLoc, getLangOpts().CPlusPlus11 ?
- diag::warn_cxx98_compat_inline_namespace : diag::ext_inline_namespace);
- // Enter a scope for the namespace.
- ParseScope NamespaceScope(this, Scope::DeclScope);
- Decl *NamespcDecl =
- Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc,
- IdentLoc, Ident, T.getOpenLocation(),
- attrs.getList());
- PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
- "parsing namespace");
- // Parse the contents of the namespace. This includes parsing recovery on
- // any improperly nested namespaces.
- ParseInnerNamespace(ExtraIdentLoc, ExtraIdent, ExtraNamespaceLoc, 0,
- InlineLoc, attrs, T);
- // Leave the namespace scope.
- NamespaceScope.Exit();
- DeclEnd = T.getCloseLocation();
- Actions.ActOnFinishNamespaceDef(NamespcDecl, DeclEnd);
- return NamespcDecl;
- }
- /// ParseInnerNamespace - Parse the contents of a namespace.
- void Parser::ParseInnerNamespace(std::vector<SourceLocation> &IdentLoc,
- std::vector<IdentifierInfo *> &Ident,
- std::vector<SourceLocation> &NamespaceLoc,
- unsigned int index, SourceLocation &InlineLoc,
- ParsedAttributes &attrs,
- BalancedDelimiterTracker &Tracker) {
- if (index == Ident.size()) {
- while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
- ParsedAttributesWithRange attrs(AttrFactory);
- MaybeParseCXX11Attributes(attrs);
- MaybeParseMicrosoftAttributes(attrs);
- ParseExternalDeclaration(attrs);
- }
- // The caller is what called check -- we are simply calling
- // the close for it.
- Tracker.consumeClose();
- return;
- }
- // Handle a nested namespace definition.
- // FIXME: Preserve the source information through to the AST rather than
- // desugaring it here.
- ParseScope NamespaceScope(this, Scope::DeclScope);
- Decl *NamespcDecl =
- Actions.ActOnStartNamespaceDef(getCurScope(), SourceLocation(),
- NamespaceLoc[index], IdentLoc[index],
- Ident[index], Tracker.getOpenLocation(),
- attrs.getList());
- ParseInnerNamespace(IdentLoc, Ident, NamespaceLoc, ++index, InlineLoc,
- attrs, Tracker);
- NamespaceScope.Exit();
- Actions.ActOnFinishNamespaceDef(NamespcDecl, Tracker.getCloseLocation());
- }
- /// ParseNamespaceAlias - Parse the part after the '=' in a namespace
- /// alias definition.
- ///
- Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
- SourceLocation AliasLoc,
- IdentifierInfo *Alias,
- SourceLocation &DeclEnd) {
- assert(Tok.is(tok::equal) && "Not equal token");
- ConsumeToken(); // eat the '='.
- if (Tok.is(tok::code_completion)) {
- Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
- cutOffParsing();
- return nullptr;
- }
- CXXScopeSpec SS;
- // Parse (optional) nested-name-specifier.
- ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
- if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
- Diag(Tok, diag::err_expected_namespace_name);
- // Skip to end of the definition and eat the ';'.
- SkipUntil(tok::semi);
- return nullptr;
- }
- // Parse identifier.
- IdentifierInfo *Ident = Tok.getIdentifierInfo();
- SourceLocation IdentLoc = ConsumeToken();
- // Eat the ';'.
- DeclEnd = Tok.getLocation();
- if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name))
- SkipUntil(tok::semi);
- return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
- SS, IdentLoc, Ident);
- }
- /// ParseLinkage - We know that the current token is a string_literal
- /// and just before that, that extern was seen.
- ///
- /// linkage-specification: [C++ 7.5p2: dcl.link]
- /// 'extern' string-literal '{' declaration-seq[opt] '}'
- /// 'extern' string-literal declaration
- ///
- Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) {
- assert(isTokenStringLiteral() && "Not a string literal!");
- ExprResult Lang = ParseStringLiteralExpression(false);
- ParseScope LinkageScope(this, Scope::DeclScope);
- Decl *LinkageSpec =
- Lang.isInvalid()
- ? nullptr
- : Actions.ActOnStartLinkageSpecification(
- getCurScope(), DS.getSourceRange().getBegin(), Lang.get(),
- Tok.is(tok::l_brace) ? Tok.getLocation() : SourceLocation());
- ParsedAttributesWithRange attrs(AttrFactory);
- MaybeParseCXX11Attributes(attrs);
- MaybeParseHLSLAttributes(attrs); // HLSL Change
- MaybeParseMicrosoftAttributes(attrs);
- if (Tok.isNot(tok::l_brace)) {
- // Reset the source range in DS, as the leading "extern"
- // does not really belong to the inner declaration ...
- DS.SetRangeStart(SourceLocation());
- DS.SetRangeEnd(SourceLocation());
- // ... but anyway remember that such an "extern" was seen.
- DS.setExternInLinkageSpec(true);
- ParseExternalDeclaration(attrs, &DS);
- return LinkageSpec ? Actions.ActOnFinishLinkageSpecification(
- getCurScope(), LinkageSpec, SourceLocation())
- : nullptr;
- }
- DS.abort();
- ProhibitAttributes(attrs);
- BalancedDelimiterTracker T(*this, tok::l_brace);
- T.consumeOpen();
- unsigned NestedModules = 0;
- while (true) {
- switch (Tok.getKind()) {
- case tok::annot_module_begin:
- ++NestedModules;
- ParseTopLevelDecl();
- continue;
- case tok::annot_module_end:
- if (!NestedModules)
- break;
- --NestedModules;
- ParseTopLevelDecl();
- continue;
- case tok::annot_module_include:
- ParseTopLevelDecl();
- continue;
- case tok::eof:
- break;
- case tok::r_brace:
- if (!NestedModules)
- break;
- // Fall through.
- default:
- ParsedAttributesWithRange attrs(AttrFactory);
- MaybeParseCXX11Attributes(attrs);
- MaybeParseHLSLAttributes(attrs); // HLSL Change
- MaybeParseMicrosoftAttributes(attrs);
- ParseExternalDeclaration(attrs);
- continue;
- }
- break;
- }
- T.consumeClose();
- return LinkageSpec ? Actions.ActOnFinishLinkageSpecification(
- getCurScope(), LinkageSpec, T.getCloseLocation())
- : nullptr;
- }
- /// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
- /// using-directive. Assumes that current token is 'using'.
- Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
- const ParsedTemplateInfo &TemplateInfo,
- SourceLocation &DeclEnd,
- ParsedAttributesWithRange &attrs,
- Decl **OwnedType) {
- assert(Tok.is(tok::kw_using) && "Not using token");
- ObjCDeclContextSwitch ObjCDC(*this);
-
- // Eat 'using'.
- SourceLocation UsingLoc = ConsumeToken();
- if (Tok.is(tok::code_completion)) {
- Actions.CodeCompleteUsing(getCurScope());
- cutOffParsing();
- return nullptr;
- }
- // 'using namespace' means this is a using-directive.
- if (Tok.is(tok::kw_namespace)) {
- // Template parameters are always an error here.
- if (TemplateInfo.Kind) {
- SourceRange R = TemplateInfo.getSourceRange();
- Diag(UsingLoc, diag::err_templated_using_directive)
- << R << FixItHint::CreateRemoval(R);
- }
- return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
- }
- // Otherwise, it must be a using-declaration or an alias-declaration.
- // Using declarations can't have attributes.
- ProhibitAttributes(attrs);
- return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd,
- AS_none, OwnedType);
- }
- /// ParseUsingDirective - Parse C++ using-directive, assumes
- /// that current token is 'namespace' and 'using' was already parsed.
- ///
- /// using-directive: [C++ 7.3.p4: namespace.udir]
- /// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
- /// namespace-name ;
- /// [GNU] using-directive:
- /// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
- /// namespace-name attributes[opt] ;
- ///
- Decl *Parser::ParseUsingDirective(unsigned Context,
- SourceLocation UsingLoc,
- SourceLocation &DeclEnd,
- ParsedAttributes &attrs) {
- assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
- // Eat 'namespace'.
- SourceLocation NamespcLoc = ConsumeToken();
- if (Tok.is(tok::code_completion)) {
- Actions.CodeCompleteUsingDirective(getCurScope());
- cutOffParsing();
- return nullptr;
- }
- CXXScopeSpec SS;
- // Parse (optional) nested-name-specifier.
- ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
- IdentifierInfo *NamespcName = nullptr;
- SourceLocation IdentLoc = SourceLocation();
- // Parse namespace-name.
- if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
- Diag(Tok, diag::err_expected_namespace_name);
- // If there was invalid namespace name, skip to end of decl, and eat ';'.
- SkipUntil(tok::semi);
- // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
- return nullptr;
- }
- // Parse identifier.
- NamespcName = Tok.getIdentifierInfo();
- IdentLoc = ConsumeToken();
- // Parse (optional) attributes (most likely GNU strong-using extension).
- bool GNUAttr = false;
- if (Tok.is(tok::kw___attribute)) {
- GNUAttr = true;
- ParseGNUAttributes(attrs);
- }
- // Eat ';'.
- DeclEnd = Tok.getLocation();
- if (ExpectAndConsume(tok::semi,
- GNUAttr ? diag::err_expected_semi_after_attribute_list
- : diag::err_expected_semi_after_namespace_name))
- SkipUntil(tok::semi);
- return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
- IdentLoc, NamespcName, attrs.getList());
- }
- /// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration.
- /// Assumes that 'using' was already seen.
- ///
- /// using-declaration: [C++ 7.3.p3: namespace.udecl]
- /// 'using' 'typename'[opt] ::[opt] nested-name-specifier
- /// unqualified-id
- /// 'using' :: unqualified-id
- ///
- /// alias-declaration: C++11 [dcl.dcl]p1
- /// 'using' identifier attribute-specifier-seq[opt] = type-id ;
- ///
- Decl *Parser::ParseUsingDeclaration(unsigned Context,
- const ParsedTemplateInfo &TemplateInfo,
- SourceLocation UsingLoc,
- SourceLocation &DeclEnd,
- AccessSpecifier AS,
- Decl **OwnedType) {
- CXXScopeSpec SS;
- SourceLocation TypenameLoc;
- bool HasTypenameKeyword = false;
- // Check for misplaced attributes before the identifier in an
- // alias-declaration.
- ParsedAttributesWithRange MisplacedAttrs(AttrFactory);
- MaybeParseCXX11Attributes(MisplacedAttrs);
- assert(!getLangOpts().HLSL); // HLSL Change: in lieu of MaybeParseHLSLAttributes - using not allowed
- // Ignore optional 'typename'.
- // FIXME: This is wrong; we should parse this as a typename-specifier.
- if (TryConsumeToken(tok::kw_typename, TypenameLoc))
- HasTypenameKeyword = true;
- if (Tok.is(tok::kw___super)) {
- Diag(Tok.getLocation(), diag::err_super_in_using_declaration);
- SkipUntil(tok::semi);
- return nullptr;
- }
- // Parse nested-name-specifier.
- IdentifierInfo *LastII = nullptr;
- ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false,
- /*MayBePseudoDtor=*/nullptr,
- /*IsTypename=*/false,
- /*LastII=*/&LastII);
- // Check nested-name specifier.
- if (SS.isInvalid()) {
- SkipUntil(tok::semi);
- return nullptr;
- }
- SourceLocation TemplateKWLoc;
- UnqualifiedId Name;
- // Parse the unqualified-id. We allow parsing of both constructor and
- // destructor names and allow the action module to diagnose any semantic
- // errors.
- //
- // C++11 [class.qual]p2:
- // [...] in a using-declaration that is a member-declaration, if the name
- // specified after the nested-name-specifier is the same as the identifier
- // or the simple-template-id's template-name in the last component of the
- // nested-name-specifier, the name is [...] considered to name the
- // constructor.
- if (getLangOpts().CPlusPlus11 && Context == Declarator::MemberContext &&
- Tok.is(tok::identifier) && NextToken().is(tok::semi) &&
- SS.isNotEmpty() && LastII == Tok.getIdentifierInfo() &&
- !SS.getScopeRep()->getAsNamespace() &&
- !SS.getScopeRep()->getAsNamespaceAlias()) {
- SourceLocation IdLoc = ConsumeToken();
- ParsedType Type = Actions.getInheritingConstructorName(SS, IdLoc, *LastII);
- Name.setConstructorName(Type, IdLoc, IdLoc);
- } else if (ParseUnqualifiedId(
- SS, /*EnteringContext=*/false,
- /*AllowDestructorName=*/true,
- /*AllowConstructorName=*/NextToken().isNot(tok::equal),
- ParsedType(), TemplateKWLoc, Name)) {
- SkipUntil(tok::semi);
- return nullptr;
- }
- ParsedAttributesWithRange Attrs(AttrFactory);
- MaybeParseGNUAttributes(Attrs);
- MaybeParseCXX11Attributes(Attrs);
- assert(!getLangOpts().HLSL); // HLSL Change: in lieu of MaybeParseHLSLAttributes - using not allowed
- // Maybe this is an alias-declaration.
- TypeResult TypeAlias;
- bool IsAliasDecl = Tok.is(tok::equal);
- Decl *DeclFromDeclSpec = nullptr;
- if (IsAliasDecl) {
- // If we had any misplaced attributes from earlier, this is where they
- // should have been written.
- if (MisplacedAttrs.Range.isValid()) {
- Diag(MisplacedAttrs.Range.getBegin(), diag::err_attributes_not_allowed)
- << FixItHint::CreateInsertionFromRange(
- Tok.getLocation(),
- CharSourceRange::getTokenRange(MisplacedAttrs.Range))
- << FixItHint::CreateRemoval(MisplacedAttrs.Range);
- Attrs.takeAllFrom(MisplacedAttrs);
- }
- ConsumeToken();
- Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
- diag::warn_cxx98_compat_alias_declaration :
- diag::ext_alias_declaration);
- // Type alias templates cannot be specialized.
- int SpecKind = -1;
- if (TemplateInfo.Kind == ParsedTemplateInfo::Template &&
- Name.getKind() == UnqualifiedId::IK_TemplateId)
- SpecKind = 0;
- if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization)
- SpecKind = 1;
- if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
- SpecKind = 2;
- if (SpecKind != -1) {
- SourceRange Range;
- if (SpecKind == 0)
- Range = SourceRange(Name.TemplateId->LAngleLoc,
- Name.TemplateId->RAngleLoc);
- else
- Range = TemplateInfo.getSourceRange();
- Diag(Range.getBegin(), diag::err_alias_declaration_specialization)
- << SpecKind << Range;
- SkipUntil(tok::semi);
- return nullptr;
- }
- // Name must be an identifier.
- if (Name.getKind() != UnqualifiedId::IK_Identifier) {
- Diag(Name.StartLocation, diag::err_alias_declaration_not_identifier);
- // No removal fixit: can't recover from this.
- SkipUntil(tok::semi);
- return nullptr;
- } else if (HasTypenameKeyword)
- Diag(TypenameLoc, diag::err_alias_declaration_not_identifier)
- << FixItHint::CreateRemoval(SourceRange(TypenameLoc,
- SS.isNotEmpty() ? SS.getEndLoc() : TypenameLoc));
- else if (SS.isNotEmpty())
- Diag(SS.getBeginLoc(), diag::err_alias_declaration_not_identifier)
- << FixItHint::CreateRemoval(SS.getRange());
- TypeAlias = ParseTypeName(nullptr, TemplateInfo.Kind
- ? Declarator::AliasTemplateContext
- : Declarator::AliasDeclContext,
- AS, &DeclFromDeclSpec, &Attrs);
- if (OwnedType)
- *OwnedType = DeclFromDeclSpec;
- } else {
- // C++11 attributes are not allowed on a using-declaration, but GNU ones
- // are.
- ProhibitAttributes(MisplacedAttrs);
- ProhibitAttributes(Attrs);
- // Parse (optional) attributes (most likely GNU strong-using extension).
- MaybeParseGNUAttributes(Attrs);
- }
- // Eat ';'.
- DeclEnd = Tok.getLocation();
- if (ExpectAndConsume(tok::semi, diag::err_expected_after,
- !Attrs.empty() ? "attributes list"
- : IsAliasDecl ? "alias declaration"
- : "using declaration"))
- SkipUntil(tok::semi);
- // Diagnose an attempt to declare a templated using-declaration.
- // In C++11, alias-declarations can be templates:
- // template <...> using id = type;
- if (TemplateInfo.Kind && !IsAliasDecl) {
- SourceRange R = TemplateInfo.getSourceRange();
- Diag(UsingLoc, diag::err_templated_using_declaration)
- << R << FixItHint::CreateRemoval(R);
- // Unfortunately, we have to bail out instead of recovering by
- // ignoring the parameters, just in case the nested name specifier
- // depends on the parameters.
- return nullptr;
- }
- // "typename" keyword is allowed for identifiers only,
- // because it may be a type definition.
- if (HasTypenameKeyword && Name.getKind() != UnqualifiedId::IK_Identifier) {
- Diag(Name.getSourceRange().getBegin(), diag::err_typename_identifiers_only)
- << FixItHint::CreateRemoval(SourceRange(TypenameLoc));
- // Proceed parsing, but reset the HasTypenameKeyword flag.
- HasTypenameKeyword = false;
- }
- if (IsAliasDecl) {
- TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
- MultiTemplateParamsArg TemplateParamsArg(
- TemplateParams ? TemplateParams->data() : nullptr,
- TemplateParams ? TemplateParams->size() : 0);
- return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg,
- UsingLoc, Name, Attrs.getList(),
- TypeAlias, DeclFromDeclSpec);
- }
- return Actions.ActOnUsingDeclaration(getCurScope(), AS,
- /* HasUsingKeyword */ true, UsingLoc,
- SS, Name, Attrs.getList(),
- HasTypenameKeyword, TypenameLoc);
- }
- /// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration.
- ///
- /// [C++0x] static_assert-declaration:
- /// static_assert ( constant-expression , string-literal ) ;
- ///
- /// [C11] static_assert-declaration:
- /// _Static_assert ( constant-expression , string-literal ) ;
- ///
- Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
- assert(Tok.isOneOf(tok::kw_static_assert, tok::kw__Static_assert) &&
- "Not a static_assert declaration");
- if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11 && !getLangOpts().HLSL) // HLSL Change - support enabled
- Diag(Tok, diag::ext_c11_static_assert);
- if (Tok.is(tok::kw_static_assert) && !getLangOpts().HLSL) // HLSL Change - support enabled
- Diag(Tok, diag::warn_cxx98_compat_static_assert);
- SourceLocation StaticAssertLoc = ConsumeToken();
- BalancedDelimiterTracker T(*this, tok::l_paren);
- if (T.consumeOpen()) {
- Diag(Tok, diag::err_expected) << tok::l_paren;
- SkipMalformedDecl();
- return nullptr;
- }
- ExprResult AssertExpr(ParseConstantExpression());
- if (AssertExpr.isInvalid()) {
- SkipMalformedDecl();
- return nullptr;
- }
- ExprResult AssertMessage;
- if (Tok.is(tok::r_paren)) {
- Diag(Tok, getLangOpts().CPlusPlus1z
- ? diag::warn_cxx14_compat_static_assert_no_message
- : diag::ext_static_assert_no_message)
- << (getLangOpts().CPlusPlus1z
- ? FixItHint()
- : FixItHint::CreateInsertion(Tok.getLocation(), ", \"\""));
- } else {
- if (ExpectAndConsume(tok::comma)) {
- SkipUntil(tok::semi);
- return nullptr;
- }
- if (!isTokenStringLiteral()) {
- Diag(Tok, diag::err_expected_string_literal)
- << /*Source='static_assert'*/1;
- SkipMalformedDecl();
- return nullptr;
- }
- AssertMessage = ParseStringLiteralExpression();
- if (AssertMessage.isInvalid()) {
- SkipMalformedDecl();
- return nullptr;
- }
- }
- T.consumeClose();
- DeclEnd = Tok.getLocation();
- ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
- return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
- AssertExpr.get(),
- AssertMessage.get(),
- T.getCloseLocation());
- }
- /// ParseDecltypeSpecifier - Parse a C++11 decltype specifier.
- ///
- /// 'decltype' ( expression )
- /// 'decltype' ( 'auto' ) [C++1y]
- ///
- SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
- assert(Tok.isOneOf(tok::kw_decltype, tok::annot_decltype)
- && "Not a decltype specifier");
-
- ExprResult Result;
- SourceLocation StartLoc = Tok.getLocation();
- SourceLocation EndLoc;
- if (Tok.is(tok::annot_decltype)) {
- Result = getExprAnnotation(Tok);
- EndLoc = Tok.getAnnotationEndLoc();
- ConsumeToken();
- if (Result.isInvalid()) {
- DS.SetTypeSpecError();
- return EndLoc;
- }
- } else {
- if (Tok.getIdentifierInfo()->isStr("decltype"))
- Diag(Tok, diag::warn_cxx98_compat_decltype);
- ConsumeToken();
- BalancedDelimiterTracker T(*this, tok::l_paren);
- if (T.expectAndConsume(diag::err_expected_lparen_after,
- "decltype", tok::r_paren)) {
- DS.SetTypeSpecError();
- return T.getOpenLocation() == Tok.getLocation() ?
- StartLoc : T.getOpenLocation();
- }
- // Check for C++1y 'decltype(auto)'.
- if (Tok.is(tok::kw_auto)) {
- // No need to disambiguate here: an expression can't start with 'auto',
- // because the typename-specifier in a function-style cast operation can't
- // be 'auto'.
- Diag(Tok.getLocation(),
- getLangOpts().CPlusPlus14
- ? diag::warn_cxx11_compat_decltype_auto_type_specifier
- : diag::ext_decltype_auto_type_specifier);
- ConsumeToken();
- } else {
- // Parse the expression
- // C++11 [dcl.type.simple]p4:
- // The operand of the decltype specifier is an unevaluated operand.
- EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
- nullptr,/*IsDecltype=*/true);
- Result =
- Actions.CorrectDelayedTyposInExpr(ParseExpression(), [](Expr *E) {
- return E->hasPlaceholderType() ? ExprError() : E;
- });
- if (Result.isInvalid()) {
- DS.SetTypeSpecError();
- if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch)) {
- EndLoc = ConsumeParen();
- } else {
- if (PP.isBacktrackEnabled() && Tok.is(tok::semi)) {
- // Backtrack to get the location of the last token before the semi.
- PP.RevertCachedTokens(2);
- ConsumeToken(); // the semi.
- EndLoc = ConsumeAnyToken();
- assert(Tok.is(tok::semi));
- } else {
- EndLoc = Tok.getLocation();
- }
- }
- return EndLoc;
- }
- Result = Actions.ActOnDecltypeExpression(Result.get());
- }
- // Match the ')'
- T.consumeClose();
- if (T.getCloseLocation().isInvalid()) {
- DS.SetTypeSpecError();
- // FIXME: this should return the location of the last token
- // that was consumed (by "consumeClose()")
- return T.getCloseLocation();
- }
- if (Result.isInvalid()) {
- DS.SetTypeSpecError();
- return T.getCloseLocation();
- }
- EndLoc = T.getCloseLocation();
- }
- assert(!Result.isInvalid());
- const char *PrevSpec = nullptr;
- unsigned DiagID;
- const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
- // Check for duplicate type specifiers (e.g. "int decltype(a)").
- if (Result.get()
- ? DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
- DiagID, Result.get(), Policy)
- : DS.SetTypeSpecType(DeclSpec::TST_decltype_auto, StartLoc, PrevSpec,
- DiagID, Policy)) {
- Diag(StartLoc, DiagID) << PrevSpec;
- DS.SetTypeSpecError();
- }
- return EndLoc;
- }
- void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS,
- SourceLocation StartLoc,
- SourceLocation EndLoc) {
- // make sure we have a token we can turn into an annotation token
- if (PP.isBacktrackEnabled())
- PP.RevertCachedTokens(1);
- else
- PP.EnterToken(Tok);
- Tok.setKind(tok::annot_decltype);
- setExprAnnotation(Tok,
- DS.getTypeSpecType() == TST_decltype ? DS.getRepAsExpr() :
- DS.getTypeSpecType() == TST_decltype_auto ? ExprResult() :
- ExprError());
- Tok.setAnnotationEndLoc(EndLoc);
- Tok.setLocation(StartLoc);
- PP.AnnotateCachedTokens(Tok);
- }
- void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) {
- // HLSL Change Starts
- if (getLangOpts().HLSL) {
- Diag(Tok, diag::err_hlsl_unsupported_construct) << Tok.getName();
- DS.SetTypeSpecError();
- ConsumeToken();
- if (Tok.is(tok::l_paren)) {
- BalancedDelimiterTracker T(*this, tok::l_paren);
- T.consumeOpen();
- T.skipToEnd();
- }
- return;
- }
- // HLSL Change Ends
- assert(Tok.is(tok::kw___underlying_type) &&
- "Not an underlying type specifier");
- SourceLocation StartLoc = ConsumeToken();
- BalancedDelimiterTracker T(*this, tok::l_paren);
- if (T.expectAndConsume(diag::err_expected_lparen_after,
- "__underlying_type", tok::r_paren)) {
- return;
- }
- TypeResult Result = ParseTypeName();
- if (Result.isInvalid()) {
- SkipUntil(tok::r_paren, StopAtSemi);
- return;
- }
- // Match the ')'
- T.consumeClose();
- if (T.getCloseLocation().isInvalid())
- return;
- const char *PrevSpec = nullptr;
- unsigned DiagID;
- if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
- DiagID, Result.get(),
- Actions.getASTContext().getPrintingPolicy()))
- Diag(StartLoc, DiagID) << PrevSpec;
- DS.setTypeofParensRange(T.getRange());
- }
- /// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a
- /// class name or decltype-specifier. Note that we only check that the result
- /// names a type; semantic analysis will need to verify that the type names a
- /// class. The result is either a type or null, depending on whether a type
- /// name was found.
- ///
- /// base-type-specifier: [C++11 class.derived]
- /// class-or-decltype
- /// class-or-decltype: [C++11 class.derived]
- /// nested-name-specifier[opt] class-name
- /// decltype-specifier
- /// class-name: [C++ class.name]
- /// identifier
- /// simple-template-id
- ///
- /// In C++98, instead of base-type-specifier, we have:
- ///
- /// ::[opt] nested-name-specifier[opt] class-name
- TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
- SourceLocation &EndLocation) {
- // Ignore attempts to use typename
- if (Tok.is(tok::kw_typename)) {
- Diag(Tok, diag::err_expected_class_name_not_template)
- << FixItHint::CreateRemoval(Tok.getLocation());
- ConsumeToken();
- }
- // Parse optional nested-name-specifier
- CXXScopeSpec SS;
- ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
- BaseLoc = Tok.getLocation();
- // Parse decltype-specifier
- // tok == kw_decltype is just error recovery, it can only happen when SS
- // isn't empty
- if (Tok.isOneOf(tok::kw_decltype, tok::annot_decltype)) {
- if (SS.isNotEmpty())
- Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype)
- << FixItHint::CreateRemoval(SS.getRange());
- // Fake up a Declarator to use with ActOnTypeName.
- DeclSpec DS(AttrFactory);
- EndLocation = ParseDecltypeSpecifier(DS);
- Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
- return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
- }
- // Check whether we have a template-id that names a type.
- if (Tok.is(tok::annot_template_id)) {
- TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
- if (TemplateId->Kind == TNK_Type_template ||
- TemplateId->Kind == TNK_Dependent_template_name) {
- AnnotateTemplateIdTokenAsType();
- assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
- ParsedType Type = getTypeAnnotation(Tok);
- EndLocation = Tok.getAnnotationEndLoc();
- ConsumeToken();
- if (Type)
- return Type;
- return true;
- }
- // Fall through to produce an error below.
- }
- if (Tok.isNot(tok::identifier)) {
- Diag(Tok, diag::err_expected_class_name);
- return true;
- }
- IdentifierInfo *Id = Tok.getIdentifierInfo();
- SourceLocation IdLoc = ConsumeToken();
- if (Tok.is(tok::less) && !getLangOpts().HLSL) { // HLSL Change - do not fix for HLSL
- // It looks the user intended to write a template-id here, but the
- // template-name was wrong. Try to fix that.
- TemplateNameKind TNK = TNK_Type_template;
- TemplateTy Template;
- if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
- &SS, Template, TNK)) {
- Diag(IdLoc, diag::err_unknown_template_name)
- << Id;
- }
- if (!Template) {
- TemplateArgList TemplateArgs;
- SourceLocation LAngleLoc, RAngleLoc;
- ParseTemplateIdAfterTemplateName(TemplateTy(), IdLoc, SS,
- true, LAngleLoc, TemplateArgs, RAngleLoc);
- return true;
- }
- // Form the template name
- UnqualifiedId TemplateName;
- TemplateName.setIdentifier(Id, IdLoc);
- // Parse the full template-id, then turn it into a type.
- if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
- TemplateName, true))
- return true;
- if (TNK == TNK_Dependent_template_name)
- AnnotateTemplateIdTokenAsType();
- // If we didn't end up with a typename token, there's nothing more we
- // can do.
- if (Tok.isNot(tok::annot_typename))
- return true;
- // Retrieve the type from the annotation token, consume that token, and
- // return.
- EndLocation = Tok.getAnnotationEndLoc();
- ParsedType Type = getTypeAnnotation(Tok);
- ConsumeToken();
- return Type;
- }
- // We have an identifier; check whether it is actually a type.
- IdentifierInfo *CorrectedII = nullptr;
- ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
- false, ParsedType(),
- /*IsCtorOrDtorName=*/false,
- /*NonTrivialTypeSourceInfo=*/true,
- &CorrectedII);
- if (!Type) {
- Diag(IdLoc, diag::err_expected_class_name);
- return true;
- }
- // Consume the identifier.
- EndLocation = IdLoc;
- // Fake up a Declarator to use with ActOnTypeName.
- DeclSpec DS(AttrFactory);
- DS.SetRangeStart(IdLoc);
- DS.SetRangeEnd(EndLocation);
- DS.getTypeSpecScope() = SS;
- const char *PrevSpec = nullptr;
- unsigned DiagID;
- DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type,
- Actions.getASTContext().getPrintingPolicy());
- Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
- return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
- }
- void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) {
- while (Tok.isOneOf(tok::kw___single_inheritance,
- tok::kw___multiple_inheritance,
- tok::kw___virtual_inheritance)) {
- IdentifierInfo *AttrName = Tok.getIdentifierInfo();
- SourceLocation AttrNameLoc = ConsumeToken();
- attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
- AttributeList::AS_Keyword);
- }
- }
- /// Determine whether the following tokens are valid after a type-specifier
- /// which could be a standalone declaration. This will conservatively return
- /// true if there's any doubt, and is appropriate for insert-';' fixits.
- bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) {
- // HLSL Change Starts - abridged version of the table below
- if (getLangOpts().HLSL) {
- switch (Tok.getKind()) {
- case tok::semi:
- case tok::identifier:
- return true;
- case tok::colon:
- return CouldBeBitfield;
- default:
- return false;
- }
- }
- // HLSL Change Ends
- // This switch enumerates the valid "follow" set for type-specifiers.
- switch (Tok.getKind()) {
- default: break;
- case tok::semi: // struct foo {...} ;
- case tok::star: // struct foo {...} * P;
- case tok::amp: // struct foo {...} & R = ...
- case tok::ampamp: // struct foo {...} && R = ...
- case tok::identifier: // struct foo {...} V ;
- case tok::r_paren: //(struct foo {...} ) {4}
- case tok::annot_cxxscope: // struct foo {...} a:: b;
- case tok::annot_typename: // struct foo {...} a ::b;
- case tok::annot_template_id: // struct foo {...} a<int> ::b;
- case tok::l_paren: // struct foo {...} ( x);
- case tok::comma: // __builtin_offsetof(struct foo{...} ,
- case tok::kw_operator: // struct foo operator ++() {...}
- case tok::kw___declspec: // struct foo {...} __declspec(...)
- case tok::l_square: // void f(struct f [ 3])
- case tok::ellipsis: // void f(struct f ... [Ns])
- // FIXME: we should emit semantic diagnostic when declaration
- // attribute is in type attribute position.
- case tok::kw___attribute: // struct foo __attribute__((used)) x;
- return true;
- case tok::colon:
- return CouldBeBitfield; // enum E { ... } : 2;
- // Type qualifiers
- case tok::kw_const: // struct foo {...} const x;
- case tok::kw_volatile: // struct foo {...} volatile x;
- case tok::kw_restrict: // struct foo {...} restrict x;
- case tok::kw__Atomic: // struct foo {...} _Atomic x;
- case tok::kw___unaligned: // struct foo {...} __unaligned *x;
- // Function specifiers
- // Note, no 'explicit'. An explicit function must be either a conversion
- // operator or a constructor. Either way, it can't have a return type.
- case tok::kw_inline: // struct foo inline f();
- case tok::kw_virtual: // struct foo virtual f();
- case tok::kw_friend: // struct foo friend f();
- // Storage-class specifiers
- case tok::kw_static: // struct foo {...} static x;
- case tok::kw_extern: // struct foo {...} extern x;
- case tok::kw_typedef: // struct foo {...} typedef x;
- case tok::kw_register: // struct foo {...} register x;
- case tok::kw_auto: // struct foo {...} auto x;
- case tok::kw_mutable: // struct foo {...} mutable x;
- case tok::kw_thread_local: // struct foo {...} thread_local x;
- case tok::kw_constexpr: // struct foo {...} constexpr x;
- // As shown above, type qualifiers and storage class specifiers absolutely
- // can occur after class specifiers according to the grammar. However,
- // almost no one actually writes code like this. If we see one of these,
- // it is much more likely that someone missed a semi colon and the
- // type/storage class specifier we're seeing is part of the *next*
- // intended declaration, as in:
- //
- // struct foo { ... }
- // typedef int X;
- //
- // We'd really like to emit a missing semicolon error instead of emitting
- // an error on the 'int' saying that you can't have two type specifiers in
- // the same declaration of X. Because of this, we look ahead past this
- // token to see if it's a type specifier. If so, we know the code is
- // otherwise invalid, so we can produce the expected semi error.
- if (!isKnownToBeTypeSpecifier(NextToken()))
- return true;
- break;
- case tok::r_brace: // struct bar { struct foo {...} }
- // Missing ';' at end of struct is accepted as an extension in C mode.
- if (!getLangOpts().CPlusPlus)
- return true;
- break;
- case tok::greater:
- // template<class T = class X>
- return getLangOpts().CPlusPlus;
- }
- return false;
- }
- /// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
- /// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
- /// until we reach the start of a definition or see a token that
- /// cannot start a definition.
- ///
- /// class-specifier: [C++ class]
- /// class-head '{' member-specification[opt] '}'
- /// class-head '{' member-specification[opt] '}' attributes[opt]
- /// class-head:
- /// class-key identifier[opt] base-clause[opt]
- /// class-key nested-name-specifier identifier base-clause[opt]
- /// class-key nested-name-specifier[opt] simple-template-id
- /// base-clause[opt]
- /// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
- /// [GNU] class-key attributes[opt] nested-name-specifier
- /// identifier base-clause[opt]
- /// [GNU] class-key attributes[opt] nested-name-specifier[opt]
- /// simple-template-id base-clause[opt]
- /// class-key:
- /// 'class'
- /// 'struct'
- /// 'union'
- ///
- /// elaborated-type-specifier: [C++ dcl.type.elab]
- /// class-key ::[opt] nested-name-specifier[opt] identifier
- /// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
- /// simple-template-id
- ///
- /// Note that the C++ class-specifier and elaborated-type-specifier,
- /// together, subsume the C99 struct-or-union-specifier:
- ///
- /// struct-or-union-specifier: [C99 6.7.2.1]
- /// struct-or-union identifier[opt] '{' struct-contents '}'
- /// struct-or-union identifier
- /// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
- /// '}' attributes[opt]
- /// [GNU] struct-or-union attributes[opt] identifier
- /// struct-or-union:
- /// 'struct'
- /// 'union'
- void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
- SourceLocation StartLoc, DeclSpec &DS,
- const ParsedTemplateInfo &TemplateInfo,
- AccessSpecifier AS,
- bool EnteringContext, DeclSpecContext DSC,
- ParsedAttributesWithRange &Attributes) {
- DeclSpec::TST TagType;
- if (TagTokKind == tok::kw_struct)
- TagType = DeclSpec::TST_struct;
- else if (TagTokKind == tok::kw___interface || TagTokKind == tok::kw_interface) // HLSL Change - support 'interface'
- TagType = DeclSpec::TST_interface;
- else if (TagTokKind == tok::kw_class)
- TagType = DeclSpec::TST_class;
- else {
- assert(TagTokKind == tok::kw_union && "Not a class specifier");
- TagType = DeclSpec::TST_union;
- }
- if (Tok.is(tok::code_completion)) {
- // Code completion for a struct, class, or union name.
- Actions.CodeCompleteTag(getCurScope(), TagType);
- return cutOffParsing();
- }
- // C++03 [temp.explicit] 14.7.2/8:
- // The usual access checking rules do not apply to names used to specify
- // explicit instantiations.
- //
- // As an extension we do not perform access checking on the names used to
- // specify explicit specializations either. This is important to allow
- // specializing traits classes for private types.
- //
- // Note that we don't suppress if this turns out to be an elaborated
- // type specifier.
- bool shouldDelayDiagsInTag =
- (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
- TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
- SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
- ParsedAttributesWithRange attrs(AttrFactory);
- // If attributes exist after tag, parse them.
- MaybeParseGNUAttributes(attrs);
- MaybeParseMicrosoftDeclSpecs(attrs);
- // Parse inheritance specifiers.
- // HLSL Change Starts - these keywords are only defined for Microsoft Extensions
- assert(!getLangOpts().HLSL ||
- (!Tok.is(tok::kw___single_inheritance) && !Tok.is(tok::kw___multiple_inheritance) && !Tok.is(tok::kw___virtual_inheritance))); // HLSL Change
- if (!getLangOpts().HLSL) {
- // HLSL Change Ends - succeeding block is now conditional
- if (Tok.isOneOf(tok::kw___single_inheritance,
- tok::kw___multiple_inheritance,
- tok::kw___virtual_inheritance))
- ParseMicrosoftInheritanceClassAttributes(attrs);
- } // HLSL Change - close conditional block
- // If C++0x attributes exist here, parse them.
- // FIXME: Are we consistent with the ordering of parsing of different
- // styles of attributes?
- MaybeParseCXX11Attributes(attrs);
- MaybeParseHLSLAttributes(attrs); // HLSL Change
- // Source location used by FIXIT to insert misplaced
- // C++11 attributes
- SourceLocation AttrFixitLoc = Tok.getLocation();
- if (!getLangOpts().HLSL) { // HLSL Change - skip fixes for GNU libstdc++ - succeeding block is now conditional
- if (TagType == DeclSpec::TST_struct &&
- Tok.isNot(tok::identifier) &&
- !Tok.isAnnotation() &&
- Tok.getIdentifierInfo() &&
- Tok.isOneOf(tok::kw___is_abstract,
- tok::kw___is_arithmetic,
- tok::kw___is_array,
- tok::kw___is_base_of,
- tok::kw___is_class,
- tok::kw___is_complete_type,
- tok::kw___is_compound,
- tok::kw___is_const,
- tok::kw___is_constructible,
- tok::kw___is_convertible,
- tok::kw___is_convertible_to,
- tok::kw___is_destructible,
- tok::kw___is_empty,
- tok::kw___is_enum,
- tok::kw___is_floating_point,
- tok::kw___is_final,
- tok::kw___is_function,
- tok::kw___is_fundamental,
- tok::kw___is_integral,
- tok::kw___is_interface_class,
- tok::kw___is_literal,
- tok::kw___is_lvalue_expr,
- tok::kw___is_lvalue_reference,
- tok::kw___is_member_function_pointer,
- tok::kw___is_member_object_pointer,
- tok::kw___is_member_pointer,
- tok::kw___is_nothrow_assignable,
- tok::kw___is_nothrow_constructible,
- tok::kw___is_nothrow_destructible,
- tok::kw___is_object,
- tok::kw___is_pod,
- tok::kw___is_pointer,
- tok::kw___is_polymorphic,
- tok::kw___is_reference,
- tok::kw___is_rvalue_expr,
- tok::kw___is_rvalue_reference,
- tok::kw___is_same,
- tok::kw___is_scalar,
- tok::kw___is_sealed,
- tok::kw___is_signed,
- tok::kw___is_standard_layout,
- tok::kw___is_trivial,
- tok::kw___is_trivially_assignable,
- tok::kw___is_trivially_constructible,
- tok::kw___is_trivially_copyable,
- tok::kw___is_union,
- tok::kw___is_unsigned,
- tok::kw___is_void,
- tok::kw___is_volatile))
- // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the
- // name of struct templates, but some are keywords in GCC >= 4.3
- // and Clang. Therefore, when we see the token sequence "struct
- // X", make X into a normal identifier rather than a keyword, to
- // allow libstdc++ 4.2 and libc++ to work properly.
- TryKeywordIdentFallback(true);
- } // HLSL Change - close conditional block
- // Parse the (optional) nested-name-specifier.
- CXXScopeSpec &SS = DS.getTypeSpecScope();
- if (getLangOpts().CPlusPlus) {
- // "FOO : BAR" is not a potential typo for "FOO::BAR". In this context it
- // is a base-specifier-list.
- ColonProtectionRAIIObject X(*this);
- CXXScopeSpec Spec;
- bool HasValidSpec = true;
- if (ParseOptionalCXXScopeSpecifier(Spec, ParsedType(), EnteringContext)) {
- DS.SetTypeSpecError();
- HasValidSpec = false;
- }
- if (Spec.isSet())
- if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id)) {
- Diag(Tok, diag::err_expected) << tok::identifier;
- HasValidSpec = false;
- }
- if (HasValidSpec)
- SS = Spec;
- }
- TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
- // Parse the (optional) class name or simple-template-id.
- IdentifierInfo *Name = nullptr;
- SourceLocation NameLoc;
- TemplateIdAnnotation *TemplateId = nullptr;
- if (Tok.is(tok::identifier)) {
- Name = Tok.getIdentifierInfo();
- NameLoc = ConsumeToken();
- if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
- // The name was supposed to refer to a template, but didn't.
- // Eat the template argument list and try to continue parsing this as
- // a class (or template thereof).
- TemplateArgList TemplateArgs;
- SourceLocation LAngleLoc, RAngleLoc;
- if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
- true, LAngleLoc,
- TemplateArgs, RAngleLoc)) {
- // We couldn't parse the template argument list at all, so don't
- // try to give any location information for the list.
- LAngleLoc = RAngleLoc = SourceLocation();
- }
- Diag(NameLoc, diag::err_explicit_spec_non_template)
- << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
- << TagTokKind << Name << SourceRange(LAngleLoc, RAngleLoc);
- // Strip off the last template parameter list if it was empty, since
- // we've removed its template argument list.
- if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
- if (TemplateParams && TemplateParams->size() > 1) {
- TemplateParams->pop_back();
- } else {
- TemplateParams = nullptr;
- const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
- = ParsedTemplateInfo::NonTemplate;
- }
- } else if (TemplateInfo.Kind
- == ParsedTemplateInfo::ExplicitInstantiation) {
- // Pretend this is just a forward declaration.
- TemplateParams = nullptr;
- const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
- = ParsedTemplateInfo::NonTemplate;
- const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
- = SourceLocation();
- const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
- = SourceLocation();
- }
- }
- } else if (Tok.is(tok::annot_template_id)) {
- TemplateId = takeTemplateIdAnnotation(Tok);
- NameLoc = ConsumeToken();
- if (TemplateId->Kind != TNK_Type_template &&
- TemplateId->Kind != TNK_Dependent_template_name) {
- // The template-name in the simple-template-id refers to
- // something other than a class template. Give an appropriate
- // error message and skip to the ';'.
- SourceRange Range(NameLoc);
- if (SS.isNotEmpty())
- Range.setBegin(SS.getBeginLoc());
- // FIXME: Name may be null here.
- Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
- << TemplateId->Name << static_cast<int>(TemplateId->Kind) << Range;
- DS.SetTypeSpecError();
- SkipUntil(tok::semi, StopBeforeMatch);
- return;
- }
- }
- // There are four options here.
- // - If we are in a trailing return type, this is always just a reference,
- // and we must not try to parse a definition. For instance,
- // [] () -> struct S { };
- // does not define a type.
- // - If we have 'struct foo {...', 'struct foo :...',
- // 'struct foo final :' or 'struct foo final {', then this is a definition.
- // - If we have 'struct foo;', then this is either a forward declaration
- // or a friend declaration, which have to be treated differently.
- // - Otherwise we have something like 'struct foo xyz', a reference.
- //
- // We also detect these erroneous cases to provide better diagnostic for
- // C++11 attributes parsing.
- // - attributes follow class name:
- // struct foo [[]] {};
- // - attributes appear before or after 'final':
- // struct foo [[]] final [[]] {};
- //
- // However, in type-specifier-seq's, things look like declarations but are
- // just references, e.g.
- // new struct s;
- // or
- // &T::operator struct s;
- // For these, DSC is DSC_type_specifier or DSC_alias_declaration.
- // If there are attributes after class name, parse them.
- MaybeParseCXX11Attributes(Attributes);
- MaybeParseHLSLAttributes(Attributes); // HLSL Change
- const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
- Sema::TagUseKind TUK;
- if (DSC == DSC_trailing)
- TUK = Sema::TUK_Reference;
- else if (Tok.is(tok::l_brace) ||
- (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
- (isCXX11FinalKeyword() &&
- (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) {
- if (DS.isFriendSpecified()) {
- // C++ [class.friend]p2:
- // A class shall not be defined in a friend declaration.
- Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
- << SourceRange(DS.getFriendSpecLoc());
- // Skip everything up to the semicolon, so that this looks like a proper
- // friend class (or template thereof) declaration.
- SkipUntil(tok::semi, StopBeforeMatch);
- TUK = Sema::TUK_Friend;
- } else {
- // Okay, this is a class definition.
- TUK = Sema::TUK_Definition;
- }
- } else if (isCXX11FinalKeyword() && (NextToken().is(tok::l_square) ||
- NextToken().is(tok::kw_alignas))) {
- // We can't tell if this is a definition or reference
- // until we skipped the 'final' and C++11 attribute specifiers.
- TentativeParsingAction PA(*this);
- // Skip the 'final' keyword.
- ConsumeToken();
- // Skip C++11 attribute specifiers.
- while (true) {
- if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
- ConsumeBracket();
- if (!SkipUntil(tok::r_square, StopAtSemi))
- break;
- } else if (Tok.is(tok::kw_alignas) && NextToken().is(tok::l_paren)) {
- ConsumeToken();
- ConsumeParen();
- if (!SkipUntil(tok::r_paren, StopAtSemi))
- break;
- } else {
- break;
- }
- }
- if (Tok.isOneOf(tok::l_brace, tok::colon))
- TUK = Sema::TUK_Definition;
- else
- TUK = Sema::TUK_Reference;
- PA.Revert();
- } else if (!isTypeSpecifier(DSC) &&
- (Tok.is(tok::semi) ||
- (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) {
- TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
- if (Tok.isNot(tok::semi)) {
- const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
- // A semicolon was missing after this declaration. Diagnose and recover.
- ExpectAndConsume(tok::semi, diag::err_expected_after,
- DeclSpec::getSpecifierName(TagType, PPol));
- PP.EnterToken(Tok);
- Tok.setKind(tok::semi);
- }
- } else
- TUK = Sema::TUK_Reference;
- // Forbid misplaced attributes. In cases of a reference, we pass attributes
- // to caller to handle.
- if (TUK != Sema::TUK_Reference) {
- // If this is not a reference, then the only possible
- // valid place for C++11 attributes to appear here
- // is between class-key and class-name. If there are
- // any attributes after class-name, we try a fixit to move
- // them to the right place.
- SourceRange AttrRange = Attributes.Range;
- if (AttrRange.isValid()) {
- Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed)
- << AttrRange
- << FixItHint::CreateInsertionFromRange(AttrFixitLoc,
- CharSourceRange(AttrRange, true))
- << FixItHint::CreateRemoval(AttrRange);
- // Recover by adding misplaced attributes to the attribute list
- // of the class so they can be applied on the class later.
- attrs.takeAllFrom(Attributes);
- }
- }
- // If this is an elaborated type specifier, and we delayed
- // diagnostics before, just merge them into the current pool.
- if (shouldDelayDiagsInTag) {
- diagsFromTag.done();
- if (TUK == Sema::TUK_Reference)
- diagsFromTag.redelay();
- }
- if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
- TUK != Sema::TUK_Definition)) {
- if (DS.getTypeSpecType() != DeclSpec::TST_error) {
- // We have a declaration or reference to an anonymous class.
- Diag(StartLoc, diag::err_anon_type_definition)
- << DeclSpec::getSpecifierName(TagType, Policy);
- }
- // If we are parsing a definition and stop at a base-clause, continue on
- // until the semicolon. Continuing from the comma will just trick us into
- // thinking we are seeing a variable declaration.
- if (TUK == Sema::TUK_Definition && Tok.is(tok::colon))
- SkipUntil(tok::semi, StopBeforeMatch);
- else
- SkipUntil(tok::comma, StopAtSemi);
- return;
- }
- // Create the tag portion of the class or class template.
- DeclResult TagOrTempResult = true; // invalid
- TypeResult TypeResult = true; // invalid
- bool Owned = false;
- Sema::SkipBodyInfo SkipBody;
- if (TemplateId) {
- // Explicit specialization, class template partial specialization,
- // or explicit instantiation.
- ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
- TemplateId->NumArgs);
- if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
- TUK == Sema::TUK_Declaration) {
- // This is an explicit instantiation of a class template.
- ProhibitAttributes(attrs);
- TagOrTempResult
- = Actions.ActOnExplicitInstantiation(getCurScope(),
- TemplateInfo.ExternLoc,
- TemplateInfo.TemplateLoc,
- TagType,
- StartLoc,
- SS,
- TemplateId->Template,
- TemplateId->TemplateNameLoc,
- TemplateId->LAngleLoc,
- TemplateArgsPtr,
- TemplateId->RAngleLoc,
- attrs.getList());
- // Friend template-ids are treated as references unless
- // they have template headers, in which case they're ill-formed
- // (FIXME: "template <class T> friend class A<T>::B<int>;").
- // We diagnose this error in ActOnClassTemplateSpecialization.
- } else if (TUK == Sema::TUK_Reference ||
- (TUK == Sema::TUK_Friend &&
- TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
- ProhibitAttributes(attrs);
- TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc,
- TemplateId->SS,
- TemplateId->TemplateKWLoc,
- TemplateId->Template,
- TemplateId->TemplateNameLoc,
- TemplateId->LAngleLoc,
- TemplateArgsPtr,
- TemplateId->RAngleLoc);
- } else {
- // This is an explicit specialization or a class template
- // partial specialization.
- TemplateParameterLists FakedParamLists;
- if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
- // This looks like an explicit instantiation, because we have
- // something like
- //
- // template class Foo<X>
- //
- // but it actually has a definition. Most likely, this was
- // meant to be an explicit specialization, but the user forgot
- // the '<>' after 'template'.
- // It this is friend declaration however, since it cannot have a
- // template header, it is most likely that the user meant to
- // remove the 'template' keyword.
- assert((TUK == Sema::TUK_Definition || TUK == Sema::TUK_Friend) &&
- "Expected a definition here");
- if (TUK == Sema::TUK_Friend) {
- Diag(DS.getFriendSpecLoc(), diag::err_friend_explicit_instantiation);
- TemplateParams = nullptr;
- } else {
- SourceLocation LAngleLoc =
- PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
- Diag(TemplateId->TemplateNameLoc,
- diag::err_explicit_instantiation_with_definition)
- << SourceRange(TemplateInfo.TemplateLoc)
- << FixItHint::CreateInsertion(LAngleLoc, "<>");
- // Create a fake template parameter list that contains only
- // "template<>", so that we treat this construct as a class
- // template specialization.
- FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
- 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, nullptr,
- 0, LAngleLoc));
- TemplateParams = &FakedParamLists;
- }
- }
- // Build the class template specialization.
- TagOrTempResult = Actions.ActOnClassTemplateSpecialization(
- getCurScope(), TagType, TUK, StartLoc, DS.getModulePrivateSpecLoc(),
- *TemplateId, attrs.getList(),
- MultiTemplateParamsArg(TemplateParams ? &(*TemplateParams)[0]
- : nullptr,
- TemplateParams ? TemplateParams->size() : 0),
- &SkipBody);
- }
- } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
- TUK == Sema::TUK_Declaration) {
- // Explicit instantiation of a member of a class template
- // specialization, e.g.,
- //
- // template struct Outer<int>::Inner;
- //
- ProhibitAttributes(attrs);
- TagOrTempResult
- = Actions.ActOnExplicitInstantiation(getCurScope(),
- TemplateInfo.ExternLoc,
- TemplateInfo.TemplateLoc,
- TagType, StartLoc, SS, Name,
- NameLoc, attrs.getList());
- } else if (TUK == Sema::TUK_Friend &&
- TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
- ProhibitAttributes(attrs);
- TagOrTempResult =
- Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
- TagType, StartLoc, SS,
- Name, NameLoc, attrs.getList(),
- MultiTemplateParamsArg(
- TemplateParams? &(*TemplateParams)[0]
- : nullptr,
- TemplateParams? TemplateParams->size() : 0));
- } else {
- if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition)
- ProhibitAttributes(attrs);
- if (TUK == Sema::TUK_Definition &&
- TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
- // If the declarator-id is not a template-id, issue a diagnostic and
- // recover by ignoring the 'template' keyword.
- Diag(Tok, diag::err_template_defn_explicit_instantiation)
- << 1 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
- TemplateParams = nullptr;
- }
- bool IsDependent = false;
- // Don't pass down template parameter lists if this is just a tag
- // reference. For example, we don't need the template parameters here:
- // template <class T> class A *makeA(T t);
- MultiTemplateParamsArg TParams;
- if (TUK != Sema::TUK_Reference && TemplateParams)
- TParams =
- MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
- handleDeclspecAlignBeforeClassKey(attrs, DS, TUK);
- // Declaration or definition of a class type
- TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
- SS, Name, NameLoc, attrs.getList(), AS,
- DS.getModulePrivateSpecLoc(),
- TParams, Owned, IsDependent,
- SourceLocation(), false,
- clang::TypeResult(),
- DSC == DSC_type_specifier,
- &SkipBody);
- // If ActOnTag said the type was dependent, try again with the
- // less common call.
- if (IsDependent) {
- assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
- TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
- SS, Name, StartLoc, NameLoc);
- }
- }
- // If there is a body, parse it and inform the actions module.
- if (TUK == Sema::TUK_Definition) {
- assert(Tok.is(tok::l_brace) ||
- (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
- isCXX11FinalKeyword());
- if (SkipBody.ShouldSkip)
- SkipCXXMemberSpecification(StartLoc, AttrFixitLoc, TagType,
- TagOrTempResult.get());
- else if (getLangOpts().CPlusPlus)
- ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs, TagType,
- TagOrTempResult.get());
- else
- ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
- }
- const char *PrevSpec = nullptr;
- unsigned DiagID;
- bool Result;
- if (!TypeResult.isInvalid()) {
- Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
- NameLoc.isValid() ? NameLoc : StartLoc,
- PrevSpec, DiagID, TypeResult.get(), Policy);
- } else if (!TagOrTempResult.isInvalid()) {
- Result = DS.SetTypeSpecType(TagType, StartLoc,
- NameLoc.isValid() ? NameLoc : StartLoc,
- PrevSpec, DiagID, TagOrTempResult.get(), Owned,
- Policy);
- } else {
- DS.SetTypeSpecError();
- return;
- }
- if (Result)
- Diag(StartLoc, DiagID) << PrevSpec;
- // At this point, we've successfully parsed a class-specifier in 'definition'
- // form (e.g. "struct foo { int x; }". While we could just return here, we're
- // going to look at what comes after it to improve error recovery. If an
- // impossible token occurs next, we assume that the programmer forgot a ; at
- // the end of the declaration and recover that way.
- //
- // Also enforce C++ [temp]p3:
- // In a template-declaration which defines a class, no declarator
- // is permitted.
- //
- // After a type-specifier, we don't expect a semicolon. This only happens in
- // C, since definitions are not permitted in this context in C++.
- if (TUK == Sema::TUK_Definition &&
- (getLangOpts().CPlusPlus || !isTypeSpecifier(DSC)) &&
- (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) {
- if (Tok.isNot(tok::semi)) {
- const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
- ExpectAndConsume(tok::semi, diag::err_expected_after,
- DeclSpec::getSpecifierName(TagType, PPol));
- // Push this token back into the preprocessor and change our current token
- // to ';' so that the rest of the code recovers as though there were an
- // ';' after the definition.
- PP.EnterToken(Tok);
- Tok.setKind(tok::semi);
- }
- }
- }
- /// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
- ///
- /// base-clause : [C++ class.derived]
- /// ':' base-specifier-list
- /// base-specifier-list:
- /// base-specifier '...'[opt]
- /// base-specifier-list ',' base-specifier '...'[opt]
- void Parser::ParseBaseClause(Decl *ClassDecl) {
- assert(Tok.is(tok::colon) && "Not a base clause");
- ConsumeToken();
- // Build up an array of parsed base specifiers.
- SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
- while (true) {
- // Parse a base-specifier.
- BaseResult Result = ParseBaseSpecifier(ClassDecl);
- if (Result.isInvalid()) {
- // Skip the rest of this base specifier, up until the comma or
- // opening brace.
- SkipUntil(tok::comma, tok::l_brace, StopAtSemi | StopBeforeMatch);
- } else {
- // Add this to our array of base specifiers.
- BaseInfo.push_back(Result.get());
- }
- // If the next token is a comma, consume it and keep reading
- // base-specifiers.
- if (!TryConsumeToken(tok::comma))
- break;
- }
- // Attach the base specifiers
- Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
- }
- /// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
- /// one entry in the base class list of a class specifier, for example:
- /// class foo : public bar, virtual private baz {
- /// 'public bar' and 'virtual private baz' are each base-specifiers.
- ///
- /// base-specifier: [C++ class.derived]
- /// attribute-specifier-seq[opt] base-type-specifier
- /// attribute-specifier-seq[opt] 'virtual' access-specifier[opt]
- /// base-type-specifier
- /// attribute-specifier-seq[opt] access-specifier 'virtual'[opt]
- /// base-type-specifier
- BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
- bool IsVirtual = false;
- SourceLocation StartLoc = Tok.getLocation();
- ParsedAttributesWithRange Attributes(AttrFactory);
- MaybeParseCXX11Attributes(Attributes);
- // Parse the 'virtual' keyword.
- if (TryConsumeToken(tok::kw_virtual)) {
- if (getLangOpts().HLSL) Diag(Tok, diag::err_hlsl_unsupported_construct) << "virtual base type"; // HLSL Change
- IsVirtual = true;
- }
- CheckMisplacedCXX11Attribute(Attributes, StartLoc);
- // Parse an (optional) access specifier.
- AccessSpecifier Access = getAccessSpecifierIfPresent();
- if (Access != AS_none) Diag(Tok, diag::err_hlsl_unsupported_construct) << "base type access specifier"; // HLSL Change
- if (Access != AS_none)
- ConsumeToken();
- if (getLangOpts().HLSL) Access = AS_public; // HLSL Change - always use public for hlsl.
- CheckMisplacedCXX11Attribute(Attributes, StartLoc);
- // Parse the 'virtual' keyword (again!), in case it came after the
- // access specifier.
- if (Tok.is(tok::kw_virtual)) {
- if (getLangOpts().HLSL) Diag(Tok, diag::err_hlsl_unsupported_construct) << "virtual base type"; // HLSL Change
- SourceLocation VirtualLoc = ConsumeToken();
- if (IsVirtual) {
- // Complain about duplicate 'virtual'
- Diag(VirtualLoc, diag::err_dup_virtual)
- << FixItHint::CreateRemoval(VirtualLoc);
- }
- IsVirtual = true;
- }
- CheckMisplacedCXX11Attribute(Attributes, StartLoc);
- // Parse the class-name.
- SourceLocation EndLocation;
- SourceLocation BaseLoc;
- TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
- if (BaseType.isInvalid())
- return true;
- // Parse the optional ellipsis (for a pack expansion). The ellipsis is
- // actually part of the base-specifier-list grammar productions, but we
- // parse it here for convenience.
- SourceLocation EllipsisLoc;
- TryConsumeToken(tok::ellipsis, EllipsisLoc);
- if (getLangOpts().HLSL && EllipsisLoc != SourceLocation()) Diag(Tok, diag::err_hlsl_unsupported_construct) << "base type ellipsis"; // HLSL Change
- // Find the complete source range for the base-specifier.
- SourceRange Range(StartLoc, EndLocation);
- // Notify semantic analysis that we have parsed a complete
- // base-specifier.
- return Actions.ActOnBaseSpecifier(ClassDecl, Range, Attributes, IsVirtual,
- Access, BaseType.get(), BaseLoc,
- EllipsisLoc);
- }
- /// getAccessSpecifierIfPresent - Determine whether the next token is
- /// a C++ access-specifier.
- ///
- /// access-specifier: [C++ class.derived]
- /// 'private'
- /// 'protected'
- /// 'public'
- AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
- switch (Tok.getKind()) {
- default: return AS_none;
- case tok::kw_private: return AS_private;
- case tok::kw_protected: return AS_protected;
- case tok::kw_public: return AS_public;
- }
- }
- /// \brief If the given declarator has any parts for which parsing has to be
- /// delayed, e.g., default arguments or an exception-specification, create a
- /// late-parsed method declaration record to handle the parsing at the end of
- /// the class definition.
- void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo,
- Decl *ThisDecl) {
- DeclaratorChunk::FunctionTypeInfo &FTI
- = DeclaratorInfo.getFunctionTypeInfo();
- // If there was a late-parsed exception-specification, we'll need a
- // late parse
- bool NeedLateParse = FTI.getExceptionSpecType() == EST_Unparsed;
- if (!NeedLateParse) {
- // Look ahead to see if there are any default args
- for (unsigned ParamIdx = 0; ParamIdx < FTI.NumParams; ++ParamIdx) {
- auto Param = cast<ParmVarDecl>(FTI.Params[ParamIdx].Param);
- if (Param->hasUnparsedDefaultArg()) {
- NeedLateParse = true;
- break;
- }
- }
- }
- if (NeedLateParse) {
- // Push this method onto the stack of late-parsed method
- // declarations.
- auto LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
- getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
- LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
- // Stash the exception-specification tokens in the late-pased method.
- LateMethod->ExceptionSpecTokens = FTI.ExceptionSpecTokens;
- FTI.ExceptionSpecTokens = 0;
- // Push tokens for each parameter. Those that do not have
- // defaults will be NULL.
- LateMethod->DefaultArgs.reserve(FTI.NumParams);
- for (unsigned ParamIdx = 0; ParamIdx < FTI.NumParams; ++ParamIdx)
- LateMethod->DefaultArgs.push_back(LateParsedDefaultArgument(
- FTI.Params[ParamIdx].Param, FTI.Params[ParamIdx].DefaultArgTokens));
- }
- }
- /// isCXX11VirtSpecifier - Determine whether the given token is a C++11
- /// virt-specifier.
- ///
- /// virt-specifier:
- /// override
- /// final
- VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const {
- if (!getLangOpts().CPlusPlus || Tok.isNot(tok::identifier) || getLangOpts().HLSL) // HLSL Change: remove support for override and final
- return VirtSpecifiers::VS_None;
- IdentifierInfo *II = Tok.getIdentifierInfo();
- // Initialize the contextual keywords.
- if (!Ident_final) {
- Ident_final = &PP.getIdentifierTable().get("final");
- if (getLangOpts().MicrosoftExt)
- Ident_sealed = &PP.getIdentifierTable().get("sealed");
- Ident_override = &PP.getIdentifierTable().get("override");
- }
- if (II == Ident_override)
- return VirtSpecifiers::VS_Override;
- if (II == Ident_sealed)
- return VirtSpecifiers::VS_Sealed;
- if (II == Ident_final)
- return VirtSpecifiers::VS_Final;
- return VirtSpecifiers::VS_None;
- }
- /// ParseOptionalCXX11VirtSpecifierSeq - Parse a virt-specifier-seq.
- ///
- /// virt-specifier-seq:
- /// virt-specifier
- /// virt-specifier-seq virt-specifier
- void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS,
- bool IsInterface,
- SourceLocation FriendLoc) {
- while (true) {
- VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
- if (Specifier == VirtSpecifiers::VS_None)
- return;
- if (FriendLoc.isValid()) {
- Diag(Tok.getLocation(), diag::err_friend_decl_spec)
- << VirtSpecifiers::getSpecifierName(Specifier)
- << FixItHint::CreateRemoval(Tok.getLocation())
- << SourceRange(FriendLoc, FriendLoc);
- ConsumeToken();
- continue;
- }
- // C++ [class.mem]p8:
- // A virt-specifier-seq shall contain at most one of each virt-specifier.
- const char *PrevSpec = nullptr;
- if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
- Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
- << PrevSpec
- << FixItHint::CreateRemoval(Tok.getLocation());
- if (IsInterface && (Specifier == VirtSpecifiers::VS_Final ||
- Specifier == VirtSpecifiers::VS_Sealed)) {
- Diag(Tok.getLocation(), diag::err_override_control_interface)
- << VirtSpecifiers::getSpecifierName(Specifier);
- } else if (Specifier == VirtSpecifiers::VS_Sealed) {
- Diag(Tok.getLocation(), diag::ext_ms_sealed_keyword);
- } else {
- Diag(Tok.getLocation(),
- getLangOpts().CPlusPlus11
- ? diag::warn_cxx98_compat_override_control_keyword
- : diag::ext_override_control_keyword)
- << VirtSpecifiers::getSpecifierName(Specifier);
- }
- ConsumeToken();
- }
- }
- /// isCXX11FinalKeyword - Determine whether the next token is a C++11
- /// 'final' or Microsoft 'sealed' contextual keyword.
- bool Parser::isCXX11FinalKeyword() const {
- if (getLangOpts().HLSL) return false; // HLSL Change - HLSL does not support final/sealed/override.
- VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
- return Specifier == VirtSpecifiers::VS_Final ||
- Specifier == VirtSpecifiers::VS_Sealed;
- }
- /// \brief Parse a C++ member-declarator up to, but not including, the optional
- /// brace-or-equal-initializer or pure-specifier.
- bool Parser::ParseCXXMemberDeclaratorBeforeInitializer(
- Declarator &DeclaratorInfo, VirtSpecifiers &VS, ExprResult &BitfieldSize,
- LateParsedAttrList &LateParsedAttrs) {
- // member-declarator:
- // declarator pure-specifier[opt]
- // declarator brace-or-equal-initializer[opt]
- // identifier[opt] ':' constant-expression
- if (Tok.isNot(tok::colon))
- ParseDeclarator(DeclaratorInfo);
- else
- DeclaratorInfo.SetIdentifier(nullptr, Tok.getLocation());
- if (!DeclaratorInfo.isFunctionDeclarator() && TryConsumeToken(tok::colon)) {
- assert(DeclaratorInfo.isPastIdentifier() &&
- "don't know where identifier would go yet?");
- BitfieldSize = ParseConstantExpression();
- if (BitfieldSize.isInvalid())
- SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
- } else {
- ParseOptionalCXX11VirtSpecifierSeq(
- VS, getCurrentClass().IsInterface,
- DeclaratorInfo.getDeclSpec().getFriendSpecLoc());
- if (!VS.isUnset())
- MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq(DeclaratorInfo, VS);
- }
- // If a simple-asm-expr is present, parse it.
- if (Tok.is(tok::kw_asm)) {
- // HLSL Change Starts - disallow asm
- if (getLangOpts().HLSL) {
- Diag(Tok, diag::err_hlsl_reserved_keyword) << Tok.getName();
- SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
- return true;
- }
- // HLSL Change Ends
- SourceLocation Loc;
- ExprResult AsmLabel(ParseSimpleAsm(&Loc));
- if (AsmLabel.isInvalid())
- SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
- DeclaratorInfo.setAsmLabel(AsmLabel.get());
- DeclaratorInfo.SetRangeEnd(Loc);
- }
- // If attributes exist after the declarator, but before an '{', parse them.
- MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
- MaybeParseHLSLAttributes(DeclaratorInfo); // HLSL Change: Parse semantic on struct members
- // For compatibility with code written to older Clang, also accept a
- // virt-specifier *after* the GNU attributes.
- if (!getLangOpts().HLSL && BitfieldSize.isUnset() && VS.isUnset()) { // HLSL Change - disallowed
- ParseOptionalCXX11VirtSpecifierSeq(
- VS, getCurrentClass().IsInterface,
- DeclaratorInfo.getDeclSpec().getFriendSpecLoc());
- if (!VS.isUnset()) {
- // If we saw any GNU-style attributes that are known to GCC followed by a
- // virt-specifier, issue a GCC-compat warning.
- const AttributeList *Attr = DeclaratorInfo.getAttributes();
- while (Attr) {
- if (Attr->isKnownToGCC() && !Attr->isCXX11Attribute())
- Diag(Attr->getLoc(), diag::warn_gcc_attribute_location);
- Attr = Attr->getNext();
- }
- MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq(DeclaratorInfo, VS);
- }
- }
- // If this has neither a name nor a bit width, something has gone seriously
- // wrong. Skip until the semi-colon or }.
- if (!DeclaratorInfo.hasName() && BitfieldSize.isUnset()) {
- // If so, skip until the semi-colon or a }.
- SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
- return true;
- }
- return false;
- }
- /// \brief Look for declaration specifiers possibly occurring after C++11
- /// virt-specifier-seq and diagnose them.
- void Parser::MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq(
- Declarator &D,
- VirtSpecifiers &VS) {
- DeclSpec DS(AttrFactory);
- // GNU-style and C++11 attributes are not allowed here, but they will be
- // handled by the caller. Diagnose everything else.
- ParseTypeQualifierListOpt(DS, AR_NoAttributesParsed, false);
- D.ExtendWithDeclSpec(DS);
- if (D.isFunctionDeclarator()) {
- auto &Function = D.getFunctionTypeInfo();
- if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
- auto DeclSpecCheck = [&] (DeclSpec::TQ TypeQual,
- const char *FixItName,
- SourceLocation SpecLoc,
- unsigned* QualifierLoc) {
- FixItHint Insertion;
- if (DS.getTypeQualifiers() & TypeQual) {
- if (!(Function.TypeQuals & TypeQual)) {
- std::string Name(FixItName);
- Name += " ";
- Insertion = FixItHint::CreateInsertion(VS.getFirstLocation(), Name.c_str());
- Function.TypeQuals |= TypeQual;
- *QualifierLoc = SpecLoc.getRawEncoding();
- }
- Diag(SpecLoc, diag::err_declspec_after_virtspec)
- << FixItName
- << VirtSpecifiers::getSpecifierName(VS.getLastSpecifier())
- << FixItHint::CreateRemoval(SpecLoc)
- << Insertion;
- }
- };
- DeclSpecCheck(DeclSpec::TQ_const, "const", DS.getConstSpecLoc(),
- &Function.ConstQualifierLoc);
- DeclSpecCheck(DeclSpec::TQ_volatile, "volatile", DS.getVolatileSpecLoc(),
- &Function.VolatileQualifierLoc);
- DeclSpecCheck(DeclSpec::TQ_restrict, "restrict", DS.getRestrictSpecLoc(),
- &Function.RestrictQualifierLoc);
- }
- // Parse ref-qualifiers.
- bool RefQualifierIsLValueRef = true;
- SourceLocation RefQualifierLoc;
- if (ParseRefQualifier(RefQualifierIsLValueRef, RefQualifierLoc)) {
- const char *Name = (RefQualifierIsLValueRef ? "& " : "&& ");
- FixItHint Insertion = FixItHint::CreateInsertion(VS.getFirstLocation(), Name);
- Function.RefQualifierIsLValueRef = RefQualifierIsLValueRef;
- Function.RefQualifierLoc = RefQualifierLoc.getRawEncoding();
- Diag(RefQualifierLoc, diag::err_declspec_after_virtspec)
- << (RefQualifierIsLValueRef ? "&" : "&&")
- << VirtSpecifiers::getSpecifierName(VS.getLastSpecifier())
- << FixItHint::CreateRemoval(RefQualifierLoc)
- << Insertion;
- D.SetRangeEnd(RefQualifierLoc);
- }
- }
- }
- /// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
- ///
- /// member-declaration:
- /// decl-specifier-seq[opt] member-declarator-list[opt] ';'
- /// function-definition ';'[opt]
- /// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
- /// using-declaration [TODO]
- /// [C++0x] static_assert-declaration
- /// template-declaration
- /// [GNU] '__extension__' member-declaration
- ///
- /// member-declarator-list:
- /// member-declarator
- /// member-declarator-list ',' member-declarator
- ///
- /// member-declarator:
- /// declarator virt-specifier-seq[opt] pure-specifier[opt]
- /// declarator constant-initializer[opt]
- /// [C++11] declarator brace-or-equal-initializer[opt]
- /// identifier[opt] ':' constant-expression
- ///
- /// virt-specifier-seq:
- /// virt-specifier
- /// virt-specifier-seq virt-specifier
- ///
- /// virt-specifier:
- /// override
- /// final
- /// [MS] sealed
- ///
- /// pure-specifier:
- /// '= 0'
- ///
- /// constant-initializer:
- /// '=' constant-expression
- ///
- void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
- AttributeList *AccessAttrs,
- const ParsedTemplateInfo &TemplateInfo,
- ParsingDeclRAIIObject *TemplateDiags) {
- if (Tok.is(tok::at)) {
- if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
- Diag(Tok, diag::err_at_defs_cxx);
- else
- Diag(Tok, diag::err_at_in_class);
- ConsumeToken();
- SkipUntil(tok::r_brace, StopAtSemi);
- return;
- }
- // Turn on colon protection early, while parsing declspec, although there is
- // nothing to protect there. It prevents from false errors if error recovery
- // incorrectly determines where the declspec ends, as in the example:
- // struct A { enum class B { C }; };
- // const int C = 4;
- // struct D { A::B : C; };
- ColonProtectionRAIIObject X(*this);
- // Access declarations.
- bool MalformedTypeSpec = false;
- if (!TemplateInfo.Kind &&
- Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw___super)) {
- if (TryAnnotateCXXScopeToken())
- MalformedTypeSpec = true;
- bool isAccessDecl;
- if (Tok.isNot(tok::annot_cxxscope))
- isAccessDecl = false;
- else if (NextToken().is(tok::identifier))
- isAccessDecl = GetLookAheadToken(2).is(tok::semi);
- else
- isAccessDecl = NextToken().is(tok::kw_operator);
- if (isAccessDecl) {
- // Collect the scope specifier token we annotated earlier.
- CXXScopeSpec SS;
- ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
- /*EnteringContext=*/false);
- if (SS.isInvalid()) {
- SkipUntil(tok::semi);
- return;
- }
- // Try to parse an unqualified-id.
- SourceLocation TemplateKWLoc;
- UnqualifiedId Name;
- if (ParseUnqualifiedId(SS, false, true, true, ParsedType(),
- TemplateKWLoc, Name)) {
- SkipUntil(tok::semi);
- return;
- }
- // TODO: recover from mistakenly-qualified operator declarations.
- if (ExpectAndConsume(tok::semi, diag::err_expected_after,
- "access declaration")) {
- SkipUntil(tok::semi);
- return;
- }
- Actions.ActOnUsingDeclaration(getCurScope(), AS,
- /* HasUsingKeyword */ false,
- SourceLocation(),
- SS, Name,
- /* AttrList */ nullptr,
- /* HasTypenameKeyword */ false,
- SourceLocation());
- return;
- }
- }
- // static_assert-declaration. A templated static_assert declaration is
- // diagnosed in Parser::ParseSingleDeclarationAfterTemplate.
- if (!TemplateInfo.Kind &&
- Tok.isOneOf(tok::kw_static_assert, tok::kw__Static_assert)) {
- SourceLocation DeclEnd;
- ParseStaticAssertDeclaration(DeclEnd);
- return;
- }
- if (Tok.is(tok::kw_template)) {
- assert(!TemplateInfo.TemplateParams &&
- "Nested template improperly parsed?");
- // HLSL Change Start - disallow template members
- if (getLangOpts().HLSL) {
- Diag(Tok, diag::err_hlsl_reserved_keyword) << Tok.getName();
- SkipUntil(tok::r_brace, StopAtSemi);
- return;
- }
- // HLSL Change End
- SourceLocation DeclEnd;
- ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
- AS, AccessAttrs);
- return;
- }
- // Handle: member-declaration ::= '__extension__' member-declaration
- if (Tok.is(tok::kw___extension__)) {
- // __extension__ silences extension warnings in the subexpression.
- ExtensionRAIIObject O(Diags); // Use RAII to do this.
- ConsumeToken();
- return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
- TemplateInfo, TemplateDiags);
- }
- ParsedAttributesWithRange attrs(AttrFactory);
- ParsedAttributesWithRange FnAttrs(AttrFactory);
- // Optional C++11 attribute-specifier
- MaybeParseCXX11Attributes(attrs);
- MaybeParseHLSLAttributes(attrs); // HLSL Change
- // We need to keep these attributes for future diagnostic
- // before they are taken over by declaration specifier.
- FnAttrs.addAll(attrs.getList());
- FnAttrs.Range = attrs.Range;
- MaybeParseMicrosoftAttributes(attrs);
- if (Tok.is(tok::kw_using)) {
- // HLSL Change Starts
- if (getLangOpts().HLSL) {
- Diag(Tok, diag::err_hlsl_reserved_keyword) << Tok.getName();
- SkipUntil(tok::semi, StopBeforeMatch);
- return;
- }
- // HLSL Change Ends
- ProhibitAttributes(attrs);
- // Eat 'using'.
- SourceLocation UsingLoc = ConsumeToken();
- if (Tok.is(tok::kw_namespace)) {
- Diag(UsingLoc, diag::err_using_namespace_in_class);
- SkipUntil(tok::semi, StopBeforeMatch);
- } else {
- SourceLocation DeclEnd;
- // Otherwise, it must be a using-declaration or an alias-declaration.
- ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
- UsingLoc, DeclEnd, AS);
- }
- return;
- }
- // Hold late-parsed attributes so we can attach a Decl to them later.
- LateParsedAttrList CommonLateParsedAttrs;
- // decl-specifier-seq:
- // Parse the common declaration-specifiers piece.
- ParsingDeclSpec DS(*this, TemplateDiags);
- DS.takeAttributesFrom(attrs);
- if (MalformedTypeSpec)
- DS.SetTypeSpecError();
- ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class,
- &CommonLateParsedAttrs);
- // Turn off colon protection that was set for declspec.
- X.restore();
- // If we had a free-standing type definition with a missing semicolon, we
- // may get this far before the problem becomes obvious.
- if (DS.hasTagDefinition() &&
- TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate &&
- DiagnoseMissingSemiAfterTagDefinition(DS, AS, DSC_class,
- &CommonLateParsedAttrs))
- return;
- MultiTemplateParamsArg TemplateParams(
- TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data()
- : nullptr,
- TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
- if (TryConsumeToken(tok::semi)) {
- if (DS.isFriendSpecified())
- ProhibitAttributes(FnAttrs);
- Decl *TheDecl =
- Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams);
- DS.complete(TheDecl);
- return;
- }
- ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
- VirtSpecifiers VS;
- // Hold late-parsed attributes so we can attach a Decl to them later.
- LateParsedAttrList LateParsedAttrs;
- SourceLocation EqualLoc;
- SourceLocation PureSpecLoc;
- auto TryConsumePureSpecifier = [&] (bool AllowDefinition) {
- if (Tok.isNot(tok::equal))
- return false;
- auto &Zero = NextToken();
- SmallString<8> Buffer;
- if (Zero.isNot(tok::numeric_constant) || Zero.getLength() != 1 ||
- PP.getSpelling(Zero, Buffer) != "0")
- return false;
- auto &After = GetLookAheadToken(2);
- if (!After.isOneOf(tok::semi, tok::comma) &&
- !(AllowDefinition &&
- After.isOneOf(tok::l_brace, tok::colon, tok::kw_try)))
- return false;
- EqualLoc = ConsumeToken();
- PureSpecLoc = ConsumeToken();
- return true;
- };
- SmallVector<Decl *, 8> DeclsInGroup;
- ExprResult BitfieldSize;
- bool ExpectSemi = true;
- // Parse the first declarator.
- if (ParseCXXMemberDeclaratorBeforeInitializer(
- DeclaratorInfo, VS, BitfieldSize, LateParsedAttrs)) {
- TryConsumeToken(tok::semi);
- return;
- }
- // Check for a member function definition.
- if (BitfieldSize.isUnset()) {
- // MSVC permits pure specifier on inline functions defined at class scope.
- // Hence check for =0 before checking for function definition.
- if (getLangOpts().MicrosoftExt && DeclaratorInfo.isDeclarationOfFunction())
- TryConsumePureSpecifier(/*AllowDefinition*/ true);
- FunctionDefinitionKind DefinitionKind = FDK_Declaration;
- // function-definition:
- //
- // In C++11, a non-function declarator followed by an open brace is a
- // braced-init-list for an in-class member initialization, not an
- // erroneous function definition.
- if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus11) {
- DefinitionKind = FDK_Definition;
- } else if (DeclaratorInfo.isFunctionDeclarator()) {
- if (Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try)) {
- DefinitionKind = FDK_Definition;
- } else if (Tok.is(tok::equal)) {
- const Token &KW = NextToken();
- if (KW.is(tok::kw_default))
- DefinitionKind = FDK_Defaulted;
- else if (KW.is(tok::kw_delete))
- DefinitionKind = FDK_Deleted;
- }
- }
- DeclaratorInfo.setFunctionDefinitionKind(DefinitionKind);
- // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
- // to a friend declaration, that declaration shall be a definition.
- if (DeclaratorInfo.isFunctionDeclarator() &&
- DefinitionKind != FDK_Definition && DS.isFriendSpecified()) {
- // Diagnose attributes that appear before decl specifier:
- // [[]] friend int foo();
- ProhibitAttributes(FnAttrs);
- }
- if (DefinitionKind != FDK_Declaration) {
- if (!DeclaratorInfo.isFunctionDeclarator()) {
- Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
- ConsumeBrace();
- SkipUntil(tok::r_brace);
- // Consume the optional ';'
- TryConsumeToken(tok::semi);
- return;
- }
- if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
- Diag(DeclaratorInfo.getIdentifierLoc(),
- diag::err_function_declared_typedef);
- // Recover by treating the 'typedef' as spurious.
- DS.ClearStorageClassSpecs();
- }
- Decl *FunDecl =
- ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
- VS, PureSpecLoc);
- if (FunDecl) {
- for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
- CommonLateParsedAttrs[i]->addDecl(FunDecl);
- }
- for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
- LateParsedAttrs[i]->addDecl(FunDecl);
- }
- }
- LateParsedAttrs.clear();
- // Consume the ';' - it's optional unless we have a delete or default
- if (Tok.is(tok::semi))
- ConsumeExtraSemi(AfterMemberFunctionDefinition);
- return;
- }
- }
- // member-declarator-list:
- // member-declarator
- // member-declarator-list ',' member-declarator
- while (1) {
- InClassInitStyle HasInClassInit = ICIS_NoInit;
- bool HasStaticInitializer = false;
- if (Tok.isOneOf(tok::equal, tok::l_brace) && PureSpecLoc.isInvalid()) {
- // HLSL Change Begin - only allow member default for static const.
- if (DS.getStorageClassSpec() != DeclSpec::SCS_static ||
- !(DS.getTypeQualifiers() & Qualifiers::Const))
- Diag(Tok, diag::err_hlsl_unsupported_member_default);
- // HLSL Change End
- if (BitfieldSize.get()) {
- Diag(Tok, diag::err_bitfield_member_init);
- SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
- } else if (DeclaratorInfo.isDeclarationOfFunction()) {
- // It's a pure-specifier.
- if (!TryConsumePureSpecifier(/*AllowFunctionDefinition*/ false))
- // Parse it as an expression so that Sema can diagnose it.
- HasStaticInitializer = true;
- } else if (DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
- DeclSpec::SCS_static &&
- DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
- DeclSpec::SCS_typedef &&
- !DS.isFriendSpecified()) {
- // It's a default member initializer.
- HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit;
- } else {
- HasStaticInitializer = true;
- }
- }
- // NOTE: If Sema is the Action module and declarator is an instance field,
- // this call will *not* return the created decl; It will return null.
- // See Sema::ActOnCXXMemberDeclarator for details.
- NamedDecl *ThisDecl = nullptr;
- if (DS.isFriendSpecified()) {
- assert(!getLangOpts().HLSL && "unreachable in HLSL"); // HLSL Change
- // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
- // to a friend declaration, that declaration shall be a definition.
- //
- // Diagnose attributes that appear in a friend member function declarator:
- // friend int foo [[]] ();
- SmallVector<SourceRange, 4> Ranges;
- DeclaratorInfo.getCXX11AttributeRanges(Ranges);
- for (SmallVectorImpl<SourceRange>::iterator I = Ranges.begin(),
- E = Ranges.end(); I != E; ++I)
- Diag((*I).getBegin(), diag::err_attributes_not_allowed) << *I;
- ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
- TemplateParams);
- } else {
- ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
- DeclaratorInfo,
- TemplateParams,
- BitfieldSize.get(),
- VS, HasInClassInit);
- if (VarTemplateDecl *VT =
- ThisDecl ? dyn_cast<VarTemplateDecl>(ThisDecl) : nullptr)
- // Re-direct this decl to refer to the templated decl so that we can
- // initialize it.
- ThisDecl = VT->getTemplatedDecl();
- if (ThisDecl && AccessAttrs)
- Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs);
- }
- // Error recovery might have converted a non-static member into a static
- // member.
- if (HasInClassInit != ICIS_NoInit &&
- DeclaratorInfo.getDeclSpec().getStorageClassSpec() ==
- DeclSpec::SCS_static) {
- HasInClassInit = ICIS_NoInit;
- HasStaticInitializer = true;
- }
- if (ThisDecl && PureSpecLoc.isValid())
- Actions.ActOnPureSpecifier(ThisDecl, PureSpecLoc);
- // Handle the initializer.
- if (HasInClassInit != ICIS_NoInit) {
- // The initializer was deferred; parse it and cache the tokens.
- Diag(Tok, getLangOpts().CPlusPlus11
- ? diag::warn_cxx98_compat_nonstatic_member_init
- : diag::ext_nonstatic_member_init);
- if (DeclaratorInfo.isArrayOfUnknownBound()) {
- // C++11 [dcl.array]p3: An array bound may also be omitted when the
- // declarator is followed by an initializer.
- //
- // A brace-or-equal-initializer for a member-declarator is not an
- // initializer in the grammar, so this is ill-formed.
- Diag(Tok, diag::err_incomplete_array_member_init);
- SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
- // Avoid later warnings about a class member of incomplete type.
- if (ThisDecl)
- ThisDecl->setInvalidDecl();
- } else
- ParseCXXNonStaticMemberInitializer(ThisDecl);
- } else if (HasStaticInitializer) {
- // Normal initializer.
- ExprResult Init = ParseCXXMemberInitializer(
- ThisDecl, DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
- if (Init.isInvalid())
- SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
- else if (ThisDecl)
- Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(),
- DS.containsPlaceholderType());
- } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static)
- // No initializer.
- Actions.ActOnUninitializedDecl(ThisDecl, DS.containsPlaceholderType());
- if (ThisDecl) {
- if (!ThisDecl->isInvalidDecl()) {
- // Set the Decl for any late parsed attributes
- for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i)
- CommonLateParsedAttrs[i]->addDecl(ThisDecl);
- for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i)
- LateParsedAttrs[i]->addDecl(ThisDecl);
- }
- Actions.FinalizeDeclaration(ThisDecl);
- DeclsInGroup.push_back(ThisDecl);
- if (DeclaratorInfo.isFunctionDeclarator() &&
- DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
- DeclSpec::SCS_typedef)
- HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
- }
- LateParsedAttrs.clear();
- DeclaratorInfo.complete(ThisDecl);
- // If we don't have a comma, it is either the end of the list (a ';')
- // or an error, bail out.
- SourceLocation CommaLoc;
- if (!TryConsumeToken(tok::comma, CommaLoc))
- break;
- if (Tok.isAtStartOfLine() &&
- !MightBeDeclarator(Declarator::MemberContext)) {
- // This comma was followed by a line-break and something which can't be
- // the start of a declarator. The comma was probably a typo for a
- // semicolon.
- Diag(CommaLoc, diag::err_expected_semi_declaration)
- << FixItHint::CreateReplacement(CommaLoc, ";");
- ExpectSemi = false;
- break;
- }
- // Parse the next declarator.
- DeclaratorInfo.clear();
- VS.clear();
- BitfieldSize = ExprResult(/*Invalid=*/false);
- EqualLoc = PureSpecLoc = SourceLocation();
- DeclaratorInfo.setCommaLoc(CommaLoc);
- // GNU attributes are allowed before the second and subsequent declarator.
- MaybeParseGNUAttributes(DeclaratorInfo);
- MaybeParseHLSLAttributes(DeclaratorInfo); // HLSL Change
- if (ParseCXXMemberDeclaratorBeforeInitializer(
- DeclaratorInfo, VS, BitfieldSize, LateParsedAttrs))
- break;
- }
- if (ExpectSemi &&
- ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
- // Skip to end of block or statement.
- SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
- // If we stopped at a ';', eat it.
- TryConsumeToken(tok::semi);
- return;
- }
- Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
- }
- /// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer.
- /// Also detect and reject any attempted defaulted/deleted function definition.
- /// The location of the '=', if any, will be placed in EqualLoc.
- ///
- /// This does not check for a pure-specifier; that's handled elsewhere.
- ///
- /// brace-or-equal-initializer:
- /// '=' initializer-expression
- /// braced-init-list
- ///
- /// initializer-clause:
- /// assignment-expression
- /// braced-init-list
- ///
- /// defaulted/deleted function-definition:
- /// '=' 'default'
- /// '=' 'delete'
- ///
- /// Prior to C++0x, the assignment-expression in an initializer-clause must
- /// be a constant-expression.
- ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
- SourceLocation &EqualLoc) {
- assert(Tok.isOneOf(tok::equal, tok::l_brace)
- && "Data member initializer not starting with '=' or '{'");
- EnterExpressionEvaluationContext Context(Actions,
- Sema::PotentiallyEvaluated,
- D);
- if (TryConsumeToken(tok::equal, EqualLoc)) {
- if (Tok.is(tok::kw_delete)) {
- // In principle, an initializer of '= delete p;' is legal, but it will
- // never type-check. It's better to diagnose it as an ill-formed expression
- // than as an ill-formed deleted non-function member.
- // An initializer of '= delete p, foo' will never be parsed, because
- // a top-level comma always ends the initializer expression.
- const Token &Next = NextToken();
- if (IsFunction || Next.isOneOf(tok::semi, tok::comma, tok::eof)) {
- if (IsFunction)
- Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
- << 1 /* delete */;
- else
- Diag(ConsumeToken(), diag::err_deleted_non_function);
- return ExprError();
- }
- } else if (Tok.is(tok::kw_default)) {
- if (IsFunction)
- Diag(Tok, diag::err_default_delete_in_multiple_declaration)
- << 0 /* default */;
- else
- Diag(ConsumeToken(), diag::err_default_special_members);
- return ExprError();
- }
- }
- if (const auto *PD = dyn_cast_or_null<MSPropertyDecl>(D)) {
- Diag(Tok, diag::err_ms_property_initializer) << PD;
- return ExprError();
- }
- return ParseInitializer();
- }
- void Parser::SkipCXXMemberSpecification(SourceLocation RecordLoc,
- SourceLocation AttrFixitLoc,
- unsigned TagType, Decl *TagDecl) {
- // Skip the optional 'final' keyword.
- if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
- assert(isCXX11FinalKeyword() && "not a class definition");
- ConsumeToken();
- // Diagnose any C++11 attributes after 'final' keyword.
- // We deliberately discard these attributes.
- ParsedAttributesWithRange Attrs(AttrFactory);
- CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc);
- // This can only happen if we had malformed misplaced attributes;
- // we only get called if there is a colon or left-brace after the
- // attributes.
- if (Tok.isNot(tok::colon) && Tok.isNot(tok::l_brace))
- return;
- }
- // Skip the base clauses. This requires actually parsing them, because
- // otherwise we can't be sure where they end (a left brace may appear
- // within a template argument).
- if (Tok.is(tok::colon)) {
- // Enter the scope of the class so that we can correctly parse its bases.
- ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
- ParsingClassDefinition ParsingDef(*this, TagDecl, /*NonNestedClass*/ true,
- TagType == DeclSpec::TST_interface);
- auto OldContext =
- Actions.ActOnTagStartSkippedDefinition(getCurScope(), TagDecl);
- // Parse the bases but don't attach them to the class.
- ParseBaseClause(nullptr);
- Actions.ActOnTagFinishSkippedDefinition(OldContext);
- if (!Tok.is(tok::l_brace)) {
- Diag(PP.getLocForEndOfToken(PrevTokLocation),
- diag::err_expected_lbrace_after_base_specifiers);
- return;
- }
- }
- // Skip the body.
- assert(Tok.is(tok::l_brace));
- BalancedDelimiterTracker T(*this, tok::l_brace);
- T.consumeOpen();
- T.skipToEnd();
- // Parse and discard any trailing attributes.
- ParsedAttributes Attrs(AttrFactory);
- if (Tok.is(tok::kw___attribute))
- MaybeParseGNUAttributes(Attrs);
- }
- /// ParseCXXMemberSpecification - Parse the class definition.
- ///
- /// member-specification:
- /// member-declaration member-specification[opt]
- /// access-specifier ':' member-specification[opt]
- ///
- void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
- SourceLocation AttrFixitLoc,
- ParsedAttributesWithRange &Attrs,
- unsigned TagType, Decl *TagDecl) {
- assert((TagType == DeclSpec::TST_struct ||
- TagType == DeclSpec::TST_interface ||
- TagType == DeclSpec::TST_union ||
- TagType == DeclSpec::TST_class) && "Invalid TagType!");
- PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
- "parsing struct/union/class body");
- // Determine whether this is a non-nested class. Note that local
- // classes are *not* considered to be nested classes.
- bool NonNestedClass = true;
- if (!ClassStack.empty()) {
- for (const Scope *S = getCurScope(); S; S = S->getParent()) {
- if (S->isClassScope()) {
- // We're inside a class scope, so this is a nested class.
- NonNestedClass = false;
- // HLSL Change Starts
- if (getLangOpts().HLSL) {
- Diag(RecordLoc, diag::err_hlsl_unsupported_construct) << "nested class";
- break;
- } else {
- // HLSL Change Ends - succeeding block is now conditional
- // The Microsoft extension __interface does not permit nested classes.
- if (getCurrentClass().IsInterface) {
- Diag(RecordLoc, diag::err_invalid_member_in_interface)
- << /*ErrorType=*/6
- << (isa<NamedDecl>(TagDecl)
- ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString()
- : "(anonymous)");
- }
- } // HLSL Change - close conditional
- break;
- }
- if ((S->getFlags() & Scope::FnScope))
- // If we're in a function or function template then this is a local
- // class rather than a nested class.
- break;
- }
- }
- // Enter a scope for the class.
- ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
- // Note that we are parsing a new (potentially-nested) class definition.
- ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass,
- TagType == DeclSpec::TST_interface);
- if (TagDecl)
- Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
- SourceLocation FinalLoc;
- bool IsFinalSpelledSealed = false;
- // Parse the optional 'final' keyword.
- if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
- VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(Tok);
- assert((Specifier == VirtSpecifiers::VS_Final ||
- Specifier == VirtSpecifiers::VS_Sealed) &&
- "not a class definition");
- FinalLoc = ConsumeToken();
- IsFinalSpelledSealed = Specifier == VirtSpecifiers::VS_Sealed;
- if (TagType == DeclSpec::TST_interface)
- Diag(FinalLoc, diag::err_override_control_interface)
- << VirtSpecifiers::getSpecifierName(Specifier);
- else if (Specifier == VirtSpecifiers::VS_Final)
- Diag(FinalLoc, getLangOpts().CPlusPlus11
- ? diag::warn_cxx98_compat_override_control_keyword
- : diag::ext_override_control_keyword)
- << VirtSpecifiers::getSpecifierName(Specifier);
- else if (Specifier == VirtSpecifiers::VS_Sealed)
- Diag(FinalLoc, diag::ext_ms_sealed_keyword);
- // Parse any C++11 attributes after 'final' keyword.
- // These attributes are not allowed to appear here,
- // and the only possible place for them to appertain
- // to the class would be between class-key and class-name.
- CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc);
- // ParseClassSpecifier() does only a superficial check for attributes before
- // deciding to call this method. For example, for
- // `class C final alignas ([l) {` it will decide that this looks like a
- // misplaced attribute since it sees `alignas '(' ')'`. But the actual
- // attribute parsing code will try to parse the '[' as a constexpr lambda
- // and consume enough tokens that the alignas parsing code will eat the
- // opening '{'. So bail out if the next token isn't one we expect.
- if (!Tok.is(tok::colon) && !Tok.is(tok::l_brace)) {
- if (TagDecl)
- Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
- return;
- }
- }
- if (Tok.is(tok::colon)) {
- ParseBaseClause(TagDecl);
- if (!Tok.is(tok::l_brace)) {
- bool SuggestFixIt = false;
- SourceLocation BraceLoc = PP.getLocForEndOfToken(PrevTokLocation);
- if (Tok.isAtStartOfLine()) {
- switch (Tok.getKind()) {
- case tok::kw_private:
- case tok::kw_protected:
- case tok::kw_public:
- SuggestFixIt = NextToken().getKind() == tok::colon;
- break;
- case tok::kw_static_assert:
- case tok::r_brace:
- case tok::kw_using:
- // base-clause can have simple-template-id; 'template' can't be there
- case tok::kw_template:
- SuggestFixIt = true;
- break;
- case tok::identifier:
- SuggestFixIt = isConstructorDeclarator(true);
- break;
- default:
- SuggestFixIt = isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false);
- break;
- }
- }
- DiagnosticBuilder LBraceDiag =
- Diag(BraceLoc, diag::err_expected_lbrace_after_base_specifiers);
- if (SuggestFixIt) {
- LBraceDiag << FixItHint::CreateInsertion(BraceLoc, " {");
- // Try recovering from missing { after base-clause.
- PP.EnterToken(Tok);
- Tok.setKind(tok::l_brace);
- } else {
- if (TagDecl)
- Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
- return;
- }
- }
- }
- assert(Tok.is(tok::l_brace));
- BalancedDelimiterTracker T(*this, tok::l_brace);
- T.consumeOpen();
- if (TagDecl)
- Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
- IsFinalSpelledSealed,
- T.getOpenLocation());
- // C++ 11p3: Members of a class defined with the keyword class are private
- // by default. Members of a class defined with the keywords struct or union
- // are public by default.
- AccessSpecifier CurAS;
- if (TagType == DeclSpec::TST_class)
- CurAS = AS_private;
- else
- CurAS = AS_public;
- // HLSL Change Starts
- if (getLangOpts().HLSL)
- CurAS = AS_public; // HLSL only has public
- // HLSL Change Ends
- ParsedAttributes AccessAttrs(AttrFactory);
- if (TagDecl) {
- // While we still have something to read, read the member-declarations.
- while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
- // Each iteration of this loop reads one member-declaration.
- if (!getLangOpts().HLSL && getLangOpts().MicrosoftExt && Tok.isOneOf(tok::kw___if_exists, // HLSL Change - remove support
- tok::kw___if_not_exists)) {
- ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
- continue;
- }
- // Check for extraneous top-level semicolon.
- if (Tok.is(tok::semi)) {
- ConsumeExtraSemi(InsideStruct, TagType);
- continue;
- }
- if (!getLangOpts().HLSL && Tok.is(tok::annot_pragma_vis)) { // HLSL Change - remove support
- HandlePragmaVisibility();
- continue;
- }
- if (!getLangOpts().HLSL && Tok.is(tok::annot_pragma_pack)) { // HLSL Change - remove support
- HandlePragmaPack();
- continue;
- }
- if (!getLangOpts().HLSL && Tok.is(tok::annot_pragma_align)) { // HLSL Change - remove support
- HandlePragmaAlign();
- continue;
- }
- if (!getLangOpts().HLSL && Tok.is(tok::annot_pragma_openmp)) { // HLSL Change - remove support
- ParseOpenMPDeclarativeDirective();
- continue;
- }
- if (!getLangOpts().HLSL && Tok.is(tok::annot_pragma_ms_pointers_to_members)) { // HLSL Change - remove support
- HandlePragmaMSPointersToMembers();
- continue;
- }
- if (!getLangOpts().HLSL && Tok.is(tok::annot_pragma_ms_pragma)) { // HLSL Change - remove support
- HandlePragmaMSPragma();
- continue;
- }
- // If we see a namespace here, a close brace was missing somewhere.
- if (Tok.is(tok::kw_namespace)) {
- DiagnoseUnexpectedNamespace(cast<NamedDecl>(TagDecl));
- break;
- }
- AccessSpecifier AS = getAccessSpecifierIfPresent();
- if (AS != AS_none) {
- // HLSL Change Starts
- if (getLangOpts().HLSL) {
- Diag(Tok, diag::err_hlsl_reserved_keyword) << Tok.getName();
- ConsumeToken();
- if (Tok.is(tok::colon))
- ConsumeToken();
- continue;
- }
- // HLSL Change Ends
- // Current token is a C++ access specifier.
- CurAS = AS;
- SourceLocation ASLoc = Tok.getLocation();
- unsigned TokLength = Tok.getLength();
- ConsumeToken();
- AccessAttrs.clear();
- MaybeParseGNUAttributes(AccessAttrs);
- SourceLocation EndLoc;
- if (TryConsumeToken(tok::colon, EndLoc)) {
- } else if (TryConsumeToken(tok::semi, EndLoc)) {
- Diag(EndLoc, diag::err_expected)
- << tok::colon << FixItHint::CreateReplacement(EndLoc, ":");
- } else {
- EndLoc = ASLoc.getLocWithOffset(TokLength);
- Diag(EndLoc, diag::err_expected)
- << tok::colon << FixItHint::CreateInsertion(EndLoc, ":");
- }
- // The Microsoft extension __interface does not permit non-public
- // access specifiers.
- if (TagType == DeclSpec::TST_interface && CurAS != AS_public) {
- Diag(ASLoc, diag::err_access_specifier_interface)
- << (CurAS == AS_protected);
- }
- if (Actions.ActOnAccessSpecifier(AS, ASLoc, EndLoc,
- AccessAttrs.getList())) {
- // found another attribute than only annotations
- AccessAttrs.clear();
- }
- continue;
- }
- // Parse all the comma separated declarators.
- ParseCXXClassMemberDeclaration(CurAS, AccessAttrs.getList());
- }
- T.consumeClose();
- } else {
- SkipUntil(tok::r_brace);
- }
- // If attributes exist after class contents, parse them.
- ParsedAttributes attrs(AttrFactory);
- MaybeParseGNUAttributes(attrs);
- if (TagDecl)
- Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
- T.getOpenLocation(),
- T.getCloseLocation(),
- attrs.getList());
- // C++11 [class.mem]p2:
- // Within the class member-specification, the class is regarded as complete
- // within function bodies, default arguments, exception-specifications, and
- // brace-or-equal-initializers for non-static data members (including such
- // things in nested classes).
- if (TagDecl && NonNestedClass) {
- // We are not inside a nested class. This class and its nested classes
- // are complete and we can parse the delayed portions of method
- // declarations and the lexed inline method definitions, along with any
- // delayed attributes.
- SourceLocation SavedPrevTokLocation = PrevTokLocation;
- ParseLexedAttributes(getCurrentClass());
- ParseLexedMethodDeclarations(getCurrentClass());
- // We've finished with all pending member declarations.
- Actions.ActOnFinishCXXMemberDecls();
- ParseLexedMemberInitializers(getCurrentClass());
- ParseLexedMethodDefs(getCurrentClass());
- PrevTokLocation = SavedPrevTokLocation;
- // We've finished parsing everything, including default argument
- // initializers.
- Actions.ActOnFinishCXXMemberDefaultArgs(TagDecl);
- }
- if (TagDecl)
- Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
- T.getCloseLocation());
- // Leave the class scope.
- ParsingDef.Pop();
- ClassScope.Exit();
- }
- void Parser::DiagnoseUnexpectedNamespace(NamedDecl *D) {
- assert(Tok.is(tok::kw_namespace));
- // FIXME: Suggest where the close brace should have gone by looking
- // at indentation changes within the definition body.
- Diag(D->getLocation(),
- diag::err_missing_end_of_definition) << D;
- Diag(Tok.getLocation(),
- diag::note_missing_end_of_definition_before) << D;
- // Push '};' onto the token stream to recover.
- PP.EnterToken(Tok);
- Tok.startToken();
- Tok.setLocation(PP.getLocForEndOfToken(PrevTokLocation));
- Tok.setKind(tok::semi);
- PP.EnterToken(Tok);
- Tok.setKind(tok::r_brace);
- }
- /// ParseConstructorInitializer - Parse a C++ constructor initializer,
- /// which explicitly initializes the members or base classes of a
- /// class (C++ [class.base.init]). For example, the three initializers
- /// after the ':' in the Derived constructor below:
- ///
- /// @code
- /// class Base { };
- /// class Derived : Base {
- /// int x;
- /// float f;
- /// public:
- /// Derived(float f) : Base(), x(17), f(f) { }
- /// };
- /// @endcode
- ///
- /// [C++] ctor-initializer:
- /// ':' mem-initializer-list
- ///
- /// [C++] mem-initializer-list:
- /// mem-initializer ...[opt]
- /// mem-initializer ...[opt] , mem-initializer-list
- void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
- assert(Tok.is(tok::colon) &&
- "Constructor initializer always starts with ':'");
- // HLSL Change Starts
- if (getLangOpts().HLSL) {
- Diag(Tok, diag::err_hlsl_unsupported_construct) << "constructor initializer";
- return;
- }
- // HLSL Change Ends
- // Poison the SEH identifiers so they are flagged as illegal in constructor
- // initializers.
- PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
- SourceLocation ColonLoc = ConsumeToken();
- SmallVector<CXXCtorInitializer*, 4> MemInitializers;
- bool AnyErrors = false;
- do {
- if (Tok.is(tok::code_completion)) {
- Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
- MemInitializers);
- return cutOffParsing();
- } else {
- MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
- if (!MemInit.isInvalid())
- MemInitializers.push_back(MemInit.get());
- else
- AnyErrors = true;
- }
-
- if (Tok.is(tok::comma))
- ConsumeToken();
- else if (Tok.is(tok::l_brace))
- break;
- // If the next token looks like a base or member initializer, assume that
- // we're just missing a comma.
- else if (Tok.isOneOf(tok::identifier, tok::coloncolon)) {
- SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
- Diag(Loc, diag::err_ctor_init_missing_comma)
- << FixItHint::CreateInsertion(Loc, ", ");
- } else {
- // Skip over garbage, until we get to '{'. Don't eat the '{'.
- Diag(Tok.getLocation(), diag::err_expected_either) << tok::l_brace
- << tok::comma;
- SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
- break;
- }
- } while (true);
- Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, MemInitializers,
- AnyErrors);
- }
- /// ParseMemInitializer - Parse a C++ member initializer, which is
- /// part of a constructor initializer that explicitly initializes one
- /// member or base class (C++ [class.base.init]). See
- /// ParseConstructorInitializer for an example.
- ///
- /// [C++] mem-initializer:
- /// mem-initializer-id '(' expression-list[opt] ')'
- /// [C++0x] mem-initializer-id braced-init-list
- ///
- /// [C++] mem-initializer-id:
- /// '::'[opt] nested-name-specifier[opt] class-name
- /// identifier
- MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
- assert(!getLangOpts().HLSL && "unreachable from HLSL - without constructors, no mem initializers"); // HLSL Change
- // parse '::'[opt] nested-name-specifier[opt]
- CXXScopeSpec SS;
- ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
- ParsedType TemplateTypeTy;
- if (Tok.is(tok::annot_template_id)) {
- TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
- if (TemplateId->Kind == TNK_Type_template ||
- TemplateId->Kind == TNK_Dependent_template_name) {
- AnnotateTemplateIdTokenAsType();
- assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
- TemplateTypeTy = getTypeAnnotation(Tok);
- }
- }
- // Uses of decltype will already have been converted to annot_decltype by
- // ParseOptionalCXXScopeSpecifier at this point.
- if (!TemplateTypeTy && Tok.isNot(tok::identifier)
- && Tok.isNot(tok::annot_decltype)) {
- Diag(Tok, diag::err_expected_member_or_base_name);
- return true;
- }
- IdentifierInfo *II = nullptr;
- DeclSpec DS(AttrFactory);
- SourceLocation IdLoc = Tok.getLocation();
- if (Tok.is(tok::annot_decltype)) {
- // Get the decltype expression, if there is one.
- ParseDecltypeSpecifier(DS);
- } else {
- if (Tok.is(tok::identifier))
- // Get the identifier. This may be a member name or a class name,
- // but we'll let the semantic analysis determine which it is.
- II = Tok.getIdentifierInfo();
- ConsumeToken();
- }
- // Parse the '('.
- if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
- Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
- ExprResult InitList = ParseBraceInitializer();
- if (InitList.isInvalid())
- return true;
- SourceLocation EllipsisLoc;
- TryConsumeToken(tok::ellipsis, EllipsisLoc);
- return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
- TemplateTypeTy, DS, IdLoc,
- InitList.get(), EllipsisLoc);
- } else if(Tok.is(tok::l_paren)) {
- BalancedDelimiterTracker T(*this, tok::l_paren);
- T.consumeOpen();
- // Parse the optional expression-list.
- ExprVector ArgExprs;
- CommaLocsTy CommaLocs;
- if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
- SkipUntil(tok::r_paren, StopAtSemi);
- return true;
- }
- T.consumeClose();
- SourceLocation EllipsisLoc;
- TryConsumeToken(tok::ellipsis, EllipsisLoc);
- return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
- TemplateTypeTy, DS, IdLoc,
- T.getOpenLocation(), ArgExprs,
- T.getCloseLocation(), EllipsisLoc);
- }
- if (getLangOpts().CPlusPlus11)
- return Diag(Tok, diag::err_expected_either) << tok::l_paren << tok::l_brace;
- else
- return Diag(Tok, diag::err_expected) << tok::l_paren;
- }
- /// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
- ///
- /// exception-specification:
- /// dynamic-exception-specification
- /// noexcept-specification
- ///
- /// noexcept-specification:
- /// 'noexcept'
- /// 'noexcept' '(' constant-expression ')'
- ExceptionSpecificationType
- Parser::tryParseExceptionSpecification(bool Delayed,
- SourceRange &SpecificationRange,
- SmallVectorImpl<ParsedType> &DynamicExceptions,
- SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
- ExprResult &NoexceptExpr,
- CachedTokens *&ExceptionSpecTokens) {
- ExceptionSpecificationType Result = EST_None;
- ExceptionSpecTokens = 0;
-
- // Handle delayed parsing of exception-specifications.
- if (Delayed) {
- if (Tok.isNot(tok::kw_throw) && Tok.isNot(tok::kw_noexcept))
- return EST_None;
- // HLSL Change Starts
- if (getLangOpts().HLSL) {
- assert(Tok.isNot(tok::kw_noexcept)); // not a keyword in the language.
- Diag(SpecificationRange.getBegin(), diag::err_hlsl_unsupported_construct) << "exception specification";
- return Result;
- }
- // HLSL Change Ends
- // Consume and cache the starting token.
- bool IsNoexcept = Tok.is(tok::kw_noexcept);
- Token StartTok = Tok;
- SpecificationRange = SourceRange(ConsumeToken());
- // Check for a '('.
- if (!Tok.is(tok::l_paren)) {
- // If this is a bare 'noexcept', we're done.
- if (IsNoexcept) {
- Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
- NoexceptExpr = 0;
- return EST_BasicNoexcept;
- }
-
- Diag(Tok, diag::err_expected_lparen_after) << "throw";
- return EST_DynamicNone;
- }
-
- // Cache the tokens for the exception-specification.
- ExceptionSpecTokens = new CachedTokens;
- ExceptionSpecTokens->push_back(StartTok); // 'throw' or 'noexcept'
- ExceptionSpecTokens->push_back(Tok); // '('
- SpecificationRange.setEnd(ConsumeParen()); // '('
- ConsumeAndStoreUntil(tok::r_paren, *ExceptionSpecTokens,
- /*StopAtSemi=*/true,
- /*ConsumeFinalToken=*/true);
- SpecificationRange.setEnd(Tok.getLocation());
- return EST_Unparsed;
- }
-
- // See if there's a dynamic specification.
- if (Tok.is(tok::kw_throw)) {
- Result = ParseDynamicExceptionSpecification(SpecificationRange,
- DynamicExceptions,
- DynamicExceptionRanges);
- assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
- "Produced different number of exception types and ranges.");
- // HLSL Change Starts
- if (getLangOpts().HLSL) {
- assert(Tok.isNot(tok::kw_noexcept)); // not a keyword in the language.
- Diag(SpecificationRange.getBegin(), diag::err_hlsl_unsupported_construct) << "exception specification";
- return Result;
- }
- // HLSL Change Ends
- }
- // HLSL Change Starts
- if (getLangOpts().HLSL) {
- assert(Tok.isNot(tok::kw_noexcept)); // not a keyword in the language.
- return Result;
- }
- // HLSL Change Ends
- // If there's no noexcept specification, we're done.
- if (Tok.isNot(tok::kw_noexcept))
- return Result;
- Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
- // If we already had a dynamic specification, parse the noexcept for,
- // recovery, but emit a diagnostic and don't store the results.
- SourceRange NoexceptRange;
- ExceptionSpecificationType NoexceptType = EST_None;
- SourceLocation KeywordLoc = ConsumeToken();
- if (Tok.is(tok::l_paren)) {
- // There is an argument.
- BalancedDelimiterTracker T(*this, tok::l_paren);
- T.consumeOpen();
- NoexceptType = EST_ComputedNoexcept;
- NoexceptExpr = ParseConstantExpression();
- T.consumeClose();
- // The argument must be contextually convertible to bool. We use
- // ActOnBooleanCondition for this purpose.
- if (!NoexceptExpr.isInvalid()) {
- NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
- NoexceptExpr.get());
- NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
- } else {
- NoexceptType = EST_None;
- }
- } else {
- // There is no argument.
- NoexceptType = EST_BasicNoexcept;
- NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
- }
- if (Result == EST_None) {
- SpecificationRange = NoexceptRange;
- Result = NoexceptType;
- // If there's a dynamic specification after a noexcept specification,
- // parse that and ignore the results.
- if (Tok.is(tok::kw_throw)) {
- Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
- ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
- DynamicExceptionRanges);
- }
- } else {
- Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
- }
- return Result;
- }
- static void diagnoseDynamicExceptionSpecification(
- Parser &P, const SourceRange &Range, bool IsNoexcept) {
- if (P.getLangOpts().CPlusPlus11 && !P.getLangOpts().HLSL) { // HLSL Change - don't try to fix this for HLSL
- const char *Replacement = IsNoexcept ? "noexcept" : "noexcept(false)";
- P.Diag(Range.getBegin(), diag::warn_exception_spec_deprecated) << Range;
- P.Diag(Range.getBegin(), diag::note_exception_spec_deprecated)
- << Replacement << FixItHint::CreateReplacement(Range, Replacement);
- }
- }
- /// ParseDynamicExceptionSpecification - Parse a C++
- /// dynamic-exception-specification (C++ [except.spec]).
- ///
- /// dynamic-exception-specification:
- /// 'throw' '(' type-id-list [opt] ')'
- /// [MS] 'throw' '(' '...' ')'
- ///
- /// type-id-list:
- /// type-id ... [opt]
- /// type-id-list ',' type-id ... [opt]
- ///
- ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
- SourceRange &SpecificationRange,
- SmallVectorImpl<ParsedType> &Exceptions,
- SmallVectorImpl<SourceRange> &Ranges) {
- assert(Tok.is(tok::kw_throw) && "expected throw");
- SpecificationRange.setBegin(ConsumeToken());
- BalancedDelimiterTracker T(*this, tok::l_paren);
- if (T.consumeOpen()) {
- Diag(Tok, diag::err_expected_lparen_after) << "throw";
- SpecificationRange.setEnd(SpecificationRange.getBegin());
- return EST_DynamicNone;
- }
- // Parse throw(...), a Microsoft extension that means "this function
- // can throw anything".
- if (Tok.is(tok::ellipsis)) {
- SourceLocation EllipsisLoc = ConsumeToken();
- if (!getLangOpts().MicrosoftExt)
- Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
- T.consumeClose();
- SpecificationRange.setEnd(T.getCloseLocation());
- diagnoseDynamicExceptionSpecification(*this, SpecificationRange, false);
- return EST_MSAny;
- }
- // Parse the sequence of type-ids.
- SourceRange Range;
- while (Tok.isNot(tok::r_paren)) {
- TypeResult Res(ParseTypeName(&Range));
- if (Tok.is(tok::ellipsis)) {
- // C++0x [temp.variadic]p5:
- // - In a dynamic-exception-specification (15.4); the pattern is a
- // type-id.
- SourceLocation Ellipsis = ConsumeToken();
- Range.setEnd(Ellipsis);
- if (!Res.isInvalid())
- Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
- }
- if (!Res.isInvalid()) {
- Exceptions.push_back(Res.get());
- Ranges.push_back(Range);
- }
- if (!TryConsumeToken(tok::comma))
- break;
- }
- T.consumeClose();
- SpecificationRange.setEnd(T.getCloseLocation());
- diagnoseDynamicExceptionSpecification(*this, SpecificationRange,
- Exceptions.empty());
- return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
- }
- /// ParseTrailingReturnType - Parse a trailing return type on a new-style
- /// function declaration.
- TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) {
- assert(!getLangOpts().HLSL && "unreachable from HLSL, no lambda or new-style function declaration support"); // HLSL Change
- assert(Tok.is(tok::arrow) && "expected arrow");
- ConsumeToken();
- return ParseTypeName(&Range, Declarator::TrailingReturnContext);
- }
- /// \brief We have just started parsing the definition of a new class,
- /// so push that class onto our stack of classes that is currently
- /// being parsed.
- Sema::ParsingClassState
- Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass,
- bool IsInterface) {
- assert((NonNestedClass || !ClassStack.empty()) &&
- "Nested class without outer class");
- ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface));
- return Actions.PushParsingClass();
- }
- /// \brief Deallocate the given parsed class and all of its nested
- /// classes.
- void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
- for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
- delete Class->LateParsedDeclarations[I];
- delete Class;
- }
- /// \brief Pop the top class of the stack of classes that are
- /// currently being parsed.
- ///
- /// This routine should be called when we have finished parsing the
- /// definition of a class, but have not yet popped the Scope
- /// associated with the class's definition.
- void Parser::PopParsingClass(Sema::ParsingClassState state) {
- assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
- Actions.PopParsingClass(state);
- ParsingClass *Victim = ClassStack.top();
- ClassStack.pop();
- if (Victim->TopLevelClass) {
- // Deallocate all of the nested classes of this class,
- // recursively: we don't need to keep any of this information.
- DeallocateParsedClasses(Victim);
- return;
- }
- assert(!ClassStack.empty() && "Missing top-level class?");
- if (Victim->LateParsedDeclarations.empty()) {
- // The victim is a nested class, but we will not need to perform
- // any processing after the definition of this class since it has
- // no members whose handling was delayed. Therefore, we can just
- // remove this nested class.
- DeallocateParsedClasses(Victim);
- return;
- }
- // This nested class has some members that will need to be processed
- // after the top-level class is completely defined. Therefore, add
- // it to the list of nested classes within its parent.
- assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
- ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
- Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
- }
- /// \brief Try to parse an 'identifier' which appears within an attribute-token.
- ///
- /// \return the parsed identifier on success, and 0 if the next token is not an
- /// attribute-token.
- ///
- /// C++11 [dcl.attr.grammar]p3:
- /// If a keyword or an alternative token that satisfies the syntactic
- /// requirements of an identifier is contained in an attribute-token,
- /// it is considered an identifier.
- IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) {
- switch (Tok.getKind()) {
- default:
- // Identifiers and keywords have identifier info attached.
- if (!Tok.isAnnotation()) {
- if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
- Loc = ConsumeToken();
- return II;
- }
- }
- return nullptr;
- case tok::ampamp: // 'and'
- case tok::pipe: // 'bitor'
- case tok::pipepipe: // 'or'
- case tok::caret: // 'xor'
- case tok::tilde: // 'compl'
- case tok::amp: // 'bitand'
- case tok::ampequal: // 'and_eq'
- case tok::pipeequal: // 'or_eq'
- case tok::caretequal: // 'xor_eq'
- case tok::exclaim: // 'not'
- case tok::exclaimequal: // 'not_eq'
- // Alternative tokens do not have identifier info, but their spelling
- // starts with an alphabetical character.
- SmallString<8> SpellingBuf;
- SourceLocation SpellingLoc =
- PP.getSourceManager().getSpellingLoc(Tok.getLocation());
- StringRef Spelling = PP.getSpelling(SpellingLoc, SpellingBuf);
- if (isLetter(Spelling[0])) {
- Loc = ConsumeToken();
- return &PP.getIdentifierTable().get(Spelling);
- }
- return nullptr;
- }
- }
- static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName,
- IdentifierInfo *ScopeName) {
- switch (AttributeList::getKind(AttrName, ScopeName,
- AttributeList::AS_CXX11)) {
- case AttributeList::AT_CarriesDependency:
- case AttributeList::AT_Deprecated:
- case AttributeList::AT_FallThrough:
- case AttributeList::AT_CXX11NoReturn: {
- return true;
- }
- default:
- return false;
- }
- }
- /// ParseCXX11AttributeArgs -- Parse a C++11 attribute-argument-clause.
- ///
- /// [C++11] attribute-argument-clause:
- /// '(' balanced-token-seq ')'
- ///
- /// [C++11] balanced-token-seq:
- /// balanced-token
- /// balanced-token-seq balanced-token
- ///
- /// [C++11] balanced-token:
- /// '(' balanced-token-seq ')'
- /// '[' balanced-token-seq ']'
- /// '{' balanced-token-seq '}'
- /// any token but '(', ')', '[', ']', '{', or '}'
- bool Parser::ParseCXX11AttributeArgs(IdentifierInfo *AttrName,
- SourceLocation AttrNameLoc,
- ParsedAttributes &Attrs,
- SourceLocation *EndLoc,
- IdentifierInfo *ScopeName,
- SourceLocation ScopeLoc) {
- assert(Tok.is(tok::l_paren) && "Not a C++11 attribute argument list");
- SourceLocation LParenLoc = Tok.getLocation();
- // If the attribute isn't known, we will not attempt to parse any
- // arguments.
- if (!hasAttribute(AttrSyntax::CXX, ScopeName, AttrName,
- getTargetInfo().getTriple(), getLangOpts())) {
- // Eat the left paren, then skip to the ending right paren.
- ConsumeParen();
- SkipUntil(tok::r_paren);
- return false;
- }
- if (ScopeName && ScopeName->getName() == "gnu")
- // GNU-scoped attributes have some special cases to handle GNU-specific
- // behaviors.
- ParseGNUAttributeArgs(AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
- ScopeLoc, AttributeList::AS_CXX11, nullptr);
- else {
- unsigned NumArgs =
- ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc,
- ScopeName, ScopeLoc, AttributeList::AS_CXX11);
-
- const AttributeList *Attr = Attrs.getList();
- if (Attr && IsBuiltInOrStandardCXX11Attribute(AttrName, ScopeName)) {
- // If the attribute is a standard or built-in attribute and we are
- // parsing an argument list, we need to determine whether this attribute
- // was allowed to have an argument list (such as [[deprecated]]), and how
- // many arguments were parsed (so we can diagnose on [[deprecated()]]).
- if (Attr->getMaxArgs() && !NumArgs) {
- // The attribute was allowed to have arguments, but none were provided
- // even though the attribute parsed successfully. This is an error.
- Diag(LParenLoc, diag::err_attribute_requires_arguments) << AttrName;
- } else if (!Attr->getMaxArgs()) {
- // The attribute parsed successfully, but was not allowed to have any
- // arguments. It doesn't matter whether any were provided -- the
- // presence of the argument list (even if empty) is diagnosed.
- Diag(LParenLoc, diag::err_cxx11_attribute_forbids_arguments)
- << AttrName
- << FixItHint::CreateRemoval(SourceRange(LParenLoc, *EndLoc));
- }
- }
- }
- return true;
- }
- /// ParseCXX11AttributeSpecifier - Parse a C++11 attribute-specifier.
- ///
- /// [C++11] attribute-specifier:
- /// '[' '[' attribute-list ']' ']'
- /// alignment-specifier
- ///
- /// [C++11] attribute-list:
- /// attribute[opt]
- /// attribute-list ',' attribute[opt]
- /// attribute '...'
- /// attribute-list ',' attribute '...'
- ///
- /// [C++11] attribute:
- /// attribute-token attribute-argument-clause[opt]
- ///
- /// [C++11] attribute-token:
- /// identifier
- /// attribute-scoped-token
- ///
- /// [C++11] attribute-scoped-token:
- /// attribute-namespace '::' identifier
- ///
- /// [C++11] attribute-namespace:
- /// identifier
- void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs,
- SourceLocation *endLoc) {
- if (Tok.is(tok::kw_alignas)) {
- Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
- ParseAlignmentSpecifier(attrs, endLoc);
- return;
- }
- assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
- && "Not a C++11 attribute list");
- Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
- ConsumeBracket();
- ConsumeBracket();
- llvm::SmallDenseMap<IdentifierInfo*, SourceLocation, 4> SeenAttrs;
- while (Tok.isNot(tok::r_square)) {
- // attribute not present
- if (TryConsumeToken(tok::comma))
- continue;
- SourceLocation ScopeLoc, AttrLoc;
- IdentifierInfo *ScopeName = nullptr, *AttrName = nullptr;
- AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
- if (!AttrName)
- // Break out to the "expected ']'" diagnostic.
- break;
- // scoped attribute
- if (TryConsumeToken(tok::coloncolon)) {
- ScopeName = AttrName;
- ScopeLoc = AttrLoc;
- AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
- if (!AttrName) {
- Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
- SkipUntil(tok::r_square, tok::comma, StopAtSemi | StopBeforeMatch);
- continue;
- }
- }
- bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName, ScopeName);
- bool AttrParsed = false;
- if (StandardAttr &&
- !SeenAttrs.insert(std::make_pair(AttrName, AttrLoc)).second)
- Diag(AttrLoc, diag::err_cxx11_attribute_repeated)
- << AttrName << SourceRange(SeenAttrs[AttrName]);
- // Parse attribute arguments
- if (Tok.is(tok::l_paren))
- AttrParsed = ParseCXX11AttributeArgs(AttrName, AttrLoc, attrs, endLoc,
- ScopeName, ScopeLoc);
- if (!AttrParsed)
- attrs.addNew(AttrName,
- SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc,
- AttrLoc),
- ScopeName, ScopeLoc, nullptr, 0, AttributeList::AS_CXX11);
- if (TryConsumeToken(tok::ellipsis))
- Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
- << AttrName->getName();
- }
- if (ExpectAndConsume(tok::r_square))
- SkipUntil(tok::r_square);
- if (endLoc)
- *endLoc = Tok.getLocation();
- if (ExpectAndConsume(tok::r_square))
- SkipUntil(tok::r_square);
- }
- /// ParseCXX11Attributes - Parse a C++11 attribute-specifier-seq.
- ///
- /// attribute-specifier-seq:
- /// attribute-specifier-seq[opt] attribute-specifier
- void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs,
- SourceLocation *endLoc) {
- assert(getLangOpts().CPlusPlus11 || getLangOpts().HLSL); // HLSL Change
- SourceLocation StartLoc = Tok.getLocation(), Loc;
- if (!endLoc)
- endLoc = &Loc;
- do {
- ParseCXX11AttributeSpecifier(attrs, endLoc);
- } while (isCXX11AttributeSpecifier());
- attrs.Range = SourceRange(StartLoc, *endLoc);
- }
- void Parser::DiagnoseAndSkipCXX11Attributes() {
- // Start and end location of an attribute or an attribute list.
- SourceLocation StartLoc = Tok.getLocation();
- SourceLocation EndLoc = SkipCXX11Attributes();
- if (EndLoc.isValid()) {
- SourceRange Range(StartLoc, EndLoc);
- Diag(StartLoc, diag::err_attributes_not_allowed)
- << Range;
- }
- }
- SourceLocation Parser::SkipCXX11Attributes() {
- SourceLocation EndLoc;
- if (!isCXX11AttributeSpecifier())
- return EndLoc;
- do {
- if (Tok.is(tok::l_square)) {
- BalancedDelimiterTracker T(*this, tok::l_square);
- T.consumeOpen();
- T.skipToEnd();
- EndLoc = T.getCloseLocation();
- } else {
- assert(Tok.is(tok::kw_alignas) && "not an attribute specifier");
- ConsumeToken();
- BalancedDelimiterTracker T(*this, tok::l_paren);
- if (!T.consumeOpen())
- T.skipToEnd();
- EndLoc = T.getCloseLocation();
- }
- } while (isCXX11AttributeSpecifier());
- return EndLoc;
- }
- /// ParseMicrosoftAttributes - Parse Microsoft attributes [Attr]
- ///
- /// [MS] ms-attribute:
- /// '[' token-seq ']'
- ///
- /// [MS] ms-attribute-seq:
- /// ms-attribute[opt]
- /// ms-attribute ms-attribute-seq
- void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
- SourceLocation *endLoc) {
- assert(!getLangOpts().HLSL && "unreachable code for HLSL - Microsoft extension parsing is disabled"); // HLSL Change
- assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
- do {
- // FIXME: If this is actually a C++11 attribute, parse it as one.
- BalancedDelimiterTracker T(*this, tok::l_square);
- T.consumeOpen();
- SkipUntil(tok::r_square, StopAtSemi | StopBeforeMatch);
- T.consumeClose();
- if (endLoc)
- *endLoc = T.getCloseLocation();
- } while (Tok.is(tok::l_square));
- }
- void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,
- AccessSpecifier& CurAS) {
- assert(!getLangOpts().HLSL && "unreachable code for HLSL - if-exists extension parsing skipped"); // HLSL Change
- IfExistsCondition Result;
- if (ParseMicrosoftIfExistsCondition(Result))
- return;
-
- BalancedDelimiterTracker Braces(*this, tok::l_brace);
- if (Braces.consumeOpen()) {
- Diag(Tok, diag::err_expected) << tok::l_brace;
- return;
- }
- switch (Result.Behavior) {
- case IEB_Parse:
- // Parse the declarations below.
- break;
-
- case IEB_Dependent:
- Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
- << Result.IsIfExists;
- // Fall through to skip.
-
- case IEB_Skip:
- Braces.skipToEnd();
- return;
- }
- while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
- // __if_exists, __if_not_exists can nest.
- if (Tok.isOneOf(tok::kw___if_exists, tok::kw___if_not_exists)) {
- ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
- continue;
- }
- // Check for extraneous top-level semicolon.
- if (Tok.is(tok::semi)) {
- ConsumeExtraSemi(InsideStruct, TagType);
- continue;
- }
- AccessSpecifier AS = getAccessSpecifierIfPresent();
- if (AS != AS_none) {
- // Current token is a C++ access specifier.
- CurAS = AS;
- SourceLocation ASLoc = Tok.getLocation();
- ConsumeToken();
- if (Tok.is(tok::colon))
- Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
- else
- Diag(Tok, diag::err_expected) << tok::colon;
- ConsumeToken();
- continue;
- }
- // Parse all the comma separated declarators.
- ParseCXXClassMemberDeclaration(CurAS, nullptr);
- }
-
- Braces.consumeClose();
- }
|