ParseDecl.cpp 168 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808
  1. //===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the Declaration portions of the Parser interfaces.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/Parse/Parser.h"
  14. #include "clang/Parse/ParseDiagnostic.h"
  15. #include "clang/Basic/OpenCL.h"
  16. #include "clang/Sema/Scope.h"
  17. #include "clang/Sema/ParsedTemplate.h"
  18. #include "clang/Sema/PrettyDeclStackTrace.h"
  19. #include "RAIIObjectsForParser.h"
  20. #include "llvm/ADT/SmallSet.h"
  21. #include "llvm/ADT/SmallString.h"
  22. #include "llvm/ADT/StringSwitch.h"
  23. using namespace clang;
  24. //===----------------------------------------------------------------------===//
  25. // C99 6.7: Declarations.
  26. //===----------------------------------------------------------------------===//
  27. /// ParseTypeName
  28. /// type-name: [C99 6.7.6]
  29. /// specifier-qualifier-list abstract-declarator[opt]
  30. ///
  31. /// Called type-id in C++.
  32. TypeResult Parser::ParseTypeName(SourceRange *Range,
  33. Declarator::TheContext Context,
  34. AccessSpecifier AS,
  35. Decl **OwnedType) {
  36. // Parse the common declaration-specifiers piece.
  37. DeclSpec DS(AttrFactory);
  38. ParseSpecifierQualifierList(DS, AS);
  39. if (OwnedType)
  40. *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
  41. // Parse the abstract-declarator, if present.
  42. Declarator DeclaratorInfo(DS, Context);
  43. ParseDeclarator(DeclaratorInfo);
  44. if (Range)
  45. *Range = DeclaratorInfo.getSourceRange();
  46. if (DeclaratorInfo.isInvalidType())
  47. return true;
  48. return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
  49. }
  50. /// isAttributeLateParsed - Return true if the attribute has arguments that
  51. /// require late parsing.
  52. static bool isAttributeLateParsed(const IdentifierInfo &II) {
  53. return llvm::StringSwitch<bool>(II.getName())
  54. #include "clang/Parse/AttrLateParsed.inc"
  55. .Default(false);
  56. }
  57. /// ParseGNUAttributes - Parse a non-empty attributes list.
  58. ///
  59. /// [GNU] attributes:
  60. /// attribute
  61. /// attributes attribute
  62. ///
  63. /// [GNU] attribute:
  64. /// '__attribute__' '(' '(' attribute-list ')' ')'
  65. ///
  66. /// [GNU] attribute-list:
  67. /// attrib
  68. /// attribute_list ',' attrib
  69. ///
  70. /// [GNU] attrib:
  71. /// empty
  72. /// attrib-name
  73. /// attrib-name '(' identifier ')'
  74. /// attrib-name '(' identifier ',' nonempty-expr-list ')'
  75. /// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
  76. ///
  77. /// [GNU] attrib-name:
  78. /// identifier
  79. /// typespec
  80. /// typequal
  81. /// storageclass
  82. ///
  83. /// FIXME: The GCC grammar/code for this construct implies we need two
  84. /// token lookahead. Comment from gcc: "If they start with an identifier
  85. /// which is followed by a comma or close parenthesis, then the arguments
  86. /// start with that identifier; otherwise they are an expression list."
  87. ///
  88. /// GCC does not require the ',' between attribs in an attribute-list.
  89. ///
  90. /// At the moment, I am not doing 2 token lookahead. I am also unaware of
  91. /// any attributes that don't work (based on my limited testing). Most
  92. /// attributes are very simple in practice. Until we find a bug, I don't see
  93. /// a pressing need to implement the 2 token lookahead.
  94. void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
  95. SourceLocation *endLoc,
  96. LateParsedAttrList *LateAttrs) {
  97. assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
  98. while (Tok.is(tok::kw___attribute)) {
  99. ConsumeToken();
  100. if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
  101. "attribute")) {
  102. SkipUntil(tok::r_paren, true); // skip until ) or ;
  103. return;
  104. }
  105. if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
  106. SkipUntil(tok::r_paren, true); // skip until ) or ;
  107. return;
  108. }
  109. // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
  110. while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
  111. Tok.is(tok::comma)) {
  112. if (Tok.is(tok::comma)) {
  113. // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
  114. ConsumeToken();
  115. continue;
  116. }
  117. // we have an identifier or declaration specifier (const, int, etc.)
  118. IdentifierInfo *AttrName = Tok.getIdentifierInfo();
  119. SourceLocation AttrNameLoc = ConsumeToken();
  120. if (Tok.is(tok::l_paren)) {
  121. // handle "parameterized" attributes
  122. if (LateAttrs && !ClassStack.empty() &&
  123. isAttributeLateParsed(*AttrName)) {
  124. // Delayed parsing is only available for attributes that occur
  125. // in certain locations within a class scope.
  126. LateParsedAttribute *LA =
  127. new LateParsedAttribute(this, *AttrName, AttrNameLoc);
  128. LateAttrs->push_back(LA);
  129. getCurrentClass().LateParsedDeclarations.push_back(LA);
  130. // consume everything up to and including the matching right parens
  131. ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
  132. Token Eof;
  133. Eof.startToken();
  134. Eof.setLocation(Tok.getLocation());
  135. LA->Toks.push_back(Eof);
  136. } else {
  137. ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc);
  138. }
  139. } else {
  140. attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
  141. 0, SourceLocation(), 0, 0);
  142. }
  143. }
  144. if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
  145. SkipUntil(tok::r_paren, false);
  146. SourceLocation Loc = Tok.getLocation();
  147. if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
  148. SkipUntil(tok::r_paren, false);
  149. }
  150. if (endLoc)
  151. *endLoc = Loc;
  152. }
  153. }
  154. /// Parse the arguments to a parameterized GNU attribute
  155. void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
  156. SourceLocation AttrNameLoc,
  157. ParsedAttributes &Attrs,
  158. SourceLocation *EndLoc) {
  159. assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
  160. // Availability attributes have their own grammar.
  161. if (AttrName->isStr("availability")) {
  162. ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
  163. return;
  164. }
  165. // Thread safety attributes fit into the FIXME case above, so we
  166. // just parse the arguments as a list of expressions
  167. if (IsThreadSafetyAttribute(AttrName->getName())) {
  168. ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
  169. return;
  170. }
  171. ConsumeParen(); // ignore the left paren loc for now
  172. IdentifierInfo *ParmName = 0;
  173. SourceLocation ParmLoc;
  174. bool BuiltinType = false;
  175. switch (Tok.getKind()) {
  176. case tok::kw_char:
  177. case tok::kw_wchar_t:
  178. case tok::kw_char16_t:
  179. case tok::kw_char32_t:
  180. case tok::kw_bool:
  181. case tok::kw_short:
  182. case tok::kw_int:
  183. case tok::kw_long:
  184. case tok::kw___int64:
  185. case tok::kw_signed:
  186. case tok::kw_unsigned:
  187. case tok::kw_float:
  188. case tok::kw_double:
  189. case tok::kw_void:
  190. case tok::kw_typeof:
  191. // __attribute__(( vec_type_hint(char) ))
  192. // FIXME: Don't just discard the builtin type token.
  193. ConsumeToken();
  194. BuiltinType = true;
  195. break;
  196. case tok::identifier:
  197. ParmName = Tok.getIdentifierInfo();
  198. ParmLoc = ConsumeToken();
  199. break;
  200. default:
  201. break;
  202. }
  203. ExprVector ArgExprs(Actions);
  204. if (!BuiltinType &&
  205. (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
  206. // Eat the comma.
  207. if (ParmLoc.isValid())
  208. ConsumeToken();
  209. // Parse the non-empty comma-separated list of expressions.
  210. while (1) {
  211. ExprResult ArgExpr(ParseAssignmentExpression());
  212. if (ArgExpr.isInvalid()) {
  213. SkipUntil(tok::r_paren);
  214. return;
  215. }
  216. ArgExprs.push_back(ArgExpr.release());
  217. if (Tok.isNot(tok::comma))
  218. break;
  219. ConsumeToken(); // Eat the comma, move to the next argument
  220. }
  221. }
  222. else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) {
  223. if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
  224. tok::greater)) {
  225. while (Tok.is(tok::identifier)) {
  226. ConsumeToken();
  227. if (Tok.is(tok::greater))
  228. break;
  229. if (Tok.is(tok::comma)) {
  230. ConsumeToken();
  231. continue;
  232. }
  233. }
  234. if (Tok.isNot(tok::greater))
  235. Diag(Tok, diag::err_iboutletcollection_with_protocol);
  236. SkipUntil(tok::r_paren, false, true); // skip until ')'
  237. }
  238. }
  239. SourceLocation RParen = Tok.getLocation();
  240. if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
  241. AttributeList *attr =
  242. Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc,
  243. ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size());
  244. if (BuiltinType && attr->getKind() == AttributeList::AT_IBOutletCollection)
  245. Diag(Tok, diag::err_iboutletcollection_builtintype);
  246. }
  247. }
  248. /// ParseMicrosoftDeclSpec - Parse an __declspec construct
  249. ///
  250. /// [MS] decl-specifier:
  251. /// __declspec ( extended-decl-modifier-seq )
  252. ///
  253. /// [MS] extended-decl-modifier-seq:
  254. /// extended-decl-modifier[opt]
  255. /// extended-decl-modifier extended-decl-modifier-seq
  256. void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) {
  257. assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
  258. ConsumeToken();
  259. if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
  260. "declspec")) {
  261. SkipUntil(tok::r_paren, true); // skip until ) or ;
  262. return;
  263. }
  264. while (Tok.getIdentifierInfo()) {
  265. IdentifierInfo *AttrName = Tok.getIdentifierInfo();
  266. SourceLocation AttrNameLoc = ConsumeToken();
  267. // FIXME: Remove this when we have proper __declspec(property()) support.
  268. // Just skip everything inside property().
  269. if (AttrName->getName() == "property") {
  270. ConsumeParen();
  271. SkipUntil(tok::r_paren);
  272. }
  273. if (Tok.is(tok::l_paren)) {
  274. ConsumeParen();
  275. // FIXME: This doesn't parse __declspec(property(get=get_func_name))
  276. // correctly.
  277. ExprResult ArgExpr(ParseAssignmentExpression());
  278. if (!ArgExpr.isInvalid()) {
  279. Expr *ExprList = ArgExpr.take();
  280. attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
  281. SourceLocation(), &ExprList, 1, true);
  282. }
  283. if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
  284. SkipUntil(tok::r_paren, false);
  285. } else {
  286. attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
  287. 0, SourceLocation(), 0, 0, true);
  288. }
  289. }
  290. if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
  291. SkipUntil(tok::r_paren, false);
  292. return;
  293. }
  294. void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
  295. // Treat these like attributes
  296. // FIXME: Allow Sema to distinguish between these and real attributes!
  297. while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
  298. Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
  299. Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
  300. Tok.is(tok::kw___ptr32) ||
  301. Tok.is(tok::kw___unaligned)) {
  302. IdentifierInfo *AttrName = Tok.getIdentifierInfo();
  303. SourceLocation AttrNameLoc = ConsumeToken();
  304. if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
  305. Tok.is(tok::kw___ptr32))
  306. // FIXME: Support these properly!
  307. continue;
  308. attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
  309. SourceLocation(), 0, 0, true);
  310. }
  311. }
  312. void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
  313. // Treat these like attributes
  314. while (Tok.is(tok::kw___pascal)) {
  315. IdentifierInfo *AttrName = Tok.getIdentifierInfo();
  316. SourceLocation AttrNameLoc = ConsumeToken();
  317. attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
  318. SourceLocation(), 0, 0, true);
  319. }
  320. }
  321. void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
  322. // Treat these like attributes
  323. while (Tok.is(tok::kw___kernel)) {
  324. SourceLocation AttrNameLoc = ConsumeToken();
  325. attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
  326. AttrNameLoc, 0, AttrNameLoc, 0,
  327. SourceLocation(), 0, 0, false);
  328. }
  329. }
  330. void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
  331. SourceLocation Loc = Tok.getLocation();
  332. switch(Tok.getKind()) {
  333. // OpenCL qualifiers:
  334. case tok::kw___private:
  335. case tok::kw_private:
  336. DS.getAttributes().addNewInteger(
  337. Actions.getASTContext(),
  338. PP.getIdentifierInfo("address_space"), Loc, 0);
  339. break;
  340. case tok::kw___global:
  341. DS.getAttributes().addNewInteger(
  342. Actions.getASTContext(),
  343. PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
  344. break;
  345. case tok::kw___local:
  346. DS.getAttributes().addNewInteger(
  347. Actions.getASTContext(),
  348. PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
  349. break;
  350. case tok::kw___constant:
  351. DS.getAttributes().addNewInteger(
  352. Actions.getASTContext(),
  353. PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
  354. break;
  355. case tok::kw___read_only:
  356. DS.getAttributes().addNewInteger(
  357. Actions.getASTContext(),
  358. PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
  359. break;
  360. case tok::kw___write_only:
  361. DS.getAttributes().addNewInteger(
  362. Actions.getASTContext(),
  363. PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
  364. break;
  365. case tok::kw___read_write:
  366. DS.getAttributes().addNewInteger(
  367. Actions.getASTContext(),
  368. PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
  369. break;
  370. default: break;
  371. }
  372. }
  373. /// \brief Parse a version number.
  374. ///
  375. /// version:
  376. /// simple-integer
  377. /// simple-integer ',' simple-integer
  378. /// simple-integer ',' simple-integer ',' simple-integer
  379. VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
  380. Range = Tok.getLocation();
  381. if (!Tok.is(tok::numeric_constant)) {
  382. Diag(Tok, diag::err_expected_version);
  383. SkipUntil(tok::comma, tok::r_paren, true, true, true);
  384. return VersionTuple();
  385. }
  386. // Parse the major (and possibly minor and subminor) versions, which
  387. // are stored in the numeric constant. We utilize a quirk of the
  388. // lexer, which is that it handles something like 1.2.3 as a single
  389. // numeric constant, rather than two separate tokens.
  390. SmallString<512> Buffer;
  391. Buffer.resize(Tok.getLength()+1);
  392. const char *ThisTokBegin = &Buffer[0];
  393. // Get the spelling of the token, which eliminates trigraphs, etc.
  394. bool Invalid = false;
  395. unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
  396. if (Invalid)
  397. return VersionTuple();
  398. // Parse the major version.
  399. unsigned AfterMajor = 0;
  400. unsigned Major = 0;
  401. while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
  402. Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
  403. ++AfterMajor;
  404. }
  405. if (AfterMajor == 0) {
  406. Diag(Tok, diag::err_expected_version);
  407. SkipUntil(tok::comma, tok::r_paren, true, true, true);
  408. return VersionTuple();
  409. }
  410. if (AfterMajor == ActualLength) {
  411. ConsumeToken();
  412. // We only had a single version component.
  413. if (Major == 0) {
  414. Diag(Tok, diag::err_zero_version);
  415. return VersionTuple();
  416. }
  417. return VersionTuple(Major);
  418. }
  419. if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
  420. Diag(Tok, diag::err_expected_version);
  421. SkipUntil(tok::comma, tok::r_paren, true, true, true);
  422. return VersionTuple();
  423. }
  424. // Parse the minor version.
  425. unsigned AfterMinor = AfterMajor + 1;
  426. unsigned Minor = 0;
  427. while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
  428. Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
  429. ++AfterMinor;
  430. }
  431. if (AfterMinor == ActualLength) {
  432. ConsumeToken();
  433. // We had major.minor.
  434. if (Major == 0 && Minor == 0) {
  435. Diag(Tok, diag::err_zero_version);
  436. return VersionTuple();
  437. }
  438. return VersionTuple(Major, Minor);
  439. }
  440. // If what follows is not a '.', we have a problem.
  441. if (ThisTokBegin[AfterMinor] != '.') {
  442. Diag(Tok, diag::err_expected_version);
  443. SkipUntil(tok::comma, tok::r_paren, true, true, true);
  444. return VersionTuple();
  445. }
  446. // Parse the subminor version.
  447. unsigned AfterSubminor = AfterMinor + 1;
  448. unsigned Subminor = 0;
  449. while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
  450. Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
  451. ++AfterSubminor;
  452. }
  453. if (AfterSubminor != ActualLength) {
  454. Diag(Tok, diag::err_expected_version);
  455. SkipUntil(tok::comma, tok::r_paren, true, true, true);
  456. return VersionTuple();
  457. }
  458. ConsumeToken();
  459. return VersionTuple(Major, Minor, Subminor);
  460. }
  461. /// \brief Parse the contents of the "availability" attribute.
  462. ///
  463. /// availability-attribute:
  464. /// 'availability' '(' platform ',' version-arg-list, opt-message')'
  465. ///
  466. /// platform:
  467. /// identifier
  468. ///
  469. /// version-arg-list:
  470. /// version-arg
  471. /// version-arg ',' version-arg-list
  472. ///
  473. /// version-arg:
  474. /// 'introduced' '=' version
  475. /// 'deprecated' '=' version
  476. /// 'removed' = version
  477. /// 'unavailable'
  478. /// opt-message:
  479. /// 'message' '=' <string>
  480. void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
  481. SourceLocation AvailabilityLoc,
  482. ParsedAttributes &attrs,
  483. SourceLocation *endLoc) {
  484. SourceLocation PlatformLoc;
  485. IdentifierInfo *Platform = 0;
  486. enum { Introduced, Deprecated, Obsoleted, Unknown };
  487. AvailabilityChange Changes[Unknown];
  488. ExprResult MessageExpr;
  489. // Opening '('.
  490. BalancedDelimiterTracker T(*this, tok::l_paren);
  491. if (T.consumeOpen()) {
  492. Diag(Tok, diag::err_expected_lparen);
  493. return;
  494. }
  495. // Parse the platform name,
  496. if (Tok.isNot(tok::identifier)) {
  497. Diag(Tok, diag::err_availability_expected_platform);
  498. SkipUntil(tok::r_paren);
  499. return;
  500. }
  501. Platform = Tok.getIdentifierInfo();
  502. PlatformLoc = ConsumeToken();
  503. // Parse the ',' following the platform name.
  504. if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
  505. return;
  506. // If we haven't grabbed the pointers for the identifiers
  507. // "introduced", "deprecated", and "obsoleted", do so now.
  508. if (!Ident_introduced) {
  509. Ident_introduced = PP.getIdentifierInfo("introduced");
  510. Ident_deprecated = PP.getIdentifierInfo("deprecated");
  511. Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
  512. Ident_unavailable = PP.getIdentifierInfo("unavailable");
  513. Ident_message = PP.getIdentifierInfo("message");
  514. }
  515. // Parse the set of introductions/deprecations/removals.
  516. SourceLocation UnavailableLoc;
  517. do {
  518. if (Tok.isNot(tok::identifier)) {
  519. Diag(Tok, diag::err_availability_expected_change);
  520. SkipUntil(tok::r_paren);
  521. return;
  522. }
  523. IdentifierInfo *Keyword = Tok.getIdentifierInfo();
  524. SourceLocation KeywordLoc = ConsumeToken();
  525. if (Keyword == Ident_unavailable) {
  526. if (UnavailableLoc.isValid()) {
  527. Diag(KeywordLoc, diag::err_availability_redundant)
  528. << Keyword << SourceRange(UnavailableLoc);
  529. }
  530. UnavailableLoc = KeywordLoc;
  531. if (Tok.isNot(tok::comma))
  532. break;
  533. ConsumeToken();
  534. continue;
  535. }
  536. if (Tok.isNot(tok::equal)) {
  537. Diag(Tok, diag::err_expected_equal_after)
  538. << Keyword;
  539. SkipUntil(tok::r_paren);
  540. return;
  541. }
  542. ConsumeToken();
  543. if (Keyword == Ident_message) {
  544. if (!isTokenStringLiteral()) {
  545. Diag(Tok, diag::err_expected_string_literal);
  546. SkipUntil(tok::r_paren);
  547. return;
  548. }
  549. MessageExpr = ParseStringLiteralExpression();
  550. break;
  551. }
  552. SourceRange VersionRange;
  553. VersionTuple Version = ParseVersionTuple(VersionRange);
  554. if (Version.empty()) {
  555. SkipUntil(tok::r_paren);
  556. return;
  557. }
  558. unsigned Index;
  559. if (Keyword == Ident_introduced)
  560. Index = Introduced;
  561. else if (Keyword == Ident_deprecated)
  562. Index = Deprecated;
  563. else if (Keyword == Ident_obsoleted)
  564. Index = Obsoleted;
  565. else
  566. Index = Unknown;
  567. if (Index < Unknown) {
  568. if (!Changes[Index].KeywordLoc.isInvalid()) {
  569. Diag(KeywordLoc, diag::err_availability_redundant)
  570. << Keyword
  571. << SourceRange(Changes[Index].KeywordLoc,
  572. Changes[Index].VersionRange.getEnd());
  573. }
  574. Changes[Index].KeywordLoc = KeywordLoc;
  575. Changes[Index].Version = Version;
  576. Changes[Index].VersionRange = VersionRange;
  577. } else {
  578. Diag(KeywordLoc, diag::err_availability_unknown_change)
  579. << Keyword << VersionRange;
  580. }
  581. if (Tok.isNot(tok::comma))
  582. break;
  583. ConsumeToken();
  584. } while (true);
  585. // Closing ')'.
  586. if (T.consumeClose())
  587. return;
  588. if (endLoc)
  589. *endLoc = T.getCloseLocation();
  590. // The 'unavailable' availability cannot be combined with any other
  591. // availability changes. Make sure that hasn't happened.
  592. if (UnavailableLoc.isValid()) {
  593. bool Complained = false;
  594. for (unsigned Index = Introduced; Index != Unknown; ++Index) {
  595. if (Changes[Index].KeywordLoc.isValid()) {
  596. if (!Complained) {
  597. Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
  598. << SourceRange(Changes[Index].KeywordLoc,
  599. Changes[Index].VersionRange.getEnd());
  600. Complained = true;
  601. }
  602. // Clear out the availability.
  603. Changes[Index] = AvailabilityChange();
  604. }
  605. }
  606. }
  607. // Record this attribute
  608. attrs.addNew(&Availability,
  609. SourceRange(AvailabilityLoc, T.getCloseLocation()),
  610. 0, AvailabilityLoc,
  611. Platform, PlatformLoc,
  612. Changes[Introduced],
  613. Changes[Deprecated],
  614. Changes[Obsoleted],
  615. UnavailableLoc, MessageExpr.take(),
  616. false, false);
  617. }
  618. // Late Parsed Attributes:
  619. // See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
  620. void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
  621. void Parser::LateParsedClass::ParseLexedAttributes() {
  622. Self->ParseLexedAttributes(*Class);
  623. }
  624. void Parser::LateParsedAttribute::ParseLexedAttributes() {
  625. Self->ParseLexedAttribute(*this);
  626. }
  627. /// Wrapper class which calls ParseLexedAttribute, after setting up the
  628. /// scope appropriately.
  629. void Parser::ParseLexedAttributes(ParsingClass &Class) {
  630. // Deal with templates
  631. // FIXME: Test cases to make sure this does the right thing for templates.
  632. bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
  633. ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
  634. HasTemplateScope);
  635. if (HasTemplateScope)
  636. Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
  637. // Set or update the scope flags to include Scope::ThisScope.
  638. bool AlreadyHasClassScope = Class.TopLevelClass;
  639. unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope|Scope::ThisScope;
  640. ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
  641. ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
  642. for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i) {
  643. Class.LateParsedDeclarations[i]->ParseLexedAttributes();
  644. }
  645. }
  646. /// \brief Finish parsing an attribute for which parsing was delayed.
  647. /// This will be called at the end of parsing a class declaration
  648. /// for each LateParsedAttribute. We consume the saved tokens and
  649. /// create an attribute with the arguments filled in. We add this
  650. /// to the Attribute list for the decl.
  651. void Parser::ParseLexedAttribute(LateParsedAttribute &LA) {
  652. // Save the current token position.
  653. SourceLocation OrigLoc = Tok.getLocation();
  654. // Append the current token at the end of the new token stream so that it
  655. // doesn't get lost.
  656. LA.Toks.push_back(Tok);
  657. PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
  658. // Consume the previously pushed token.
  659. ConsumeAnyToken();
  660. ParsedAttributes Attrs(AttrFactory);
  661. SourceLocation endLoc;
  662. // If the Decl is templatized, add template parameters to scope.
  663. bool HasTemplateScope = LA.D && LA.D->isTemplateDecl();
  664. ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
  665. if (HasTemplateScope)
  666. Actions.ActOnReenterTemplateScope(Actions.CurScope, LA.D);
  667. // If the Decl is on a function, add function parameters to the scope.
  668. bool HasFunctionScope = LA.D && LA.D->isFunctionOrFunctionTemplate();
  669. ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope);
  670. if (HasFunctionScope)
  671. Actions.ActOnReenterFunctionContext(Actions.CurScope, LA.D);
  672. ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
  673. if (HasFunctionScope) {
  674. Actions.ActOnExitFunctionContext();
  675. FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
  676. }
  677. if (HasTemplateScope) {
  678. TempScope.Exit();
  679. }
  680. // Late parsed attributes must be attached to Decls by hand. If the
  681. // LA.D is not set, then this was not done properly.
  682. assert(LA.D && "No decl attached to late parsed attribute");
  683. Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.D, Attrs);
  684. if (Tok.getLocation() != OrigLoc) {
  685. // Due to a parsing error, we either went over the cached tokens or
  686. // there are still cached tokens left, so we skip the leftover tokens.
  687. // Since this is an uncommon situation that should be avoided, use the
  688. // expensive isBeforeInTranslationUnit call.
  689. if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
  690. OrigLoc))
  691. while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
  692. ConsumeAnyToken();
  693. }
  694. }
  695. /// \brief Wrapper around a case statement checking if AttrName is
  696. /// one of the thread safety attributes
  697. bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
  698. return llvm::StringSwitch<bool>(AttrName)
  699. .Case("guarded_by", true)
  700. .Case("guarded_var", true)
  701. .Case("pt_guarded_by", true)
  702. .Case("pt_guarded_var", true)
  703. .Case("lockable", true)
  704. .Case("scoped_lockable", true)
  705. .Case("no_thread_safety_analysis", true)
  706. .Case("acquired_after", true)
  707. .Case("acquired_before", true)
  708. .Case("exclusive_lock_function", true)
  709. .Case("shared_lock_function", true)
  710. .Case("exclusive_trylock_function", true)
  711. .Case("shared_trylock_function", true)
  712. .Case("unlock_function", true)
  713. .Case("lock_returned", true)
  714. .Case("locks_excluded", true)
  715. .Case("exclusive_locks_required", true)
  716. .Case("shared_locks_required", true)
  717. .Default(false);
  718. }
  719. /// \brief Parse the contents of thread safety attributes. These
  720. /// should always be parsed as an expression list.
  721. ///
  722. /// We need to special case the parsing due to the fact that if the first token
  723. /// of the first argument is an identifier, the main parse loop will store
  724. /// that token as a "parameter" and the rest of
  725. /// the arguments will be added to a list of "arguments". However,
  726. /// subsequent tokens in the first argument are lost. We instead parse each
  727. /// argument as an expression and add all arguments to the list of "arguments".
  728. /// In future, we will take advantage of this special case to also
  729. /// deal with some argument scoping issues here (for example, referring to a
  730. /// function parameter in the attribute on that function).
  731. void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
  732. SourceLocation AttrNameLoc,
  733. ParsedAttributes &Attrs,
  734. SourceLocation *EndLoc) {
  735. assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
  736. BalancedDelimiterTracker T(*this, tok::l_paren);
  737. T.consumeOpen();
  738. ExprVector ArgExprs(Actions);
  739. bool ArgExprsOk = true;
  740. // now parse the list of expressions
  741. while (Tok.isNot(tok::r_paren)) {
  742. ExprResult ArgExpr(ParseAssignmentExpression());
  743. if (ArgExpr.isInvalid()) {
  744. ArgExprsOk = false;
  745. T.consumeClose();
  746. break;
  747. } else {
  748. ArgExprs.push_back(ArgExpr.release());
  749. }
  750. if (Tok.isNot(tok::comma))
  751. break;
  752. ConsumeToken(); // Eat the comma, move to the next argument
  753. }
  754. // Match the ')'.
  755. if (ArgExprsOk && !T.consumeClose()) {
  756. Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
  757. ArgExprs.take(), ArgExprs.size());
  758. }
  759. if (EndLoc)
  760. *EndLoc = T.getCloseLocation();
  761. }
  762. void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
  763. Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
  764. << attrs.Range;
  765. }
  766. /// ParseDeclaration - Parse a full 'declaration', which consists of
  767. /// declaration-specifiers, some number of declarators, and a semicolon.
  768. /// 'Context' should be a Declarator::TheContext value. This returns the
  769. /// location of the semicolon in DeclEnd.
  770. ///
  771. /// declaration: [C99 6.7]
  772. /// block-declaration ->
  773. /// simple-declaration
  774. /// others [FIXME]
  775. /// [C++] template-declaration
  776. /// [C++] namespace-definition
  777. /// [C++] using-directive
  778. /// [C++] using-declaration
  779. /// [C++0x/C11] static_assert-declaration
  780. /// others... [FIXME]
  781. ///
  782. Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
  783. unsigned Context,
  784. SourceLocation &DeclEnd,
  785. ParsedAttributesWithRange &attrs) {
  786. ParenBraceBracketBalancer BalancerRAIIObj(*this);
  787. // Must temporarily exit the objective-c container scope for
  788. // parsing c none objective-c decls.
  789. ObjCDeclContextSwitch ObjCDC(*this);
  790. Decl *SingleDecl = 0;
  791. Decl *OwnedType = 0;
  792. switch (Tok.getKind()) {
  793. case tok::kw_template:
  794. case tok::kw_export:
  795. ProhibitAttributes(attrs);
  796. SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
  797. break;
  798. case tok::kw_inline:
  799. // Could be the start of an inline namespace. Allowed as an ext in C++03.
  800. if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) {
  801. ProhibitAttributes(attrs);
  802. SourceLocation InlineLoc = ConsumeToken();
  803. SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
  804. break;
  805. }
  806. return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
  807. true);
  808. case tok::kw_namespace:
  809. ProhibitAttributes(attrs);
  810. SingleDecl = ParseNamespace(Context, DeclEnd);
  811. break;
  812. case tok::kw_using:
  813. SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
  814. DeclEnd, attrs, &OwnedType);
  815. break;
  816. case tok::kw_static_assert:
  817. case tok::kw__Static_assert:
  818. ProhibitAttributes(attrs);
  819. SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
  820. break;
  821. default:
  822. return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
  823. }
  824. // This routine returns a DeclGroup, if the thing we parsed only contains a
  825. // single decl, convert it now. Alias declarations can also declare a type;
  826. // include that too if it is present.
  827. return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
  828. }
  829. /// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
  830. /// declaration-specifiers init-declarator-list[opt] ';'
  831. ///[C90/C++]init-declarator-list ';' [TODO]
  832. /// [OMP] threadprivate-directive [TODO]
  833. ///
  834. /// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
  835. /// attribute-specifier-seq[opt] type-specifier-seq declarator
  836. ///
  837. /// If RequireSemi is false, this does not check for a ';' at the end of the
  838. /// declaration. If it is true, it checks for and eats it.
  839. ///
  840. /// If FRI is non-null, we might be parsing a for-range-declaration instead
  841. /// of a simple-declaration. If we find that we are, we also parse the
  842. /// for-range-initializer, and place it here.
  843. Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
  844. unsigned Context,
  845. SourceLocation &DeclEnd,
  846. ParsedAttributes &attrs,
  847. bool RequireSemi,
  848. ForRangeInit *FRI) {
  849. // Parse the common declaration-specifiers piece.
  850. ParsingDeclSpec DS(*this);
  851. DS.takeAttributesFrom(attrs);
  852. ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
  853. getDeclSpecContextFromDeclaratorContext(Context));
  854. // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
  855. // declaration-specifiers init-declarator-list[opt] ';'
  856. if (Tok.is(tok::semi)) {
  857. if (RequireSemi) ConsumeToken();
  858. Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
  859. DS);
  860. DS.complete(TheDecl);
  861. return Actions.ConvertDeclToDeclGroup(TheDecl);
  862. }
  863. return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
  864. }
  865. /// Returns true if this might be the start of a declarator, or a common typo
  866. /// for a declarator.
  867. bool Parser::MightBeDeclarator(unsigned Context) {
  868. switch (Tok.getKind()) {
  869. case tok::annot_cxxscope:
  870. case tok::annot_template_id:
  871. case tok::caret:
  872. case tok::code_completion:
  873. case tok::coloncolon:
  874. case tok::ellipsis:
  875. case tok::kw___attribute:
  876. case tok::kw_operator:
  877. case tok::l_paren:
  878. case tok::star:
  879. return true;
  880. case tok::amp:
  881. case tok::ampamp:
  882. return getLang().CPlusPlus;
  883. case tok::l_square: // Might be an attribute on an unnamed bit-field.
  884. return Context == Declarator::MemberContext && getLang().CPlusPlus0x &&
  885. NextToken().is(tok::l_square);
  886. case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
  887. return Context == Declarator::MemberContext || getLang().CPlusPlus;
  888. case tok::identifier:
  889. switch (NextToken().getKind()) {
  890. case tok::code_completion:
  891. case tok::coloncolon:
  892. case tok::comma:
  893. case tok::equal:
  894. case tok::equalequal: // Might be a typo for '='.
  895. case tok::kw_alignas:
  896. case tok::kw_asm:
  897. case tok::kw___attribute:
  898. case tok::l_brace:
  899. case tok::l_paren:
  900. case tok::l_square:
  901. case tok::less:
  902. case tok::r_brace:
  903. case tok::r_paren:
  904. case tok::r_square:
  905. case tok::semi:
  906. return true;
  907. case tok::colon:
  908. // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
  909. // and in block scope it's probably a label. Inside a class definition,
  910. // this is a bit-field.
  911. return Context == Declarator::MemberContext ||
  912. (getLang().CPlusPlus && Context == Declarator::FileContext);
  913. case tok::identifier: // Possible virt-specifier.
  914. return getLang().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken());
  915. default:
  916. return false;
  917. }
  918. default:
  919. return false;
  920. }
  921. }
  922. /// ParseDeclGroup - Having concluded that this is either a function
  923. /// definition or a group of object declarations, actually parse the
  924. /// result.
  925. Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
  926. unsigned Context,
  927. bool AllowFunctionDefinitions,
  928. SourceLocation *DeclEnd,
  929. ForRangeInit *FRI) {
  930. // Parse the first declarator.
  931. ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
  932. ParseDeclarator(D);
  933. // Bail out if the first declarator didn't seem well-formed.
  934. if (!D.hasName() && !D.mayOmitIdentifier()) {
  935. // Skip until ; or }.
  936. SkipUntil(tok::r_brace, true, true);
  937. if (Tok.is(tok::semi))
  938. ConsumeToken();
  939. return DeclGroupPtrTy();
  940. }
  941. // Check to see if we have a function *definition* which must have a body.
  942. if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
  943. // Look at the next token to make sure that this isn't a function
  944. // declaration. We have to check this because __attribute__ might be the
  945. // start of a function definition in GCC-extended K&R C.
  946. !isDeclarationAfterDeclarator()) {
  947. if (isStartOfFunctionDefinition(D)) {
  948. if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
  949. Diag(Tok, diag::err_function_declared_typedef);
  950. // Recover by treating the 'typedef' as spurious.
  951. DS.ClearStorageClassSpecs();
  952. }
  953. Decl *TheDecl = ParseFunctionDefinition(D);
  954. return Actions.ConvertDeclToDeclGroup(TheDecl);
  955. }
  956. if (isDeclarationSpecifier()) {
  957. // If there is an invalid declaration specifier right after the function
  958. // prototype, then we must be in a missing semicolon case where this isn't
  959. // actually a body. Just fall through into the code that handles it as a
  960. // prototype, and let the top-level code handle the erroneous declspec
  961. // where it would otherwise expect a comma or semicolon.
  962. } else {
  963. Diag(Tok, diag::err_expected_fn_body);
  964. SkipUntil(tok::semi);
  965. return DeclGroupPtrTy();
  966. }
  967. }
  968. if (ParseAttributesAfterDeclarator(D))
  969. return DeclGroupPtrTy();
  970. // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
  971. // must parse and analyze the for-range-initializer before the declaration is
  972. // analyzed.
  973. if (FRI && Tok.is(tok::colon)) {
  974. FRI->ColonLoc = ConsumeToken();
  975. if (Tok.is(tok::l_brace))
  976. FRI->RangeExpr = ParseBraceInitializer();
  977. else
  978. FRI->RangeExpr = ParseExpression();
  979. Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
  980. Actions.ActOnCXXForRangeDecl(ThisDecl);
  981. Actions.FinalizeDeclaration(ThisDecl);
  982. D.complete(ThisDecl);
  983. return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
  984. }
  985. SmallVector<Decl *, 8> DeclsInGroup;
  986. Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
  987. D.complete(FirstDecl);
  988. if (FirstDecl)
  989. DeclsInGroup.push_back(FirstDecl);
  990. bool ExpectSemi = Context != Declarator::ForContext;
  991. // If we don't have a comma, it is either the end of the list (a ';') or an
  992. // error, bail out.
  993. while (Tok.is(tok::comma)) {
  994. SourceLocation CommaLoc = ConsumeToken();
  995. if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
  996. // This comma was followed by a line-break and something which can't be
  997. // the start of a declarator. The comma was probably a typo for a
  998. // semicolon.
  999. Diag(CommaLoc, diag::err_expected_semi_declaration)
  1000. << FixItHint::CreateReplacement(CommaLoc, ";");
  1001. ExpectSemi = false;
  1002. break;
  1003. }
  1004. // Parse the next declarator.
  1005. D.clear();
  1006. D.setCommaLoc(CommaLoc);
  1007. // Accept attributes in an init-declarator. In the first declarator in a
  1008. // declaration, these would be part of the declspec. In subsequent
  1009. // declarators, they become part of the declarator itself, so that they
  1010. // don't apply to declarators after *this* one. Examples:
  1011. // short __attribute__((common)) var; -> declspec
  1012. // short var __attribute__((common)); -> declarator
  1013. // short x, __attribute__((common)) var; -> declarator
  1014. MaybeParseGNUAttributes(D);
  1015. ParseDeclarator(D);
  1016. if (!D.isInvalidType()) {
  1017. Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
  1018. D.complete(ThisDecl);
  1019. if (ThisDecl)
  1020. DeclsInGroup.push_back(ThisDecl);
  1021. }
  1022. }
  1023. if (DeclEnd)
  1024. *DeclEnd = Tok.getLocation();
  1025. if (ExpectSemi &&
  1026. ExpectAndConsume(tok::semi,
  1027. Context == Declarator::FileContext
  1028. ? diag::err_invalid_token_after_toplevel_declarator
  1029. : diag::err_expected_semi_declaration)) {
  1030. // Okay, there was no semicolon and one was expected. If we see a
  1031. // declaration specifier, just assume it was missing and continue parsing.
  1032. // Otherwise things are very confused and we skip to recover.
  1033. if (!isDeclarationSpecifier()) {
  1034. SkipUntil(tok::r_brace, true, true);
  1035. if (Tok.is(tok::semi))
  1036. ConsumeToken();
  1037. }
  1038. }
  1039. return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
  1040. DeclsInGroup.data(),
  1041. DeclsInGroup.size());
  1042. }
  1043. /// Parse an optional simple-asm-expr and attributes, and attach them to a
  1044. /// declarator. Returns true on an error.
  1045. bool Parser::ParseAttributesAfterDeclarator(Declarator &D) {
  1046. // If a simple-asm-expr is present, parse it.
  1047. if (Tok.is(tok::kw_asm)) {
  1048. SourceLocation Loc;
  1049. ExprResult AsmLabel(ParseSimpleAsm(&Loc));
  1050. if (AsmLabel.isInvalid()) {
  1051. SkipUntil(tok::semi, true, true);
  1052. return true;
  1053. }
  1054. D.setAsmLabel(AsmLabel.release());
  1055. D.SetRangeEnd(Loc);
  1056. }
  1057. MaybeParseGNUAttributes(D);
  1058. return false;
  1059. }
  1060. /// \brief Parse 'declaration' after parsing 'declaration-specifiers
  1061. /// declarator'. This method parses the remainder of the declaration
  1062. /// (including any attributes or initializer, among other things) and
  1063. /// finalizes the declaration.
  1064. ///
  1065. /// init-declarator: [C99 6.7]
  1066. /// declarator
  1067. /// declarator '=' initializer
  1068. /// [GNU] declarator simple-asm-expr[opt] attributes[opt]
  1069. /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
  1070. /// [C++] declarator initializer[opt]
  1071. ///
  1072. /// [C++] initializer:
  1073. /// [C++] '=' initializer-clause
  1074. /// [C++] '(' expression-list ')'
  1075. /// [C++0x] '=' 'default' [TODO]
  1076. /// [C++0x] '=' 'delete'
  1077. /// [C++0x] braced-init-list
  1078. ///
  1079. /// According to the standard grammar, =default and =delete are function
  1080. /// definitions, but that definitely doesn't fit with the parser here.
  1081. ///
  1082. Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
  1083. const ParsedTemplateInfo &TemplateInfo) {
  1084. if (ParseAttributesAfterDeclarator(D))
  1085. return 0;
  1086. return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
  1087. }
  1088. Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
  1089. const ParsedTemplateInfo &TemplateInfo) {
  1090. // Inform the current actions module that we just parsed this declarator.
  1091. Decl *ThisDecl = 0;
  1092. switch (TemplateInfo.Kind) {
  1093. case ParsedTemplateInfo::NonTemplate:
  1094. ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
  1095. break;
  1096. case ParsedTemplateInfo::Template:
  1097. case ParsedTemplateInfo::ExplicitSpecialization:
  1098. ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
  1099. MultiTemplateParamsArg(Actions,
  1100. TemplateInfo.TemplateParams->data(),
  1101. TemplateInfo.TemplateParams->size()),
  1102. D);
  1103. break;
  1104. case ParsedTemplateInfo::ExplicitInstantiation: {
  1105. DeclResult ThisRes
  1106. = Actions.ActOnExplicitInstantiation(getCurScope(),
  1107. TemplateInfo.ExternLoc,
  1108. TemplateInfo.TemplateLoc,
  1109. D);
  1110. if (ThisRes.isInvalid()) {
  1111. SkipUntil(tok::semi, true, true);
  1112. return 0;
  1113. }
  1114. ThisDecl = ThisRes.get();
  1115. break;
  1116. }
  1117. }
  1118. bool TypeContainsAuto =
  1119. D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
  1120. // Parse declarator '=' initializer.
  1121. // If a '==' or '+=' is found, suggest a fixit to '='.
  1122. if (isTokenEqualOrEqualTypo()) {
  1123. ConsumeToken();
  1124. if (Tok.is(tok::kw_delete)) {
  1125. if (D.isFunctionDeclarator())
  1126. Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
  1127. << 1 /* delete */;
  1128. else
  1129. Diag(ConsumeToken(), diag::err_deleted_non_function);
  1130. } else if (Tok.is(tok::kw_default)) {
  1131. if (D.isFunctionDeclarator())
  1132. Diag(Tok, diag::err_default_delete_in_multiple_declaration)
  1133. << 1 /* delete */;
  1134. else
  1135. Diag(ConsumeToken(), diag::err_default_special_members);
  1136. } else {
  1137. if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
  1138. EnterScope(0);
  1139. Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
  1140. }
  1141. if (Tok.is(tok::code_completion)) {
  1142. Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
  1143. cutOffParsing();
  1144. return 0;
  1145. }
  1146. ExprResult Init(ParseInitializer());
  1147. if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
  1148. Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
  1149. ExitScope();
  1150. }
  1151. if (Init.isInvalid()) {
  1152. SkipUntil(tok::comma, true, true);
  1153. Actions.ActOnInitializerError(ThisDecl);
  1154. } else
  1155. Actions.AddInitializerToDecl(ThisDecl, Init.take(),
  1156. /*DirectInit=*/false, TypeContainsAuto);
  1157. }
  1158. } else if (Tok.is(tok::l_paren)) {
  1159. // Parse C++ direct initializer: '(' expression-list ')'
  1160. BalancedDelimiterTracker T(*this, tok::l_paren);
  1161. T.consumeOpen();
  1162. ExprVector Exprs(Actions);
  1163. CommaLocsTy CommaLocs;
  1164. if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
  1165. EnterScope(0);
  1166. Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
  1167. }
  1168. if (ParseExpressionList(Exprs, CommaLocs)) {
  1169. SkipUntil(tok::r_paren);
  1170. if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
  1171. Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
  1172. ExitScope();
  1173. }
  1174. } else {
  1175. // Match the ')'.
  1176. T.consumeClose();
  1177. assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
  1178. "Unexpected number of commas!");
  1179. if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
  1180. Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
  1181. ExitScope();
  1182. }
  1183. Actions.AddCXXDirectInitializerToDecl(ThisDecl, T.getOpenLocation(),
  1184. move_arg(Exprs),
  1185. T.getCloseLocation(),
  1186. TypeContainsAuto);
  1187. }
  1188. } else if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) {
  1189. // Parse C++0x braced-init-list.
  1190. Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
  1191. if (D.getCXXScopeSpec().isSet()) {
  1192. EnterScope(0);
  1193. Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
  1194. }
  1195. ExprResult Init(ParseBraceInitializer());
  1196. if (D.getCXXScopeSpec().isSet()) {
  1197. Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
  1198. ExitScope();
  1199. }
  1200. if (Init.isInvalid()) {
  1201. Actions.ActOnInitializerError(ThisDecl);
  1202. } else
  1203. Actions.AddInitializerToDecl(ThisDecl, Init.take(),
  1204. /*DirectInit=*/true, TypeContainsAuto);
  1205. } else {
  1206. Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
  1207. }
  1208. Actions.FinalizeDeclaration(ThisDecl);
  1209. return ThisDecl;
  1210. }
  1211. /// ParseSpecifierQualifierList
  1212. /// specifier-qualifier-list:
  1213. /// type-specifier specifier-qualifier-list[opt]
  1214. /// type-qualifier specifier-qualifier-list[opt]
  1215. /// [GNU] attributes specifier-qualifier-list[opt]
  1216. ///
  1217. void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS) {
  1218. /// specifier-qualifier-list is a subset of declaration-specifiers. Just
  1219. /// parse declaration-specifiers and complain about extra stuff.
  1220. /// TODO: diagnose attribute-specifiers and alignment-specifiers.
  1221. ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS);
  1222. // Validate declspec for type-name.
  1223. unsigned Specs = DS.getParsedSpecifiers();
  1224. if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
  1225. !DS.hasAttributes())
  1226. Diag(Tok, diag::err_typename_requires_specqual);
  1227. // Issue diagnostic and remove storage class if present.
  1228. if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
  1229. if (DS.getStorageClassSpecLoc().isValid())
  1230. Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
  1231. else
  1232. Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
  1233. DS.ClearStorageClassSpecs();
  1234. }
  1235. // Issue diagnostic and remove function specfier if present.
  1236. if (Specs & DeclSpec::PQ_FunctionSpecifier) {
  1237. if (DS.isInlineSpecified())
  1238. Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
  1239. if (DS.isVirtualSpecified())
  1240. Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
  1241. if (DS.isExplicitSpecified())
  1242. Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
  1243. DS.ClearFunctionSpecs();
  1244. }
  1245. }
  1246. /// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
  1247. /// specified token is valid after the identifier in a declarator which
  1248. /// immediately follows the declspec. For example, these things are valid:
  1249. ///
  1250. /// int x [ 4]; // direct-declarator
  1251. /// int x ( int y); // direct-declarator
  1252. /// int(int x ) // direct-declarator
  1253. /// int x ; // simple-declaration
  1254. /// int x = 17; // init-declarator-list
  1255. /// int x , y; // init-declarator-list
  1256. /// int x __asm__ ("foo"); // init-declarator-list
  1257. /// int x : 4; // struct-declarator
  1258. /// int x { 5}; // C++'0x unified initializers
  1259. ///
  1260. /// This is not, because 'x' does not immediately follow the declspec (though
  1261. /// ')' happens to be valid anyway).
  1262. /// int (x)
  1263. ///
  1264. static bool isValidAfterIdentifierInDeclarator(const Token &T) {
  1265. return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
  1266. T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
  1267. T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
  1268. }
  1269. /// ParseImplicitInt - This method is called when we have an non-typename
  1270. /// identifier in a declspec (which normally terminates the decl spec) when
  1271. /// the declspec has no type specifier. In this case, the declspec is either
  1272. /// malformed or is "implicit int" (in K&R and C89).
  1273. ///
  1274. /// This method handles diagnosing this prettily and returns false if the
  1275. /// declspec is done being processed. If it recovers and thinks there may be
  1276. /// other pieces of declspec after it, it returns true.
  1277. ///
  1278. bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
  1279. const ParsedTemplateInfo &TemplateInfo,
  1280. AccessSpecifier AS) {
  1281. assert(Tok.is(tok::identifier) && "should have identifier");
  1282. SourceLocation Loc = Tok.getLocation();
  1283. // If we see an identifier that is not a type name, we normally would
  1284. // parse it as the identifer being declared. However, when a typename
  1285. // is typo'd or the definition is not included, this will incorrectly
  1286. // parse the typename as the identifier name and fall over misparsing
  1287. // later parts of the diagnostic.
  1288. //
  1289. // As such, we try to do some look-ahead in cases where this would
  1290. // otherwise be an "implicit-int" case to see if this is invalid. For
  1291. // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
  1292. // an identifier with implicit int, we'd get a parse error because the
  1293. // next token is obviously invalid for a type. Parse these as a case
  1294. // with an invalid type specifier.
  1295. assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
  1296. // Since we know that this either implicit int (which is rare) or an
  1297. // error, we'd do lookahead to try to do better recovery.
  1298. if (isValidAfterIdentifierInDeclarator(NextToken())) {
  1299. // If this token is valid for implicit int, e.g. "static x = 4", then
  1300. // we just avoid eating the identifier, so it will be parsed as the
  1301. // identifier in the declarator.
  1302. return false;
  1303. }
  1304. // Otherwise, if we don't consume this token, we are going to emit an
  1305. // error anyway. Try to recover from various common problems. Check
  1306. // to see if this was a reference to a tag name without a tag specified.
  1307. // This is a common problem in C (saying 'foo' instead of 'struct foo').
  1308. //
  1309. // C++ doesn't need this, and isTagName doesn't take SS.
  1310. if (SS == 0) {
  1311. const char *TagName = 0, *FixitTagName = 0;
  1312. tok::TokenKind TagKind = tok::unknown;
  1313. switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
  1314. default: break;
  1315. case DeclSpec::TST_enum:
  1316. TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
  1317. case DeclSpec::TST_union:
  1318. TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
  1319. case DeclSpec::TST_struct:
  1320. TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
  1321. case DeclSpec::TST_class:
  1322. TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
  1323. }
  1324. if (TagName) {
  1325. Diag(Loc, diag::err_use_of_tag_name_without_tag)
  1326. << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
  1327. << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
  1328. // Parse this as a tag as if the missing tag were present.
  1329. if (TagKind == tok::kw_enum)
  1330. ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
  1331. else
  1332. ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
  1333. return true;
  1334. }
  1335. }
  1336. // This is almost certainly an invalid type name. Let the action emit a
  1337. // diagnostic and attempt to recover.
  1338. ParsedType T;
  1339. if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
  1340. getCurScope(), SS, T)) {
  1341. // The action emitted a diagnostic, so we don't have to.
  1342. if (T) {
  1343. // The action has suggested that the type T could be used. Set that as
  1344. // the type in the declaration specifiers, consume the would-be type
  1345. // name token, and we're done.
  1346. const char *PrevSpec;
  1347. unsigned DiagID;
  1348. DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
  1349. DS.SetRangeEnd(Tok.getLocation());
  1350. ConsumeToken();
  1351. // There may be other declaration specifiers after this.
  1352. return true;
  1353. }
  1354. // Fall through; the action had no suggestion for us.
  1355. } else {
  1356. // The action did not emit a diagnostic, so emit one now.
  1357. SourceRange R;
  1358. if (SS) R = SS->getRange();
  1359. Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
  1360. }
  1361. // Mark this as an error.
  1362. const char *PrevSpec;
  1363. unsigned DiagID;
  1364. DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
  1365. DS.SetRangeEnd(Tok.getLocation());
  1366. ConsumeToken();
  1367. // TODO: Could inject an invalid typedef decl in an enclosing scope to
  1368. // avoid rippling error messages on subsequent uses of the same type,
  1369. // could be useful if #include was forgotten.
  1370. return false;
  1371. }
  1372. /// \brief Determine the declaration specifier context from the declarator
  1373. /// context.
  1374. ///
  1375. /// \param Context the declarator context, which is one of the
  1376. /// Declarator::TheContext enumerator values.
  1377. Parser::DeclSpecContext
  1378. Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
  1379. if (Context == Declarator::MemberContext)
  1380. return DSC_class;
  1381. if (Context == Declarator::FileContext)
  1382. return DSC_top_level;
  1383. return DSC_normal;
  1384. }
  1385. /// ParseAlignArgument - Parse the argument to an alignment-specifier.
  1386. ///
  1387. /// FIXME: Simply returns an alignof() expression if the argument is a
  1388. /// type. Ideally, the type should be propagated directly into Sema.
  1389. ///
  1390. /// [C11] type-id
  1391. /// [C11] constant-expression
  1392. /// [C++0x] type-id ...[opt]
  1393. /// [C++0x] assignment-expression ...[opt]
  1394. ExprResult Parser::ParseAlignArgument(SourceLocation Start,
  1395. SourceLocation &EllipsisLoc) {
  1396. ExprResult ER;
  1397. if (isTypeIdInParens()) {
  1398. SourceLocation TypeLoc = Tok.getLocation();
  1399. ParsedType Ty = ParseTypeName().get();
  1400. SourceRange TypeRange(Start, Tok.getLocation());
  1401. ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
  1402. Ty.getAsOpaquePtr(), TypeRange);
  1403. } else
  1404. ER = ParseConstantExpression();
  1405. if (getLang().CPlusPlus0x && Tok.is(tok::ellipsis))
  1406. EllipsisLoc = ConsumeToken();
  1407. return ER;
  1408. }
  1409. /// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
  1410. /// attribute to Attrs.
  1411. ///
  1412. /// alignment-specifier:
  1413. /// [C11] '_Alignas' '(' type-id ')'
  1414. /// [C11] '_Alignas' '(' constant-expression ')'
  1415. /// [C++0x] 'alignas' '(' type-id ...[opt] ')'
  1416. /// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
  1417. void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
  1418. SourceLocation *endLoc) {
  1419. assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
  1420. "Not an alignment-specifier!");
  1421. SourceLocation KWLoc = Tok.getLocation();
  1422. ConsumeToken();
  1423. BalancedDelimiterTracker T(*this, tok::l_paren);
  1424. if (T.expectAndConsume(diag::err_expected_lparen))
  1425. return;
  1426. SourceLocation EllipsisLoc;
  1427. ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
  1428. if (ArgExpr.isInvalid()) {
  1429. SkipUntil(tok::r_paren);
  1430. return;
  1431. }
  1432. T.consumeClose();
  1433. if (endLoc)
  1434. *endLoc = T.getCloseLocation();
  1435. // FIXME: Handle pack-expansions here.
  1436. if (EllipsisLoc.isValid()) {
  1437. Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
  1438. return;
  1439. }
  1440. ExprVector ArgExprs(Actions);
  1441. ArgExprs.push_back(ArgExpr.release());
  1442. Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
  1443. 0, T.getOpenLocation(), ArgExprs.take(), 1, false, true);
  1444. }
  1445. /// ParseDeclarationSpecifiers
  1446. /// declaration-specifiers: [C99 6.7]
  1447. /// storage-class-specifier declaration-specifiers[opt]
  1448. /// type-specifier declaration-specifiers[opt]
  1449. /// [C99] function-specifier declaration-specifiers[opt]
  1450. /// [C11] alignment-specifier declaration-specifiers[opt]
  1451. /// [GNU] attributes declaration-specifiers[opt]
  1452. /// [Clang] '__module_private__' declaration-specifiers[opt]
  1453. ///
  1454. /// storage-class-specifier: [C99 6.7.1]
  1455. /// 'typedef'
  1456. /// 'extern'
  1457. /// 'static'
  1458. /// 'auto'
  1459. /// 'register'
  1460. /// [C++] 'mutable'
  1461. /// [GNU] '__thread'
  1462. /// function-specifier: [C99 6.7.4]
  1463. /// [C99] 'inline'
  1464. /// [C++] 'virtual'
  1465. /// [C++] 'explicit'
  1466. /// [OpenCL] '__kernel'
  1467. /// 'friend': [C++ dcl.friend]
  1468. /// 'constexpr': [C++0x dcl.constexpr]
  1469. ///
  1470. void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
  1471. const ParsedTemplateInfo &TemplateInfo,
  1472. AccessSpecifier AS,
  1473. DeclSpecContext DSContext) {
  1474. if (DS.getSourceRange().isInvalid()) {
  1475. DS.SetRangeStart(Tok.getLocation());
  1476. DS.SetRangeEnd(Tok.getLocation());
  1477. }
  1478. bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
  1479. while (1) {
  1480. bool isInvalid = false;
  1481. const char *PrevSpec = 0;
  1482. unsigned DiagID = 0;
  1483. SourceLocation Loc = Tok.getLocation();
  1484. switch (Tok.getKind()) {
  1485. default:
  1486. DoneWithDeclSpec:
  1487. // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt]
  1488. MaybeParseCXX0XAttributes(DS.getAttributes());
  1489. // If this is not a declaration specifier token, we're done reading decl
  1490. // specifiers. First verify that DeclSpec's are consistent.
  1491. DS.Finish(Diags, PP);
  1492. return;
  1493. case tok::code_completion: {
  1494. Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
  1495. if (DS.hasTypeSpecifier()) {
  1496. bool AllowNonIdentifiers
  1497. = (getCurScope()->getFlags() & (Scope::ControlScope |
  1498. Scope::BlockScope |
  1499. Scope::TemplateParamScope |
  1500. Scope::FunctionPrototypeScope |
  1501. Scope::AtCatchScope)) == 0;
  1502. bool AllowNestedNameSpecifiers
  1503. = DSContext == DSC_top_level ||
  1504. (DSContext == DSC_class && DS.isFriendSpecified());
  1505. Actions.CodeCompleteDeclSpec(getCurScope(), DS,
  1506. AllowNonIdentifiers,
  1507. AllowNestedNameSpecifiers);
  1508. return cutOffParsing();
  1509. }
  1510. if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
  1511. CCC = Sema::PCC_LocalDeclarationSpecifiers;
  1512. else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
  1513. CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
  1514. : Sema::PCC_Template;
  1515. else if (DSContext == DSC_class)
  1516. CCC = Sema::PCC_Class;
  1517. else if (ObjCImpDecl)
  1518. CCC = Sema::PCC_ObjCImplementation;
  1519. Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
  1520. return cutOffParsing();
  1521. }
  1522. case tok::coloncolon: // ::foo::bar
  1523. // C++ scope specifier. Annotate and loop, or bail out on error.
  1524. if (TryAnnotateCXXScopeToken(true)) {
  1525. if (!DS.hasTypeSpecifier())
  1526. DS.SetTypeSpecError();
  1527. goto DoneWithDeclSpec;
  1528. }
  1529. if (Tok.is(tok::coloncolon)) // ::new or ::delete
  1530. goto DoneWithDeclSpec;
  1531. continue;
  1532. case tok::annot_cxxscope: {
  1533. if (DS.hasTypeSpecifier())
  1534. goto DoneWithDeclSpec;
  1535. CXXScopeSpec SS;
  1536. Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
  1537. Tok.getAnnotationRange(),
  1538. SS);
  1539. // We are looking for a qualified typename.
  1540. Token Next = NextToken();
  1541. if (Next.is(tok::annot_template_id) &&
  1542. static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
  1543. ->Kind == TNK_Type_template) {
  1544. // We have a qualified template-id, e.g., N::A<int>
  1545. // C++ [class.qual]p2:
  1546. // In a lookup in which the constructor is an acceptable lookup
  1547. // result and the nested-name-specifier nominates a class C:
  1548. //
  1549. // - if the name specified after the
  1550. // nested-name-specifier, when looked up in C, is the
  1551. // injected-class-name of C (Clause 9), or
  1552. //
  1553. // - if the name specified after the nested-name-specifier
  1554. // is the same as the identifier or the
  1555. // simple-template-id's template-name in the last
  1556. // component of the nested-name-specifier,
  1557. //
  1558. // the name is instead considered to name the constructor of
  1559. // class C.
  1560. //
  1561. // Thus, if the template-name is actually the constructor
  1562. // name, then the code is ill-formed; this interpretation is
  1563. // reinforced by the NAD status of core issue 635.
  1564. TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
  1565. if ((DSContext == DSC_top_level ||
  1566. (DSContext == DSC_class && DS.isFriendSpecified())) &&
  1567. TemplateId->Name &&
  1568. Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
  1569. if (isConstructorDeclarator()) {
  1570. // The user meant this to be an out-of-line constructor
  1571. // definition, but template arguments are not allowed
  1572. // there. Just allow this as a constructor; we'll
  1573. // complain about it later.
  1574. goto DoneWithDeclSpec;
  1575. }
  1576. // The user meant this to name a type, but it actually names
  1577. // a constructor with some extraneous template
  1578. // arguments. Complain, then parse it as a type as the user
  1579. // intended.
  1580. Diag(TemplateId->TemplateNameLoc,
  1581. diag::err_out_of_line_template_id_names_constructor)
  1582. << TemplateId->Name;
  1583. }
  1584. DS.getTypeSpecScope() = SS;
  1585. ConsumeToken(); // The C++ scope.
  1586. assert(Tok.is(tok::annot_template_id) &&
  1587. "ParseOptionalCXXScopeSpecifier not working");
  1588. AnnotateTemplateIdTokenAsType();
  1589. continue;
  1590. }
  1591. if (Next.is(tok::annot_typename)) {
  1592. DS.getTypeSpecScope() = SS;
  1593. ConsumeToken(); // The C++ scope.
  1594. if (Tok.getAnnotationValue()) {
  1595. ParsedType T = getTypeAnnotation(Tok);
  1596. isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
  1597. Tok.getAnnotationEndLoc(),
  1598. PrevSpec, DiagID, T);
  1599. }
  1600. else
  1601. DS.SetTypeSpecError();
  1602. DS.SetRangeEnd(Tok.getAnnotationEndLoc());
  1603. ConsumeToken(); // The typename
  1604. }
  1605. if (Next.isNot(tok::identifier))
  1606. goto DoneWithDeclSpec;
  1607. // If we're in a context where the identifier could be a class name,
  1608. // check whether this is a constructor declaration.
  1609. if ((DSContext == DSC_top_level ||
  1610. (DSContext == DSC_class && DS.isFriendSpecified())) &&
  1611. Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
  1612. &SS)) {
  1613. if (isConstructorDeclarator())
  1614. goto DoneWithDeclSpec;
  1615. // As noted in C++ [class.qual]p2 (cited above), when the name
  1616. // of the class is qualified in a context where it could name
  1617. // a constructor, its a constructor name. However, we've
  1618. // looked at the declarator, and the user probably meant this
  1619. // to be a type. Complain that it isn't supposed to be treated
  1620. // as a type, then proceed to parse it as a type.
  1621. Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
  1622. << Next.getIdentifierInfo();
  1623. }
  1624. ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
  1625. Next.getLocation(),
  1626. getCurScope(), &SS,
  1627. false, false, ParsedType(),
  1628. /*IsCtorOrDtorName=*/false,
  1629. /*NonTrivialSourceInfo=*/true);
  1630. // If the referenced identifier is not a type, then this declspec is
  1631. // erroneous: We already checked about that it has no type specifier, and
  1632. // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
  1633. // typename.
  1634. if (TypeRep == 0) {
  1635. ConsumeToken(); // Eat the scope spec so the identifier is current.
  1636. if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
  1637. goto DoneWithDeclSpec;
  1638. }
  1639. DS.getTypeSpecScope() = SS;
  1640. ConsumeToken(); // The C++ scope.
  1641. isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
  1642. DiagID, TypeRep);
  1643. if (isInvalid)
  1644. break;
  1645. DS.SetRangeEnd(Tok.getLocation());
  1646. ConsumeToken(); // The typename.
  1647. continue;
  1648. }
  1649. case tok::annot_typename: {
  1650. if (Tok.getAnnotationValue()) {
  1651. ParsedType T = getTypeAnnotation(Tok);
  1652. isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
  1653. DiagID, T);
  1654. } else
  1655. DS.SetTypeSpecError();
  1656. if (isInvalid)
  1657. break;
  1658. DS.SetRangeEnd(Tok.getAnnotationEndLoc());
  1659. ConsumeToken(); // The typename
  1660. // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
  1661. // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
  1662. // Objective-C interface.
  1663. if (Tok.is(tok::less) && getLang().ObjC1)
  1664. ParseObjCProtocolQualifiers(DS);
  1665. continue;
  1666. }
  1667. case tok::kw___is_signed:
  1668. // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
  1669. // typically treats it as a trait. If we see __is_signed as it appears
  1670. // in libstdc++, e.g.,
  1671. //
  1672. // static const bool __is_signed;
  1673. //
  1674. // then treat __is_signed as an identifier rather than as a keyword.
  1675. if (DS.getTypeSpecType() == TST_bool &&
  1676. DS.getTypeQualifiers() == DeclSpec::TQ_const &&
  1677. DS.getStorageClassSpec() == DeclSpec::SCS_static) {
  1678. Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
  1679. Tok.setKind(tok::identifier);
  1680. }
  1681. // We're done with the declaration-specifiers.
  1682. goto DoneWithDeclSpec;
  1683. // typedef-name
  1684. case tok::kw_decltype:
  1685. case tok::identifier: {
  1686. // In C++, check to see if this is a scope specifier like foo::bar::, if
  1687. // so handle it as such. This is important for ctor parsing.
  1688. if (getLang().CPlusPlus) {
  1689. if (TryAnnotateCXXScopeToken(true)) {
  1690. if (!DS.hasTypeSpecifier())
  1691. DS.SetTypeSpecError();
  1692. goto DoneWithDeclSpec;
  1693. }
  1694. if (!Tok.is(tok::identifier))
  1695. continue;
  1696. }
  1697. // This identifier can only be a typedef name if we haven't already seen
  1698. // a type-specifier. Without this check we misparse:
  1699. // typedef int X; struct Y { short X; }; as 'short int'.
  1700. if (DS.hasTypeSpecifier())
  1701. goto DoneWithDeclSpec;
  1702. // Check for need to substitute AltiVec keyword tokens.
  1703. if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
  1704. break;
  1705. // It has to be available as a typedef too!
  1706. ParsedType TypeRep =
  1707. Actions.getTypeName(*Tok.getIdentifierInfo(),
  1708. Tok.getLocation(), getCurScope());
  1709. // If this is not a typedef name, don't parse it as part of the declspec,
  1710. // it must be an implicit int or an error.
  1711. if (!TypeRep) {
  1712. if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
  1713. goto DoneWithDeclSpec;
  1714. }
  1715. // If we're in a context where the identifier could be a class name,
  1716. // check whether this is a constructor declaration.
  1717. if (getLang().CPlusPlus && DSContext == DSC_class &&
  1718. Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
  1719. isConstructorDeclarator())
  1720. goto DoneWithDeclSpec;
  1721. isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
  1722. DiagID, TypeRep);
  1723. if (isInvalid)
  1724. break;
  1725. DS.SetRangeEnd(Tok.getLocation());
  1726. ConsumeToken(); // The identifier
  1727. // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
  1728. // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
  1729. // Objective-C interface.
  1730. if (Tok.is(tok::less) && getLang().ObjC1)
  1731. ParseObjCProtocolQualifiers(DS);
  1732. // Need to support trailing type qualifiers (e.g. "id<p> const").
  1733. // If a type specifier follows, it will be diagnosed elsewhere.
  1734. continue;
  1735. }
  1736. // type-name
  1737. case tok::annot_template_id: {
  1738. TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
  1739. if (TemplateId->Kind != TNK_Type_template) {
  1740. // This template-id does not refer to a type name, so we're
  1741. // done with the type-specifiers.
  1742. goto DoneWithDeclSpec;
  1743. }
  1744. // If we're in a context where the template-id could be a
  1745. // constructor name or specialization, check whether this is a
  1746. // constructor declaration.
  1747. if (getLang().CPlusPlus && DSContext == DSC_class &&
  1748. Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
  1749. isConstructorDeclarator())
  1750. goto DoneWithDeclSpec;
  1751. // Turn the template-id annotation token into a type annotation
  1752. // token, then try again to parse it as a type-specifier.
  1753. AnnotateTemplateIdTokenAsType();
  1754. continue;
  1755. }
  1756. // GNU attributes support.
  1757. case tok::kw___attribute:
  1758. ParseGNUAttributes(DS.getAttributes());
  1759. continue;
  1760. // Microsoft declspec support.
  1761. case tok::kw___declspec:
  1762. ParseMicrosoftDeclSpec(DS.getAttributes());
  1763. continue;
  1764. // Microsoft single token adornments.
  1765. case tok::kw___forceinline:
  1766. // FIXME: Add handling here!
  1767. break;
  1768. case tok::kw___ptr64:
  1769. case tok::kw___ptr32:
  1770. case tok::kw___w64:
  1771. case tok::kw___cdecl:
  1772. case tok::kw___stdcall:
  1773. case tok::kw___fastcall:
  1774. case tok::kw___thiscall:
  1775. case tok::kw___unaligned:
  1776. ParseMicrosoftTypeAttributes(DS.getAttributes());
  1777. continue;
  1778. // Borland single token adornments.
  1779. case tok::kw___pascal:
  1780. ParseBorlandTypeAttributes(DS.getAttributes());
  1781. continue;
  1782. // OpenCL single token adornments.
  1783. case tok::kw___kernel:
  1784. ParseOpenCLAttributes(DS.getAttributes());
  1785. continue;
  1786. // storage-class-specifier
  1787. case tok::kw_typedef:
  1788. isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
  1789. PrevSpec, DiagID);
  1790. break;
  1791. case tok::kw_extern:
  1792. if (DS.isThreadSpecified())
  1793. Diag(Tok, diag::ext_thread_before) << "extern";
  1794. isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
  1795. PrevSpec, DiagID);
  1796. break;
  1797. case tok::kw___private_extern__:
  1798. isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
  1799. Loc, PrevSpec, DiagID);
  1800. break;
  1801. case tok::kw_static:
  1802. if (DS.isThreadSpecified())
  1803. Diag(Tok, diag::ext_thread_before) << "static";
  1804. isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
  1805. PrevSpec, DiagID);
  1806. break;
  1807. case tok::kw_auto:
  1808. if (getLang().CPlusPlus0x) {
  1809. if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
  1810. isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
  1811. PrevSpec, DiagID);
  1812. if (!isInvalid)
  1813. Diag(Tok, diag::ext_auto_storage_class)
  1814. << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
  1815. } else
  1816. isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
  1817. DiagID);
  1818. } else
  1819. isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
  1820. PrevSpec, DiagID);
  1821. break;
  1822. case tok::kw_register:
  1823. isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
  1824. PrevSpec, DiagID);
  1825. break;
  1826. case tok::kw_mutable:
  1827. isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
  1828. PrevSpec, DiagID);
  1829. break;
  1830. case tok::kw___thread:
  1831. isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
  1832. break;
  1833. // function-specifier
  1834. case tok::kw_inline:
  1835. isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
  1836. break;
  1837. case tok::kw_virtual:
  1838. isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
  1839. break;
  1840. case tok::kw_explicit:
  1841. isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
  1842. break;
  1843. // alignment-specifier
  1844. case tok::kw__Alignas:
  1845. if (!getLang().C11)
  1846. Diag(Tok, diag::ext_c11_alignas);
  1847. ParseAlignmentSpecifier(DS.getAttributes());
  1848. continue;
  1849. // friend
  1850. case tok::kw_friend:
  1851. if (DSContext == DSC_class)
  1852. isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
  1853. else {
  1854. PrevSpec = ""; // not actually used by the diagnostic
  1855. DiagID = diag::err_friend_invalid_in_context;
  1856. isInvalid = true;
  1857. }
  1858. break;
  1859. // Modules
  1860. case tok::kw___module_private__:
  1861. isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
  1862. break;
  1863. // constexpr
  1864. case tok::kw_constexpr:
  1865. isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
  1866. break;
  1867. // type-specifier
  1868. case tok::kw_short:
  1869. isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
  1870. DiagID);
  1871. break;
  1872. case tok::kw_long:
  1873. if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
  1874. isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
  1875. DiagID);
  1876. else
  1877. isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
  1878. DiagID);
  1879. break;
  1880. case tok::kw___int64:
  1881. isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
  1882. DiagID);
  1883. break;
  1884. case tok::kw_signed:
  1885. isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
  1886. DiagID);
  1887. break;
  1888. case tok::kw_unsigned:
  1889. isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
  1890. DiagID);
  1891. break;
  1892. case tok::kw__Complex:
  1893. isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
  1894. DiagID);
  1895. break;
  1896. case tok::kw__Imaginary:
  1897. isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
  1898. DiagID);
  1899. break;
  1900. case tok::kw_void:
  1901. isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
  1902. DiagID);
  1903. break;
  1904. case tok::kw_char:
  1905. isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
  1906. DiagID);
  1907. break;
  1908. case tok::kw_int:
  1909. isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
  1910. DiagID);
  1911. break;
  1912. case tok::kw_half:
  1913. isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
  1914. DiagID);
  1915. break;
  1916. case tok::kw_float:
  1917. isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
  1918. DiagID);
  1919. break;
  1920. case tok::kw_double:
  1921. isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
  1922. DiagID);
  1923. break;
  1924. case tok::kw_wchar_t:
  1925. isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
  1926. DiagID);
  1927. break;
  1928. case tok::kw_char16_t:
  1929. isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
  1930. DiagID);
  1931. break;
  1932. case tok::kw_char32_t:
  1933. isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
  1934. DiagID);
  1935. break;
  1936. case tok::kw_bool:
  1937. case tok::kw__Bool:
  1938. if (Tok.is(tok::kw_bool) &&
  1939. DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
  1940. DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
  1941. PrevSpec = ""; // Not used by the diagnostic.
  1942. DiagID = diag::err_bool_redeclaration;
  1943. // For better error recovery.
  1944. Tok.setKind(tok::identifier);
  1945. isInvalid = true;
  1946. } else {
  1947. isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
  1948. DiagID);
  1949. }
  1950. break;
  1951. case tok::kw__Decimal32:
  1952. isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
  1953. DiagID);
  1954. break;
  1955. case tok::kw__Decimal64:
  1956. isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
  1957. DiagID);
  1958. break;
  1959. case tok::kw__Decimal128:
  1960. isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
  1961. DiagID);
  1962. break;
  1963. case tok::kw___vector:
  1964. isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
  1965. break;
  1966. case tok::kw___pixel:
  1967. isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
  1968. break;
  1969. case tok::kw___unknown_anytype:
  1970. isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
  1971. PrevSpec, DiagID);
  1972. break;
  1973. // class-specifier:
  1974. case tok::kw_class:
  1975. case tok::kw_struct:
  1976. case tok::kw_union: {
  1977. tok::TokenKind Kind = Tok.getKind();
  1978. ConsumeToken();
  1979. ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS, EnteringContext);
  1980. continue;
  1981. }
  1982. // enum-specifier:
  1983. case tok::kw_enum:
  1984. ConsumeToken();
  1985. ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
  1986. continue;
  1987. // cv-qualifier:
  1988. case tok::kw_const:
  1989. isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
  1990. getLang());
  1991. break;
  1992. case tok::kw_volatile:
  1993. isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
  1994. getLang());
  1995. break;
  1996. case tok::kw_restrict:
  1997. isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
  1998. getLang());
  1999. break;
  2000. // C++ typename-specifier:
  2001. case tok::kw_typename:
  2002. if (TryAnnotateTypeOrScopeToken()) {
  2003. DS.SetTypeSpecError();
  2004. goto DoneWithDeclSpec;
  2005. }
  2006. if (!Tok.is(tok::kw_typename))
  2007. continue;
  2008. break;
  2009. // GNU typeof support.
  2010. case tok::kw_typeof:
  2011. ParseTypeofSpecifier(DS);
  2012. continue;
  2013. case tok::annot_decltype:
  2014. ParseDecltypeSpecifier(DS);
  2015. continue;
  2016. case tok::kw___underlying_type:
  2017. ParseUnderlyingTypeSpecifier(DS);
  2018. continue;
  2019. case tok::kw__Atomic:
  2020. ParseAtomicSpecifier(DS);
  2021. continue;
  2022. // OpenCL qualifiers:
  2023. case tok::kw_private:
  2024. if (!getLang().OpenCL)
  2025. goto DoneWithDeclSpec;
  2026. case tok::kw___private:
  2027. case tok::kw___global:
  2028. case tok::kw___local:
  2029. case tok::kw___constant:
  2030. case tok::kw___read_only:
  2031. case tok::kw___write_only:
  2032. case tok::kw___read_write:
  2033. ParseOpenCLQualifiers(DS);
  2034. break;
  2035. case tok::less:
  2036. // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
  2037. // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
  2038. // but we support it.
  2039. if (DS.hasTypeSpecifier() || !getLang().ObjC1)
  2040. goto DoneWithDeclSpec;
  2041. if (!ParseObjCProtocolQualifiers(DS))
  2042. Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
  2043. << FixItHint::CreateInsertion(Loc, "id")
  2044. << SourceRange(Loc, DS.getSourceRange().getEnd());
  2045. // Need to support trailing type qualifiers (e.g. "id<p> const").
  2046. // If a type specifier follows, it will be diagnosed elsewhere.
  2047. continue;
  2048. }
  2049. // If the specifier wasn't legal, issue a diagnostic.
  2050. if (isInvalid) {
  2051. assert(PrevSpec && "Method did not return previous specifier!");
  2052. assert(DiagID);
  2053. if (DiagID == diag::ext_duplicate_declspec)
  2054. Diag(Tok, DiagID)
  2055. << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
  2056. else
  2057. Diag(Tok, DiagID) << PrevSpec;
  2058. }
  2059. DS.SetRangeEnd(Tok.getLocation());
  2060. if (DiagID != diag::err_bool_redeclaration)
  2061. ConsumeToken();
  2062. }
  2063. }
  2064. /// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
  2065. /// primarily follow the C++ grammar with additions for C99 and GNU,
  2066. /// which together subsume the C grammar. Note that the C++
  2067. /// type-specifier also includes the C type-qualifier (for const,
  2068. /// volatile, and C99 restrict). Returns true if a type-specifier was
  2069. /// found (and parsed), false otherwise.
  2070. ///
  2071. /// type-specifier: [C++ 7.1.5]
  2072. /// simple-type-specifier
  2073. /// class-specifier
  2074. /// enum-specifier
  2075. /// elaborated-type-specifier [TODO]
  2076. /// cv-qualifier
  2077. ///
  2078. /// cv-qualifier: [C++ 7.1.5.1]
  2079. /// 'const'
  2080. /// 'volatile'
  2081. /// [C99] 'restrict'
  2082. ///
  2083. /// simple-type-specifier: [ C++ 7.1.5.2]
  2084. /// '::'[opt] nested-name-specifier[opt] type-name [TODO]
  2085. /// '::'[opt] nested-name-specifier 'template' template-id [TODO]
  2086. /// 'char'
  2087. /// 'wchar_t'
  2088. /// 'bool'
  2089. /// 'short'
  2090. /// 'int'
  2091. /// 'long'
  2092. /// 'signed'
  2093. /// 'unsigned'
  2094. /// 'float'
  2095. /// 'double'
  2096. /// 'void'
  2097. /// [C99] '_Bool'
  2098. /// [C99] '_Complex'
  2099. /// [C99] '_Imaginary' // Removed in TC2?
  2100. /// [GNU] '_Decimal32'
  2101. /// [GNU] '_Decimal64'
  2102. /// [GNU] '_Decimal128'
  2103. /// [GNU] typeof-specifier
  2104. /// [OBJC] class-name objc-protocol-refs[opt] [TODO]
  2105. /// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
  2106. /// [C++0x] 'decltype' ( expression )
  2107. /// [AltiVec] '__vector'
  2108. bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
  2109. const char *&PrevSpec,
  2110. unsigned &DiagID,
  2111. const ParsedTemplateInfo &TemplateInfo,
  2112. bool SuppressDeclarations) {
  2113. SourceLocation Loc = Tok.getLocation();
  2114. switch (Tok.getKind()) {
  2115. case tok::identifier: // foo::bar
  2116. // If we already have a type specifier, this identifier is not a type.
  2117. if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
  2118. DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
  2119. DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
  2120. return false;
  2121. // Check for need to substitute AltiVec keyword tokens.
  2122. if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
  2123. break;
  2124. // Fall through.
  2125. case tok::kw_decltype:
  2126. case tok::kw_typename: // typename foo::bar
  2127. // Annotate typenames and C++ scope specifiers. If we get one, just
  2128. // recurse to handle whatever we get.
  2129. if (TryAnnotateTypeOrScopeToken(/*EnteringContext=*/false,
  2130. /*NeedType=*/true))
  2131. return true;
  2132. if (Tok.is(tok::identifier))
  2133. return false;
  2134. return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
  2135. TemplateInfo, SuppressDeclarations);
  2136. case tok::coloncolon: // ::foo::bar
  2137. if (NextToken().is(tok::kw_new) || // ::new
  2138. NextToken().is(tok::kw_delete)) // ::delete
  2139. return false;
  2140. // Annotate typenames and C++ scope specifiers. If we get one, just
  2141. // recurse to handle whatever we get.
  2142. if (TryAnnotateTypeOrScopeToken(/*EnteringContext=*/false,
  2143. /*NeedType=*/true))
  2144. return true;
  2145. return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
  2146. TemplateInfo, SuppressDeclarations);
  2147. // simple-type-specifier:
  2148. case tok::annot_typename: {
  2149. if (ParsedType T = getTypeAnnotation(Tok)) {
  2150. isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
  2151. Tok.getAnnotationEndLoc(), PrevSpec,
  2152. DiagID, T);
  2153. } else
  2154. DS.SetTypeSpecError();
  2155. DS.SetRangeEnd(Tok.getAnnotationEndLoc());
  2156. ConsumeToken(); // The typename
  2157. // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
  2158. // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
  2159. // Objective-C interface. If we don't have Objective-C or a '<', this is
  2160. // just a normal reference to a typedef name.
  2161. if (Tok.is(tok::less) && getLang().ObjC1)
  2162. ParseObjCProtocolQualifiers(DS);
  2163. return true;
  2164. }
  2165. case tok::kw_short:
  2166. isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
  2167. break;
  2168. case tok::kw_long:
  2169. if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
  2170. isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
  2171. DiagID);
  2172. else
  2173. isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
  2174. DiagID);
  2175. break;
  2176. case tok::kw___int64:
  2177. isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
  2178. DiagID);
  2179. break;
  2180. case tok::kw_signed:
  2181. isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
  2182. break;
  2183. case tok::kw_unsigned:
  2184. isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
  2185. DiagID);
  2186. break;
  2187. case tok::kw__Complex:
  2188. isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
  2189. DiagID);
  2190. break;
  2191. case tok::kw__Imaginary:
  2192. isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
  2193. DiagID);
  2194. break;
  2195. case tok::kw_void:
  2196. isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
  2197. break;
  2198. case tok::kw_char:
  2199. isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
  2200. break;
  2201. case tok::kw_int:
  2202. isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
  2203. break;
  2204. case tok::kw_half:
  2205. isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
  2206. break;
  2207. case tok::kw_float:
  2208. isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
  2209. break;
  2210. case tok::kw_double:
  2211. isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
  2212. break;
  2213. case tok::kw_wchar_t:
  2214. isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
  2215. break;
  2216. case tok::kw_char16_t:
  2217. isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
  2218. break;
  2219. case tok::kw_char32_t:
  2220. isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
  2221. break;
  2222. case tok::kw_bool:
  2223. case tok::kw__Bool:
  2224. isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
  2225. break;
  2226. case tok::kw__Decimal32:
  2227. isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
  2228. DiagID);
  2229. break;
  2230. case tok::kw__Decimal64:
  2231. isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
  2232. DiagID);
  2233. break;
  2234. case tok::kw__Decimal128:
  2235. isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
  2236. DiagID);
  2237. break;
  2238. case tok::kw___vector:
  2239. isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
  2240. break;
  2241. case tok::kw___pixel:
  2242. isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
  2243. break;
  2244. // class-specifier:
  2245. case tok::kw_class:
  2246. case tok::kw_struct:
  2247. case tok::kw_union: {
  2248. tok::TokenKind Kind = Tok.getKind();
  2249. ConsumeToken();
  2250. ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
  2251. /*EnteringContext=*/false,
  2252. SuppressDeclarations);
  2253. return true;
  2254. }
  2255. // enum-specifier:
  2256. case tok::kw_enum:
  2257. ConsumeToken();
  2258. ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
  2259. return true;
  2260. // cv-qualifier:
  2261. case tok::kw_const:
  2262. isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
  2263. DiagID, getLang());
  2264. break;
  2265. case tok::kw_volatile:
  2266. isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
  2267. DiagID, getLang());
  2268. break;
  2269. case tok::kw_restrict:
  2270. isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
  2271. DiagID, getLang());
  2272. break;
  2273. // GNU typeof support.
  2274. case tok::kw_typeof:
  2275. ParseTypeofSpecifier(DS);
  2276. return true;
  2277. // C++0x decltype support.
  2278. case tok::annot_decltype:
  2279. ParseDecltypeSpecifier(DS);
  2280. return true;
  2281. // C++0x type traits support.
  2282. case tok::kw___underlying_type:
  2283. ParseUnderlyingTypeSpecifier(DS);
  2284. return true;
  2285. case tok::kw__Atomic:
  2286. ParseAtomicSpecifier(DS);
  2287. return true;
  2288. // OpenCL qualifiers:
  2289. case tok::kw_private:
  2290. if (!getLang().OpenCL)
  2291. return false;
  2292. case tok::kw___private:
  2293. case tok::kw___global:
  2294. case tok::kw___local:
  2295. case tok::kw___constant:
  2296. case tok::kw___read_only:
  2297. case tok::kw___write_only:
  2298. case tok::kw___read_write:
  2299. ParseOpenCLQualifiers(DS);
  2300. break;
  2301. // C++0x auto support.
  2302. case tok::kw_auto:
  2303. // This is only called in situations where a storage-class specifier is
  2304. // illegal, so we can assume an auto type specifier was intended even in
  2305. // C++98. In C++98 mode, DeclSpec::Finish will produce an appropriate
  2306. // extension diagnostic.
  2307. if (!getLang().CPlusPlus)
  2308. return false;
  2309. isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
  2310. break;
  2311. case tok::kw___ptr64:
  2312. case tok::kw___ptr32:
  2313. case tok::kw___w64:
  2314. case tok::kw___cdecl:
  2315. case tok::kw___stdcall:
  2316. case tok::kw___fastcall:
  2317. case tok::kw___thiscall:
  2318. case tok::kw___unaligned:
  2319. ParseMicrosoftTypeAttributes(DS.getAttributes());
  2320. return true;
  2321. case tok::kw___pascal:
  2322. ParseBorlandTypeAttributes(DS.getAttributes());
  2323. return true;
  2324. default:
  2325. // Not a type-specifier; do nothing.
  2326. return false;
  2327. }
  2328. // If the specifier combination wasn't legal, issue a diagnostic.
  2329. if (isInvalid) {
  2330. assert(PrevSpec && "Method did not return previous specifier!");
  2331. // Pick between error or extwarn.
  2332. Diag(Tok, DiagID) << PrevSpec;
  2333. }
  2334. DS.SetRangeEnd(Tok.getLocation());
  2335. ConsumeToken(); // whatever we parsed above.
  2336. return true;
  2337. }
  2338. /// ParseStructDeclaration - Parse a struct declaration without the terminating
  2339. /// semicolon.
  2340. ///
  2341. /// struct-declaration:
  2342. /// specifier-qualifier-list struct-declarator-list
  2343. /// [GNU] __extension__ struct-declaration
  2344. /// [GNU] specifier-qualifier-list
  2345. /// struct-declarator-list:
  2346. /// struct-declarator
  2347. /// struct-declarator-list ',' struct-declarator
  2348. /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
  2349. /// struct-declarator:
  2350. /// declarator
  2351. /// [GNU] declarator attributes[opt]
  2352. /// declarator[opt] ':' constant-expression
  2353. /// [GNU] declarator[opt] ':' constant-expression attributes[opt]
  2354. ///
  2355. void Parser::
  2356. ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
  2357. if (Tok.is(tok::kw___extension__)) {
  2358. // __extension__ silences extension warnings in the subexpression.
  2359. ExtensionRAIIObject O(Diags); // Use RAII to do this.
  2360. ConsumeToken();
  2361. return ParseStructDeclaration(DS, Fields);
  2362. }
  2363. // Parse the common specifier-qualifiers-list piece.
  2364. ParseSpecifierQualifierList(DS);
  2365. // If there are no declarators, this is a free-standing declaration
  2366. // specifier. Let the actions module cope with it.
  2367. if (Tok.is(tok::semi)) {
  2368. Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
  2369. return;
  2370. }
  2371. // Read struct-declarators until we find the semicolon.
  2372. bool FirstDeclarator = true;
  2373. SourceLocation CommaLoc;
  2374. while (1) {
  2375. ParsingDeclRAIIObject PD(*this);
  2376. FieldDeclarator DeclaratorInfo(DS);
  2377. DeclaratorInfo.D.setCommaLoc(CommaLoc);
  2378. // Attributes are only allowed here on successive declarators.
  2379. if (!FirstDeclarator)
  2380. MaybeParseGNUAttributes(DeclaratorInfo.D);
  2381. /// struct-declarator: declarator
  2382. /// struct-declarator: declarator[opt] ':' constant-expression
  2383. if (Tok.isNot(tok::colon)) {
  2384. // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
  2385. ColonProtectionRAIIObject X(*this);
  2386. ParseDeclarator(DeclaratorInfo.D);
  2387. }
  2388. if (Tok.is(tok::colon)) {
  2389. ConsumeToken();
  2390. ExprResult Res(ParseConstantExpression());
  2391. if (Res.isInvalid())
  2392. SkipUntil(tok::semi, true, true);
  2393. else
  2394. DeclaratorInfo.BitfieldSize = Res.release();
  2395. }
  2396. // If attributes exist after the declarator, parse them.
  2397. MaybeParseGNUAttributes(DeclaratorInfo.D);
  2398. // We're done with this declarator; invoke the callback.
  2399. Decl *D = Fields.invoke(DeclaratorInfo);
  2400. PD.complete(D);
  2401. // If we don't have a comma, it is either the end of the list (a ';')
  2402. // or an error, bail out.
  2403. if (Tok.isNot(tok::comma))
  2404. return;
  2405. // Consume the comma.
  2406. CommaLoc = ConsumeToken();
  2407. FirstDeclarator = false;
  2408. }
  2409. }
  2410. /// ParseStructUnionBody
  2411. /// struct-contents:
  2412. /// struct-declaration-list
  2413. /// [EXT] empty
  2414. /// [GNU] "struct-declaration-list" without terminatoring ';'
  2415. /// struct-declaration-list:
  2416. /// struct-declaration
  2417. /// struct-declaration-list struct-declaration
  2418. /// [OBC] '@' 'defs' '(' class-name ')'
  2419. ///
  2420. void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
  2421. unsigned TagType, Decl *TagDecl) {
  2422. PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
  2423. "parsing struct/union body");
  2424. BalancedDelimiterTracker T(*this, tok::l_brace);
  2425. if (T.consumeOpen())
  2426. return;
  2427. ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
  2428. Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
  2429. // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
  2430. // C++.
  2431. if (Tok.is(tok::r_brace) && !getLang().CPlusPlus) {
  2432. Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
  2433. Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
  2434. }
  2435. SmallVector<Decl *, 32> FieldDecls;
  2436. // While we still have something to read, read the declarations in the struct.
  2437. while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
  2438. // Each iteration of this loop reads one struct-declaration.
  2439. // Check for extraneous top-level semicolon.
  2440. if (Tok.is(tok::semi)) {
  2441. Diag(Tok, diag::ext_extra_struct_semi)
  2442. << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
  2443. << FixItHint::CreateRemoval(Tok.getLocation());
  2444. ConsumeToken();
  2445. continue;
  2446. }
  2447. // Parse all the comma separated declarators.
  2448. DeclSpec DS(AttrFactory);
  2449. if (!Tok.is(tok::at)) {
  2450. struct CFieldCallback : FieldCallback {
  2451. Parser &P;
  2452. Decl *TagDecl;
  2453. SmallVectorImpl<Decl *> &FieldDecls;
  2454. CFieldCallback(Parser &P, Decl *TagDecl,
  2455. SmallVectorImpl<Decl *> &FieldDecls) :
  2456. P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
  2457. virtual Decl *invoke(FieldDeclarator &FD) {
  2458. // Install the declarator into the current TagDecl.
  2459. Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
  2460. FD.D.getDeclSpec().getSourceRange().getBegin(),
  2461. FD.D, FD.BitfieldSize);
  2462. FieldDecls.push_back(Field);
  2463. return Field;
  2464. }
  2465. } Callback(*this, TagDecl, FieldDecls);
  2466. ParseStructDeclaration(DS, Callback);
  2467. } else { // Handle @defs
  2468. ConsumeToken();
  2469. if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
  2470. Diag(Tok, diag::err_unexpected_at);
  2471. SkipUntil(tok::semi, true);
  2472. continue;
  2473. }
  2474. ConsumeToken();
  2475. ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
  2476. if (!Tok.is(tok::identifier)) {
  2477. Diag(Tok, diag::err_expected_ident);
  2478. SkipUntil(tok::semi, true);
  2479. continue;
  2480. }
  2481. SmallVector<Decl *, 16> Fields;
  2482. Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
  2483. Tok.getIdentifierInfo(), Fields);
  2484. FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
  2485. ConsumeToken();
  2486. ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
  2487. }
  2488. if (Tok.is(tok::semi)) {
  2489. ConsumeToken();
  2490. } else if (Tok.is(tok::r_brace)) {
  2491. ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
  2492. break;
  2493. } else {
  2494. ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
  2495. // Skip to end of block or statement to avoid ext-warning on extra ';'.
  2496. SkipUntil(tok::r_brace, true, true);
  2497. // If we stopped at a ';', eat it.
  2498. if (Tok.is(tok::semi)) ConsumeToken();
  2499. }
  2500. }
  2501. T.consumeClose();
  2502. ParsedAttributes attrs(AttrFactory);
  2503. // If attributes exist after struct contents, parse them.
  2504. MaybeParseGNUAttributes(attrs);
  2505. Actions.ActOnFields(getCurScope(),
  2506. RecordLoc, TagDecl, FieldDecls,
  2507. T.getOpenLocation(), T.getCloseLocation(),
  2508. attrs.getList());
  2509. StructScope.Exit();
  2510. Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
  2511. T.getCloseLocation());
  2512. }
  2513. /// ParseEnumSpecifier
  2514. /// enum-specifier: [C99 6.7.2.2]
  2515. /// 'enum' identifier[opt] '{' enumerator-list '}'
  2516. ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
  2517. /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
  2518. /// '}' attributes[opt]
  2519. /// 'enum' identifier
  2520. /// [GNU] 'enum' attributes[opt] identifier
  2521. ///
  2522. /// [C++0x] enum-head '{' enumerator-list[opt] '}'
  2523. /// [C++0x] enum-head '{' enumerator-list ',' '}'
  2524. ///
  2525. /// enum-head: [C++0x]
  2526. /// enum-key attributes[opt] identifier[opt] enum-base[opt]
  2527. /// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
  2528. ///
  2529. /// enum-key: [C++0x]
  2530. /// 'enum'
  2531. /// 'enum' 'class'
  2532. /// 'enum' 'struct'
  2533. ///
  2534. /// enum-base: [C++0x]
  2535. /// ':' type-specifier-seq
  2536. ///
  2537. /// [C++] elaborated-type-specifier:
  2538. /// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
  2539. ///
  2540. void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
  2541. const ParsedTemplateInfo &TemplateInfo,
  2542. AccessSpecifier AS) {
  2543. // Parse the tag portion of this.
  2544. if (Tok.is(tok::code_completion)) {
  2545. // Code completion for an enum name.
  2546. Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
  2547. return cutOffParsing();
  2548. }
  2549. SourceLocation ScopedEnumKWLoc;
  2550. bool IsScopedUsingClassTag = false;
  2551. if (getLang().CPlusPlus0x &&
  2552. (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
  2553. Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
  2554. IsScopedUsingClassTag = Tok.is(tok::kw_class);
  2555. ScopedEnumKWLoc = ConsumeToken();
  2556. }
  2557. // If attributes exist after tag, parse them.
  2558. ParsedAttributes attrs(AttrFactory);
  2559. MaybeParseGNUAttributes(attrs);
  2560. bool AllowFixedUnderlyingType
  2561. = getLang().CPlusPlus0x || getLang().MicrosoftExt || getLang().ObjC2;
  2562. CXXScopeSpec &SS = DS.getTypeSpecScope();
  2563. if (getLang().CPlusPlus) {
  2564. // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
  2565. // if a fixed underlying type is allowed.
  2566. ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
  2567. if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
  2568. /*EnteringContext=*/false))
  2569. return;
  2570. if (SS.isSet() && Tok.isNot(tok::identifier)) {
  2571. Diag(Tok, diag::err_expected_ident);
  2572. if (Tok.isNot(tok::l_brace)) {
  2573. // Has no name and is not a definition.
  2574. // Skip the rest of this declarator, up until the comma or semicolon.
  2575. SkipUntil(tok::comma, true);
  2576. return;
  2577. }
  2578. }
  2579. }
  2580. // Must have either 'enum name' or 'enum {...}'.
  2581. if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
  2582. (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
  2583. Diag(Tok, diag::err_expected_ident_lbrace);
  2584. // Skip the rest of this declarator, up until the comma or semicolon.
  2585. SkipUntil(tok::comma, true);
  2586. return;
  2587. }
  2588. // If an identifier is present, consume and remember it.
  2589. IdentifierInfo *Name = 0;
  2590. SourceLocation NameLoc;
  2591. if (Tok.is(tok::identifier)) {
  2592. Name = Tok.getIdentifierInfo();
  2593. NameLoc = ConsumeToken();
  2594. }
  2595. if (!Name && ScopedEnumKWLoc.isValid()) {
  2596. // C++0x 7.2p2: The optional identifier shall not be omitted in the
  2597. // declaration of a scoped enumeration.
  2598. Diag(Tok, diag::err_scoped_enum_missing_identifier);
  2599. ScopedEnumKWLoc = SourceLocation();
  2600. IsScopedUsingClassTag = false;
  2601. }
  2602. TypeResult BaseType;
  2603. // Parse the fixed underlying type.
  2604. if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
  2605. bool PossibleBitfield = false;
  2606. if (getCurScope()->getFlags() & Scope::ClassScope) {
  2607. // If we're in class scope, this can either be an enum declaration with
  2608. // an underlying type, or a declaration of a bitfield member. We try to
  2609. // use a simple disambiguation scheme first to catch the common cases
  2610. // (integer literal, sizeof); if it's still ambiguous, we then consider
  2611. // anything that's a simple-type-specifier followed by '(' as an
  2612. // expression. This suffices because function types are not valid
  2613. // underlying types anyway.
  2614. TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
  2615. // If the next token starts an expression, we know we're parsing a
  2616. // bit-field. This is the common case.
  2617. if (TPR == TPResult::True())
  2618. PossibleBitfield = true;
  2619. // If the next token starts a type-specifier-seq, it may be either a
  2620. // a fixed underlying type or the start of a function-style cast in C++;
  2621. // lookahead one more token to see if it's obvious that we have a
  2622. // fixed underlying type.
  2623. else if (TPR == TPResult::False() &&
  2624. GetLookAheadToken(2).getKind() == tok::semi) {
  2625. // Consume the ':'.
  2626. ConsumeToken();
  2627. } else {
  2628. // We have the start of a type-specifier-seq, so we have to perform
  2629. // tentative parsing to determine whether we have an expression or a
  2630. // type.
  2631. TentativeParsingAction TPA(*this);
  2632. // Consume the ':'.
  2633. ConsumeToken();
  2634. if ((getLang().CPlusPlus &&
  2635. isCXXDeclarationSpecifier() != TPResult::True()) ||
  2636. (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
  2637. // We'll parse this as a bitfield later.
  2638. PossibleBitfield = true;
  2639. TPA.Revert();
  2640. } else {
  2641. // We have a type-specifier-seq.
  2642. TPA.Commit();
  2643. }
  2644. }
  2645. } else {
  2646. // Consume the ':'.
  2647. ConsumeToken();
  2648. }
  2649. if (!PossibleBitfield) {
  2650. SourceRange Range;
  2651. BaseType = ParseTypeName(&Range);
  2652. if (!getLang().CPlusPlus0x && !getLang().ObjC2)
  2653. Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
  2654. << Range;
  2655. if (getLang().CPlusPlus0x)
  2656. Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
  2657. }
  2658. }
  2659. // There are four options here. If we have 'friend enum foo;' then this is a
  2660. // friend declaration, and cannot have an accompanying definition. If we have
  2661. // 'enum foo;', then this is a forward declaration. If we have
  2662. // 'enum foo {...' then this is a definition. Otherwise we have something
  2663. // like 'enum foo xyz', a reference.
  2664. //
  2665. // This is needed to handle stuff like this right (C99 6.7.2.3p11):
  2666. // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
  2667. // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
  2668. //
  2669. Sema::TagUseKind TUK;
  2670. if (DS.isFriendSpecified())
  2671. TUK = Sema::TUK_Friend;
  2672. else if (Tok.is(tok::l_brace))
  2673. TUK = Sema::TUK_Definition;
  2674. else if (Tok.is(tok::semi))
  2675. TUK = Sema::TUK_Declaration;
  2676. else
  2677. TUK = Sema::TUK_Reference;
  2678. // enums cannot be templates, although they can be referenced from a
  2679. // template.
  2680. if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
  2681. TUK != Sema::TUK_Reference) {
  2682. Diag(Tok, diag::err_enum_template);
  2683. // Skip the rest of this declarator, up until the comma or semicolon.
  2684. SkipUntil(tok::comma, true);
  2685. return;
  2686. }
  2687. if (!Name && TUK != Sema::TUK_Definition) {
  2688. Diag(Tok, diag::err_enumerator_unnamed_no_def);
  2689. // Skip the rest of this declarator, up until the comma or semicolon.
  2690. SkipUntil(tok::comma, true);
  2691. return;
  2692. }
  2693. bool Owned = false;
  2694. bool IsDependent = false;
  2695. const char *PrevSpec = 0;
  2696. unsigned DiagID;
  2697. Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
  2698. StartLoc, SS, Name, NameLoc, attrs.getList(),
  2699. AS, DS.getModulePrivateSpecLoc(),
  2700. MultiTemplateParamsArg(Actions),
  2701. Owned, IsDependent, ScopedEnumKWLoc,
  2702. IsScopedUsingClassTag, BaseType);
  2703. if (IsDependent) {
  2704. // This enum has a dependent nested-name-specifier. Handle it as a
  2705. // dependent tag.
  2706. if (!Name) {
  2707. DS.SetTypeSpecError();
  2708. Diag(Tok, diag::err_expected_type_name_after_typename);
  2709. return;
  2710. }
  2711. TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
  2712. TUK, SS, Name, StartLoc,
  2713. NameLoc);
  2714. if (Type.isInvalid()) {
  2715. DS.SetTypeSpecError();
  2716. return;
  2717. }
  2718. if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
  2719. NameLoc.isValid() ? NameLoc : StartLoc,
  2720. PrevSpec, DiagID, Type.get()))
  2721. Diag(StartLoc, DiagID) << PrevSpec;
  2722. return;
  2723. }
  2724. if (!TagDecl) {
  2725. // The action failed to produce an enumeration tag. If this is a
  2726. // definition, consume the entire definition.
  2727. if (Tok.is(tok::l_brace)) {
  2728. ConsumeBrace();
  2729. SkipUntil(tok::r_brace);
  2730. }
  2731. DS.SetTypeSpecError();
  2732. return;
  2733. }
  2734. if (Tok.is(tok::l_brace)) {
  2735. if (TUK == Sema::TUK_Friend)
  2736. Diag(Tok, diag::err_friend_decl_defines_type)
  2737. << SourceRange(DS.getFriendSpecLoc());
  2738. ParseEnumBody(StartLoc, TagDecl);
  2739. }
  2740. if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
  2741. NameLoc.isValid() ? NameLoc : StartLoc,
  2742. PrevSpec, DiagID, TagDecl, Owned))
  2743. Diag(StartLoc, DiagID) << PrevSpec;
  2744. }
  2745. /// ParseEnumBody - Parse a {} enclosed enumerator-list.
  2746. /// enumerator-list:
  2747. /// enumerator
  2748. /// enumerator-list ',' enumerator
  2749. /// enumerator:
  2750. /// enumeration-constant
  2751. /// enumeration-constant '=' constant-expression
  2752. /// enumeration-constant:
  2753. /// identifier
  2754. ///
  2755. void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
  2756. // Enter the scope of the enum body and start the definition.
  2757. ParseScope EnumScope(this, Scope::DeclScope);
  2758. Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
  2759. BalancedDelimiterTracker T(*this, tok::l_brace);
  2760. T.consumeOpen();
  2761. // C does not allow an empty enumerator-list, C++ does [dcl.enum].
  2762. if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
  2763. Diag(Tok, diag::error_empty_enum);
  2764. SmallVector<Decl *, 32> EnumConstantDecls;
  2765. Decl *LastEnumConstDecl = 0;
  2766. // Parse the enumerator-list.
  2767. while (Tok.is(tok::identifier)) {
  2768. IdentifierInfo *Ident = Tok.getIdentifierInfo();
  2769. SourceLocation IdentLoc = ConsumeToken();
  2770. // If attributes exist after the enumerator, parse them.
  2771. ParsedAttributes attrs(AttrFactory);
  2772. MaybeParseGNUAttributes(attrs);
  2773. SourceLocation EqualLoc;
  2774. ExprResult AssignedVal;
  2775. ParsingDeclRAIIObject PD(*this);
  2776. if (Tok.is(tok::equal)) {
  2777. EqualLoc = ConsumeToken();
  2778. AssignedVal = ParseConstantExpression();
  2779. if (AssignedVal.isInvalid())
  2780. SkipUntil(tok::comma, tok::r_brace, true, true);
  2781. }
  2782. // Install the enumerator constant into EnumDecl.
  2783. Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
  2784. LastEnumConstDecl,
  2785. IdentLoc, Ident,
  2786. attrs.getList(), EqualLoc,
  2787. AssignedVal.release());
  2788. PD.complete(EnumConstDecl);
  2789. EnumConstantDecls.push_back(EnumConstDecl);
  2790. LastEnumConstDecl = EnumConstDecl;
  2791. if (Tok.is(tok::identifier)) {
  2792. // We're missing a comma between enumerators.
  2793. SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
  2794. Diag(Loc, diag::err_enumerator_list_missing_comma)
  2795. << FixItHint::CreateInsertion(Loc, ", ");
  2796. continue;
  2797. }
  2798. if (Tok.isNot(tok::comma))
  2799. break;
  2800. SourceLocation CommaLoc = ConsumeToken();
  2801. if (Tok.isNot(tok::identifier)) {
  2802. if (!getLang().C99 && !getLang().CPlusPlus0x)
  2803. Diag(CommaLoc, diag::ext_enumerator_list_comma)
  2804. << getLang().CPlusPlus
  2805. << FixItHint::CreateRemoval(CommaLoc);
  2806. else if (getLang().CPlusPlus0x)
  2807. Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
  2808. << FixItHint::CreateRemoval(CommaLoc);
  2809. }
  2810. }
  2811. // Eat the }.
  2812. T.consumeClose();
  2813. // If attributes exist after the identifier list, parse them.
  2814. ParsedAttributes attrs(AttrFactory);
  2815. MaybeParseGNUAttributes(attrs);
  2816. Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
  2817. EnumDecl, EnumConstantDecls.data(),
  2818. EnumConstantDecls.size(), getCurScope(),
  2819. attrs.getList());
  2820. EnumScope.Exit();
  2821. Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
  2822. T.getCloseLocation());
  2823. }
  2824. /// isTypeSpecifierQualifier - Return true if the current token could be the
  2825. /// start of a type-qualifier-list.
  2826. bool Parser::isTypeQualifier() const {
  2827. switch (Tok.getKind()) {
  2828. default: return false;
  2829. // type-qualifier only in OpenCL
  2830. case tok::kw_private:
  2831. return getLang().OpenCL;
  2832. // type-qualifier
  2833. case tok::kw_const:
  2834. case tok::kw_volatile:
  2835. case tok::kw_restrict:
  2836. case tok::kw___private:
  2837. case tok::kw___local:
  2838. case tok::kw___global:
  2839. case tok::kw___constant:
  2840. case tok::kw___read_only:
  2841. case tok::kw___read_write:
  2842. case tok::kw___write_only:
  2843. return true;
  2844. }
  2845. }
  2846. /// isKnownToBeTypeSpecifier - Return true if we know that the specified token
  2847. /// is definitely a type-specifier. Return false if it isn't part of a type
  2848. /// specifier or if we're not sure.
  2849. bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
  2850. switch (Tok.getKind()) {
  2851. default: return false;
  2852. // type-specifiers
  2853. case tok::kw_short:
  2854. case tok::kw_long:
  2855. case tok::kw___int64:
  2856. case tok::kw_signed:
  2857. case tok::kw_unsigned:
  2858. case tok::kw__Complex:
  2859. case tok::kw__Imaginary:
  2860. case tok::kw_void:
  2861. case tok::kw_char:
  2862. case tok::kw_wchar_t:
  2863. case tok::kw_char16_t:
  2864. case tok::kw_char32_t:
  2865. case tok::kw_int:
  2866. case tok::kw_half:
  2867. case tok::kw_float:
  2868. case tok::kw_double:
  2869. case tok::kw_bool:
  2870. case tok::kw__Bool:
  2871. case tok::kw__Decimal32:
  2872. case tok::kw__Decimal64:
  2873. case tok::kw__Decimal128:
  2874. case tok::kw___vector:
  2875. // struct-or-union-specifier (C99) or class-specifier (C++)
  2876. case tok::kw_class:
  2877. case tok::kw_struct:
  2878. case tok::kw_union:
  2879. // enum-specifier
  2880. case tok::kw_enum:
  2881. // typedef-name
  2882. case tok::annot_typename:
  2883. return true;
  2884. }
  2885. }
  2886. /// isTypeSpecifierQualifier - Return true if the current token could be the
  2887. /// start of a specifier-qualifier-list.
  2888. bool Parser::isTypeSpecifierQualifier() {
  2889. switch (Tok.getKind()) {
  2890. default: return false;
  2891. case tok::identifier: // foo::bar
  2892. if (TryAltiVecVectorToken())
  2893. return true;
  2894. // Fall through.
  2895. case tok::kw_typename: // typename T::type
  2896. // Annotate typenames and C++ scope specifiers. If we get one, just
  2897. // recurse to handle whatever we get.
  2898. if (TryAnnotateTypeOrScopeToken())
  2899. return true;
  2900. if (Tok.is(tok::identifier))
  2901. return false;
  2902. return isTypeSpecifierQualifier();
  2903. case tok::coloncolon: // ::foo::bar
  2904. if (NextToken().is(tok::kw_new) || // ::new
  2905. NextToken().is(tok::kw_delete)) // ::delete
  2906. return false;
  2907. if (TryAnnotateTypeOrScopeToken())
  2908. return true;
  2909. return isTypeSpecifierQualifier();
  2910. // GNU attributes support.
  2911. case tok::kw___attribute:
  2912. // GNU typeof support.
  2913. case tok::kw_typeof:
  2914. // type-specifiers
  2915. case tok::kw_short:
  2916. case tok::kw_long:
  2917. case tok::kw___int64:
  2918. case tok::kw_signed:
  2919. case tok::kw_unsigned:
  2920. case tok::kw__Complex:
  2921. case tok::kw__Imaginary:
  2922. case tok::kw_void:
  2923. case tok::kw_char:
  2924. case tok::kw_wchar_t:
  2925. case tok::kw_char16_t:
  2926. case tok::kw_char32_t:
  2927. case tok::kw_int:
  2928. case tok::kw_half:
  2929. case tok::kw_float:
  2930. case tok::kw_double:
  2931. case tok::kw_bool:
  2932. case tok::kw__Bool:
  2933. case tok::kw__Decimal32:
  2934. case tok::kw__Decimal64:
  2935. case tok::kw__Decimal128:
  2936. case tok::kw___vector:
  2937. // struct-or-union-specifier (C99) or class-specifier (C++)
  2938. case tok::kw_class:
  2939. case tok::kw_struct:
  2940. case tok::kw_union:
  2941. // enum-specifier
  2942. case tok::kw_enum:
  2943. // type-qualifier
  2944. case tok::kw_const:
  2945. case tok::kw_volatile:
  2946. case tok::kw_restrict:
  2947. // typedef-name
  2948. case tok::annot_typename:
  2949. return true;
  2950. // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
  2951. case tok::less:
  2952. return getLang().ObjC1;
  2953. case tok::kw___cdecl:
  2954. case tok::kw___stdcall:
  2955. case tok::kw___fastcall:
  2956. case tok::kw___thiscall:
  2957. case tok::kw___w64:
  2958. case tok::kw___ptr64:
  2959. case tok::kw___ptr32:
  2960. case tok::kw___pascal:
  2961. case tok::kw___unaligned:
  2962. case tok::kw___private:
  2963. case tok::kw___local:
  2964. case tok::kw___global:
  2965. case tok::kw___constant:
  2966. case tok::kw___read_only:
  2967. case tok::kw___read_write:
  2968. case tok::kw___write_only:
  2969. return true;
  2970. case tok::kw_private:
  2971. return getLang().OpenCL;
  2972. // C11 _Atomic()
  2973. case tok::kw__Atomic:
  2974. return true;
  2975. }
  2976. }
  2977. /// isDeclarationSpecifier() - Return true if the current token is part of a
  2978. /// declaration specifier.
  2979. ///
  2980. /// \param DisambiguatingWithExpression True to indicate that the purpose of
  2981. /// this check is to disambiguate between an expression and a declaration.
  2982. bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
  2983. switch (Tok.getKind()) {
  2984. default: return false;
  2985. case tok::kw_private:
  2986. return getLang().OpenCL;
  2987. case tok::identifier: // foo::bar
  2988. // Unfortunate hack to support "Class.factoryMethod" notation.
  2989. if (getLang().ObjC1 && NextToken().is(tok::period))
  2990. return false;
  2991. if (TryAltiVecVectorToken())
  2992. return true;
  2993. // Fall through.
  2994. case tok::kw_decltype: // decltype(T())::type
  2995. case tok::kw_typename: // typename T::type
  2996. // Annotate typenames and C++ scope specifiers. If we get one, just
  2997. // recurse to handle whatever we get.
  2998. if (TryAnnotateTypeOrScopeToken())
  2999. return true;
  3000. if (Tok.is(tok::identifier))
  3001. return false;
  3002. // If we're in Objective-C and we have an Objective-C class type followed
  3003. // by an identifier and then either ':' or ']', in a place where an
  3004. // expression is permitted, then this is probably a class message send
  3005. // missing the initial '['. In this case, we won't consider this to be
  3006. // the start of a declaration.
  3007. if (DisambiguatingWithExpression &&
  3008. isStartOfObjCClassMessageMissingOpenBracket())
  3009. return false;
  3010. return isDeclarationSpecifier();
  3011. case tok::coloncolon: // ::foo::bar
  3012. if (NextToken().is(tok::kw_new) || // ::new
  3013. NextToken().is(tok::kw_delete)) // ::delete
  3014. return false;
  3015. // Annotate typenames and C++ scope specifiers. If we get one, just
  3016. // recurse to handle whatever we get.
  3017. if (TryAnnotateTypeOrScopeToken())
  3018. return true;
  3019. return isDeclarationSpecifier();
  3020. // storage-class-specifier
  3021. case tok::kw_typedef:
  3022. case tok::kw_extern:
  3023. case tok::kw___private_extern__:
  3024. case tok::kw_static:
  3025. case tok::kw_auto:
  3026. case tok::kw_register:
  3027. case tok::kw___thread:
  3028. // Modules
  3029. case tok::kw___module_private__:
  3030. // type-specifiers
  3031. case tok::kw_short:
  3032. case tok::kw_long:
  3033. case tok::kw___int64:
  3034. case tok::kw_signed:
  3035. case tok::kw_unsigned:
  3036. case tok::kw__Complex:
  3037. case tok::kw__Imaginary:
  3038. case tok::kw_void:
  3039. case tok::kw_char:
  3040. case tok::kw_wchar_t:
  3041. case tok::kw_char16_t:
  3042. case tok::kw_char32_t:
  3043. case tok::kw_int:
  3044. case tok::kw_half:
  3045. case tok::kw_float:
  3046. case tok::kw_double:
  3047. case tok::kw_bool:
  3048. case tok::kw__Bool:
  3049. case tok::kw__Decimal32:
  3050. case tok::kw__Decimal64:
  3051. case tok::kw__Decimal128:
  3052. case tok::kw___vector:
  3053. // struct-or-union-specifier (C99) or class-specifier (C++)
  3054. case tok::kw_class:
  3055. case tok::kw_struct:
  3056. case tok::kw_union:
  3057. // enum-specifier
  3058. case tok::kw_enum:
  3059. // type-qualifier
  3060. case tok::kw_const:
  3061. case tok::kw_volatile:
  3062. case tok::kw_restrict:
  3063. // function-specifier
  3064. case tok::kw_inline:
  3065. case tok::kw_virtual:
  3066. case tok::kw_explicit:
  3067. // static_assert-declaration
  3068. case tok::kw__Static_assert:
  3069. // GNU typeof support.
  3070. case tok::kw_typeof:
  3071. // GNU attributes.
  3072. case tok::kw___attribute:
  3073. return true;
  3074. // C++0x decltype.
  3075. case tok::annot_decltype:
  3076. return true;
  3077. // C11 _Atomic()
  3078. case tok::kw__Atomic:
  3079. return true;
  3080. // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
  3081. case tok::less:
  3082. return getLang().ObjC1;
  3083. // typedef-name
  3084. case tok::annot_typename:
  3085. return !DisambiguatingWithExpression ||
  3086. !isStartOfObjCClassMessageMissingOpenBracket();
  3087. case tok::kw___declspec:
  3088. case tok::kw___cdecl:
  3089. case tok::kw___stdcall:
  3090. case tok::kw___fastcall:
  3091. case tok::kw___thiscall:
  3092. case tok::kw___w64:
  3093. case tok::kw___ptr64:
  3094. case tok::kw___ptr32:
  3095. case tok::kw___forceinline:
  3096. case tok::kw___pascal:
  3097. case tok::kw___unaligned:
  3098. case tok::kw___private:
  3099. case tok::kw___local:
  3100. case tok::kw___global:
  3101. case tok::kw___constant:
  3102. case tok::kw___read_only:
  3103. case tok::kw___read_write:
  3104. case tok::kw___write_only:
  3105. return true;
  3106. }
  3107. }
  3108. bool Parser::isConstructorDeclarator() {
  3109. TentativeParsingAction TPA(*this);
  3110. // Parse the C++ scope specifier.
  3111. CXXScopeSpec SS;
  3112. if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
  3113. /*EnteringContext=*/true)) {
  3114. TPA.Revert();
  3115. return false;
  3116. }
  3117. // Parse the constructor name.
  3118. if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
  3119. // We already know that we have a constructor name; just consume
  3120. // the token.
  3121. ConsumeToken();
  3122. } else {
  3123. TPA.Revert();
  3124. return false;
  3125. }
  3126. // Current class name must be followed by a left parentheses.
  3127. if (Tok.isNot(tok::l_paren)) {
  3128. TPA.Revert();
  3129. return false;
  3130. }
  3131. ConsumeParen();
  3132. // A right parentheses or ellipsis signals that we have a constructor.
  3133. if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
  3134. TPA.Revert();
  3135. return true;
  3136. }
  3137. // If we need to, enter the specified scope.
  3138. DeclaratorScopeObj DeclScopeObj(*this, SS);
  3139. if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
  3140. DeclScopeObj.EnterDeclaratorScope();
  3141. // Optionally skip Microsoft attributes.
  3142. ParsedAttributes Attrs(AttrFactory);
  3143. MaybeParseMicrosoftAttributes(Attrs);
  3144. // Check whether the next token(s) are part of a declaration
  3145. // specifier, in which case we have the start of a parameter and,
  3146. // therefore, we know that this is a constructor.
  3147. bool IsConstructor = isDeclarationSpecifier();
  3148. TPA.Revert();
  3149. return IsConstructor;
  3150. }
  3151. /// ParseTypeQualifierListOpt
  3152. /// type-qualifier-list: [C99 6.7.5]
  3153. /// type-qualifier
  3154. /// [vendor] attributes
  3155. /// [ only if VendorAttributesAllowed=true ]
  3156. /// type-qualifier-list type-qualifier
  3157. /// [vendor] type-qualifier-list attributes
  3158. /// [ only if VendorAttributesAllowed=true ]
  3159. /// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
  3160. /// [ only if CXX0XAttributesAllowed=true ]
  3161. /// Note: vendor can be GNU, MS, etc.
  3162. ///
  3163. void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
  3164. bool VendorAttributesAllowed,
  3165. bool CXX0XAttributesAllowed) {
  3166. if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
  3167. SourceLocation Loc = Tok.getLocation();
  3168. ParsedAttributesWithRange attrs(AttrFactory);
  3169. ParseCXX0XAttributes(attrs);
  3170. if (CXX0XAttributesAllowed)
  3171. DS.takeAttributesFrom(attrs);
  3172. else
  3173. Diag(Loc, diag::err_attributes_not_allowed);
  3174. }
  3175. SourceLocation EndLoc;
  3176. while (1) {
  3177. bool isInvalid = false;
  3178. const char *PrevSpec = 0;
  3179. unsigned DiagID = 0;
  3180. SourceLocation Loc = Tok.getLocation();
  3181. switch (Tok.getKind()) {
  3182. case tok::code_completion:
  3183. Actions.CodeCompleteTypeQualifiers(DS);
  3184. return cutOffParsing();
  3185. case tok::kw_const:
  3186. isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
  3187. getLang());
  3188. break;
  3189. case tok::kw_volatile:
  3190. isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
  3191. getLang());
  3192. break;
  3193. case tok::kw_restrict:
  3194. isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
  3195. getLang());
  3196. break;
  3197. // OpenCL qualifiers:
  3198. case tok::kw_private:
  3199. if (!getLang().OpenCL)
  3200. goto DoneWithTypeQuals;
  3201. case tok::kw___private:
  3202. case tok::kw___global:
  3203. case tok::kw___local:
  3204. case tok::kw___constant:
  3205. case tok::kw___read_only:
  3206. case tok::kw___write_only:
  3207. case tok::kw___read_write:
  3208. ParseOpenCLQualifiers(DS);
  3209. break;
  3210. case tok::kw___w64:
  3211. case tok::kw___ptr64:
  3212. case tok::kw___ptr32:
  3213. case tok::kw___cdecl:
  3214. case tok::kw___stdcall:
  3215. case tok::kw___fastcall:
  3216. case tok::kw___thiscall:
  3217. case tok::kw___unaligned:
  3218. if (VendorAttributesAllowed) {
  3219. ParseMicrosoftTypeAttributes(DS.getAttributes());
  3220. continue;
  3221. }
  3222. goto DoneWithTypeQuals;
  3223. case tok::kw___pascal:
  3224. if (VendorAttributesAllowed) {
  3225. ParseBorlandTypeAttributes(DS.getAttributes());
  3226. continue;
  3227. }
  3228. goto DoneWithTypeQuals;
  3229. case tok::kw___attribute:
  3230. if (VendorAttributesAllowed) {
  3231. ParseGNUAttributes(DS.getAttributes());
  3232. continue; // do *not* consume the next token!
  3233. }
  3234. // otherwise, FALL THROUGH!
  3235. default:
  3236. DoneWithTypeQuals:
  3237. // If this is not a type-qualifier token, we're done reading type
  3238. // qualifiers. First verify that DeclSpec's are consistent.
  3239. DS.Finish(Diags, PP);
  3240. if (EndLoc.isValid())
  3241. DS.SetRangeEnd(EndLoc);
  3242. return;
  3243. }
  3244. // If the specifier combination wasn't legal, issue a diagnostic.
  3245. if (isInvalid) {
  3246. assert(PrevSpec && "Method did not return previous specifier!");
  3247. Diag(Tok, DiagID) << PrevSpec;
  3248. }
  3249. EndLoc = ConsumeToken();
  3250. }
  3251. }
  3252. /// ParseDeclarator - Parse and verify a newly-initialized declarator.
  3253. ///
  3254. void Parser::ParseDeclarator(Declarator &D) {
  3255. /// This implements the 'declarator' production in the C grammar, then checks
  3256. /// for well-formedness and issues diagnostics.
  3257. ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
  3258. }
  3259. /// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
  3260. /// is parsed by the function passed to it. Pass null, and the direct-declarator
  3261. /// isn't parsed at all, making this function effectively parse the C++
  3262. /// ptr-operator production.
  3263. ///
  3264. /// If the grammar of this construct is extended, matching changes must also be
  3265. /// made to TryParseDeclarator and MightBeDeclarator.
  3266. ///
  3267. /// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
  3268. /// [C] pointer[opt] direct-declarator
  3269. /// [C++] direct-declarator
  3270. /// [C++] ptr-operator declarator
  3271. ///
  3272. /// pointer: [C99 6.7.5]
  3273. /// '*' type-qualifier-list[opt]
  3274. /// '*' type-qualifier-list[opt] pointer
  3275. ///
  3276. /// ptr-operator:
  3277. /// '*' cv-qualifier-seq[opt]
  3278. /// '&'
  3279. /// [C++0x] '&&'
  3280. /// [GNU] '&' restrict[opt] attributes[opt]
  3281. /// [GNU?] '&&' restrict[opt] attributes[opt]
  3282. /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
  3283. void Parser::ParseDeclaratorInternal(Declarator &D,
  3284. DirectDeclParseFunction DirectDeclParser) {
  3285. if (Diags.hasAllExtensionsSilenced())
  3286. D.setExtension();
  3287. // C++ member pointers start with a '::' or a nested-name.
  3288. // Member pointers get special handling, since there's no place for the
  3289. // scope spec in the generic path below.
  3290. if (getLang().CPlusPlus &&
  3291. (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
  3292. Tok.is(tok::annot_cxxscope))) {
  3293. bool EnteringContext = D.getContext() == Declarator::FileContext ||
  3294. D.getContext() == Declarator::MemberContext;
  3295. CXXScopeSpec SS;
  3296. ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
  3297. if (SS.isNotEmpty()) {
  3298. if (Tok.isNot(tok::star)) {
  3299. // The scope spec really belongs to the direct-declarator.
  3300. D.getCXXScopeSpec() = SS;
  3301. if (DirectDeclParser)
  3302. (this->*DirectDeclParser)(D);
  3303. return;
  3304. }
  3305. SourceLocation Loc = ConsumeToken();
  3306. D.SetRangeEnd(Loc);
  3307. DeclSpec DS(AttrFactory);
  3308. ParseTypeQualifierListOpt(DS);
  3309. D.ExtendWithDeclSpec(DS);
  3310. // Recurse to parse whatever is left.
  3311. ParseDeclaratorInternal(D, DirectDeclParser);
  3312. // Sema will have to catch (syntactically invalid) pointers into global
  3313. // scope. It has to catch pointers into namespace scope anyway.
  3314. D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
  3315. Loc),
  3316. DS.getAttributes(),
  3317. /* Don't replace range end. */SourceLocation());
  3318. return;
  3319. }
  3320. }
  3321. tok::TokenKind Kind = Tok.getKind();
  3322. // Not a pointer, C++ reference, or block.
  3323. if (Kind != tok::star && Kind != tok::caret &&
  3324. (Kind != tok::amp || !getLang().CPlusPlus) &&
  3325. // We parse rvalue refs in C++03, because otherwise the errors are scary.
  3326. (Kind != tok::ampamp || !getLang().CPlusPlus)) {
  3327. if (DirectDeclParser)
  3328. (this->*DirectDeclParser)(D);
  3329. return;
  3330. }
  3331. // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
  3332. // '&&' -> rvalue reference
  3333. SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
  3334. D.SetRangeEnd(Loc);
  3335. if (Kind == tok::star || Kind == tok::caret) {
  3336. // Is a pointer.
  3337. DeclSpec DS(AttrFactory);
  3338. ParseTypeQualifierListOpt(DS);
  3339. D.ExtendWithDeclSpec(DS);
  3340. // Recursively parse the declarator.
  3341. ParseDeclaratorInternal(D, DirectDeclParser);
  3342. if (Kind == tok::star)
  3343. // Remember that we parsed a pointer type, and remember the type-quals.
  3344. D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
  3345. DS.getConstSpecLoc(),
  3346. DS.getVolatileSpecLoc(),
  3347. DS.getRestrictSpecLoc()),
  3348. DS.getAttributes(),
  3349. SourceLocation());
  3350. else
  3351. // Remember that we parsed a Block type, and remember the type-quals.
  3352. D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
  3353. Loc),
  3354. DS.getAttributes(),
  3355. SourceLocation());
  3356. } else {
  3357. // Is a reference
  3358. DeclSpec DS(AttrFactory);
  3359. // Complain about rvalue references in C++03, but then go on and build
  3360. // the declarator.
  3361. if (Kind == tok::ampamp)
  3362. Diag(Loc, getLang().CPlusPlus0x ?
  3363. diag::warn_cxx98_compat_rvalue_reference :
  3364. diag::ext_rvalue_reference);
  3365. // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
  3366. // cv-qualifiers are introduced through the use of a typedef or of a
  3367. // template type argument, in which case the cv-qualifiers are ignored.
  3368. //
  3369. // [GNU] Retricted references are allowed.
  3370. // [GNU] Attributes on references are allowed.
  3371. // [C++0x] Attributes on references are not allowed.
  3372. ParseTypeQualifierListOpt(DS, true, false);
  3373. D.ExtendWithDeclSpec(DS);
  3374. if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
  3375. if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
  3376. Diag(DS.getConstSpecLoc(),
  3377. diag::err_invalid_reference_qualifier_application) << "const";
  3378. if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
  3379. Diag(DS.getVolatileSpecLoc(),
  3380. diag::err_invalid_reference_qualifier_application) << "volatile";
  3381. }
  3382. // Recursively parse the declarator.
  3383. ParseDeclaratorInternal(D, DirectDeclParser);
  3384. if (D.getNumTypeObjects() > 0) {
  3385. // C++ [dcl.ref]p4: There shall be no references to references.
  3386. DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
  3387. if (InnerChunk.Kind == DeclaratorChunk::Reference) {
  3388. if (const IdentifierInfo *II = D.getIdentifier())
  3389. Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
  3390. << II;
  3391. else
  3392. Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
  3393. << "type name";
  3394. // Once we've complained about the reference-to-reference, we
  3395. // can go ahead and build the (technically ill-formed)
  3396. // declarator: reference collapsing will take care of it.
  3397. }
  3398. }
  3399. // Remember that we parsed a reference type. It doesn't have type-quals.
  3400. D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
  3401. Kind == tok::amp),
  3402. DS.getAttributes(),
  3403. SourceLocation());
  3404. }
  3405. }
  3406. /// ParseDirectDeclarator
  3407. /// direct-declarator: [C99 6.7.5]
  3408. /// [C99] identifier
  3409. /// '(' declarator ')'
  3410. /// [GNU] '(' attributes declarator ')'
  3411. /// [C90] direct-declarator '[' constant-expression[opt] ']'
  3412. /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
  3413. /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
  3414. /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
  3415. /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
  3416. /// direct-declarator '(' parameter-type-list ')'
  3417. /// direct-declarator '(' identifier-list[opt] ')'
  3418. /// [GNU] direct-declarator '(' parameter-forward-declarations
  3419. /// parameter-type-list[opt] ')'
  3420. /// [C++] direct-declarator '(' parameter-declaration-clause ')'
  3421. /// cv-qualifier-seq[opt] exception-specification[opt]
  3422. /// [C++] declarator-id
  3423. ///
  3424. /// declarator-id: [C++ 8]
  3425. /// '...'[opt] id-expression
  3426. /// '::'[opt] nested-name-specifier[opt] type-name
  3427. ///
  3428. /// id-expression: [C++ 5.1]
  3429. /// unqualified-id
  3430. /// qualified-id
  3431. ///
  3432. /// unqualified-id: [C++ 5.1]
  3433. /// identifier
  3434. /// operator-function-id
  3435. /// conversion-function-id
  3436. /// '~' class-name
  3437. /// template-id
  3438. ///
  3439. void Parser::ParseDirectDeclarator(Declarator &D) {
  3440. DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
  3441. if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
  3442. // ParseDeclaratorInternal might already have parsed the scope.
  3443. if (D.getCXXScopeSpec().isEmpty()) {
  3444. bool EnteringContext = D.getContext() == Declarator::FileContext ||
  3445. D.getContext() == Declarator::MemberContext;
  3446. ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
  3447. EnteringContext);
  3448. }
  3449. if (D.getCXXScopeSpec().isValid()) {
  3450. if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
  3451. // Change the declaration context for name lookup, until this function
  3452. // is exited (and the declarator has been parsed).
  3453. DeclScopeObj.EnterDeclaratorScope();
  3454. }
  3455. // C++0x [dcl.fct]p14:
  3456. // There is a syntactic ambiguity when an ellipsis occurs at the end
  3457. // of a parameter-declaration-clause without a preceding comma. In
  3458. // this case, the ellipsis is parsed as part of the
  3459. // abstract-declarator if the type of the parameter names a template
  3460. // parameter pack that has not been expanded; otherwise, it is parsed
  3461. // as part of the parameter-declaration-clause.
  3462. if (Tok.is(tok::ellipsis) &&
  3463. !((D.getContext() == Declarator::PrototypeContext ||
  3464. D.getContext() == Declarator::BlockLiteralContext) &&
  3465. NextToken().is(tok::r_paren) &&
  3466. !Actions.containsUnexpandedParameterPacks(D)))
  3467. D.setEllipsisLoc(ConsumeToken());
  3468. if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
  3469. Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
  3470. // We found something that indicates the start of an unqualified-id.
  3471. // Parse that unqualified-id.
  3472. bool AllowConstructorName;
  3473. if (D.getDeclSpec().hasTypeSpecifier())
  3474. AllowConstructorName = false;
  3475. else if (D.getCXXScopeSpec().isSet())
  3476. AllowConstructorName =
  3477. (D.getContext() == Declarator::FileContext ||
  3478. (D.getContext() == Declarator::MemberContext &&
  3479. D.getDeclSpec().isFriendSpecified()));
  3480. else
  3481. AllowConstructorName = (D.getContext() == Declarator::MemberContext);
  3482. SourceLocation TemplateKWLoc;
  3483. if (ParseUnqualifiedId(D.getCXXScopeSpec(),
  3484. /*EnteringContext=*/true,
  3485. /*AllowDestructorName=*/true,
  3486. AllowConstructorName,
  3487. ParsedType(),
  3488. TemplateKWLoc,
  3489. D.getName()) ||
  3490. // Once we're past the identifier, if the scope was bad, mark the
  3491. // whole declarator bad.
  3492. D.getCXXScopeSpec().isInvalid()) {
  3493. D.SetIdentifier(0, Tok.getLocation());
  3494. D.setInvalidType(true);
  3495. } else {
  3496. // Parsed the unqualified-id; update range information and move along.
  3497. if (D.getSourceRange().getBegin().isInvalid())
  3498. D.SetRangeBegin(D.getName().getSourceRange().getBegin());
  3499. D.SetRangeEnd(D.getName().getSourceRange().getEnd());
  3500. }
  3501. goto PastIdentifier;
  3502. }
  3503. } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
  3504. assert(!getLang().CPlusPlus &&
  3505. "There's a C++-specific check for tok::identifier above");
  3506. assert(Tok.getIdentifierInfo() && "Not an identifier?");
  3507. D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
  3508. ConsumeToken();
  3509. goto PastIdentifier;
  3510. }
  3511. if (Tok.is(tok::l_paren)) {
  3512. // direct-declarator: '(' declarator ')'
  3513. // direct-declarator: '(' attributes declarator ')'
  3514. // Example: 'char (*X)' or 'int (*XX)(void)'
  3515. ParseParenDeclarator(D);
  3516. // If the declarator was parenthesized, we entered the declarator
  3517. // scope when parsing the parenthesized declarator, then exited
  3518. // the scope already. Re-enter the scope, if we need to.
  3519. if (D.getCXXScopeSpec().isSet()) {
  3520. // If there was an error parsing parenthesized declarator, declarator
  3521. // scope may have been enterred before. Don't do it again.
  3522. if (!D.isInvalidType() &&
  3523. Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
  3524. // Change the declaration context for name lookup, until this function
  3525. // is exited (and the declarator has been parsed).
  3526. DeclScopeObj.EnterDeclaratorScope();
  3527. }
  3528. } else if (D.mayOmitIdentifier()) {
  3529. // This could be something simple like "int" (in which case the declarator
  3530. // portion is empty), if an abstract-declarator is allowed.
  3531. D.SetIdentifier(0, Tok.getLocation());
  3532. } else {
  3533. if (D.getContext() == Declarator::MemberContext)
  3534. Diag(Tok, diag::err_expected_member_name_or_semi)
  3535. << D.getDeclSpec().getSourceRange();
  3536. else if (getLang().CPlusPlus)
  3537. Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
  3538. else
  3539. Diag(Tok, diag::err_expected_ident_lparen);
  3540. D.SetIdentifier(0, Tok.getLocation());
  3541. D.setInvalidType(true);
  3542. }
  3543. PastIdentifier:
  3544. assert(D.isPastIdentifier() &&
  3545. "Haven't past the location of the identifier yet?");
  3546. // Don't parse attributes unless we have an identifier.
  3547. if (D.getIdentifier())
  3548. MaybeParseCXX0XAttributes(D);
  3549. while (1) {
  3550. if (Tok.is(tok::l_paren)) {
  3551. // Enter function-declaration scope, limiting any declarators to the
  3552. // function prototype scope, including parameter declarators.
  3553. ParseScope PrototypeScope(this,
  3554. Scope::FunctionPrototypeScope|Scope::DeclScope);
  3555. // The paren may be part of a C++ direct initializer, eg. "int x(1);".
  3556. // In such a case, check if we actually have a function declarator; if it
  3557. // is not, the declarator has been fully parsed.
  3558. if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
  3559. // When not in file scope, warn for ambiguous function declarators, just
  3560. // in case the author intended it as a variable definition.
  3561. bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
  3562. if (!isCXXFunctionDeclarator(warnIfAmbiguous))
  3563. break;
  3564. }
  3565. ParsedAttributes attrs(AttrFactory);
  3566. BalancedDelimiterTracker T(*this, tok::l_paren);
  3567. T.consumeOpen();
  3568. ParseFunctionDeclarator(D, attrs, T);
  3569. PrototypeScope.Exit();
  3570. } else if (Tok.is(tok::l_square)) {
  3571. ParseBracketDeclarator(D);
  3572. } else {
  3573. break;
  3574. }
  3575. }
  3576. }
  3577. /// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
  3578. /// only called before the identifier, so these are most likely just grouping
  3579. /// parens for precedence. If we find that these are actually function
  3580. /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
  3581. ///
  3582. /// direct-declarator:
  3583. /// '(' declarator ')'
  3584. /// [GNU] '(' attributes declarator ')'
  3585. /// direct-declarator '(' parameter-type-list ')'
  3586. /// direct-declarator '(' identifier-list[opt] ')'
  3587. /// [GNU] direct-declarator '(' parameter-forward-declarations
  3588. /// parameter-type-list[opt] ')'
  3589. ///
  3590. void Parser::ParseParenDeclarator(Declarator &D) {
  3591. BalancedDelimiterTracker T(*this, tok::l_paren);
  3592. T.consumeOpen();
  3593. assert(!D.isPastIdentifier() && "Should be called before passing identifier");
  3594. // Eat any attributes before we look at whether this is a grouping or function
  3595. // declarator paren. If this is a grouping paren, the attribute applies to
  3596. // the type being built up, for example:
  3597. // int (__attribute__(()) *x)(long y)
  3598. // If this ends up not being a grouping paren, the attribute applies to the
  3599. // first argument, for example:
  3600. // int (__attribute__(()) int x)
  3601. // In either case, we need to eat any attributes to be able to determine what
  3602. // sort of paren this is.
  3603. //
  3604. ParsedAttributes attrs(AttrFactory);
  3605. bool RequiresArg = false;
  3606. if (Tok.is(tok::kw___attribute)) {
  3607. ParseGNUAttributes(attrs);
  3608. // We require that the argument list (if this is a non-grouping paren) be
  3609. // present even if the attribute list was empty.
  3610. RequiresArg = true;
  3611. }
  3612. // Eat any Microsoft extensions.
  3613. if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
  3614. Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
  3615. Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
  3616. Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
  3617. ParseMicrosoftTypeAttributes(attrs);
  3618. }
  3619. // Eat any Borland extensions.
  3620. if (Tok.is(tok::kw___pascal))
  3621. ParseBorlandTypeAttributes(attrs);
  3622. // If we haven't past the identifier yet (or where the identifier would be
  3623. // stored, if this is an abstract declarator), then this is probably just
  3624. // grouping parens. However, if this could be an abstract-declarator, then
  3625. // this could also be the start of function arguments (consider 'void()').
  3626. bool isGrouping;
  3627. if (!D.mayOmitIdentifier()) {
  3628. // If this can't be an abstract-declarator, this *must* be a grouping
  3629. // paren, because we haven't seen the identifier yet.
  3630. isGrouping = true;
  3631. } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
  3632. (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
  3633. isDeclarationSpecifier()) { // 'int(int)' is a function.
  3634. // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
  3635. // considered to be a type, not a K&R identifier-list.
  3636. isGrouping = false;
  3637. } else {
  3638. // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
  3639. isGrouping = true;
  3640. }
  3641. // If this is a grouping paren, handle:
  3642. // direct-declarator: '(' declarator ')'
  3643. // direct-declarator: '(' attributes declarator ')'
  3644. if (isGrouping) {
  3645. bool hadGroupingParens = D.hasGroupingParens();
  3646. D.setGroupingParens(true);
  3647. ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
  3648. // Match the ')'.
  3649. T.consumeClose();
  3650. D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
  3651. T.getCloseLocation()),
  3652. attrs, T.getCloseLocation());
  3653. D.setGroupingParens(hadGroupingParens);
  3654. return;
  3655. }
  3656. // Okay, if this wasn't a grouping paren, it must be the start of a function
  3657. // argument list. Recognize that this declarator will never have an
  3658. // identifier (and remember where it would have been), then call into
  3659. // ParseFunctionDeclarator to handle of argument list.
  3660. D.SetIdentifier(0, Tok.getLocation());
  3661. // Enter function-declaration scope, limiting any declarators to the
  3662. // function prototype scope, including parameter declarators.
  3663. ParseScope PrototypeScope(this,
  3664. Scope::FunctionPrototypeScope|Scope::DeclScope);
  3665. ParseFunctionDeclarator(D, attrs, T, RequiresArg);
  3666. PrototypeScope.Exit();
  3667. }
  3668. /// ParseFunctionDeclarator - We are after the identifier and have parsed the
  3669. /// declarator D up to a paren, which indicates that we are parsing function
  3670. /// arguments.
  3671. ///
  3672. /// If attrs is non-null, then the caller parsed those arguments immediately
  3673. /// after the open paren - they should be considered to be the first argument of
  3674. /// a parameter. If RequiresArg is true, then the first argument of the
  3675. /// function is required to be present and required to not be an identifier
  3676. /// list.
  3677. ///
  3678. /// For C++, after the parameter-list, it also parses cv-qualifier-seq[opt],
  3679. /// (C++0x) ref-qualifier[opt], exception-specification[opt], and
  3680. /// (C++0x) trailing-return-type[opt].
  3681. ///
  3682. /// [C++0x] exception-specification:
  3683. /// dynamic-exception-specification
  3684. /// noexcept-specification
  3685. ///
  3686. void Parser::ParseFunctionDeclarator(Declarator &D,
  3687. ParsedAttributes &attrs,
  3688. BalancedDelimiterTracker &Tracker,
  3689. bool RequiresArg) {
  3690. assert(getCurScope()->isFunctionPrototypeScope() &&
  3691. "Should call from a Function scope");
  3692. // lparen is already consumed!
  3693. assert(D.isPastIdentifier() && "Should not call before identifier!");
  3694. // This should be true when the function has typed arguments.
  3695. // Otherwise, it is treated as a K&R-style function.
  3696. bool HasProto = false;
  3697. // Build up an array of information about the parsed arguments.
  3698. SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
  3699. // Remember where we see an ellipsis, if any.
  3700. SourceLocation EllipsisLoc;
  3701. DeclSpec DS(AttrFactory);
  3702. bool RefQualifierIsLValueRef = true;
  3703. SourceLocation RefQualifierLoc;
  3704. SourceLocation ConstQualifierLoc;
  3705. SourceLocation VolatileQualifierLoc;
  3706. ExceptionSpecificationType ESpecType = EST_None;
  3707. SourceRange ESpecRange;
  3708. SmallVector<ParsedType, 2> DynamicExceptions;
  3709. SmallVector<SourceRange, 2> DynamicExceptionRanges;
  3710. ExprResult NoexceptExpr;
  3711. ParsedType TrailingReturnType;
  3712. SourceLocation EndLoc;
  3713. if (isFunctionDeclaratorIdentifierList()) {
  3714. if (RequiresArg)
  3715. Diag(Tok, diag::err_argument_required_after_attribute);
  3716. ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
  3717. Tracker.consumeClose();
  3718. EndLoc = Tracker.getCloseLocation();
  3719. } else {
  3720. if (Tok.isNot(tok::r_paren))
  3721. ParseParameterDeclarationClause(D, attrs, ParamInfo, EllipsisLoc);
  3722. else if (RequiresArg)
  3723. Diag(Tok, diag::err_argument_required_after_attribute);
  3724. HasProto = ParamInfo.size() || getLang().CPlusPlus;
  3725. // If we have the closing ')', eat it.
  3726. Tracker.consumeClose();
  3727. EndLoc = Tracker.getCloseLocation();
  3728. if (getLang().CPlusPlus) {
  3729. MaybeParseCXX0XAttributes(attrs);
  3730. // Parse cv-qualifier-seq[opt].
  3731. ParseTypeQualifierListOpt(DS, false /*no attributes*/);
  3732. if (!DS.getSourceRange().getEnd().isInvalid()) {
  3733. EndLoc = DS.getSourceRange().getEnd();
  3734. ConstQualifierLoc = DS.getConstSpecLoc();
  3735. VolatileQualifierLoc = DS.getVolatileSpecLoc();
  3736. }
  3737. // Parse ref-qualifier[opt].
  3738. if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
  3739. Diag(Tok, getLang().CPlusPlus0x ?
  3740. diag::warn_cxx98_compat_ref_qualifier :
  3741. diag::ext_ref_qualifier);
  3742. RefQualifierIsLValueRef = Tok.is(tok::amp);
  3743. RefQualifierLoc = ConsumeToken();
  3744. EndLoc = RefQualifierLoc;
  3745. }
  3746. // Parse exception-specification[opt].
  3747. ESpecType = MaybeParseExceptionSpecification(ESpecRange,
  3748. DynamicExceptions,
  3749. DynamicExceptionRanges,
  3750. NoexceptExpr);
  3751. if (ESpecType != EST_None)
  3752. EndLoc = ESpecRange.getEnd();
  3753. // Parse trailing-return-type[opt].
  3754. if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
  3755. Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
  3756. SourceRange Range;
  3757. TrailingReturnType = ParseTrailingReturnType(Range).get();
  3758. if (Range.getEnd().isValid())
  3759. EndLoc = Range.getEnd();
  3760. }
  3761. }
  3762. }
  3763. // Remember that we parsed a function type, and remember the attributes.
  3764. D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
  3765. /*isVariadic=*/EllipsisLoc.isValid(),
  3766. EllipsisLoc,
  3767. ParamInfo.data(), ParamInfo.size(),
  3768. DS.getTypeQualifiers(),
  3769. RefQualifierIsLValueRef,
  3770. RefQualifierLoc, ConstQualifierLoc,
  3771. VolatileQualifierLoc,
  3772. /*MutableLoc=*/SourceLocation(),
  3773. ESpecType, ESpecRange.getBegin(),
  3774. DynamicExceptions.data(),
  3775. DynamicExceptionRanges.data(),
  3776. DynamicExceptions.size(),
  3777. NoexceptExpr.isUsable() ?
  3778. NoexceptExpr.get() : 0,
  3779. Tracker.getOpenLocation(),
  3780. EndLoc, D,
  3781. TrailingReturnType),
  3782. attrs, EndLoc);
  3783. }
  3784. /// isFunctionDeclaratorIdentifierList - This parameter list may have an
  3785. /// identifier list form for a K&R-style function: void foo(a,b,c)
  3786. ///
  3787. /// Note that identifier-lists are only allowed for normal declarators, not for
  3788. /// abstract-declarators.
  3789. bool Parser::isFunctionDeclaratorIdentifierList() {
  3790. return !getLang().CPlusPlus
  3791. && Tok.is(tok::identifier)
  3792. && !TryAltiVecVectorToken()
  3793. // K&R identifier lists can't have typedefs as identifiers, per C99
  3794. // 6.7.5.3p11.
  3795. && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
  3796. // Identifier lists follow a really simple grammar: the identifiers can
  3797. // be followed *only* by a ", identifier" or ")". However, K&R
  3798. // identifier lists are really rare in the brave new modern world, and
  3799. // it is very common for someone to typo a type in a non-K&R style
  3800. // list. If we are presented with something like: "void foo(intptr x,
  3801. // float y)", we don't want to start parsing the function declarator as
  3802. // though it is a K&R style declarator just because intptr is an
  3803. // invalid type.
  3804. //
  3805. // To handle this, we check to see if the token after the first
  3806. // identifier is a "," or ")". Only then do we parse it as an
  3807. // identifier list.
  3808. && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
  3809. }
  3810. /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
  3811. /// we found a K&R-style identifier list instead of a typed parameter list.
  3812. ///
  3813. /// After returning, ParamInfo will hold the parsed parameters.
  3814. ///
  3815. /// identifier-list: [C99 6.7.5]
  3816. /// identifier
  3817. /// identifier-list ',' identifier
  3818. ///
  3819. void Parser::ParseFunctionDeclaratorIdentifierList(
  3820. Declarator &D,
  3821. SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
  3822. // If there was no identifier specified for the declarator, either we are in
  3823. // an abstract-declarator, or we are in a parameter declarator which was found
  3824. // to be abstract. In abstract-declarators, identifier lists are not valid:
  3825. // diagnose this.
  3826. if (!D.getIdentifier())
  3827. Diag(Tok, diag::ext_ident_list_in_param);
  3828. // Maintain an efficient lookup of params we have seen so far.
  3829. llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
  3830. while (1) {
  3831. // If this isn't an identifier, report the error and skip until ')'.
  3832. if (Tok.isNot(tok::identifier)) {
  3833. Diag(Tok, diag::err_expected_ident);
  3834. SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
  3835. // Forget we parsed anything.
  3836. ParamInfo.clear();
  3837. return;
  3838. }
  3839. IdentifierInfo *ParmII = Tok.getIdentifierInfo();
  3840. // Reject 'typedef int y; int test(x, y)', but continue parsing.
  3841. if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
  3842. Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
  3843. // Verify that the argument identifier has not already been mentioned.
  3844. if (!ParamsSoFar.insert(ParmII)) {
  3845. Diag(Tok, diag::err_param_redefinition) << ParmII;
  3846. } else {
  3847. // Remember this identifier in ParamInfo.
  3848. ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
  3849. Tok.getLocation(),
  3850. 0));
  3851. }
  3852. // Eat the identifier.
  3853. ConsumeToken();
  3854. // The list continues if we see a comma.
  3855. if (Tok.isNot(tok::comma))
  3856. break;
  3857. ConsumeToken();
  3858. }
  3859. }
  3860. /// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
  3861. /// after the opening parenthesis. This function will not parse a K&R-style
  3862. /// identifier list.
  3863. ///
  3864. /// D is the declarator being parsed. If attrs is non-null, then the caller
  3865. /// parsed those arguments immediately after the open paren - they should be
  3866. /// considered to be the first argument of a parameter.
  3867. ///
  3868. /// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
  3869. /// be the location of the ellipsis, if any was parsed.
  3870. ///
  3871. /// parameter-type-list: [C99 6.7.5]
  3872. /// parameter-list
  3873. /// parameter-list ',' '...'
  3874. /// [C++] parameter-list '...'
  3875. ///
  3876. /// parameter-list: [C99 6.7.5]
  3877. /// parameter-declaration
  3878. /// parameter-list ',' parameter-declaration
  3879. ///
  3880. /// parameter-declaration: [C99 6.7.5]
  3881. /// declaration-specifiers declarator
  3882. /// [C++] declaration-specifiers declarator '=' assignment-expression
  3883. /// [GNU] declaration-specifiers declarator attributes
  3884. /// declaration-specifiers abstract-declarator[opt]
  3885. /// [C++] declaration-specifiers abstract-declarator[opt]
  3886. /// '=' assignment-expression
  3887. /// [GNU] declaration-specifiers abstract-declarator[opt] attributes
  3888. ///
  3889. void Parser::ParseParameterDeclarationClause(
  3890. Declarator &D,
  3891. ParsedAttributes &attrs,
  3892. SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
  3893. SourceLocation &EllipsisLoc) {
  3894. while (1) {
  3895. if (Tok.is(tok::ellipsis)) {
  3896. EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
  3897. break;
  3898. }
  3899. // Parse the declaration-specifiers.
  3900. // Just use the ParsingDeclaration "scope" of the declarator.
  3901. DeclSpec DS(AttrFactory);
  3902. // Skip any Microsoft attributes before a param.
  3903. if (getLang().MicrosoftExt && Tok.is(tok::l_square))
  3904. ParseMicrosoftAttributes(DS.getAttributes());
  3905. SourceLocation DSStart = Tok.getLocation();
  3906. // If the caller parsed attributes for the first argument, add them now.
  3907. // Take them so that we only apply the attributes to the first parameter.
  3908. // FIXME: If we saw an ellipsis first, this code is not reached. Are the
  3909. // attributes lost? Should they even be allowed?
  3910. // FIXME: If we can leave the attributes in the token stream somehow, we can
  3911. // get rid of a parameter (attrs) and this statement. It might be too much
  3912. // hassle.
  3913. DS.takeAttributesFrom(attrs);
  3914. ParseDeclarationSpecifiers(DS);
  3915. // Parse the declarator. This is "PrototypeContext", because we must
  3916. // accept either 'declarator' or 'abstract-declarator' here.
  3917. Declarator ParmDecl(DS, Declarator::PrototypeContext);
  3918. ParseDeclarator(ParmDecl);
  3919. // Parse GNU attributes, if present.
  3920. MaybeParseGNUAttributes(ParmDecl);
  3921. // Remember this parsed parameter in ParamInfo.
  3922. IdentifierInfo *ParmII = ParmDecl.getIdentifier();
  3923. // DefArgToks is used when the parsing of default arguments needs
  3924. // to be delayed.
  3925. CachedTokens *DefArgToks = 0;
  3926. // If no parameter was specified, verify that *something* was specified,
  3927. // otherwise we have a missing type and identifier.
  3928. if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
  3929. ParmDecl.getNumTypeObjects() == 0) {
  3930. // Completely missing, emit error.
  3931. Diag(DSStart, diag::err_missing_param);
  3932. } else {
  3933. // Otherwise, we have something. Add it and let semantic analysis try
  3934. // to grok it and add the result to the ParamInfo we are building.
  3935. // Inform the actions module about the parameter declarator, so it gets
  3936. // added to the current scope.
  3937. Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
  3938. // Parse the default argument, if any. We parse the default
  3939. // arguments in all dialects; the semantic analysis in
  3940. // ActOnParamDefaultArgument will reject the default argument in
  3941. // C.
  3942. if (Tok.is(tok::equal)) {
  3943. SourceLocation EqualLoc = Tok.getLocation();
  3944. // Parse the default argument
  3945. if (D.getContext() == Declarator::MemberContext) {
  3946. // If we're inside a class definition, cache the tokens
  3947. // corresponding to the default argument. We'll actually parse
  3948. // them when we see the end of the class definition.
  3949. // FIXME: Templates will require something similar.
  3950. // FIXME: Can we use a smart pointer for Toks?
  3951. DefArgToks = new CachedTokens;
  3952. if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
  3953. /*StopAtSemi=*/true,
  3954. /*ConsumeFinalToken=*/false)) {
  3955. delete DefArgToks;
  3956. DefArgToks = 0;
  3957. Actions.ActOnParamDefaultArgumentError(Param);
  3958. } else {
  3959. // Mark the end of the default argument so that we know when to
  3960. // stop when we parse it later on.
  3961. Token DefArgEnd;
  3962. DefArgEnd.startToken();
  3963. DefArgEnd.setKind(tok::cxx_defaultarg_end);
  3964. DefArgEnd.setLocation(Tok.getLocation());
  3965. DefArgToks->push_back(DefArgEnd);
  3966. Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
  3967. (*DefArgToks)[1].getLocation());
  3968. }
  3969. } else {
  3970. // Consume the '='.
  3971. ConsumeToken();
  3972. // The argument isn't actually potentially evaluated unless it is
  3973. // used.
  3974. EnterExpressionEvaluationContext Eval(Actions,
  3975. Sema::PotentiallyEvaluatedIfUsed);
  3976. ExprResult DefArgResult(ParseAssignmentExpression());
  3977. if (DefArgResult.isInvalid()) {
  3978. Actions.ActOnParamDefaultArgumentError(Param);
  3979. SkipUntil(tok::comma, tok::r_paren, true, true);
  3980. } else {
  3981. // Inform the actions module about the default argument
  3982. Actions.ActOnParamDefaultArgument(Param, EqualLoc,
  3983. DefArgResult.take());
  3984. }
  3985. }
  3986. }
  3987. ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
  3988. ParmDecl.getIdentifierLoc(), Param,
  3989. DefArgToks));
  3990. }
  3991. // If the next token is a comma, consume it and keep reading arguments.
  3992. if (Tok.isNot(tok::comma)) {
  3993. if (Tok.is(tok::ellipsis)) {
  3994. EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
  3995. if (!getLang().CPlusPlus) {
  3996. // We have ellipsis without a preceding ',', which is ill-formed
  3997. // in C. Complain and provide the fix.
  3998. Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
  3999. << FixItHint::CreateInsertion(EllipsisLoc, ", ");
  4000. }
  4001. }
  4002. break;
  4003. }
  4004. // Consume the comma.
  4005. ConsumeToken();
  4006. }
  4007. }
  4008. /// [C90] direct-declarator '[' constant-expression[opt] ']'
  4009. /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
  4010. /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
  4011. /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
  4012. /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
  4013. void Parser::ParseBracketDeclarator(Declarator &D) {
  4014. BalancedDelimiterTracker T(*this, tok::l_square);
  4015. T.consumeOpen();
  4016. // C array syntax has many features, but by-far the most common is [] and [4].
  4017. // This code does a fast path to handle some of the most obvious cases.
  4018. if (Tok.getKind() == tok::r_square) {
  4019. T.consumeClose();
  4020. ParsedAttributes attrs(AttrFactory);
  4021. MaybeParseCXX0XAttributes(attrs);
  4022. // Remember that we parsed the empty array type.
  4023. ExprResult NumElements;
  4024. D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
  4025. T.getOpenLocation(),
  4026. T.getCloseLocation()),
  4027. attrs, T.getCloseLocation());
  4028. return;
  4029. } else if (Tok.getKind() == tok::numeric_constant &&
  4030. GetLookAheadToken(1).is(tok::r_square)) {
  4031. // [4] is very common. Parse the numeric constant expression.
  4032. ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
  4033. ConsumeToken();
  4034. T.consumeClose();
  4035. ParsedAttributes attrs(AttrFactory);
  4036. MaybeParseCXX0XAttributes(attrs);
  4037. // Remember that we parsed a array type, and remember its features.
  4038. D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
  4039. ExprRes.release(),
  4040. T.getOpenLocation(),
  4041. T.getCloseLocation()),
  4042. attrs, T.getCloseLocation());
  4043. return;
  4044. }
  4045. // If valid, this location is the position where we read the 'static' keyword.
  4046. SourceLocation StaticLoc;
  4047. if (Tok.is(tok::kw_static))
  4048. StaticLoc = ConsumeToken();
  4049. // If there is a type-qualifier-list, read it now.
  4050. // Type qualifiers in an array subscript are a C99 feature.
  4051. DeclSpec DS(AttrFactory);
  4052. ParseTypeQualifierListOpt(DS, false /*no attributes*/);
  4053. // If we haven't already read 'static', check to see if there is one after the
  4054. // type-qualifier-list.
  4055. if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
  4056. StaticLoc = ConsumeToken();
  4057. // Handle "direct-declarator [ type-qual-list[opt] * ]".
  4058. bool isStar = false;
  4059. ExprResult NumElements;
  4060. // Handle the case where we have '[*]' as the array size. However, a leading
  4061. // star could be the start of an expression, for example 'X[*p + 4]'. Verify
  4062. // the the token after the star is a ']'. Since stars in arrays are
  4063. // infrequent, use of lookahead is not costly here.
  4064. if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
  4065. ConsumeToken(); // Eat the '*'.
  4066. if (StaticLoc.isValid()) {
  4067. Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
  4068. StaticLoc = SourceLocation(); // Drop the static.
  4069. }
  4070. isStar = true;
  4071. } else if (Tok.isNot(tok::r_square)) {
  4072. // Note, in C89, this production uses the constant-expr production instead
  4073. // of assignment-expr. The only difference is that assignment-expr allows
  4074. // things like '=' and '*='. Sema rejects these in C89 mode because they
  4075. // are not i-c-e's, so we don't need to distinguish between the two here.
  4076. // Parse the constant-expression or assignment-expression now (depending
  4077. // on dialect).
  4078. if (getLang().CPlusPlus) {
  4079. NumElements = ParseConstantExpression();
  4080. } else {
  4081. EnterExpressionEvaluationContext Unevaluated(Actions,
  4082. Sema::ConstantEvaluated);
  4083. NumElements = ParseAssignmentExpression();
  4084. }
  4085. }
  4086. // If there was an error parsing the assignment-expression, recover.
  4087. if (NumElements.isInvalid()) {
  4088. D.setInvalidType(true);
  4089. // If the expression was invalid, skip it.
  4090. SkipUntil(tok::r_square);
  4091. return;
  4092. }
  4093. T.consumeClose();
  4094. ParsedAttributes attrs(AttrFactory);
  4095. MaybeParseCXX0XAttributes(attrs);
  4096. // Remember that we parsed a array type, and remember its features.
  4097. D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
  4098. StaticLoc.isValid(), isStar,
  4099. NumElements.release(),
  4100. T.getOpenLocation(),
  4101. T.getCloseLocation()),
  4102. attrs, T.getCloseLocation());
  4103. }
  4104. /// [GNU] typeof-specifier:
  4105. /// typeof ( expressions )
  4106. /// typeof ( type-name )
  4107. /// [GNU/C++] typeof unary-expression
  4108. ///
  4109. void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
  4110. assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
  4111. Token OpTok = Tok;
  4112. SourceLocation StartLoc = ConsumeToken();
  4113. const bool hasParens = Tok.is(tok::l_paren);
  4114. EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
  4115. bool isCastExpr;
  4116. ParsedType CastTy;
  4117. SourceRange CastRange;
  4118. ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
  4119. CastTy, CastRange);
  4120. if (hasParens)
  4121. DS.setTypeofParensRange(CastRange);
  4122. if (CastRange.getEnd().isInvalid())
  4123. // FIXME: Not accurate, the range gets one token more than it should.
  4124. DS.SetRangeEnd(Tok.getLocation());
  4125. else
  4126. DS.SetRangeEnd(CastRange.getEnd());
  4127. if (isCastExpr) {
  4128. if (!CastTy) {
  4129. DS.SetTypeSpecError();
  4130. return;
  4131. }
  4132. const char *PrevSpec = 0;
  4133. unsigned DiagID;
  4134. // Check for duplicate type specifiers (e.g. "int typeof(int)").
  4135. if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
  4136. DiagID, CastTy))
  4137. Diag(StartLoc, DiagID) << PrevSpec;
  4138. return;
  4139. }
  4140. // If we get here, the operand to the typeof was an expresion.
  4141. if (Operand.isInvalid()) {
  4142. DS.SetTypeSpecError();
  4143. return;
  4144. }
  4145. // We might need to transform the operand if it is potentially evaluated.
  4146. Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
  4147. if (Operand.isInvalid()) {
  4148. DS.SetTypeSpecError();
  4149. return;
  4150. }
  4151. const char *PrevSpec = 0;
  4152. unsigned DiagID;
  4153. // Check for duplicate type specifiers (e.g. "int typeof(int)").
  4154. if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
  4155. DiagID, Operand.get()))
  4156. Diag(StartLoc, DiagID) << PrevSpec;
  4157. }
  4158. /// [C11] atomic-specifier:
  4159. /// _Atomic ( type-name )
  4160. ///
  4161. void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
  4162. assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
  4163. SourceLocation StartLoc = ConsumeToken();
  4164. BalancedDelimiterTracker T(*this, tok::l_paren);
  4165. if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
  4166. SkipUntil(tok::r_paren);
  4167. return;
  4168. }
  4169. TypeResult Result = ParseTypeName();
  4170. if (Result.isInvalid()) {
  4171. SkipUntil(tok::r_paren);
  4172. return;
  4173. }
  4174. // Match the ')'
  4175. T.consumeClose();
  4176. if (T.getCloseLocation().isInvalid())
  4177. return;
  4178. DS.setTypeofParensRange(T.getRange());
  4179. DS.SetRangeEnd(T.getCloseLocation());
  4180. const char *PrevSpec = 0;
  4181. unsigned DiagID;
  4182. if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
  4183. DiagID, Result.release()))
  4184. Diag(StartLoc, DiagID) << PrevSpec;
  4185. }
  4186. /// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
  4187. /// from TryAltiVecVectorToken.
  4188. bool Parser::TryAltiVecVectorTokenOutOfLine() {
  4189. Token Next = NextToken();
  4190. switch (Next.getKind()) {
  4191. default: return false;
  4192. case tok::kw_short:
  4193. case tok::kw_long:
  4194. case tok::kw_signed:
  4195. case tok::kw_unsigned:
  4196. case tok::kw_void:
  4197. case tok::kw_char:
  4198. case tok::kw_int:
  4199. case tok::kw_float:
  4200. case tok::kw_double:
  4201. case tok::kw_bool:
  4202. case tok::kw___pixel:
  4203. Tok.setKind(tok::kw___vector);
  4204. return true;
  4205. case tok::identifier:
  4206. if (Next.getIdentifierInfo() == Ident_pixel) {
  4207. Tok.setKind(tok::kw___vector);
  4208. return true;
  4209. }
  4210. return false;
  4211. }
  4212. }
  4213. bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
  4214. const char *&PrevSpec, unsigned &DiagID,
  4215. bool &isInvalid) {
  4216. if (Tok.getIdentifierInfo() == Ident_vector) {
  4217. Token Next = NextToken();
  4218. switch (Next.getKind()) {
  4219. case tok::kw_short:
  4220. case tok::kw_long:
  4221. case tok::kw_signed:
  4222. case tok::kw_unsigned:
  4223. case tok::kw_void:
  4224. case tok::kw_char:
  4225. case tok::kw_int:
  4226. case tok::kw_float:
  4227. case tok::kw_double:
  4228. case tok::kw_bool:
  4229. case tok::kw___pixel:
  4230. isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
  4231. return true;
  4232. case tok::identifier:
  4233. if (Next.getIdentifierInfo() == Ident_pixel) {
  4234. isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
  4235. return true;
  4236. }
  4237. break;
  4238. default:
  4239. break;
  4240. }
  4241. } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
  4242. DS.isTypeAltiVecVector()) {
  4243. isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
  4244. return true;
  4245. }
  4246. return false;
  4247. }