libcp1plugin.cc 97 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566
  1. /* Library interface to C++ front end.
  2. Copyright (C) 2014-2022 Free Software Foundation, Inc.
  3. This file is part of GCC. As it interacts with GDB through libcc1,
  4. they all become a single program as regards the GNU GPL's requirements.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <cc1plugin-config.h>
  17. #undef PACKAGE_NAME
  18. #undef PACKAGE_STRING
  19. #undef PACKAGE_TARNAME
  20. #undef PACKAGE_VERSION
  21. #include "../gcc/config.h"
  22. #undef PACKAGE_NAME
  23. #undef PACKAGE_STRING
  24. #undef PACKAGE_TARNAME
  25. #undef PACKAGE_VERSION
  26. #include "gcc-plugin.h"
  27. #include "system.h"
  28. #include "coretypes.h"
  29. #include "stringpool.h"
  30. #include "gcc-interface.h"
  31. #include "machmode.h"
  32. #include "vec.h"
  33. #include "double-int.h"
  34. #include "input.h"
  35. #include "alias.h"
  36. #include "symtab.h"
  37. #include "options.h"
  38. #include "wide-int.h"
  39. #include "inchash.h"
  40. #include "tree.h"
  41. #include "fold-const.h"
  42. #include "stor-layout.h"
  43. #include "cp-tree.h"
  44. #include "toplev.h"
  45. #include "timevar.h"
  46. #include "hash-table.h"
  47. #include "tm.h"
  48. #include "c-family/c-pragma.h"
  49. // #include "c-lang.h"
  50. #include "diagnostic.h"
  51. #include "langhooks.h"
  52. #include "langhooks-def.h"
  53. #include "decl.h"
  54. #include "function.h"
  55. #undef cfun // we want to assign to it, and function.h won't let us
  56. #include "callbacks.hh"
  57. #include "connection.hh"
  58. #include "marshall-cp.hh"
  59. #include "rpc.hh"
  60. #include "context.hh"
  61. #include <vector>
  62. using namespace cc1_plugin;
  63. static_assert (GCC_CP_SYMBOL_MASK >= GCC_CP_SYMBOL_END,
  64. "GCC_CP_SYMBOL_MASK >= GCC_CP_SYMBOL_END");
  65. static void
  66. plugin_binding_oracle (enum cp_oracle_request kind, tree identifier)
  67. {
  68. enum gcc_cp_oracle_request request;
  69. gcc_assert (current_context != NULL);
  70. switch (kind)
  71. {
  72. case CP_ORACLE_IDENTIFIER:
  73. request = GCC_CP_ORACLE_IDENTIFIER;
  74. break;
  75. default:
  76. abort ();
  77. }
  78. int ignore;
  79. cc1_plugin::call (current_context, "binding_oracle", &ignore,
  80. request, IDENTIFIER_POINTER (identifier));
  81. }
  82. static int push_count;
  83. /* at_function_scope_p () tests cfun, indicating we're actually
  84. compiling the function, but we don't even set it when pretending to
  85. enter a function scope. We use this distinction to tell these two
  86. cases apart: we don't want to define e.g. class names in the user
  87. expression function's scope, when they're local to the original
  88. function, because they'd get the wrong linkage name. */
  89. static bool
  90. at_fake_function_scope_p ()
  91. {
  92. return (!cfun || cfun->decl != current_function_decl)
  93. && current_scope () == current_function_decl;
  94. }
  95. static void
  96. push_fake_function (tree fndecl, scope_kind kind = sk_function_parms)
  97. {
  98. current_function_decl = fndecl;
  99. begin_scope (kind, fndecl);
  100. ++function_depth;
  101. begin_scope (sk_block, NULL);
  102. }
  103. static void
  104. pop_scope ()
  105. {
  106. if (toplevel_bindings_p () && current_namespace == global_namespace)
  107. pop_from_top_level ();
  108. else if (at_namespace_scope_p ())
  109. pop_namespace ();
  110. else if (at_class_scope_p ())
  111. popclass ();
  112. else
  113. {
  114. gcc_assert (at_fake_function_scope_p ());
  115. gcc_assert (!at_function_scope_p ());
  116. gcc_assert (current_binding_level->kind == sk_block
  117. && current_binding_level->this_entity == NULL);
  118. leave_scope ();
  119. --function_depth;
  120. gcc_assert (current_binding_level->this_entity
  121. == current_function_decl);
  122. leave_scope ();
  123. current_function_decl = NULL;
  124. for (cp_binding_level *scope = current_binding_level;
  125. scope; scope = scope->level_chain)
  126. if (scope->kind == sk_function_parms)
  127. {
  128. current_function_decl = scope->this_entity;
  129. break;
  130. }
  131. }
  132. }
  133. static void
  134. supplement_binding (cxx_binding *binding, tree decl)
  135. {
  136. /* FIXME: this is pretty much a copy of supplement_binding_1 in
  137. ../gcc/cp/name-lookup.c; the few replaced/removed bits are marked
  138. with "// _1:". */
  139. tree bval = binding->value;
  140. bool ok = true;
  141. tree target_bval = strip_using_decl (bval);
  142. tree target_decl = strip_using_decl (decl);
  143. if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl)
  144. && target_decl != target_bval
  145. && (TREE_CODE (target_bval) != TYPE_DECL
  146. /* We allow pushing an enum multiple times in a class
  147. template in order to handle late matching of underlying
  148. type on an opaque-enum-declaration followed by an
  149. enum-specifier. */
  150. || (processing_template_decl
  151. && TREE_CODE (TREE_TYPE (target_decl)) == ENUMERAL_TYPE
  152. && TREE_CODE (TREE_TYPE (target_bval)) == ENUMERAL_TYPE
  153. && (dependent_type_p (ENUM_UNDERLYING_TYPE
  154. (TREE_TYPE (target_decl)))
  155. || dependent_type_p (ENUM_UNDERLYING_TYPE
  156. (TREE_TYPE (target_bval)))))))
  157. /* The new name is the type name. */
  158. binding->type = decl;
  159. else if (/* TARGET_BVAL is null when push_class_level_binding moves
  160. an inherited type-binding out of the way to make room
  161. for a new value binding. */
  162. !target_bval
  163. /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
  164. has been used in a non-class scope prior declaration.
  165. In that case, we should have already issued a
  166. diagnostic; for graceful error recovery purpose, pretend
  167. this was the intended declaration for that name. */
  168. || target_bval == error_mark_node
  169. /* If TARGET_BVAL is anticipated but has not yet been
  170. declared, pretend it is not there at all. */
  171. || (TREE_CODE (target_bval) == FUNCTION_DECL
  172. && DECL_IS_UNDECLARED_BUILTIN (target_bval)))
  173. binding->value = decl;
  174. else if (TREE_CODE (target_bval) == TYPE_DECL
  175. && DECL_ARTIFICIAL (target_bval)
  176. && target_decl != target_bval
  177. && (TREE_CODE (target_decl) != TYPE_DECL
  178. || same_type_p (TREE_TYPE (target_decl),
  179. TREE_TYPE (target_bval))))
  180. {
  181. /* The old binding was a type name. It was placed in
  182. VALUE field because it was thought, at the point it was
  183. declared, to be the only entity with such a name. Move the
  184. type name into the type slot; it is now hidden by the new
  185. binding. */
  186. binding->type = bval;
  187. binding->value = decl;
  188. binding->value_is_inherited = false;
  189. }
  190. else if (TREE_CODE (target_bval) == TYPE_DECL
  191. && TREE_CODE (target_decl) == TYPE_DECL
  192. && DECL_NAME (target_decl) == DECL_NAME (target_bval)
  193. && binding->scope->kind != sk_class
  194. && (same_type_p (TREE_TYPE (target_decl), TREE_TYPE (target_bval))
  195. /* If either type involves template parameters, we must
  196. wait until instantiation. */
  197. || uses_template_parms (TREE_TYPE (target_decl))
  198. || uses_template_parms (TREE_TYPE (target_bval))))
  199. /* We have two typedef-names, both naming the same type to have
  200. the same name. In general, this is OK because of:
  201. [dcl.typedef]
  202. In a given scope, a typedef specifier can be used to redefine
  203. the name of any type declared in that scope to refer to the
  204. type to which it already refers.
  205. However, in class scopes, this rule does not apply due to the
  206. stricter language in [class.mem] prohibiting redeclarations of
  207. members. */
  208. ok = false;
  209. /* There can be two block-scope declarations of the same variable,
  210. so long as they are `extern' declarations. However, there cannot
  211. be two declarations of the same static data member:
  212. [class.mem]
  213. A member shall not be declared twice in the
  214. member-specification. */
  215. else if (VAR_P (target_decl)
  216. && VAR_P (target_bval)
  217. && DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval)
  218. && !DECL_CLASS_SCOPE_P (target_decl))
  219. {
  220. duplicate_decls (decl, binding->value);
  221. ok = false;
  222. }
  223. else if (TREE_CODE (decl) == NAMESPACE_DECL
  224. && TREE_CODE (bval) == NAMESPACE_DECL
  225. && DECL_NAMESPACE_ALIAS (decl)
  226. && DECL_NAMESPACE_ALIAS (bval)
  227. && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
  228. /* [namespace.alias]
  229. In a declarative region, a namespace-alias-definition can be
  230. used to redefine a namespace-alias declared in that declarative
  231. region to refer only to the namespace to which it already
  232. refers. */
  233. ok = false;
  234. else
  235. {
  236. // _1: diagnose_name_conflict (decl, bval);
  237. ok = false;
  238. }
  239. gcc_assert (ok); // _1: return ok;
  240. }
  241. static void
  242. reactivate_decl (tree decl, cp_binding_level *b)
  243. {
  244. bool in_function_p = TREE_CODE (b->this_entity) == FUNCTION_DECL;
  245. gcc_assert (in_function_p
  246. || (b == current_binding_level
  247. && !at_class_scope_p ()));
  248. tree id = DECL_NAME (decl);
  249. tree type = NULL_TREE;
  250. if (TREE_CODE (decl) == TYPE_DECL)
  251. type = TREE_TYPE (decl);
  252. if (type && TYPE_NAME (type) == decl
  253. && (RECORD_OR_UNION_CODE_P (TREE_CODE (type))
  254. || TREE_CODE (type) == ENUMERAL_TYPE))
  255. {
  256. gcc_assert (in_function_p && DECL_CONTEXT (decl) == b->this_entity);
  257. type = TREE_TYPE (decl);
  258. }
  259. else
  260. {
  261. gcc_assert (DECL_CONTEXT (decl) == b->this_entity
  262. || DECL_CONTEXT (decl) == global_namespace
  263. || TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL);
  264. type = NULL_TREE;
  265. }
  266. /* Adjust IDENTIFIER_BINDING to what it would have been if we were
  267. at binding level B. Save the binding chain up to that point in
  268. [binding, *chainp), and take note of the outermost bindings found
  269. before B. */
  270. cxx_binding *binding = IDENTIFIER_BINDING (id), **chainp = NULL;
  271. tree *shadowing_type_p = NULL;
  272. if (binding)
  273. {
  274. cp_binding_level *bc = current_binding_level;
  275. for (cxx_binding *prev_binding = binding;
  276. prev_binding; prev_binding = prev_binding->previous)
  277. {
  278. while (bc != b && bc != prev_binding->scope)
  279. bc = bc->level_chain;
  280. if (bc == b)
  281. {
  282. if (!chainp)
  283. binding = NULL;
  284. break;
  285. }
  286. chainp = &prev_binding->previous;
  287. if (type)
  288. for (tree tshadow = prev_binding->scope->type_shadowed;
  289. tshadow; tshadow = TREE_CHAIN (tshadow))
  290. if (TREE_PURPOSE (tshadow) == id)
  291. {
  292. shadowing_type_p = &TREE_VALUE (tshadow);
  293. break;
  294. }
  295. }
  296. }
  297. if (chainp)
  298. {
  299. IDENTIFIER_BINDING (id) = *chainp;
  300. *chainp = NULL;
  301. }
  302. /* Like push_local_binding, supplement or add a binding to the
  303. desired level. */
  304. if (IDENTIFIER_BINDING (id) && IDENTIFIER_BINDING (id)->scope == b)
  305. supplement_binding (IDENTIFIER_BINDING (id), decl);
  306. else
  307. push_binding (id, decl, b);
  308. /* Now restore the binding chain we'd temporarily removed. */
  309. if (chainp)
  310. {
  311. *chainp = IDENTIFIER_BINDING (id);
  312. IDENTIFIER_BINDING (id) = binding;
  313. if (type)
  314. {
  315. /* Insert the new type binding in the shadowing_type_p
  316. TREE_VALUE chain. */
  317. tree shadowed_type = NULL_TREE;
  318. if (shadowing_type_p)
  319. {
  320. shadowed_type = *shadowing_type_p;
  321. *shadowing_type_p = type;
  322. }
  323. b->type_shadowed = tree_cons (id, shadowed_type, b->type_shadowed);
  324. TREE_TYPE (b->type_shadowed) = type;
  325. }
  326. }
  327. else if (type)
  328. {
  329. /* Our new binding is the active one, so shadow the earlier
  330. binding. */
  331. b->type_shadowed = tree_cons (id, REAL_IDENTIFIER_TYPE_VALUE (id),
  332. b->type_shadowed);
  333. TREE_TYPE (b->type_shadowed) = type;
  334. SET_IDENTIFIER_TYPE_VALUE (id, type);
  335. }
  336. /* Record that we have a binding for ID, like add_decl_to_level. */
  337. tree node = build_tree_list (NULL_TREE, decl);
  338. TREE_CHAIN (node) = b->names;
  339. b->names = node;
  340. }
  341. static void
  342. plugin_pragma_push_user_expression (cpp_reader *)
  343. {
  344. if (push_count++)
  345. return;
  346. gcc_assert (!current_class_ptr);
  347. gcc_assert (!current_class_ref);
  348. gcc_assert (!cp_binding_oracle);
  349. cp_binding_oracle = plugin_binding_oracle;
  350. /* Make the function containing the user expression a global
  351. friend, so as to bypass access controls in it. */
  352. if (at_function_scope_p ())
  353. set_global_friend (current_function_decl);
  354. gcc_assert (at_function_scope_p ());
  355. function *save_cfun = cfun;
  356. cp_binding_level *orig_binding_level = current_binding_level;
  357. {
  358. int success;
  359. cc1_plugin::call (current_context, "enter_scope", &success);
  360. }
  361. gcc_assert (at_fake_function_scope_p () || at_function_scope_p ());
  362. function *unchanged_cfun = cfun;
  363. tree changed_func_decl = current_function_decl;
  364. gcc_assert (current_class_type == DECL_CONTEXT (current_function_decl)
  365. || !(RECORD_OR_UNION_CODE_P
  366. (TREE_CODE (DECL_CONTEXT (current_function_decl)))));
  367. push_fake_function (save_cfun->decl, sk_block);
  368. current_class_type = NULL_TREE;
  369. if (unchanged_cfun)
  370. {
  371. /* If we get here, GDB did NOT change the context. */
  372. gcc_assert (cfun == save_cfun);
  373. gcc_assert (at_function_scope_p ());
  374. gcc_assert (orig_binding_level
  375. == current_binding_level->level_chain->level_chain);
  376. }
  377. else
  378. {
  379. cfun = save_cfun;
  380. gcc_assert (at_function_scope_p ());
  381. cp_binding_level *b = current_binding_level->level_chain;
  382. gcc_assert (b->this_entity == cfun->decl);
  383. /* Reactivate local names from the previous context. Use
  384. IDENTIFIER_MARKED to avoid reactivating shadowed names. */
  385. for (cp_binding_level *level = orig_binding_level;;)
  386. {
  387. for (tree name = level->names;
  388. name; name = TREE_CHAIN (name))
  389. {
  390. tree decl = name;
  391. if (TREE_CODE (decl) == TREE_LIST)
  392. decl = TREE_VALUE (decl);
  393. if (IDENTIFIER_MARKED (DECL_NAME (decl)))
  394. continue;
  395. IDENTIFIER_MARKED (DECL_NAME (decl)) = 1;
  396. reactivate_decl (decl, b);
  397. }
  398. if (level->kind == sk_function_parms
  399. && level->this_entity == cfun->decl)
  400. break;
  401. gcc_assert (!level->this_entity);
  402. level = level->level_chain;
  403. }
  404. /* Now, clear the markers. */
  405. for (tree name = b->names; name; name = TREE_CHAIN (name))
  406. {
  407. tree decl = name;
  408. if (TREE_CODE (decl) == TREE_LIST)
  409. decl = TREE_VALUE (decl);
  410. gcc_assert (IDENTIFIER_MARKED (DECL_NAME (decl)));
  411. IDENTIFIER_MARKED (DECL_NAME (decl)) = 0;
  412. }
  413. }
  414. if (unchanged_cfun || DECL_NONSTATIC_MEMBER_FUNCTION_P (changed_func_decl))
  415. {
  416. /* Check whether the oracle supplies us with a "this", and if
  417. so, arrange for data members and this itself to be
  418. usable. */
  419. tree this_val = lookup_name (get_identifier ("this"));
  420. current_class_ref = !this_val ? NULL_TREE
  421. : cp_build_indirect_ref (input_location, this_val, RO_NULL,
  422. tf_warning_or_error);
  423. current_class_ptr = this_val;
  424. }
  425. }
  426. static void
  427. plugin_pragma_pop_user_expression (cpp_reader *)
  428. {
  429. if (--push_count)
  430. return;
  431. gcc_assert (cp_binding_oracle);
  432. gcc_assert (at_function_scope_p ());
  433. function *save_cfun = cfun;
  434. current_class_ptr = NULL_TREE;
  435. current_class_ref = NULL_TREE;
  436. cfun = NULL;
  437. pop_scope ();
  438. if (RECORD_OR_UNION_CODE_P (TREE_CODE (DECL_CONTEXT (current_function_decl))))
  439. current_class_type = DECL_CONTEXT (current_function_decl);
  440. {
  441. int success;
  442. cc1_plugin::call (current_context, "leave_scope", &success);
  443. }
  444. if (!cfun)
  445. cfun = save_cfun;
  446. else
  447. gcc_assert (cfun == save_cfun);
  448. cp_binding_oracle = NULL;
  449. gcc_assert (at_function_scope_p ());
  450. }
  451. static void
  452. plugin_init_extra_pragmas (void *, void *)
  453. {
  454. c_register_pragma ("GCC", "push_user_expression", plugin_pragma_push_user_expression);
  455. c_register_pragma ("GCC", "pop_user_expression", plugin_pragma_pop_user_expression);
  456. /* FIXME: this one should go once we get GDB to use push and pop. */
  457. c_register_pragma ("GCC", "user_expression", plugin_pragma_push_user_expression);
  458. }
  459. static decl_addr_value
  460. build_decl_addr_value (tree decl, gcc_address address)
  461. {
  462. decl_addr_value value = {
  463. decl,
  464. build_int_cst_type (ptr_type_node, address)
  465. };
  466. return value;
  467. }
  468. static decl_addr_value *
  469. record_decl_address (plugin_context *ctx, decl_addr_value value)
  470. {
  471. decl_addr_value **slot = ctx->address_map.find_slot (&value, INSERT);
  472. gcc_assert (*slot == NULL);
  473. *slot
  474. = static_cast<decl_addr_value *> (xmalloc (sizeof (decl_addr_value)));
  475. **slot = value;
  476. /* We don't want GCC to warn about e.g. static functions
  477. without a code definition. */
  478. suppress_warning (value.decl);
  479. return *slot;
  480. }
  481. // Maybe rewrite a decl to its address.
  482. static tree
  483. address_rewriter (tree *in, int *walk_subtrees, void *arg)
  484. {
  485. plugin_context *ctx = (plugin_context *) arg;
  486. if (!DECL_P (*in)
  487. || TREE_CODE (*in) == NAMESPACE_DECL
  488. || DECL_NAME (*in) == NULL_TREE)
  489. return NULL_TREE;
  490. decl_addr_value value;
  491. value.decl = *in;
  492. decl_addr_value *found_value = ctx->address_map.find (&value);
  493. if (found_value != NULL)
  494. ;
  495. else if (HAS_DECL_ASSEMBLER_NAME_P (*in))
  496. {
  497. gcc_address address;
  498. if (!cc1_plugin::call (ctx, "address_oracle", &address,
  499. IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (*in))))
  500. return NULL_TREE;
  501. if (address == 0)
  502. return NULL_TREE;
  503. // Insert the decl into the address map in case it is referenced
  504. // again.
  505. value = build_decl_addr_value (value.decl, address);
  506. found_value = record_decl_address (ctx, value);
  507. }
  508. else
  509. return NULL_TREE;
  510. if (found_value->address != error_mark_node)
  511. {
  512. // We have an address for the decl, so rewrite the tree.
  513. tree ptr_type = build_pointer_type (TREE_TYPE (*in));
  514. *in = fold_build1 (INDIRECT_REF, TREE_TYPE (*in),
  515. fold_build1 (CONVERT_EXPR, ptr_type,
  516. found_value->address));
  517. }
  518. *walk_subtrees = 0;
  519. return NULL_TREE;
  520. }
  521. // When generating code for gdb, we want to be able to use absolute
  522. // addresses to refer to otherwise external objects that gdb knows
  523. // about. gdb passes in these addresses when building decls, and then
  524. // before gimplification we go through the trees, rewriting uses to
  525. // the equivalent of "*(TYPE *) ADDR".
  526. static void
  527. rewrite_decls_to_addresses (void *function_in, void *)
  528. {
  529. tree function = (tree) function_in;
  530. // Do nothing if we're not in gdb.
  531. if (current_context == NULL)
  532. return;
  533. walk_tree (&DECL_SAVED_TREE (function), address_rewriter, current_context,
  534. NULL);
  535. }
  536. static inline tree
  537. safe_push_template_decl (tree decl)
  538. {
  539. void (*save_oracle) (enum cp_oracle_request, tree identifier);
  540. save_oracle = cp_binding_oracle;
  541. cp_binding_oracle = NULL;
  542. tree ret = push_template_decl (decl);
  543. cp_binding_oracle = save_oracle;
  544. return ret;
  545. }
  546. static inline tree
  547. safe_pushtag (tree name, tree type)
  548. {
  549. void (*save_oracle) (enum cp_oracle_request, tree identifier);
  550. save_oracle = cp_binding_oracle;
  551. cp_binding_oracle = NULL;
  552. tree ret = pushtag (name, type);
  553. cp_binding_oracle = save_oracle;
  554. return ret;
  555. }
  556. static inline tree
  557. safe_pushdecl (tree decl)
  558. {
  559. void (*save_oracle) (enum cp_oracle_request, tree identifier);
  560. save_oracle = cp_binding_oracle;
  561. cp_binding_oracle = NULL;
  562. tree ret = pushdecl (decl);
  563. cp_binding_oracle = save_oracle;
  564. return ret;
  565. }
  566. int
  567. plugin_push_namespace (cc1_plugin::connection *,
  568. const char *name)
  569. {
  570. if (name && !*name)
  571. push_to_top_level ();
  572. else
  573. push_namespace (name ? get_identifier (name) : NULL);
  574. return 1;
  575. }
  576. int
  577. plugin_push_class (cc1_plugin::connection *,
  578. gcc_type type_in)
  579. {
  580. tree type = convert_in (type_in);
  581. gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (type)));
  582. gcc_assert (TYPE_CONTEXT (type) == FROB_CONTEXT (current_scope ()));
  583. pushclass (type);
  584. return 1;
  585. }
  586. int
  587. plugin_push_function (cc1_plugin::connection *,
  588. gcc_decl function_decl_in)
  589. {
  590. tree fndecl = convert_in (function_decl_in);
  591. gcc_assert (TREE_CODE (fndecl) == FUNCTION_DECL);
  592. gcc_assert (DECL_CONTEXT (fndecl) == FROB_CONTEXT (current_scope ()));
  593. push_fake_function (fndecl);
  594. return 1;
  595. }
  596. int
  597. plugin_pop_binding_level (cc1_plugin::connection *)
  598. {
  599. pop_scope ();
  600. return 1;
  601. }
  602. int
  603. plugin_reactivate_decl (cc1_plugin::connection *,
  604. gcc_decl decl_in,
  605. gcc_decl scope_in)
  606. {
  607. tree decl = convert_in (decl_in);
  608. tree scope = convert_in (scope_in);
  609. gcc_assert (TREE_CODE (decl) == VAR_DECL
  610. || TREE_CODE (decl) == FUNCTION_DECL
  611. || TREE_CODE (decl) == TYPE_DECL);
  612. cp_binding_level *b;
  613. if (scope)
  614. {
  615. gcc_assert (TREE_CODE (scope) == FUNCTION_DECL);
  616. for (b = current_binding_level;
  617. b->this_entity != scope;
  618. b = b->level_chain)
  619. gcc_assert (b->this_entity != global_namespace);
  620. }
  621. else
  622. {
  623. gcc_assert (!at_class_scope_p ());
  624. b = current_binding_level;
  625. }
  626. reactivate_decl (decl, b);
  627. return 1;
  628. }
  629. static tree
  630. get_current_scope ()
  631. {
  632. tree decl;
  633. if (at_namespace_scope_p ())
  634. decl = current_namespace;
  635. else if (at_class_scope_p ())
  636. decl = TYPE_NAME (current_class_type);
  637. else if (at_fake_function_scope_p () || at_function_scope_p ())
  638. decl = current_function_decl;
  639. else
  640. gcc_unreachable ();
  641. return decl;
  642. }
  643. gcc_decl
  644. plugin_get_current_binding_level_decl (cc1_plugin::connection *)
  645. {
  646. tree decl = get_current_scope ();
  647. return convert_out (decl);
  648. }
  649. int
  650. plugin_make_namespace_inline (cc1_plugin::connection *)
  651. {
  652. tree inline_ns = current_namespace;
  653. gcc_assert (toplevel_bindings_p ());
  654. gcc_assert (inline_ns != global_namespace);
  655. tree parent_ns = CP_DECL_CONTEXT (inline_ns);
  656. if (DECL_NAMESPACE_INLINE_P (inline_ns))
  657. return 0;
  658. DECL_NAMESPACE_INLINE_P (inline_ns) = true;
  659. vec_safe_push (DECL_NAMESPACE_INLINEES (parent_ns), inline_ns);
  660. return 1;
  661. }
  662. int
  663. plugin_add_using_namespace (cc1_plugin::connection *,
  664. gcc_decl used_ns_in)
  665. {
  666. tree used_ns = convert_in (used_ns_in);
  667. gcc_assert (TREE_CODE (used_ns) == NAMESPACE_DECL);
  668. finish_using_directive (used_ns, NULL_TREE);
  669. return 1;
  670. }
  671. int
  672. plugin_add_namespace_alias (cc1_plugin::connection *,
  673. const char *id,
  674. gcc_decl target_in)
  675. {
  676. tree name = get_identifier (id);
  677. tree target = convert_in (target_in);
  678. do_namespace_alias (name, target);
  679. return 1;
  680. }
  681. static inline void
  682. set_access_flags (tree decl, enum gcc_cp_symbol_kind flags)
  683. {
  684. gcc_assert (!(flags & GCC_CP_ACCESS_MASK) == !DECL_CLASS_SCOPE_P (decl));
  685. switch (flags & GCC_CP_ACCESS_MASK)
  686. {
  687. case GCC_CP_ACCESS_PRIVATE:
  688. TREE_PRIVATE (decl) = true;
  689. current_access_specifier = access_private_node;
  690. break;
  691. case GCC_CP_ACCESS_PROTECTED:
  692. TREE_PROTECTED (decl) = true;
  693. current_access_specifier = access_protected_node;
  694. break;
  695. case GCC_CP_ACCESS_PUBLIC:
  696. current_access_specifier = access_public_node;
  697. break;
  698. default:
  699. break;
  700. }
  701. }
  702. int
  703. plugin_add_using_decl (cc1_plugin::connection *,
  704. enum gcc_cp_symbol_kind flags,
  705. gcc_decl target_in)
  706. {
  707. tree target = convert_in (target_in);
  708. gcc_assert ((flags & GCC_CP_SYMBOL_MASK) == GCC_CP_SYMBOL_USING);
  709. gcc_assert (!(flags & GCC_CP_FLAG_MASK));
  710. enum gcc_cp_symbol_kind acc_flags;
  711. acc_flags = (enum gcc_cp_symbol_kind) (flags & GCC_CP_ACCESS_MASK);
  712. gcc_assert (!template_parm_scope_p ());
  713. bool class_member_p = at_class_scope_p ();
  714. gcc_assert (!(acc_flags & GCC_CP_ACCESS_MASK) == !class_member_p);
  715. tree identifier = DECL_NAME (target);
  716. tree tcontext = DECL_CONTEXT (target);
  717. if (UNSCOPED_ENUM_P (tcontext))
  718. tcontext = CP_TYPE_CONTEXT (tcontext);
  719. if (class_member_p)
  720. {
  721. tree decl = do_class_using_decl (tcontext, identifier);
  722. set_access_flags (decl, flags);
  723. finish_member_declaration (decl);
  724. }
  725. else
  726. {
  727. /* We can't be at local scope. */
  728. gcc_assert (at_namespace_scope_p ());
  729. finish_nonmember_using_decl (tcontext, identifier);
  730. }
  731. return 1;
  732. }
  733. static tree
  734. build_named_class_type (enum tree_code code,
  735. tree id,
  736. location_t loc)
  737. {
  738. /* See at_fake_function_scope_p. */
  739. gcc_assert (!at_function_scope_p ());
  740. tree type = make_class_type (code);
  741. tree type_decl = build_decl (loc, TYPE_DECL, id, type);
  742. TYPE_NAME (type) = type_decl;
  743. TYPE_STUB_DECL (type) = type_decl;
  744. DECL_CONTEXT (type_decl) = TYPE_CONTEXT (type);
  745. return type_decl;
  746. }
  747. /* Abuse an unused field of the dummy template parms entry to hold the
  748. parm list. */
  749. #define TP_PARM_LIST TREE_TYPE (current_template_parms)
  750. gcc_decl
  751. plugin_build_decl (cc1_plugin::connection *self,
  752. const char *name,
  753. enum gcc_cp_symbol_kind sym_kind,
  754. gcc_type sym_type_in,
  755. const char *substitution_name,
  756. gcc_address address,
  757. const char *filename,
  758. unsigned int line_number)
  759. {
  760. plugin_context *ctx = static_cast<plugin_context *> (self);
  761. gcc_assert (!name || !strchr (name, ':')); // FIXME: this can go eventually.
  762. enum tree_code code;
  763. tree decl;
  764. tree sym_type = convert_in (sym_type_in);
  765. enum gcc_cp_symbol_kind sym_flags;
  766. sym_flags = (enum gcc_cp_symbol_kind) (sym_kind & GCC_CP_FLAG_MASK);
  767. enum gcc_cp_symbol_kind acc_flags;
  768. acc_flags = (enum gcc_cp_symbol_kind) (sym_kind & GCC_CP_ACCESS_MASK);
  769. sym_kind = (enum gcc_cp_symbol_kind) (sym_kind & GCC_CP_SYMBOL_MASK);
  770. switch (sym_kind)
  771. {
  772. case GCC_CP_SYMBOL_FUNCTION:
  773. code = FUNCTION_DECL;
  774. gcc_assert (!(sym_flags & ~GCC_CP_FLAG_MASK_FUNCTION));
  775. break;
  776. case GCC_CP_SYMBOL_VARIABLE:
  777. code = VAR_DECL;
  778. gcc_assert (!(sym_flags & ~GCC_CP_FLAG_MASK_VARIABLE));
  779. break;
  780. case GCC_CP_SYMBOL_TYPEDEF:
  781. code = TYPE_DECL;
  782. gcc_assert (!sym_flags);
  783. break;
  784. case GCC_CP_SYMBOL_CLASS:
  785. code = RECORD_TYPE;
  786. gcc_assert (!(sym_flags & ~GCC_CP_FLAG_MASK_CLASS));
  787. gcc_assert (!sym_type);
  788. break;
  789. case GCC_CP_SYMBOL_UNION:
  790. code = UNION_TYPE;
  791. gcc_assert (!sym_flags);
  792. gcc_assert (!sym_type);
  793. break;
  794. default:
  795. gcc_unreachable ();
  796. }
  797. bool template_decl_p = template_parm_scope_p ();
  798. if (template_decl_p)
  799. {
  800. gcc_assert (code == FUNCTION_DECL || code == RECORD_TYPE
  801. || code == TYPE_DECL);
  802. /* Finish the template parm list that started this template parm. */
  803. end_template_parm_list (TP_PARM_LIST);
  804. gcc_assert (!address);
  805. gcc_assert (!substitution_name);
  806. }
  807. location_t loc = ctx->get_location_t (filename, line_number);
  808. bool class_member_p = at_class_scope_p ();
  809. bool ctor = false, dtor = false, assop = false;
  810. tree_code opcode = ERROR_MARK;
  811. gcc_assert (!(acc_flags & GCC_CP_ACCESS_MASK) == !class_member_p);
  812. tree identifier;
  813. if (code != FUNCTION_DECL
  814. || !(sym_flags & GCC_CP_FLAG_SPECIAL_FUNCTION))
  815. {
  816. if (name)
  817. identifier = get_identifier (name);
  818. else
  819. {
  820. gcc_assert (RECORD_OR_UNION_CODE_P (code));
  821. identifier = make_anon_name ();
  822. }
  823. }
  824. if (code == FUNCTION_DECL)
  825. {
  826. if (sym_flags & GCC_CP_FLAG_SPECIAL_FUNCTION)
  827. {
  828. #define CHARS2(f,s) (((unsigned char)f << CHAR_BIT) | (unsigned char)s)
  829. switch (CHARS2 (name[0], name[1]))
  830. {
  831. case CHARS2 ('C', 0x0): // ctor base declaration
  832. case CHARS2 ('C', ' '):
  833. case CHARS2 ('C', '1'):
  834. case CHARS2 ('C', '2'):
  835. case CHARS2 ('C', '4'):
  836. ctor = true;
  837. cdtor:
  838. gcc_assert (!address);
  839. gcc_assert (!substitution_name);
  840. identifier = DECL_NAME (TYPE_NAME (current_class_type));
  841. break;
  842. case CHARS2 ('D', 0x0): // dtor base declaration
  843. case CHARS2 ('D', ' '):
  844. case CHARS2 ('D', '0'):
  845. case CHARS2 ('D', '1'):
  846. case CHARS2 ('D', '2'):
  847. case CHARS2 ('D', '4'):
  848. gcc_assert (!template_decl_p);
  849. dtor = true;
  850. goto cdtor;
  851. case CHARS2 ('n', 'w'): // operator new
  852. opcode = NEW_EXPR;
  853. break;
  854. case CHARS2 ('n', 'a'): // operator new[]
  855. opcode = VEC_NEW_EXPR;
  856. break;
  857. case CHARS2 ('d', 'l'): // operator delete
  858. opcode = DELETE_EXPR;
  859. break;
  860. case CHARS2 ('d', 'a'): // operator delete[]
  861. opcode = VEC_DELETE_EXPR;
  862. break;
  863. case CHARS2 ('p', 's'): // operator + (unary)
  864. opcode = PLUS_EXPR;
  865. break;
  866. case CHARS2 ('n', 'g'): // operator - (unary)
  867. opcode = MINUS_EXPR;
  868. break;
  869. case CHARS2 ('a', 'd'): // operator & (unary)
  870. opcode = BIT_AND_EXPR;
  871. break;
  872. case CHARS2 ('d', 'e'): // operator * (unary)
  873. opcode = MULT_EXPR;
  874. break;
  875. case CHARS2 ('c', 'o'): // operator ~
  876. opcode = BIT_NOT_EXPR;
  877. break;
  878. case CHARS2 ('p', 'l'): // operator +
  879. opcode = PLUS_EXPR;
  880. break;
  881. case CHARS2 ('m', 'i'): // operator -
  882. opcode = MINUS_EXPR;
  883. break;
  884. case CHARS2 ('m', 'l'): // operator *
  885. opcode = MULT_EXPR;
  886. break;
  887. case CHARS2 ('d', 'v'): // operator /
  888. opcode = TRUNC_DIV_EXPR;
  889. break;
  890. case CHARS2 ('r', 'm'): // operator %
  891. opcode = TRUNC_MOD_EXPR;
  892. break;
  893. case CHARS2 ('a', 'n'): // operator &
  894. opcode = BIT_AND_EXPR;
  895. break;
  896. case CHARS2 ('o', 'r'): // operator |
  897. opcode = BIT_IOR_EXPR;
  898. break;
  899. case CHARS2 ('e', 'o'): // operator ^
  900. opcode = BIT_XOR_EXPR;
  901. break;
  902. case CHARS2 ('a', 'S'): // operator =
  903. opcode = NOP_EXPR;
  904. assop = true;
  905. break;
  906. case CHARS2 ('p', 'L'): // operator +=
  907. opcode = PLUS_EXPR;
  908. assop = true;
  909. break;
  910. case CHARS2 ('m', 'I'): // operator -=
  911. opcode = MINUS_EXPR;
  912. assop = true;
  913. break;
  914. case CHARS2 ('m', 'L'): // operator *=
  915. opcode = MULT_EXPR;
  916. assop = true;
  917. break;
  918. case CHARS2 ('d', 'V'): // operator /=
  919. opcode = TRUNC_DIV_EXPR;
  920. assop = true;
  921. break;
  922. case CHARS2 ('r', 'M'): // operator %=
  923. opcode = TRUNC_MOD_EXPR;
  924. assop = true;
  925. break;
  926. case CHARS2 ('a', 'N'): // operator &=
  927. opcode = BIT_AND_EXPR;
  928. assop = true;
  929. break;
  930. case CHARS2 ('o', 'R'): // operator |=
  931. opcode = BIT_IOR_EXPR;
  932. assop = true;
  933. break;
  934. case CHARS2 ('e', 'O'): // operator ^=
  935. opcode = BIT_XOR_EXPR;
  936. assop = true;
  937. break;
  938. case CHARS2 ('l', 's'): // operator <<
  939. opcode = LSHIFT_EXPR;
  940. break;
  941. case CHARS2 ('r', 's'): // operator >>
  942. opcode = RSHIFT_EXPR;
  943. break;
  944. case CHARS2 ('l', 'S'): // operator <<=
  945. opcode = LSHIFT_EXPR;
  946. assop = true;
  947. break;
  948. case CHARS2 ('r', 'S'): // operator >>=
  949. opcode = RSHIFT_EXPR;
  950. assop = true;
  951. break;
  952. case CHARS2 ('e', 'q'): // operator ==
  953. opcode = EQ_EXPR;
  954. break;
  955. case CHARS2 ('n', 'e'): // operator !=
  956. opcode = NE_EXPR;
  957. break;
  958. case CHARS2 ('l', 't'): // operator <
  959. opcode = LT_EXPR;
  960. break;
  961. case CHARS2 ('g', 't'): // operator >
  962. opcode = GT_EXPR;
  963. break;
  964. case CHARS2 ('l', 'e'): // operator <=
  965. opcode = LE_EXPR;
  966. break;
  967. case CHARS2 ('g', 'e'): // operator >=
  968. opcode = GE_EXPR;
  969. break;
  970. case CHARS2 ('n', 't'): // operator !
  971. opcode = TRUTH_NOT_EXPR;
  972. break;
  973. case CHARS2 ('a', 'a'): // operator &&
  974. opcode = TRUTH_ANDIF_EXPR;
  975. break;
  976. case CHARS2 ('o', 'o'): // operator ||
  977. opcode = TRUTH_ORIF_EXPR;
  978. break;
  979. case CHARS2 ('p', 'p'): // operator ++
  980. opcode = POSTINCREMENT_EXPR;
  981. break;
  982. case CHARS2 ('m', 'm'): // operator --
  983. /* This stands for either one as an operator name, and
  984. "pp" and "mm" stand for POST??CREMENT, but for some
  985. reason the parser uses this opcode name for
  986. operator--; let's follow their practice. */
  987. opcode = PREDECREMENT_EXPR;
  988. break;
  989. case CHARS2 ('c', 'm'): // operator ,
  990. opcode = COMPOUND_EXPR;
  991. break;
  992. case CHARS2 ('p', 'm'): // operator ->*
  993. opcode = MEMBER_REF;
  994. break;
  995. case CHARS2 ('p', 't'): // operator ->
  996. opcode = COMPONENT_REF;
  997. break;
  998. case CHARS2 ('c', 'l'): // operator ()
  999. opcode = CALL_EXPR;
  1000. break;
  1001. case CHARS2 ('i', 'x'): // operator []
  1002. opcode = ARRAY_REF;
  1003. break;
  1004. case CHARS2 ('c', 'v'): // operator <T> (conversion operator)
  1005. identifier = make_conv_op_name (TREE_TYPE (sym_type));
  1006. break;
  1007. // C++11-only:
  1008. case CHARS2 ('l', 'i'): // operator "" <id>
  1009. {
  1010. char *id = (char *)name + 2;
  1011. bool freeid = false;
  1012. if (*id >= '0' && *id <= '9')
  1013. {
  1014. unsigned len = 0;
  1015. do
  1016. {
  1017. len *= 10;
  1018. len += id[0] - '0';
  1019. id++;
  1020. }
  1021. while (*id && *id >= '0' && *id <= '9');
  1022. id = xstrndup (id, len);
  1023. freeid = true;
  1024. }
  1025. identifier = cp_literal_operator_id (id);
  1026. if (freeid)
  1027. free (id);
  1028. }
  1029. break;
  1030. case CHARS2 ('q', 'u'): // ternary operator, not overloadable.
  1031. default:
  1032. gcc_unreachable ();
  1033. }
  1034. if (opcode != ERROR_MARK)
  1035. identifier = ovl_op_identifier (assop, opcode);
  1036. }
  1037. decl = build_lang_decl_loc (loc, code, identifier, sym_type);
  1038. /* FIXME: current_lang_name is lang_name_c while compiling an
  1039. extern "C" function, and we haven't switched to a global
  1040. context at this point, and this breaks function
  1041. overloading. */
  1042. SET_DECL_LANGUAGE (decl, lang_cplusplus);
  1043. if (TREE_CODE (sym_type) == METHOD_TYPE)
  1044. DECL_ARGUMENTS (decl) = build_this_parm (decl, current_class_type,
  1045. cp_type_quals (sym_type));
  1046. for (tree arg = TREE_CODE (sym_type) == METHOD_TYPE
  1047. ? TREE_CHAIN (TYPE_ARG_TYPES (sym_type))
  1048. : TYPE_ARG_TYPES (sym_type);
  1049. arg && arg != void_list_node;
  1050. arg = TREE_CHAIN (arg))
  1051. {
  1052. tree parm = cp_build_parm_decl (decl, NULL_TREE, TREE_VALUE (arg));
  1053. DECL_CHAIN (parm) = DECL_ARGUMENTS (decl);
  1054. DECL_ARGUMENTS (decl) = parm;
  1055. }
  1056. DECL_ARGUMENTS (decl) = nreverse (DECL_ARGUMENTS (decl));
  1057. if (class_member_p)
  1058. {
  1059. if (TREE_CODE (sym_type) == FUNCTION_TYPE)
  1060. DECL_STATIC_FUNCTION_P (decl) = 1;
  1061. if (sym_flags & GCC_CP_FLAG_VIRTUAL_FUNCTION)
  1062. {
  1063. DECL_VIRTUAL_P (decl) = 1;
  1064. if (sym_flags & GCC_CP_FLAG_PURE_VIRTUAL_FUNCTION)
  1065. DECL_PURE_VIRTUAL_P (decl) = 1;
  1066. if (sym_flags & GCC_CP_FLAG_FINAL_VIRTUAL_FUNCTION)
  1067. DECL_FINAL_P (decl) = 1;
  1068. }
  1069. else
  1070. gcc_assert (!(sym_flags & (GCC_CP_FLAG_PURE_VIRTUAL_FUNCTION
  1071. | GCC_CP_FLAG_FINAL_VIRTUAL_FUNCTION)));
  1072. }
  1073. else
  1074. {
  1075. gcc_assert (!(sym_flags & (GCC_CP_FLAG_VIRTUAL_FUNCTION
  1076. | GCC_CP_FLAG_PURE_VIRTUAL_FUNCTION
  1077. | GCC_CP_FLAG_FINAL_VIRTUAL_FUNCTION)));
  1078. gcc_assert (!ctor && !dtor && !assop);
  1079. }
  1080. if (sym_flags & GCC_CP_FLAG_EXPLICIT_FUNCTION)
  1081. DECL_NONCONVERTING_P (decl) = 1;
  1082. if (sym_flags & GCC_CP_FLAG_DEFAULTED_FUNCTION)
  1083. {
  1084. DECL_INITIAL (decl) = ridpointers[(int)RID_DEFAULT];
  1085. DECL_DEFAULTED_FN (decl) = 1;
  1086. }
  1087. if (sym_flags & GCC_CP_FLAG_DELETED_FUNCTION)
  1088. {
  1089. // DECL_INITIAL (decl) = ridpointers[(int)RID_DELETE];
  1090. DECL_DELETED_FN (decl) = 1;
  1091. DECL_DECLARED_INLINE_P (decl) = 1;
  1092. DECL_INITIAL (decl) = error_mark_node;
  1093. }
  1094. if (ctor)
  1095. DECL_CXX_CONSTRUCTOR_P (decl) = 1;
  1096. else if (dtor)
  1097. DECL_CXX_DESTRUCTOR_P (decl) = 1;
  1098. else if ((sym_flags & GCC_CP_FLAG_SPECIAL_FUNCTION)
  1099. && opcode != ERROR_MARK)
  1100. DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op_mapping[opcode];
  1101. }
  1102. else if (RECORD_OR_UNION_CODE_P (code))
  1103. {
  1104. decl = build_named_class_type (code, identifier, loc);
  1105. tree type = TREE_TYPE (decl);
  1106. if (code == RECORD_TYPE
  1107. && !(sym_flags & GCC_CP_FLAG_CLASS_IS_STRUCT))
  1108. CLASSTYPE_DECLARED_CLASS (type) = true;
  1109. }
  1110. else if (class_member_p)
  1111. {
  1112. decl = build_lang_decl_loc (loc, code, identifier, sym_type);
  1113. if (TREE_CODE (decl) == VAR_DECL)
  1114. {
  1115. DECL_THIS_STATIC (decl) = 1;
  1116. // The remainder of this block does the same as:
  1117. // set_linkage_for_static_data_member (decl);
  1118. TREE_PUBLIC (decl) = 1;
  1119. TREE_STATIC (decl) = 1;
  1120. DECL_INTERFACE_KNOWN (decl) = 1;
  1121. // FIXME: sym_flags & GCC_CP_FLAG_THREAD_LOCAL_VARIABLE
  1122. gcc_assert (!(sym_flags & GCC_CP_FLAG_THREAD_LOCAL_VARIABLE));
  1123. if (sym_flags & GCC_CP_FLAG_CONSTEXPR_VARIABLE)
  1124. DECL_DECLARED_CONSTEXPR_P (decl) = true;
  1125. }
  1126. }
  1127. else
  1128. {
  1129. decl = build_decl (loc, code, identifier, sym_type);
  1130. if (TREE_CODE (decl) == VAR_DECL)
  1131. {
  1132. // FIXME: sym_flags & GCC_CP_FLAG_THREAD_LOCAL_VARIABLE
  1133. gcc_assert (!(sym_flags & GCC_CP_FLAG_THREAD_LOCAL_VARIABLE));
  1134. if (sym_flags & GCC_CP_FLAG_CONSTEXPR_VARIABLE)
  1135. DECL_DECLARED_CONSTEXPR_P (decl) = true;
  1136. }
  1137. }
  1138. TREE_USED (decl) = 1;
  1139. TREE_ADDRESSABLE (decl) = 1;
  1140. if (class_member_p)
  1141. DECL_CONTEXT (decl) = FROB_CONTEXT (current_class_type);
  1142. else if (at_namespace_scope_p ())
  1143. DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
  1144. set_access_flags (decl, acc_flags);
  1145. /* If this is the typedef that names an otherwise anonymous type,
  1146. propagate the typedef name to the type. In normal compilation,
  1147. this is done in grokdeclarator. */
  1148. if (sym_kind == GCC_CP_SYMBOL_TYPEDEF
  1149. && !template_decl_p
  1150. && DECL_CONTEXT (decl) == TYPE_CONTEXT (sym_type)
  1151. && TYPE_UNNAMED_P (sym_type))
  1152. name_unnamed_type (sym_type, decl);
  1153. if (sym_kind != GCC_CP_SYMBOL_TYPEDEF
  1154. && sym_kind != GCC_CP_SYMBOL_CLASS
  1155. && sym_kind != GCC_CP_SYMBOL_UNION
  1156. && !template_decl_p && !ctor && !dtor)
  1157. {
  1158. decl_addr_value value;
  1159. DECL_EXTERNAL (decl) = 1;
  1160. value.decl = decl;
  1161. if (substitution_name != NULL)
  1162. {
  1163. // If the translator gave us a name without a binding,
  1164. // we can just substitute error_mark_node, since we know the
  1165. // translator will be reporting an error anyhow.
  1166. value.address
  1167. = lookup_name (get_identifier (substitution_name));
  1168. if (value.address == NULL_TREE)
  1169. value.address = error_mark_node;
  1170. }
  1171. else if (address)
  1172. value.address = build_int_cst_type (ptr_type_node, address);
  1173. else
  1174. value.address = NULL;
  1175. if (value.address)
  1176. record_decl_address (ctx, value);
  1177. }
  1178. if (class_member_p && code == FUNCTION_DECL)
  1179. {
  1180. if (ctor || dtor)
  1181. maybe_retrofit_in_chrg (decl);
  1182. grok_special_member_properties (decl);
  1183. }
  1184. if (template_decl_p)
  1185. {
  1186. if (RECORD_OR_UNION_CODE_P (code))
  1187. safe_pushtag (identifier, TREE_TYPE (decl));
  1188. else
  1189. decl = safe_push_template_decl (decl);
  1190. tree tdecl = NULL_TREE;
  1191. if (class_member_p)
  1192. tdecl = finish_member_template_decl (decl);
  1193. end_template_decl ();
  1194. /* We only support one level of templates, because we only
  1195. support declaring generics; actual definitions are only of
  1196. specializations. */
  1197. gcc_assert (!template_parm_scope_p ());
  1198. if (class_member_p)
  1199. finish_member_declaration (tdecl);
  1200. }
  1201. else if (RECORD_OR_UNION_CODE_P (code))
  1202. safe_pushtag (identifier, TREE_TYPE (decl));
  1203. else if (class_member_p)
  1204. finish_member_declaration (decl);
  1205. else
  1206. decl = safe_pushdecl (decl);
  1207. if ((ctor || dtor)
  1208. /* Don't crash after a duplicate declaration of a cdtor. */
  1209. && TYPE_FIELDS (current_class_type) == decl)
  1210. {
  1211. /* ctors and dtors clones are chained after DECL.
  1212. However, we create the clones before TYPE_METHODS is
  1213. reversed. We test for cloned methods after reversal,
  1214. however, and the test requires the clones to follow
  1215. DECL. So, we reverse the chain of clones now, so
  1216. that it will come out in the right order after
  1217. reversal. */
  1218. tree save = DECL_CHAIN (decl);
  1219. DECL_CHAIN (decl) = NULL_TREE;
  1220. clone_cdtor (decl, /*update_methods=*/true);
  1221. gcc_assert (TYPE_FIELDS (current_class_type) == decl);
  1222. TYPE_FIELDS (current_class_type)
  1223. = nreverse (TYPE_FIELDS (current_class_type));
  1224. DECL_CHAIN (decl) = save;
  1225. }
  1226. rest_of_decl_compilation (decl, toplevel_bindings_p (), 0);
  1227. return convert_out (ctx->preserve (decl));
  1228. }
  1229. gcc_decl
  1230. plugin_define_cdtor_clone (cc1_plugin::connection *self,
  1231. const char *name,
  1232. gcc_decl cdtor_in,
  1233. gcc_address address)
  1234. {
  1235. plugin_context *ctx = static_cast<plugin_context *> (self);
  1236. tree decl = convert_in (cdtor_in);
  1237. bool ctor = false;
  1238. bool dtor = false;
  1239. tree identifier;
  1240. switch (CHARS2 (name[0], name[1]))
  1241. {
  1242. case CHARS2 ('C', '1'): // in-charge constructor
  1243. identifier = complete_ctor_identifier;
  1244. ctor = true;
  1245. break;
  1246. case CHARS2 ('C', '2'): // not-in-charge constructor
  1247. identifier = base_ctor_identifier;
  1248. ctor = true;
  1249. break;
  1250. case CHARS2 ('C', '4'):
  1251. identifier = ctor_identifier; // unified constructor
  1252. ctor = true;
  1253. break;
  1254. case CHARS2 ('D', '0'): // deleting destructor
  1255. identifier = deleting_dtor_identifier;
  1256. dtor = true;
  1257. break;
  1258. case CHARS2 ('D', '1'): // in-charge destructor
  1259. identifier = complete_dtor_identifier;
  1260. dtor = true;
  1261. break;
  1262. case CHARS2 ('D', '2'): // not-in-charge destructor
  1263. identifier = base_dtor_identifier;
  1264. dtor = true;
  1265. break;
  1266. case CHARS2 ('D', '4'):
  1267. identifier = dtor_identifier; // unified destructor
  1268. dtor = true;
  1269. break;
  1270. default:
  1271. gcc_unreachable ();
  1272. }
  1273. gcc_assert (!ctor != !dtor);
  1274. gcc_assert (ctor
  1275. ? (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
  1276. && DECL_NAME (decl) == ctor_identifier)
  1277. : (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl)
  1278. && DECL_NAME (decl) == dtor_identifier));
  1279. while (decl && DECL_NAME (decl) != identifier)
  1280. {
  1281. decl = DECL_CHAIN (decl);
  1282. if (decl && !DECL_CLONED_FUNCTION_P (decl))
  1283. decl = NULL_TREE;
  1284. }
  1285. gcc_assert (decl);
  1286. record_decl_address (ctx, build_decl_addr_value (decl, address));
  1287. return convert_out (decl);
  1288. }
  1289. int
  1290. plugin_add_friend (cc1_plugin::connection * /* self */,
  1291. gcc_decl decl_in,
  1292. gcc_type type_in)
  1293. {
  1294. tree decl = convert_in (decl_in);
  1295. tree type = convert_in (type_in);
  1296. gcc_assert (type || at_class_scope_p ());
  1297. if (!type)
  1298. type = current_class_type;
  1299. else
  1300. gcc_assert (TREE_CODE (type) == RECORD_TYPE);
  1301. if (TYPE_P (decl))
  1302. make_friend_class (type, TREE_TYPE (decl), true);
  1303. else
  1304. {
  1305. DECL_UNIQUE_FRIEND_P (decl) = true;
  1306. add_friend (type, decl, true);
  1307. }
  1308. return 1;
  1309. }
  1310. gcc_type
  1311. plugin_build_pointer_type (cc1_plugin::connection *,
  1312. gcc_type base_type)
  1313. {
  1314. // No need to preserve a pointer type as the base type is preserved.
  1315. return convert_out (build_pointer_type (convert_in (base_type)));
  1316. }
  1317. gcc_type
  1318. plugin_build_reference_type (cc1_plugin::connection *,
  1319. gcc_type base_type_in,
  1320. enum gcc_cp_ref_qualifiers rquals)
  1321. {
  1322. bool rval;
  1323. switch (rquals)
  1324. {
  1325. case GCC_CP_REF_QUAL_LVALUE:
  1326. rval = false;
  1327. break;
  1328. case GCC_CP_REF_QUAL_RVALUE:
  1329. rval = true;
  1330. break;
  1331. case GCC_CP_REF_QUAL_NONE:
  1332. default:
  1333. gcc_unreachable ();
  1334. }
  1335. tree rtype = cp_build_reference_type (convert_in (base_type_in), rval);
  1336. return convert_out (rtype);
  1337. }
  1338. static tree
  1339. start_class_def (tree type,
  1340. const gcc_vbase_array *base_classes)
  1341. {
  1342. tree bases = NULL;
  1343. if (base_classes)
  1344. {
  1345. for (int i = 0; i < base_classes->n_elements; i++)
  1346. {
  1347. tree access;
  1348. gcc_assert ((base_classes->flags[i] & GCC_CP_SYMBOL_MASK)
  1349. == GCC_CP_SYMBOL_BASECLASS);
  1350. switch (base_classes->flags[i] & GCC_CP_ACCESS_MASK)
  1351. {
  1352. case GCC_CP_ACCESS_PRIVATE:
  1353. access = ridpointers[(int)RID_PRIVATE];
  1354. break;
  1355. case GCC_CP_ACCESS_PROTECTED:
  1356. access = ridpointers[(int)RID_PROTECTED];
  1357. break;
  1358. case GCC_CP_ACCESS_PUBLIC:
  1359. access = ridpointers[(int)RID_PUBLIC];
  1360. break;
  1361. default:
  1362. gcc_unreachable ();
  1363. }
  1364. tree base = finish_base_specifier
  1365. (convert_in (base_classes->elements[i]), access,
  1366. (base_classes->flags[i] & GCC_CP_FLAG_BASECLASS_VIRTUAL) != 0);
  1367. TREE_CHAIN (base) = bases;
  1368. bases = base;
  1369. }
  1370. bases = nreverse (bases);
  1371. }
  1372. xref_basetypes (type, bases);
  1373. begin_class_definition (type);
  1374. return type;
  1375. }
  1376. gcc_type
  1377. plugin_start_class_type (cc1_plugin::connection *self,
  1378. gcc_decl typedecl_in,
  1379. const gcc_vbase_array *base_classes,
  1380. const char *filename,
  1381. unsigned int line_number)
  1382. {
  1383. plugin_context *ctx = static_cast<plugin_context *> (self);
  1384. location_t loc = ctx->get_location_t (filename, line_number);
  1385. tree typedecl = convert_in (typedecl_in);
  1386. tree type = TREE_TYPE (typedecl);
  1387. gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (type)));
  1388. gcc_assert (!COMPLETE_TYPE_P (type));
  1389. DECL_SOURCE_LOCATION (typedecl) = loc;
  1390. tree result = start_class_def (type, base_classes);
  1391. return convert_out (ctx->preserve (result));
  1392. }
  1393. gcc_type
  1394. plugin_start_closure_class_type (cc1_plugin::connection *self,
  1395. int discriminator,
  1396. gcc_decl extra_scope_in,
  1397. enum gcc_cp_symbol_kind flags,
  1398. const char *filename,
  1399. unsigned int line_number)
  1400. {
  1401. plugin_context *ctx = static_cast<plugin_context *> (self);
  1402. tree extra_scope = convert_in (extra_scope_in);
  1403. gcc_assert ((flags & GCC_CP_SYMBOL_MASK) == GCC_CP_SYMBOL_LAMBDA_CLOSURE);
  1404. gcc_assert ((flags & (~(GCC_CP_SYMBOL_MASK | GCC_CP_ACCESS_MASK))) == 0);
  1405. gcc_assert (!(flags & GCC_CP_ACCESS_MASK) == !at_class_scope_p ());
  1406. /* See at_fake_function_scope_p. */
  1407. gcc_assert (!at_function_scope_p ());
  1408. if (extra_scope)
  1409. {
  1410. if (TREE_CODE (extra_scope) == PARM_DECL)
  1411. {
  1412. gcc_assert (at_fake_function_scope_p ());
  1413. /* Check that the given extra_scope is one of the parameters of
  1414. the current function. */
  1415. for (tree parm = DECL_ARGUMENTS (current_function_decl);
  1416. ; parm = DECL_CHAIN (parm))
  1417. {
  1418. gcc_assert (parm);
  1419. if (parm == extra_scope)
  1420. break;
  1421. }
  1422. }
  1423. else if (TREE_CODE (extra_scope) == FIELD_DECL)
  1424. {
  1425. gcc_assert (at_class_scope_p ());
  1426. gcc_assert (DECL_CONTEXT (extra_scope) == current_class_type);
  1427. }
  1428. else
  1429. /* FIXME: does this ever really occur? */
  1430. gcc_assert (TREE_CODE (extra_scope) == VAR_DECL);
  1431. }
  1432. tree lambda_expr = build_lambda_expr ();
  1433. LAMBDA_EXPR_LOCATION (lambda_expr) = ctx->get_location_t (filename,
  1434. line_number);
  1435. tree type = begin_lambda_type (lambda_expr);
  1436. /* Instead of calling record_lambda_scope, do this: */
  1437. LAMBDA_EXPR_EXTRA_SCOPE (lambda_expr) = extra_scope;
  1438. LAMBDA_EXPR_DISCRIMINATOR (lambda_expr) = discriminator;
  1439. tree decl = TYPE_NAME (type);
  1440. determine_visibility (decl);
  1441. set_access_flags (decl, flags);
  1442. return convert_out (ctx->preserve (type));
  1443. }
  1444. gcc_expr
  1445. plugin_build_lambda_expr (cc1_plugin::connection *self,
  1446. gcc_type closure_type_in)
  1447. {
  1448. plugin_context *ctx = static_cast<plugin_context *> (self);
  1449. tree closure_type = convert_in (closure_type_in);
  1450. gcc_assert (LAMBDA_TYPE_P (closure_type));
  1451. tree lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure_type);
  1452. tree lambda_object = build_lambda_object (lambda_expr);
  1453. return convert_out (ctx->preserve (lambda_object));
  1454. }
  1455. gcc_decl
  1456. plugin_build_field (cc1_plugin::connection *,
  1457. const char *field_name,
  1458. gcc_type field_type_in,
  1459. enum gcc_cp_symbol_kind flags,
  1460. unsigned long bitsize,
  1461. unsigned long bitpos)
  1462. {
  1463. tree record_or_union_type = current_class_type;
  1464. tree field_type = convert_in (field_type_in);
  1465. gcc_assert (at_class_scope_p ());
  1466. gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (record_or_union_type)));
  1467. gcc_assert ((flags & GCC_CP_SYMBOL_MASK) == GCC_CP_SYMBOL_FIELD);
  1468. gcc_assert ((flags & (~(GCC_CP_SYMBOL_MASK | GCC_CP_ACCESS_MASK
  1469. | GCC_CP_FLAG_MASK_FIELD))) == 0);
  1470. gcc_assert ((flags & GCC_CP_ACCESS_MASK));
  1471. /* Note that gdb does not preserve the location of field decls, so
  1472. we can't provide a decent location here. */
  1473. tree decl = build_decl (BUILTINS_LOCATION, FIELD_DECL,
  1474. get_identifier (field_name), field_type);
  1475. DECL_FIELD_CONTEXT (decl) = record_or_union_type;
  1476. set_access_flags (decl, flags);
  1477. if ((flags & GCC_CP_FLAG_FIELD_MUTABLE) != 0)
  1478. DECL_MUTABLE_P (decl) = 1;
  1479. if (TREE_CODE (field_type) == INTEGER_TYPE
  1480. && TYPE_PRECISION (field_type) != bitsize)
  1481. {
  1482. DECL_BIT_FIELD_TYPE (decl) = field_type;
  1483. TREE_TYPE (decl)
  1484. = c_build_bitfield_integer_type (bitsize, TYPE_UNSIGNED (field_type));
  1485. }
  1486. SET_DECL_MODE (decl, TYPE_MODE (TREE_TYPE (decl)));
  1487. // There's no way to recover this from DWARF.
  1488. SET_DECL_OFFSET_ALIGN (decl, TYPE_PRECISION (pointer_sized_int_node));
  1489. tree pos = bitsize_int (bitpos);
  1490. pos_from_bit (&DECL_FIELD_OFFSET (decl), &DECL_FIELD_BIT_OFFSET (decl),
  1491. DECL_OFFSET_ALIGN (decl), pos);
  1492. DECL_SIZE (decl) = bitsize_int (bitsize);
  1493. DECL_SIZE_UNIT (decl) = size_int ((bitsize + BITS_PER_UNIT - 1)
  1494. / BITS_PER_UNIT);
  1495. DECL_CHAIN (decl) = TYPE_FIELDS (record_or_union_type);
  1496. TYPE_FIELDS (record_or_union_type) = decl;
  1497. return convert_out (decl);
  1498. }
  1499. int
  1500. plugin_finish_class_type (cc1_plugin::connection *,
  1501. unsigned long size_in_bytes)
  1502. {
  1503. tree record_or_union_type = current_class_type;
  1504. gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (record_or_union_type)));
  1505. finish_struct (record_or_union_type, NULL);
  1506. gcc_assert (compare_tree_int (TYPE_SIZE_UNIT (record_or_union_type),
  1507. size_in_bytes) == 0);
  1508. return 1;
  1509. }
  1510. gcc_type
  1511. plugin_start_enum_type (cc1_plugin::connection *self,
  1512. const char *name,
  1513. gcc_type underlying_int_type_in,
  1514. enum gcc_cp_symbol_kind flags,
  1515. const char *filename,
  1516. unsigned int line_number)
  1517. {
  1518. plugin_context *ctx = static_cast<plugin_context *> (self);
  1519. tree underlying_int_type = convert_in (underlying_int_type_in);
  1520. gcc_assert ((flags & GCC_CP_SYMBOL_MASK) == GCC_CP_SYMBOL_ENUM);
  1521. gcc_assert ((flags & (~(GCC_CP_SYMBOL_MASK | GCC_CP_ACCESS_MASK
  1522. | GCC_CP_FLAG_MASK_ENUM))) == 0);
  1523. gcc_assert (!(flags & GCC_CP_ACCESS_MASK) == !at_class_scope_p ());
  1524. if (underlying_int_type == error_mark_node)
  1525. return convert_out (error_mark_node);
  1526. bool is_new_type = false;
  1527. tree id = name ? get_identifier (name) : make_anon_name ();
  1528. tree type = start_enum (id, NULL_TREE,
  1529. underlying_int_type,
  1530. /* attributes = */ NULL_TREE,
  1531. !!(flags & GCC_CP_FLAG_ENUM_SCOPED), &is_new_type);
  1532. gcc_assert (is_new_type);
  1533. location_t loc = ctx->get_location_t (filename, line_number);
  1534. tree type_decl = TYPE_NAME (type);
  1535. DECL_SOURCE_LOCATION (type_decl) = loc;
  1536. SET_OPAQUE_ENUM_P (type, false);
  1537. set_access_flags (type_decl, flags);
  1538. return convert_out (ctx->preserve (type));
  1539. }
  1540. gcc_decl
  1541. plugin_build_enum_constant (cc1_plugin::connection *,
  1542. gcc_type enum_type_in,
  1543. const char *name,
  1544. unsigned long value)
  1545. {
  1546. tree enum_type = convert_in (enum_type_in);
  1547. gcc_assert (TREE_CODE (enum_type) == ENUMERAL_TYPE);
  1548. build_enumerator (get_identifier (name), build_int_cst (enum_type, value),
  1549. enum_type, NULL_TREE, BUILTINS_LOCATION);
  1550. return convert_out (TREE_VALUE (TYPE_VALUES (enum_type)));
  1551. }
  1552. int
  1553. plugin_finish_enum_type (cc1_plugin::connection *,
  1554. gcc_type enum_type_in)
  1555. {
  1556. tree enum_type = convert_in (enum_type_in);
  1557. finish_enum_value_list (enum_type);
  1558. finish_enum (enum_type);
  1559. return 1;
  1560. }
  1561. gcc_type
  1562. plugin_build_function_type (cc1_plugin::connection *self,
  1563. gcc_type return_type_in,
  1564. const struct gcc_type_array *argument_types_in,
  1565. int is_varargs)
  1566. {
  1567. tree return_type = convert_in (return_type_in);
  1568. tree result;
  1569. std::vector<tree> argument_types (argument_types_in->n_elements);
  1570. for (int i = 0; i < argument_types_in->n_elements; ++i)
  1571. argument_types[i] = convert_in (argument_types_in->elements[i]);
  1572. if (is_varargs)
  1573. result = build_varargs_function_type_array (return_type,
  1574. argument_types_in->n_elements,
  1575. argument_types.data ());
  1576. else
  1577. result = build_function_type_array (return_type,
  1578. argument_types_in->n_elements,
  1579. argument_types.data ());
  1580. plugin_context *ctx = static_cast<plugin_context *> (self);
  1581. return convert_out (ctx->preserve (result));
  1582. }
  1583. #if 0
  1584. gcc_type
  1585. plugin_add_function_default_args (cc1_plugin::connection *self,
  1586. gcc_type function_type_in,
  1587. const struct gcc_cp_function_args *defaults)
  1588. {
  1589. tree function_type = convert_in (function_type_in);
  1590. gcc_assert (TREE_CODE (function_type) == FUNCTION_TYPE);
  1591. if (!defaults || !defaults->n_elements)
  1592. return function_type_in;
  1593. tree pargs = TYPE_ARG_TYPES (function_type);
  1594. tree nargs = NULL_TREE;
  1595. /* Build a reversed copy of the list of default-less arguments in
  1596. NARGS. At the end of the loop, PARGS will point to the end of
  1597. the argument list, or to the first argument that had a default
  1598. value. */
  1599. while (pargs && TREE_VALUE (pargs) != void_list_node
  1600. && !TREE_PURPOSE (pargs))
  1601. {
  1602. nargs = tree_cons (NULL_TREE, TREE_VALUE (pargs), nargs);
  1603. pargs = TREE_CHAIN (pargs);
  1604. }
  1605. /* Set the defaults in the now-leading NARGS, taking into account
  1606. that NARGS is reversed but DEFAULTS->elements isn't. */
  1607. tree ndargs = nargs;
  1608. int i = defaults->n_elements;
  1609. while (i--)
  1610. {
  1611. gcc_assert (ndargs);
  1612. tree deflt = convert_in (defaults->elements[i]);
  1613. if (!deflt)
  1614. deflt = error_mark_node;
  1615. TREE_PURPOSE (ndargs) = deflt;
  1616. ndargs = TREE_CHAIN (ndargs);
  1617. }
  1618. /* Finally, reverse NARGS, and append the remaining PARGS that
  1619. already had defaults. */
  1620. nargs = nreverse (nargs);
  1621. nargs = chainon (nargs, pargs);
  1622. tree result = build_function_type (TREE_TYPE (function_type), nargs);
  1623. /* Copy exceptions, attributes and whatnot. */
  1624. result = build_exception_variant (result,
  1625. TYPE_RAISES_EXCEPTIONS (function_type));
  1626. result = cp_build_type_attribute_variant (result,
  1627. TYPE_ATTRIBUTES (function_type));
  1628. plugin_context *ctx = static_cast<plugin_context *> (self);
  1629. return convert_out (ctx->preserve (result));
  1630. }
  1631. int
  1632. plugin_set_deferred_function_default_args (cc1_plugin::connection *,
  1633. gcc_decl function_in,
  1634. const struct gcc_cp_function_args
  1635. *defaults)
  1636. {
  1637. tree function = convert_in (function_in);
  1638. gcc_assert (TREE_CODE (function) == FUNCTION_DECL);
  1639. if (!defaults || !defaults->n_elements)
  1640. return 1;
  1641. tree arg = FUNCTION_FIRST_USER_PARMTYPE (function);
  1642. for (int i = 0; i < defaults->n_elements; i++)
  1643. {
  1644. while (arg && TREE_PURPOSE (arg) != error_mark_node)
  1645. arg = TREE_CHAIN (arg);
  1646. if (!arg)
  1647. return 0;
  1648. TREE_PURPOSE (arg) = convert_in (defaults->elements[i]);
  1649. arg = TREE_CHAIN (arg);
  1650. }
  1651. return 1;
  1652. }
  1653. #endif
  1654. gcc_decl
  1655. plugin_get_function_parameter_decl (cc1_plugin::connection *,
  1656. gcc_decl function_in,
  1657. int index)
  1658. {
  1659. tree function = convert_in (function_in);
  1660. gcc_assert (TREE_CODE (function) == FUNCTION_DECL);
  1661. if (index == -1)
  1662. {
  1663. gcc_assert (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE);
  1664. return convert_out (DECL_ARGUMENTS (function));
  1665. }
  1666. gcc_assert (index >= 0);
  1667. tree args = FUNCTION_FIRST_USER_PARM (function);
  1668. for (int i = 0; args && i < index; i++)
  1669. args = DECL_CHAIN (args);
  1670. return convert_out (args);
  1671. }
  1672. gcc_type
  1673. plugin_build_exception_spec_variant (cc1_plugin::connection *self,
  1674. gcc_type function_type_in,
  1675. const struct gcc_type_array *except_types_in)
  1676. {
  1677. tree function_type = convert_in (function_type_in);
  1678. tree except_types = NULL_TREE;
  1679. if (!except_types_in)
  1680. except_types = noexcept_false_spec;
  1681. else if (!except_types_in->n_elements)
  1682. except_types = empty_except_spec;
  1683. else
  1684. for (int i = 0; i < except_types_in->n_elements; i++)
  1685. except_types = add_exception_specifier (except_types,
  1686. convert_in
  1687. (except_types_in->elements[i]),
  1688. 0);
  1689. function_type = build_exception_variant (function_type,
  1690. except_types);
  1691. plugin_context *ctx = static_cast<plugin_context *> (self);
  1692. return convert_out (ctx->preserve (function_type));
  1693. }
  1694. gcc_type
  1695. plugin_build_method_type (cc1_plugin::connection *self,
  1696. gcc_type class_type_in,
  1697. gcc_type func_type_in,
  1698. enum gcc_cp_qualifiers quals_in,
  1699. enum gcc_cp_ref_qualifiers rquals_in)
  1700. {
  1701. tree class_type = convert_in (class_type_in);
  1702. tree func_type = convert_in (func_type_in);
  1703. cp_cv_quals quals = 0;
  1704. cp_ref_qualifier rquals;
  1705. if ((quals_in & GCC_CP_QUALIFIER_CONST) != 0)
  1706. quals |= TYPE_QUAL_CONST;
  1707. if ((quals_in & GCC_CP_QUALIFIER_VOLATILE) != 0)
  1708. quals |= TYPE_QUAL_VOLATILE;
  1709. gcc_assert ((quals_in & GCC_CP_QUALIFIER_RESTRICT) == 0);
  1710. switch (rquals_in)
  1711. {
  1712. case GCC_CP_REF_QUAL_NONE:
  1713. rquals = REF_QUAL_NONE;
  1714. break;
  1715. case GCC_CP_REF_QUAL_LVALUE:
  1716. rquals = REF_QUAL_LVALUE;
  1717. break;
  1718. case GCC_CP_REF_QUAL_RVALUE:
  1719. rquals = REF_QUAL_RVALUE;
  1720. break;
  1721. default:
  1722. gcc_unreachable ();
  1723. }
  1724. tree method_type = class_type
  1725. ? build_memfn_type (func_type, class_type, quals, rquals)
  1726. : apply_memfn_quals (func_type, quals, rquals);
  1727. plugin_context *ctx = static_cast<plugin_context *> (self);
  1728. return convert_out (ctx->preserve (method_type));
  1729. }
  1730. gcc_type
  1731. plugin_build_pointer_to_member_type (cc1_plugin::connection *self,
  1732. gcc_type class_type_in,
  1733. gcc_type member_type_in)
  1734. {
  1735. tree class_type = convert_in (class_type_in);
  1736. tree member_type = convert_in (member_type_in);
  1737. tree memptr_type = build_ptrmem_type (class_type, member_type);
  1738. plugin_context *ctx = static_cast<plugin_context *> (self);
  1739. return convert_out (ctx->preserve (memptr_type));
  1740. }
  1741. int
  1742. plugin_start_template_decl (cc1_plugin::connection *)
  1743. {
  1744. begin_template_parm_list ();
  1745. TP_PARM_LIST = NULL_TREE;
  1746. return 1;
  1747. }
  1748. gcc_decl
  1749. plugin_get_type_decl (cc1_plugin::connection *,
  1750. gcc_type type_in)
  1751. {
  1752. tree type = convert_in (type_in);
  1753. tree name = TYPE_NAME (type);
  1754. gcc_assert (name);
  1755. return convert_out (name);
  1756. }
  1757. gcc_type
  1758. plugin_get_decl_type (cc1_plugin::connection *,
  1759. gcc_decl decl_in)
  1760. {
  1761. tree decl = convert_in (decl_in);
  1762. tree type = TREE_TYPE (decl);
  1763. gcc_assert (type);
  1764. return convert_out (type);
  1765. }
  1766. gcc_type
  1767. plugin_build_type_template_parameter (cc1_plugin::connection *self,
  1768. const char *id,
  1769. int /* bool */ pack_p,
  1770. gcc_type default_type,
  1771. const char *filename,
  1772. unsigned int line_number)
  1773. {
  1774. plugin_context *ctx = static_cast<plugin_context *> (self);
  1775. location_t loc = ctx->get_location_t (filename, line_number);
  1776. gcc_assert (template_parm_scope_p ());
  1777. tree parm = finish_template_type_parm (class_type_node, get_identifier (id));
  1778. parm = build_tree_list (convert_in (default_type), parm);
  1779. gcc_assert (!(pack_p && default_type));
  1780. /* Create a type and a decl for the type parm, and add the decl to
  1781. TP_PARM_LIST. */
  1782. TP_PARM_LIST = process_template_parm (TP_PARM_LIST, loc, parm,
  1783. /* is_non_type = */ false, pack_p);
  1784. /* Locate the decl of the newly-added, processed template parm. */
  1785. parm = TREE_VALUE (tree_last (TP_PARM_LIST));
  1786. /* Return its type. */
  1787. return convert_out (ctx->preserve (TREE_TYPE (parm)));
  1788. }
  1789. gcc_utempl
  1790. plugin_build_template_template_parameter (cc1_plugin::connection *self,
  1791. const char *id,
  1792. int /* bool */ pack_p,
  1793. gcc_utempl default_templ,
  1794. const char *filename,
  1795. unsigned int line_number)
  1796. {
  1797. plugin_context *ctx = static_cast<plugin_context *> (self);
  1798. location_t loc = ctx->get_location_t (filename, line_number);
  1799. gcc_assert (template_parm_scope_p ());
  1800. /* Finish the template parm list that started this template parm. */
  1801. end_template_parm_list (TP_PARM_LIST);
  1802. gcc_assert (template_parm_scope_p ());
  1803. tree parm = finish_template_template_parm (class_type_node,
  1804. get_identifier (id));
  1805. parm = build_tree_list (convert_in (default_templ), parm);
  1806. gcc_assert (!(pack_p && default_templ));
  1807. /* Create a type and a decl for the template parm, and add the decl
  1808. to TP_PARM_LIST. */
  1809. TP_PARM_LIST = process_template_parm (TP_PARM_LIST, loc, parm,
  1810. /* is_non_type = */ false, pack_p);
  1811. /* Locate the decl of the newly-added, processed template parm. */
  1812. parm = TREE_VALUE (tree_last (TP_PARM_LIST));
  1813. return convert_out (ctx->preserve (parm));
  1814. }
  1815. gcc_decl
  1816. plugin_build_value_template_parameter (cc1_plugin::connection *self,
  1817. gcc_type type,
  1818. const char *id,
  1819. gcc_expr default_value,
  1820. const char *filename,
  1821. unsigned int line_number)
  1822. {
  1823. plugin_context *ctx = static_cast<plugin_context *> (self);
  1824. location_t loc = ctx->get_location_t (filename, line_number);
  1825. gcc_assert (template_parm_scope_p ());
  1826. cp_declarator declarator;
  1827. memset (&declarator, 0, sizeof (declarator));
  1828. // &declarator = make_id_declarator (NULL, get_identifier (id), sfk_none):
  1829. declarator.kind = cdk_id;
  1830. declarator.u.id.qualifying_scope = NULL;
  1831. declarator.u.id.unqualified_name = get_identifier (id);
  1832. declarator.u.id.sfk = sfk_none;
  1833. cp_decl_specifier_seq declspec;
  1834. memset (&declspec, 0, sizeof (declspec));
  1835. // cp_parser_set_decl_spec_type (&declspec, convert_in (type), -token-, false):
  1836. declspec.any_specifiers_p = declspec.any_type_specifiers_p = true;
  1837. declspec.type = convert_in (type);
  1838. declspec.locations[ds_type_spec] = loc;
  1839. tree parm = grokdeclarator (&declarator, &declspec, TPARM, 0, 0);
  1840. parm = build_tree_list (convert_in (default_value), parm);
  1841. /* Create a type and a decl for the template parm, and add the decl
  1842. to TP_PARM_LIST. */
  1843. TP_PARM_LIST = process_template_parm (TP_PARM_LIST, loc, parm,
  1844. /* is_non_type = */ true, false);
  1845. /* Locate the decl of the newly-added, processed template parm. */
  1846. parm = TREE_VALUE (tree_last (TP_PARM_LIST));
  1847. return convert_out (ctx->preserve (parm));
  1848. }
  1849. static tree
  1850. targlist (const gcc_cp_template_args *targs)
  1851. {
  1852. int n = targs->n_elements;
  1853. tree vec = make_tree_vec (n);
  1854. while (n--)
  1855. {
  1856. switch (targs->kinds[n])
  1857. {
  1858. case GCC_CP_TPARG_VALUE:
  1859. TREE_VEC_ELT (vec, n) = convert_in (targs->elements[n].value);
  1860. break;
  1861. case GCC_CP_TPARG_CLASS:
  1862. TREE_VEC_ELT (vec, n) = convert_in (targs->elements[n].type);
  1863. break;
  1864. case GCC_CP_TPARG_TEMPL:
  1865. TREE_VEC_ELT (vec, n) = convert_in (targs->elements[n].templ);
  1866. break;
  1867. case GCC_CP_TPARG_PACK:
  1868. TREE_VEC_ELT (vec, n) = convert_in (targs->elements[n].pack);
  1869. break;
  1870. default:
  1871. gcc_unreachable ();
  1872. }
  1873. }
  1874. return vec;
  1875. }
  1876. gcc_type
  1877. plugin_build_dependent_typename (cc1_plugin::connection *self,
  1878. gcc_type enclosing_type,
  1879. const char *id,
  1880. const gcc_cp_template_args *targs)
  1881. {
  1882. plugin_context *ctx = static_cast<plugin_context *> (self);
  1883. tree type = convert_in (enclosing_type);
  1884. tree name = get_identifier (id);
  1885. if (targs)
  1886. name = build_min_nt_loc (/*loc=*/0, TEMPLATE_ID_EXPR,
  1887. name, targlist (targs));
  1888. tree res = make_typename_type (type, name, typename_type,
  1889. /*complain=*/tf_error);
  1890. return convert_out (ctx->preserve (res));
  1891. }
  1892. gcc_utempl
  1893. plugin_build_dependent_class_template (cc1_plugin::connection *self,
  1894. gcc_type enclosing_type,
  1895. const char *id)
  1896. {
  1897. plugin_context *ctx = static_cast<plugin_context *> (self);
  1898. tree type = convert_in (enclosing_type);
  1899. tree name = get_identifier (id);
  1900. tree res = make_unbound_class_template (type, name, NULL_TREE,
  1901. /*complain=*/tf_error);
  1902. return convert_out (ctx->preserve (res));
  1903. }
  1904. gcc_type
  1905. plugin_build_dependent_type_template_id (cc1_plugin::connection *self,
  1906. gcc_utempl template_decl,
  1907. const gcc_cp_template_args *targs)
  1908. {
  1909. plugin_context *ctx = static_cast<plugin_context *> (self);
  1910. tree type = convert_in (template_decl);
  1911. tree decl = finish_template_type (type, targlist (targs),
  1912. /*entering_scope=*/false);
  1913. return convert_out (ctx->preserve (TREE_TYPE (decl)));
  1914. }
  1915. gcc_expr
  1916. plugin_build_dependent_expr (cc1_plugin::connection *self,
  1917. gcc_decl enclosing_scope,
  1918. enum gcc_cp_symbol_kind flags,
  1919. const char *name,
  1920. gcc_type conv_type_in,
  1921. const gcc_cp_template_args *targs)
  1922. {
  1923. plugin_context *ctx = static_cast<plugin_context *> (self);
  1924. tree scope = convert_in (enclosing_scope);
  1925. tree conv_type = convert_in (conv_type_in);
  1926. tree identifier;
  1927. if (TREE_CODE (scope) != NAMESPACE_DECL)
  1928. {
  1929. tree type = TREE_TYPE (scope);
  1930. gcc_assert (TYPE_NAME (type) == scope);
  1931. scope = type;
  1932. }
  1933. if (flags == (GCC_CP_SYMBOL_FUNCTION | GCC_CP_FLAG_SPECIAL_FUNCTION))
  1934. {
  1935. bool assop = false, convop = false;
  1936. tree_code opcode = ERROR_MARK;
  1937. switch (CHARS2 (name[0], name[1]))
  1938. {
  1939. case CHARS2 ('C', 0x0): // ctor base declaration
  1940. case CHARS2 ('C', ' '):
  1941. case CHARS2 ('C', '1'):
  1942. case CHARS2 ('C', '2'):
  1943. case CHARS2 ('C', '4'):
  1944. identifier = ctor_identifier;
  1945. break;
  1946. case CHARS2 ('D', 0x0): // dtor base declaration
  1947. case CHARS2 ('D', ' '):
  1948. case CHARS2 ('D', '0'):
  1949. case CHARS2 ('D', '1'):
  1950. case CHARS2 ('D', '2'):
  1951. case CHARS2 ('D', '4'):
  1952. gcc_assert (!targs);
  1953. identifier = dtor_identifier;
  1954. break;
  1955. case CHARS2 ('n', 'w'): // operator new
  1956. opcode = NEW_EXPR;
  1957. break;
  1958. case CHARS2 ('n', 'a'): // operator new[]
  1959. opcode = VEC_NEW_EXPR;
  1960. break;
  1961. case CHARS2 ('d', 'l'): // operator delete
  1962. opcode = DELETE_EXPR;
  1963. break;
  1964. case CHARS2 ('d', 'a'): // operator delete[]
  1965. opcode = VEC_DELETE_EXPR;
  1966. break;
  1967. case CHARS2 ('p', 's'): // operator + (unary)
  1968. opcode = PLUS_EXPR;
  1969. break;
  1970. case CHARS2 ('n', 'g'): // operator - (unary)
  1971. opcode = MINUS_EXPR;
  1972. break;
  1973. case CHARS2 ('a', 'd'): // operator & (unary)
  1974. opcode = BIT_AND_EXPR;
  1975. break;
  1976. case CHARS2 ('d', 'e'): // operator * (unary)
  1977. opcode = MULT_EXPR;
  1978. break;
  1979. case CHARS2 ('c', 'o'): // operator ~
  1980. opcode = BIT_NOT_EXPR;
  1981. break;
  1982. case CHARS2 ('p', 'l'): // operator +
  1983. opcode = PLUS_EXPR;
  1984. break;
  1985. case CHARS2 ('m', 'i'): // operator -
  1986. opcode = MINUS_EXPR;
  1987. break;
  1988. case CHARS2 ('m', 'l'): // operator *
  1989. opcode = MULT_EXPR;
  1990. break;
  1991. case CHARS2 ('d', 'v'): // operator /
  1992. opcode = TRUNC_DIV_EXPR;
  1993. break;
  1994. case CHARS2 ('r', 'm'): // operator %
  1995. opcode = TRUNC_MOD_EXPR;
  1996. break;
  1997. case CHARS2 ('a', 'n'): // operator &
  1998. opcode = BIT_AND_EXPR;
  1999. break;
  2000. case CHARS2 ('o', 'r'): // operator |
  2001. opcode = BIT_IOR_EXPR;
  2002. break;
  2003. case CHARS2 ('e', 'o'): // operator ^
  2004. opcode = BIT_XOR_EXPR;
  2005. break;
  2006. case CHARS2 ('a', 'S'): // operator =
  2007. opcode = NOP_EXPR;
  2008. assop = true;
  2009. break;
  2010. case CHARS2 ('p', 'L'): // operator +=
  2011. opcode = PLUS_EXPR;
  2012. assop = true;
  2013. break;
  2014. case CHARS2 ('m', 'I'): // operator -=
  2015. opcode = MINUS_EXPR;
  2016. assop = true;
  2017. break;
  2018. case CHARS2 ('m', 'L'): // operator *=
  2019. opcode = MULT_EXPR;
  2020. assop = true;
  2021. break;
  2022. case CHARS2 ('d', 'V'): // operator /=
  2023. opcode = TRUNC_DIV_EXPR;
  2024. assop = true;
  2025. break;
  2026. case CHARS2 ('r', 'M'): // operator %=
  2027. opcode = TRUNC_MOD_EXPR;
  2028. assop = true;
  2029. break;
  2030. case CHARS2 ('a', 'N'): // operator &=
  2031. opcode = BIT_AND_EXPR;
  2032. assop = true;
  2033. break;
  2034. case CHARS2 ('o', 'R'): // operator |=
  2035. opcode = BIT_IOR_EXPR;
  2036. assop = true;
  2037. break;
  2038. case CHARS2 ('e', 'O'): // operator ^=
  2039. opcode = BIT_XOR_EXPR;
  2040. assop = true;
  2041. break;
  2042. case CHARS2 ('l', 's'): // operator <<
  2043. opcode = LSHIFT_EXPR;
  2044. break;
  2045. case CHARS2 ('r', 's'): // operator >>
  2046. opcode = RSHIFT_EXPR;
  2047. break;
  2048. case CHARS2 ('l', 'S'): // operator <<=
  2049. opcode = LSHIFT_EXPR;
  2050. assop = true;
  2051. break;
  2052. case CHARS2 ('r', 'S'): // operator >>=
  2053. opcode = RSHIFT_EXPR;
  2054. assop = true;
  2055. break;
  2056. case CHARS2 ('e', 'q'): // operator ==
  2057. opcode = EQ_EXPR;
  2058. break;
  2059. case CHARS2 ('n', 'e'): // operator !=
  2060. opcode = NE_EXPR;
  2061. break;
  2062. case CHARS2 ('l', 't'): // operator <
  2063. opcode = LT_EXPR;
  2064. break;
  2065. case CHARS2 ('g', 't'): // operator >
  2066. opcode = GT_EXPR;
  2067. break;
  2068. case CHARS2 ('l', 'e'): // operator <=
  2069. opcode = LE_EXPR;
  2070. break;
  2071. case CHARS2 ('g', 'e'): // operator >=
  2072. opcode = GE_EXPR;
  2073. break;
  2074. case CHARS2 ('n', 't'): // operator !
  2075. opcode = TRUTH_NOT_EXPR;
  2076. break;
  2077. case CHARS2 ('a', 'a'): // operator &&
  2078. opcode = TRUTH_ANDIF_EXPR;
  2079. break;
  2080. case CHARS2 ('o', 'o'): // operator ||
  2081. opcode = TRUTH_ORIF_EXPR;
  2082. break;
  2083. case CHARS2 ('p', 'p'): // operator ++
  2084. opcode = POSTINCREMENT_EXPR;
  2085. break;
  2086. case CHARS2 ('m', 'm'): // operator --
  2087. opcode = PREDECREMENT_EXPR;
  2088. break;
  2089. case CHARS2 ('c', 'm'): // operator ,
  2090. opcode = COMPOUND_EXPR;
  2091. break;
  2092. case CHARS2 ('p', 'm'): // operator ->*
  2093. opcode = MEMBER_REF;
  2094. break;
  2095. case CHARS2 ('p', 't'): // operator ->
  2096. opcode = COMPONENT_REF;
  2097. break;
  2098. case CHARS2 ('c', 'l'): // operator ()
  2099. opcode = CALL_EXPR;
  2100. break;
  2101. case CHARS2 ('i', 'x'): // operator []
  2102. opcode = ARRAY_REF;
  2103. break;
  2104. case CHARS2 ('c', 'v'): // operator <T> (conversion operator)
  2105. convop = true;
  2106. identifier = make_conv_op_name (conv_type);
  2107. break;
  2108. // C++11-only:
  2109. case CHARS2 ('l', 'i'): // operator "" <id>
  2110. {
  2111. char *id = (char *)name + 2;
  2112. bool freeid = false;
  2113. if (*id >= '0' && *id <= '9')
  2114. {
  2115. unsigned len = 0;
  2116. do
  2117. {
  2118. len *= 10;
  2119. len += id[0] - '0';
  2120. id++;
  2121. }
  2122. while (*id && *id >= '0' && *id <= '9');
  2123. id = xstrndup (id, len);
  2124. freeid = true;
  2125. }
  2126. identifier = cp_literal_operator_id (id);
  2127. if (freeid)
  2128. free (id);
  2129. }
  2130. break;
  2131. case CHARS2 ('q', 'u'): // ternary operator, not overloadable.
  2132. default:
  2133. gcc_unreachable ();
  2134. }
  2135. gcc_assert (convop || !conv_type);
  2136. if (opcode != ERROR_MARK)
  2137. identifier = ovl_op_identifier (assop, opcode);
  2138. gcc_assert (identifier);
  2139. }
  2140. else
  2141. {
  2142. gcc_assert (flags == GCC_CP_SYMBOL_MASK);
  2143. gcc_assert (!conv_type);
  2144. identifier = get_identifier (name);
  2145. }
  2146. tree res = identifier;
  2147. if (!scope)
  2148. res = lookup_name (res, LOOK_where::BLOCK_NAMESPACE);
  2149. else if (!TYPE_P (scope) || !dependent_scope_p (scope))
  2150. {
  2151. res = lookup_qualified_name (scope, res, LOOK_want::NORMAL, true);
  2152. /* We've already resolved the name in the scope, so skip the
  2153. build_qualified_name call below. */
  2154. scope = NULL;
  2155. }
  2156. if (targs)
  2157. res = lookup_template_function (res, targlist (targs));
  2158. if (scope)
  2159. res = build_qualified_name (NULL_TREE, scope, res, !!targs);
  2160. return convert_out (ctx->preserve (res));
  2161. }
  2162. gcc_expr
  2163. plugin_build_literal_expr (cc1_plugin::connection *self,
  2164. gcc_type type, unsigned long value)
  2165. {
  2166. plugin_context *ctx = static_cast<plugin_context *> (self);
  2167. tree t = convert_in (type);
  2168. tree val = build_int_cst_type (t, (unsigned HOST_WIDE_INT) value);
  2169. return convert_out (ctx->preserve (val));
  2170. }
  2171. gcc_expr
  2172. plugin_build_decl_expr (cc1_plugin::connection *self,
  2173. gcc_decl decl_in,
  2174. int qualified_p)
  2175. {
  2176. plugin_context *ctx = static_cast<plugin_context *> (self);
  2177. tree decl = convert_in (decl_in);
  2178. gcc_assert (DECL_P (decl));
  2179. tree result = decl;
  2180. if (qualified_p)
  2181. {
  2182. gcc_assert (DECL_CLASS_SCOPE_P (decl));
  2183. result = build_offset_ref (DECL_CONTEXT (decl), decl,
  2184. /*address_p=*/true, tf_error);
  2185. }
  2186. return convert_out (ctx->preserve (result));
  2187. }
  2188. gcc_expr
  2189. plugin_build_unary_expr (cc1_plugin::connection *self,
  2190. const char *unary_op,
  2191. gcc_expr operand)
  2192. {
  2193. plugin_context *ctx = static_cast<plugin_context *> (self);
  2194. tree op0 = convert_in (operand);
  2195. tree_code opcode = ERROR_MARK;
  2196. bool global_scope_p = false;
  2197. once_more:
  2198. switch (CHARS2 (unary_op[0], unary_op[1]))
  2199. {
  2200. case CHARS2 ('p', 's'): // operator + (unary)
  2201. opcode = UNARY_PLUS_EXPR;
  2202. break;
  2203. case CHARS2 ('n', 'g'): // operator - (unary)
  2204. opcode = NEGATE_EXPR;
  2205. break;
  2206. case CHARS2 ('a', 'd'): // operator & (unary)
  2207. opcode = ADDR_EXPR;
  2208. break;
  2209. case CHARS2 ('d', 'e'): // operator * (unary)
  2210. opcode = INDIRECT_REF;
  2211. break;
  2212. case CHARS2 ('c', 'o'): // operator ~
  2213. opcode = BIT_NOT_EXPR;
  2214. break;
  2215. case CHARS2 ('n', 't'): // operator !
  2216. opcode = TRUTH_NOT_EXPR;
  2217. break;
  2218. case CHARS2 ('p', 'p'): // operator ++
  2219. opcode = unary_op[2] == '_' ? PREINCREMENT_EXPR : POSTINCREMENT_EXPR;
  2220. break;
  2221. case CHARS2 ('m', 'm'): // operator --
  2222. opcode = unary_op[2] == '_' ? PREDECREMENT_EXPR : POSTDECREMENT_EXPR;
  2223. break;
  2224. case CHARS2 ('n', 'x'): // noexcept
  2225. opcode = NOEXCEPT_EXPR;
  2226. break;
  2227. case CHARS2 ('t', 'w'): // throw
  2228. gcc_assert (op0);
  2229. opcode = THROW_EXPR;
  2230. break;
  2231. case CHARS2 ('t', 'r'): // rethrow
  2232. gcc_assert (!op0);
  2233. opcode = THROW_EXPR;
  2234. break;
  2235. case CHARS2 ('t', 'e'): // typeid (value)
  2236. opcode = TYPEID_EXPR;
  2237. break;
  2238. case CHARS2 ('s', 'z'): // sizeof (value)
  2239. opcode = SIZEOF_EXPR;
  2240. break;
  2241. case CHARS2 ('a', 'z'): // alignof (value)
  2242. opcode = ALIGNOF_EXPR;
  2243. break;
  2244. case CHARS2 ('g', 's'): // global scope (for delete, delete[])
  2245. gcc_assert (!global_scope_p);
  2246. global_scope_p = true;
  2247. unary_op += 2;
  2248. goto once_more;
  2249. case CHARS2 ('d', 'l'): // delete
  2250. opcode = DELETE_EXPR;
  2251. break;
  2252. case CHARS2 ('d', 'a'): // delete[]
  2253. opcode = VEC_DELETE_EXPR;
  2254. break;
  2255. case CHARS2 ('s', 'p'): // pack...
  2256. opcode = EXPR_PACK_EXPANSION;
  2257. break;
  2258. case CHARS2 ('s', 'Z'): // sizeof...(pack)
  2259. opcode = TYPE_PACK_EXPANSION; // Not really, but let's use its code.
  2260. break;
  2261. /* FIXME: __real__, __imag__? */
  2262. default:
  2263. gcc_unreachable ();
  2264. }
  2265. gcc_assert (!global_scope_p
  2266. || opcode == DELETE_EXPR || opcode == VEC_DELETE_EXPR);
  2267. processing_template_decl++;
  2268. bool template_dependent_p = op0
  2269. && (type_dependent_expression_p (op0)
  2270. || value_dependent_expression_p (op0));
  2271. if (!template_dependent_p)
  2272. processing_template_decl--;
  2273. tree result;
  2274. gcc_assert (op0 || opcode == THROW_EXPR);
  2275. switch (opcode)
  2276. {
  2277. case NOEXCEPT_EXPR:
  2278. result = finish_noexcept_expr (op0, tf_error);
  2279. break;
  2280. case THROW_EXPR:
  2281. result = build_throw (input_location, op0);
  2282. break;
  2283. case TYPEID_EXPR:
  2284. result = build_typeid (op0, tf_error);
  2285. break;
  2286. case SIZEOF_EXPR:
  2287. case ALIGNOF_EXPR:
  2288. result = cxx_sizeof_or_alignof_expr (input_location,
  2289. op0, opcode, true, true);
  2290. break;
  2291. case DELETE_EXPR:
  2292. case VEC_DELETE_EXPR:
  2293. result = delete_sanity (input_location, op0, NULL_TREE,
  2294. opcode == VEC_DELETE_EXPR,
  2295. global_scope_p, tf_error);
  2296. break;
  2297. case EXPR_PACK_EXPANSION:
  2298. result = make_pack_expansion (op0);
  2299. break;
  2300. // We're using this for sizeof...(pack). */
  2301. case TYPE_PACK_EXPANSION:
  2302. result = make_pack_expansion (op0);
  2303. PACK_EXPANSION_SIZEOF_P (result) = true;
  2304. break;
  2305. default:
  2306. result = build_x_unary_op (/*loc=*/0, opcode, op0, NULL_TREE, tf_error);
  2307. break;
  2308. }
  2309. if (template_dependent_p)
  2310. processing_template_decl--;
  2311. return convert_out (ctx->preserve (result));
  2312. }
  2313. gcc_expr
  2314. plugin_build_binary_expr (cc1_plugin::connection *self,
  2315. const char *binary_op,
  2316. gcc_expr operand1,
  2317. gcc_expr operand2)
  2318. {
  2319. plugin_context *ctx = static_cast<plugin_context *> (self);
  2320. tree op0 = convert_in (operand1);
  2321. tree op1 = convert_in (operand2);
  2322. tree_code opcode = ERROR_MARK;
  2323. switch (CHARS2 (binary_op[0], binary_op[1]))
  2324. {
  2325. case CHARS2 ('p', 'l'): // operator +
  2326. opcode = PLUS_EXPR;
  2327. break;
  2328. case CHARS2 ('m', 'i'): // operator -
  2329. opcode = MINUS_EXPR;
  2330. break;
  2331. case CHARS2 ('m', 'l'): // operator *
  2332. opcode = MULT_EXPR;
  2333. break;
  2334. case CHARS2 ('d', 'v'): // operator /
  2335. opcode = TRUNC_DIV_EXPR;
  2336. break;
  2337. case CHARS2 ('r', 'm'): // operator %
  2338. opcode = TRUNC_MOD_EXPR;
  2339. break;
  2340. case CHARS2 ('a', 'n'): // operator &
  2341. opcode = BIT_AND_EXPR;
  2342. break;
  2343. case CHARS2 ('o', 'r'): // operator |
  2344. opcode = BIT_IOR_EXPR;
  2345. break;
  2346. case CHARS2 ('e', 'o'): // operator ^
  2347. opcode = BIT_XOR_EXPR;
  2348. break;
  2349. case CHARS2 ('l', 's'): // operator <<
  2350. opcode = LSHIFT_EXPR;
  2351. break;
  2352. case CHARS2 ('r', 's'): // operator >>
  2353. opcode = RSHIFT_EXPR;
  2354. break;
  2355. case CHARS2 ('e', 'q'): // operator ==
  2356. opcode = EQ_EXPR;
  2357. break;
  2358. case CHARS2 ('n', 'e'): // operator !=
  2359. opcode = NE_EXPR;
  2360. break;
  2361. case CHARS2 ('l', 't'): // operator <
  2362. opcode = LT_EXPR;
  2363. break;
  2364. case CHARS2 ('g', 't'): // operator >
  2365. opcode = GT_EXPR;
  2366. break;
  2367. case CHARS2 ('l', 'e'): // operator <=
  2368. opcode = LE_EXPR;
  2369. break;
  2370. case CHARS2 ('g', 'e'): // operator >=
  2371. opcode = GE_EXPR;
  2372. break;
  2373. case CHARS2 ('a', 'a'): // operator &&
  2374. opcode = TRUTH_ANDIF_EXPR;
  2375. break;
  2376. case CHARS2 ('o', 'o'): // operator ||
  2377. opcode = TRUTH_ORIF_EXPR;
  2378. break;
  2379. case CHARS2 ('c', 'm'): // operator ,
  2380. opcode = COMPOUND_EXPR;
  2381. break;
  2382. case CHARS2 ('p', 'm'): // operator ->*
  2383. opcode = MEMBER_REF;
  2384. break;
  2385. case CHARS2 ('p', 't'): // operator ->
  2386. opcode = INDIRECT_REF; // Not really! This will stand for
  2387. // INDIRECT_REF followed by COMPONENT_REF
  2388. // later on.
  2389. break;
  2390. case CHARS2 ('i', 'x'): // operator []
  2391. opcode = ARRAY_REF;
  2392. break;
  2393. case CHARS2 ('d', 's'): // operator .*
  2394. opcode = DOTSTAR_EXPR;
  2395. break;
  2396. case CHARS2 ('d', 't'): // operator .
  2397. opcode = COMPONENT_REF;
  2398. break;
  2399. default:
  2400. gcc_unreachable ();
  2401. }
  2402. processing_template_decl++;
  2403. bool template_dependent_p = type_dependent_expression_p (op0)
  2404. || value_dependent_expression_p (op0)
  2405. || type_dependent_expression_p (op1)
  2406. || value_dependent_expression_p (op1);
  2407. if (!template_dependent_p)
  2408. processing_template_decl--;
  2409. tree result;
  2410. switch (opcode)
  2411. {
  2412. case INDIRECT_REF: // This is actually a "->".
  2413. op0 = build_x_arrow (/*loc=*/0, op0, tf_error);
  2414. /* Fall through. */
  2415. case COMPONENT_REF:
  2416. result = finish_class_member_access_expr (op0, op1,
  2417. /*template_p=*/false,
  2418. tf_error);
  2419. break;
  2420. default:
  2421. result = build_x_binary_op (/*loc=*/0, opcode, op0, ERROR_MARK,
  2422. op1, ERROR_MARK, NULL_TREE, NULL, tf_error);
  2423. break;
  2424. }
  2425. if (template_dependent_p)
  2426. processing_template_decl--;
  2427. return convert_out (ctx->preserve (result));
  2428. }
  2429. gcc_expr
  2430. plugin_build_ternary_expr (cc1_plugin::connection *self,
  2431. const char *ternary_op,
  2432. gcc_expr operand1,
  2433. gcc_expr operand2,
  2434. gcc_expr operand3)
  2435. {
  2436. plugin_context *ctx = static_cast<plugin_context *> (self);
  2437. tree op0 = convert_in (operand1);
  2438. tree op1 = convert_in (operand2);
  2439. tree op2 = convert_in (operand3);
  2440. gcc_assert (CHARS2 (ternary_op[0], ternary_op[1])
  2441. == CHARS2 ('q', 'u')); // ternary operator
  2442. processing_template_decl++;
  2443. bool template_dependent_p = type_dependent_expression_p (op0)
  2444. || value_dependent_expression_p (op0)
  2445. || type_dependent_expression_p (op1)
  2446. || value_dependent_expression_p (op1)
  2447. || type_dependent_expression_p (op2)
  2448. || value_dependent_expression_p (op2);
  2449. if (!template_dependent_p)
  2450. processing_template_decl--;
  2451. tree val = build_x_conditional_expr (/*loc=*/0, op0, op1, op2, tf_error);
  2452. if (template_dependent_p)
  2453. processing_template_decl--;
  2454. return convert_out (ctx->preserve (val));
  2455. }
  2456. gcc_expr
  2457. plugin_build_unary_type_expr (cc1_plugin::connection *self,
  2458. const char *unary_op,
  2459. gcc_type operand)
  2460. {
  2461. plugin_context *ctx = static_cast<plugin_context *> (self);
  2462. tree type = convert_in (operand);
  2463. tree_code opcode = ERROR_MARK;
  2464. switch (CHARS2 (unary_op[0], unary_op[1]))
  2465. {
  2466. case CHARS2 ('t', 'i'): // typeid (type)
  2467. opcode = TYPEID_EXPR;
  2468. break;
  2469. case CHARS2 ('s', 't'): // sizeof (type)
  2470. opcode = SIZEOF_EXPR;
  2471. break;
  2472. case CHARS2 ('a', 't'): // alignof (type)
  2473. opcode = ALIGNOF_EXPR;
  2474. break;
  2475. case CHARS2 ('s', 'Z'): // sizeof...(pack)
  2476. opcode = TYPE_PACK_EXPANSION; // Not really, but let's use its code.
  2477. break;
  2478. // FIXME: do we have to handle "sp", for the size of a captured
  2479. // template parameter pack from an alias template, taking
  2480. // multiple template arguments?
  2481. default:
  2482. gcc_unreachable ();
  2483. }
  2484. processing_template_decl++;
  2485. bool template_dependent_p = dependent_type_p (type);
  2486. if (!template_dependent_p)
  2487. processing_template_decl--;
  2488. tree result;
  2489. switch (opcode)
  2490. {
  2491. case TYPEID_EXPR:
  2492. result = get_typeid (type, tf_error);
  2493. break;
  2494. // We're using this for sizeof...(pack). */
  2495. case TYPE_PACK_EXPANSION:
  2496. result = make_pack_expansion (type);
  2497. PACK_EXPANSION_SIZEOF_P (result) = true;
  2498. break;
  2499. default:
  2500. /* Use the C++11 alignof semantics. */
  2501. result = cxx_sizeof_or_alignof_type (input_location, type,
  2502. opcode, true, true);
  2503. }
  2504. if (template_dependent_p)
  2505. processing_template_decl--;
  2506. return convert_out (ctx->preserve (result));
  2507. }
  2508. gcc_expr
  2509. plugin_build_cast_expr (cc1_plugin::connection *self,
  2510. const char *binary_op,
  2511. gcc_type operand1,
  2512. gcc_expr operand2)
  2513. {
  2514. plugin_context *ctx = static_cast<plugin_context *> (self);
  2515. tree (*build_cast)(location_t loc, tree type, tree expr,
  2516. tsubst_flags_t complain) = NULL;
  2517. tree type = convert_in (operand1);
  2518. tree expr = convert_in (operand2);
  2519. switch (CHARS2 (binary_op[0], binary_op[1]))
  2520. {
  2521. case CHARS2 ('d', 'c'): // dynamic_cast
  2522. build_cast = build_dynamic_cast;
  2523. break;
  2524. case CHARS2 ('s', 'c'): // static_cast
  2525. build_cast = build_static_cast;
  2526. break;
  2527. case CHARS2 ('c', 'c'): // const_cast
  2528. build_cast = build_const_cast;
  2529. break;
  2530. case CHARS2 ('r', 'c'): // reinterpret_cast
  2531. build_cast = build_reinterpret_cast;
  2532. break;
  2533. case CHARS2 ('c', 'v'): // C cast, conversion with one argument
  2534. build_cast = cp_build_c_cast;
  2535. break;
  2536. default:
  2537. gcc_unreachable ();
  2538. }
  2539. processing_template_decl++;
  2540. bool template_dependent_p = dependent_type_p (type)
  2541. || type_dependent_expression_p (expr)
  2542. || value_dependent_expression_p (expr);
  2543. if (!template_dependent_p)
  2544. processing_template_decl--;
  2545. tree val = build_cast (input_location, type, expr, tf_error);
  2546. if (template_dependent_p)
  2547. processing_template_decl--;
  2548. return convert_out (ctx->preserve (val));
  2549. }
  2550. static inline vec<tree, va_gc> *
  2551. args_to_tree_vec (const struct gcc_cp_function_args *args_in)
  2552. {
  2553. vec<tree, va_gc> *args = make_tree_vector ();
  2554. for (int i = 0; i < args_in->n_elements; i++)
  2555. vec_safe_push (args, convert_in (args_in->elements[i]));
  2556. return args;
  2557. }
  2558. static inline tree
  2559. args_to_tree_list (const struct gcc_cp_function_args *args_in)
  2560. {
  2561. tree args, *tail = &args;
  2562. for (int i = 0; i < args_in->n_elements; i++)
  2563. {
  2564. *tail = build_tree_list (NULL, convert_in (args_in->elements[i]));
  2565. tail = &TREE_CHAIN (*tail);
  2566. }
  2567. return args;
  2568. }
  2569. static inline vec<constructor_elt, va_gc> *
  2570. args_to_ctor_elts (const struct gcc_cp_function_args *args_in)
  2571. {
  2572. vec<constructor_elt, va_gc> *args = NULL;
  2573. for (int i = 0; i < args_in->n_elements; i++)
  2574. CONSTRUCTOR_APPEND_ELT (args, NULL_TREE, convert_in (args_in->elements[i]));
  2575. return args;
  2576. }
  2577. gcc_expr
  2578. plugin_build_expression_list_expr (cc1_plugin::connection *self,
  2579. const char *conv_op,
  2580. gcc_type type_in,
  2581. const struct gcc_cp_function_args *values_in)
  2582. {
  2583. plugin_context *ctx = static_cast<plugin_context *> (self);
  2584. tree type = convert_in (type_in);
  2585. tree args;
  2586. tree result;
  2587. switch (CHARS2 (conv_op[0], conv_op[1]))
  2588. {
  2589. case CHARS2 ('c', 'v'): // conversion with parenthesized expression list
  2590. gcc_assert (TYPE_P (type));
  2591. args = args_to_tree_list (values_in);
  2592. result = build_functional_cast (input_location, type, args, tf_error);
  2593. break;
  2594. case CHARS2 ('t', 'l'): // conversion with braced expression list
  2595. gcc_assert (type);
  2596. gcc_assert (TYPE_P (type));
  2597. args = make_node (CONSTRUCTOR);
  2598. CONSTRUCTOR_ELTS (args) = args_to_ctor_elts (values_in);
  2599. CONSTRUCTOR_IS_DIRECT_INIT (args) = 1;
  2600. result = finish_compound_literal (type, args, tf_error);
  2601. break;
  2602. case CHARS2 ('i', 'l'): // untyped braced expression list
  2603. gcc_assert (!type);
  2604. result = make_node (CONSTRUCTOR);
  2605. CONSTRUCTOR_ELTS (result) = args_to_ctor_elts (values_in);
  2606. break;
  2607. default:
  2608. gcc_unreachable ();
  2609. }
  2610. return convert_out (ctx->preserve (result));
  2611. }
  2612. gcc_expr
  2613. plugin_build_new_expr (cc1_plugin::connection *self,
  2614. const char *new_op,
  2615. const struct gcc_cp_function_args *placement_in,
  2616. gcc_type type_in,
  2617. const struct gcc_cp_function_args *initializer_in)
  2618. {
  2619. plugin_context *ctx = static_cast<plugin_context *> (self);
  2620. tree type = convert_in (type_in);
  2621. vec<tree, va_gc> *placement = NULL, *initializer = NULL;
  2622. bool global_scope_p = false;
  2623. tree nelts = NULL;
  2624. if (placement_in)
  2625. placement = args_to_tree_vec (placement_in);
  2626. if (initializer_in)
  2627. initializer = args_to_tree_vec (initializer_in);
  2628. gcc_assert (TYPE_P (type));
  2629. once_more:
  2630. switch (CHARS2 (new_op[0], new_op[1]))
  2631. {
  2632. case CHARS2 ('g', 's'):
  2633. gcc_assert (!global_scope_p);
  2634. global_scope_p = true;
  2635. new_op += 2;
  2636. goto once_more;
  2637. case CHARS2 ('n', 'w'): // non-array new
  2638. gcc_assert (TREE_CODE (type) != ARRAY_TYPE);
  2639. break;
  2640. case CHARS2 ('n', 'a'): // array new
  2641. gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
  2642. gcc_assert (TYPE_DOMAIN (type));
  2643. {
  2644. // Compute the length of the outermost array type, then discard it.
  2645. tree maxelt = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
  2646. tree eltype = TREE_TYPE (maxelt);
  2647. tree onecst = integer_one_node;
  2648. processing_template_decl++;
  2649. bool template_dependent_p = value_dependent_expression_p (maxelt)
  2650. || type_dependent_expression_p (maxelt);
  2651. if (!template_dependent_p)
  2652. {
  2653. processing_template_decl--;
  2654. onecst = fold_convert (eltype, onecst);
  2655. }
  2656. nelts = fold_build2 (PLUS_EXPR, eltype, nelts, onecst);
  2657. if (template_dependent_p)
  2658. processing_template_decl--;
  2659. type = TREE_TYPE (type);
  2660. }
  2661. break;
  2662. default:
  2663. gcc_unreachable ();
  2664. }
  2665. processing_template_decl++;
  2666. bool template_dependent_p = dependent_type_p (type)
  2667. || value_dependent_expression_p (nelts)
  2668. || (placement
  2669. && any_type_dependent_arguments_p (placement))
  2670. || (initializer
  2671. && any_type_dependent_arguments_p (initializer));
  2672. if (!template_dependent_p)
  2673. processing_template_decl--;
  2674. tree result = build_new (input_location, &placement, type, nelts,
  2675. &initializer, global_scope_p, tf_error);
  2676. if (template_dependent_p)
  2677. processing_template_decl--;
  2678. if (placement != NULL)
  2679. release_tree_vector (placement);
  2680. if (initializer != NULL)
  2681. release_tree_vector (initializer);
  2682. return convert_out (ctx->preserve (result));
  2683. }
  2684. gcc_expr
  2685. plugin_build_call_expr (cc1_plugin::connection *self,
  2686. gcc_expr callable_in, int qualified_p,
  2687. const struct gcc_cp_function_args *args_in)
  2688. {
  2689. plugin_context *ctx = static_cast<plugin_context *> (self);
  2690. tree callable = convert_in (callable_in);
  2691. tree call_expr;
  2692. vec<tree, va_gc> *args = args_to_tree_vec (args_in);
  2693. bool koenig_p = false;
  2694. if (!qualified_p && !args->is_empty ())
  2695. {
  2696. if (identifier_p (callable))
  2697. koenig_p = true;
  2698. else if (is_overloaded_fn (callable))
  2699. {
  2700. tree fn = get_first_fn (callable);
  2701. fn = STRIP_TEMPLATE (fn);
  2702. if (!DECL_FUNCTION_MEMBER_P (fn)
  2703. && !DECL_LOCAL_DECL_P (fn))
  2704. koenig_p = true;
  2705. }
  2706. }
  2707. if (koenig_p && !any_type_dependent_arguments_p (args))
  2708. callable = perform_koenig_lookup (callable, args, tf_none);
  2709. if (TREE_CODE (callable) == COMPONENT_REF)
  2710. {
  2711. tree object = TREE_OPERAND (callable, 0);
  2712. tree memfn = TREE_OPERAND (callable, 1);
  2713. if (type_dependent_expression_p (object)
  2714. || (!BASELINK_P (memfn) && TREE_CODE (memfn) != FIELD_DECL)
  2715. || type_dependent_expression_p (memfn)
  2716. || any_type_dependent_arguments_p (args))
  2717. call_expr = build_nt_call_vec (callable, args);
  2718. else if (BASELINK_P (memfn))
  2719. call_expr = build_new_method_call (object, memfn, &args, NULL_TREE,
  2720. qualified_p
  2721. ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
  2722. : LOOKUP_NORMAL,
  2723. NULL, tf_none);
  2724. else
  2725. call_expr = finish_call_expr (callable, &args, false, false, tf_none);
  2726. }
  2727. else if (TREE_CODE (callable) == OFFSET_REF
  2728. || TREE_CODE (callable) == MEMBER_REF
  2729. || TREE_CODE (callable) == DOTSTAR_EXPR)
  2730. call_expr = build_offset_ref_call_from_tree (callable, &args, tf_none);
  2731. else
  2732. call_expr = finish_call_expr (callable, &args,
  2733. !!qualified_p, koenig_p, tf_none);
  2734. release_tree_vector (args);
  2735. return convert_out (ctx->preserve (call_expr));
  2736. }
  2737. gcc_type
  2738. plugin_get_expr_type (cc1_plugin::connection *self,
  2739. gcc_expr operand)
  2740. {
  2741. plugin_context *ctx = static_cast<plugin_context *> (self);
  2742. tree op0 = convert_in (operand);
  2743. tree type;
  2744. if (op0)
  2745. type = TREE_TYPE (op0);
  2746. else
  2747. type = make_decltype_auto ();
  2748. return convert_out (ctx->preserve (type));
  2749. }
  2750. gcc_decl
  2751. plugin_build_function_template_specialization (cc1_plugin::connection *self,
  2752. gcc_decl template_decl,
  2753. const gcc_cp_template_args *targs,
  2754. gcc_address address,
  2755. const char *filename,
  2756. unsigned int line_number)
  2757. {
  2758. plugin_context *ctx = static_cast<plugin_context *> (self);
  2759. location_t loc = ctx->get_location_t (filename, line_number);
  2760. tree name = convert_in (template_decl);
  2761. tree targsl = targlist (targs);
  2762. tree decl = tsubst (name, targsl, tf_error, NULL_TREE);
  2763. DECL_SOURCE_LOCATION (decl) = loc;
  2764. record_decl_address (ctx, build_decl_addr_value (decl, address));
  2765. return convert_out (ctx->preserve (decl));
  2766. }
  2767. gcc_decl
  2768. plugin_build_class_template_specialization (cc1_plugin::connection *self,
  2769. gcc_decl template_decl,
  2770. const gcc_cp_template_args *args,
  2771. const char *filename,
  2772. unsigned int line_number)
  2773. {
  2774. plugin_context *ctx = static_cast<plugin_context *> (self);
  2775. location_t loc = ctx->get_location_t (filename, line_number);
  2776. tree name = convert_in (template_decl);
  2777. tree tdecl = finish_template_type (name, targlist (args), false);;
  2778. DECL_SOURCE_LOCATION (tdecl) = loc;
  2779. return convert_out (ctx->preserve (tdecl));
  2780. }
  2781. /* Return a builtin type associated with BUILTIN_NAME. */
  2782. static tree
  2783. safe_lookup_builtin_type (const char *builtin_name)
  2784. {
  2785. tree result = NULL_TREE;
  2786. if (!builtin_name)
  2787. return result;
  2788. result = identifier_global_value (get_identifier (builtin_name));
  2789. if (!result)
  2790. return result;
  2791. gcc_assert (TREE_CODE (result) == TYPE_DECL);
  2792. result = TREE_TYPE (result);
  2793. return result;
  2794. }
  2795. gcc_type
  2796. plugin_get_int_type (cc1_plugin::connection *self,
  2797. int is_unsigned, unsigned long size_in_bytes,
  2798. const char *builtin_name)
  2799. {
  2800. tree result;
  2801. if (builtin_name)
  2802. {
  2803. result = safe_lookup_builtin_type (builtin_name);
  2804. gcc_assert (!result || TREE_CODE (result) == INTEGER_TYPE);
  2805. }
  2806. else
  2807. result = c_common_type_for_size (BITS_PER_UNIT * size_in_bytes,
  2808. is_unsigned);
  2809. if (result == NULL_TREE)
  2810. result = error_mark_node;
  2811. else
  2812. {
  2813. gcc_assert (!TYPE_UNSIGNED (result) == !is_unsigned);
  2814. gcc_assert (TREE_CODE (TYPE_SIZE (result)) == INTEGER_CST);
  2815. gcc_assert (TYPE_PRECISION (result) == BITS_PER_UNIT * size_in_bytes);
  2816. plugin_context *ctx = static_cast<plugin_context *> (self);
  2817. ctx->preserve (result);
  2818. }
  2819. return convert_out (result);
  2820. }
  2821. gcc_type
  2822. plugin_get_char_type (cc1_plugin::connection *)
  2823. {
  2824. return convert_out (char_type_node);
  2825. }
  2826. gcc_type
  2827. plugin_get_float_type (cc1_plugin::connection *,
  2828. unsigned long size_in_bytes,
  2829. const char *builtin_name)
  2830. {
  2831. if (builtin_name)
  2832. {
  2833. tree result = safe_lookup_builtin_type (builtin_name);
  2834. if (!result)
  2835. return convert_out (error_mark_node);
  2836. gcc_assert (TREE_CODE (result) == REAL_TYPE);
  2837. gcc_assert (BITS_PER_UNIT * size_in_bytes == TYPE_PRECISION (result));
  2838. return convert_out (result);
  2839. }
  2840. if (BITS_PER_UNIT * size_in_bytes == TYPE_PRECISION (float_type_node))
  2841. return convert_out (float_type_node);
  2842. if (BITS_PER_UNIT * size_in_bytes == TYPE_PRECISION (double_type_node))
  2843. return convert_out (double_type_node);
  2844. if (BITS_PER_UNIT * size_in_bytes == TYPE_PRECISION (long_double_type_node))
  2845. return convert_out (long_double_type_node);
  2846. return convert_out (error_mark_node);
  2847. }
  2848. gcc_type
  2849. plugin_get_void_type (cc1_plugin::connection *)
  2850. {
  2851. return convert_out (void_type_node);
  2852. }
  2853. gcc_type
  2854. plugin_get_bool_type (cc1_plugin::connection *)
  2855. {
  2856. return convert_out (boolean_type_node);
  2857. }
  2858. gcc_type
  2859. plugin_get_nullptr_type (cc1_plugin::connection *)
  2860. {
  2861. return convert_out (nullptr_type_node);
  2862. }
  2863. gcc_expr
  2864. plugin_get_nullptr_constant (cc1_plugin::connection *)
  2865. {
  2866. return convert_out (nullptr_node);
  2867. }
  2868. gcc_type
  2869. plugin_build_array_type (cc1_plugin::connection *self,
  2870. gcc_type element_type_in, int num_elements)
  2871. {
  2872. tree element_type = convert_in (element_type_in);
  2873. tree result;
  2874. if (num_elements == -1)
  2875. result = build_array_type (element_type, NULL_TREE);
  2876. else
  2877. result = build_array_type_nelts (element_type, num_elements);
  2878. plugin_context *ctx = static_cast<plugin_context *> (self);
  2879. return convert_out (ctx->preserve (result));
  2880. }
  2881. gcc_type
  2882. plugin_build_dependent_array_type (cc1_plugin::connection *self,
  2883. gcc_type element_type_in,
  2884. gcc_expr num_elements_in)
  2885. {
  2886. plugin_context *ctx = static_cast<plugin_context *> (self);
  2887. tree element_type = convert_in (element_type_in);
  2888. tree size = convert_in (num_elements_in);
  2889. tree name = get_identifier ("dependent array type");
  2890. processing_template_decl++;
  2891. bool template_dependent_p = dependent_type_p (element_type)
  2892. || type_dependent_expression_p (size)
  2893. || value_dependent_expression_p (size);
  2894. if (!template_dependent_p)
  2895. processing_template_decl--;
  2896. tree itype = compute_array_index_type (name, size, tf_error);
  2897. tree type = build_cplus_array_type (element_type, itype);
  2898. if (template_dependent_p)
  2899. processing_template_decl--;
  2900. return convert_out (ctx->preserve (type));
  2901. }
  2902. gcc_type
  2903. plugin_build_vla_array_type (cc1_plugin::connection *self,
  2904. gcc_type element_type_in,
  2905. const char *upper_bound_name)
  2906. {
  2907. tree element_type = convert_in (element_type_in);
  2908. tree upper_bound = lookup_name (get_identifier (upper_bound_name));
  2909. tree size = fold_build2 (PLUS_EXPR, TREE_TYPE (upper_bound), upper_bound,
  2910. build_one_cst (TREE_TYPE (upper_bound)));
  2911. tree range = compute_array_index_type (NULL_TREE, size,
  2912. tf_error);
  2913. tree result = build_cplus_array_type (element_type, range);
  2914. plugin_context *ctx = static_cast<plugin_context *> (self);
  2915. return convert_out (ctx->preserve (result));
  2916. }
  2917. gcc_type
  2918. plugin_build_qualified_type (cc1_plugin::connection *,
  2919. gcc_type unqualified_type_in,
  2920. enum gcc_cp_qualifiers qualifiers)
  2921. {
  2922. tree unqualified_type = convert_in (unqualified_type_in);
  2923. cp_cv_quals quals = 0;
  2924. if ((qualifiers & GCC_CP_QUALIFIER_CONST) != 0)
  2925. quals |= TYPE_QUAL_CONST;
  2926. if ((qualifiers & GCC_CP_QUALIFIER_VOLATILE) != 0)
  2927. quals |= TYPE_QUAL_VOLATILE;
  2928. if ((qualifiers & GCC_CP_QUALIFIER_RESTRICT) != 0)
  2929. quals |= TYPE_QUAL_RESTRICT;
  2930. gcc_assert ((TREE_CODE (unqualified_type) != METHOD_TYPE
  2931. && TREE_CODE (unqualified_type) != REFERENCE_TYPE)
  2932. || quals == 0);
  2933. return convert_out (build_qualified_type (unqualified_type, quals));
  2934. }
  2935. gcc_type
  2936. plugin_build_complex_type (cc1_plugin::connection *self,
  2937. gcc_type base_type)
  2938. {
  2939. plugin_context *ctx = static_cast<plugin_context *> (self);
  2940. return convert_out (ctx->preserve (build_complex_type (convert_in (base_type))));
  2941. }
  2942. gcc_type
  2943. plugin_build_vector_type (cc1_plugin::connection *self,
  2944. gcc_type base_type, int nunits)
  2945. {
  2946. plugin_context *ctx = static_cast<plugin_context *> (self);
  2947. return convert_out (ctx->preserve (build_vector_type (convert_in (base_type),
  2948. nunits)));
  2949. }
  2950. int
  2951. plugin_build_constant (cc1_plugin::connection *self, gcc_type type_in,
  2952. const char *name, unsigned long value,
  2953. const char *filename, unsigned int line_number)
  2954. {
  2955. plugin_context *ctx = static_cast<plugin_context *> (self);
  2956. tree cst, decl;
  2957. tree type = convert_in (type_in);
  2958. cst = build_int_cst (type, value);
  2959. if (!TYPE_READONLY (type))
  2960. type = build_qualified_type (type, TYPE_QUAL_CONST);
  2961. decl = build_decl (ctx->get_location_t (filename, line_number),
  2962. VAR_DECL, get_identifier (name), type);
  2963. TREE_STATIC (decl) = 1;
  2964. TREE_READONLY (decl) = 1;
  2965. cp_finish_decl (decl, cst, true, NULL, LOOKUP_ONLYCONVERTING);
  2966. safe_pushdecl (decl);
  2967. return 1;
  2968. }
  2969. gcc_type
  2970. plugin_error (cc1_plugin::connection *,
  2971. const char *message)
  2972. {
  2973. error ("%s", message);
  2974. return convert_out (error_mark_node);
  2975. }
  2976. int
  2977. plugin_add_static_assert (cc1_plugin::connection *self,
  2978. gcc_expr condition_in,
  2979. const char *errormsg,
  2980. const char *filename,
  2981. unsigned int line_number)
  2982. {
  2983. plugin_context *ctx = static_cast<plugin_context *> (self);
  2984. tree condition = convert_in (condition_in);
  2985. if (!errormsg)
  2986. errormsg = "";
  2987. tree message = build_string (strlen (errormsg) + 1, errormsg);
  2988. TREE_TYPE (message) = char_array_type_node;
  2989. fix_string_type (message);
  2990. location_t loc = ctx->get_location_t (filename, line_number);
  2991. bool member_p = at_class_scope_p ();
  2992. finish_static_assert (condition, message, loc, member_p, false);
  2993. return 1;
  2994. }
  2995. #ifdef __GNUC__
  2996. #pragma GCC visibility push(default)
  2997. #endif
  2998. int
  2999. plugin_init (struct plugin_name_args *plugin_info,
  3000. struct plugin_gcc_version *)
  3001. {
  3002. generic_plugin_init (plugin_info, GCC_CP_FE_VERSION_0);
  3003. register_callback (plugin_info->base_name, PLUGIN_PRAGMAS,
  3004. plugin_init_extra_pragmas, NULL);
  3005. register_callback (plugin_info->base_name, PLUGIN_PRE_GENERICIZE,
  3006. rewrite_decls_to_addresses, NULL);
  3007. #define GCC_METHOD0(R, N) \
  3008. { \
  3009. cc1_plugin::callback_ftype *fun \
  3010. = cc1_plugin::invoker<R>::invoke<plugin_ ## N>; \
  3011. current_context->add_callback (# N, fun); \
  3012. }
  3013. #define GCC_METHOD1(R, N, A) \
  3014. { \
  3015. cc1_plugin::callback_ftype *fun \
  3016. = cc1_plugin::invoker<R, A>::invoke<plugin_ ## N>; \
  3017. current_context->add_callback (# N, fun); \
  3018. }
  3019. #define GCC_METHOD2(R, N, A, B) \
  3020. { \
  3021. cc1_plugin::callback_ftype *fun \
  3022. = cc1_plugin::invoker<R, A, B>::invoke<plugin_ ## N>; \
  3023. current_context->add_callback (# N, fun); \
  3024. }
  3025. #define GCC_METHOD3(R, N, A, B, C) \
  3026. { \
  3027. cc1_plugin::callback_ftype *fun \
  3028. = cc1_plugin::invoker<R, A, B, C>::invoke<plugin_ ## N>; \
  3029. current_context->add_callback (# N, fun); \
  3030. }
  3031. #define GCC_METHOD4(R, N, A, B, C, D) \
  3032. { \
  3033. cc1_plugin::callback_ftype *fun \
  3034. = cc1_plugin::invoker<R, A, B, C, \
  3035. D>::invoke<plugin_ ## N>; \
  3036. current_context->add_callback (# N, fun); \
  3037. }
  3038. #define GCC_METHOD5(R, N, A, B, C, D, E) \
  3039. { \
  3040. cc1_plugin::callback_ftype *fun \
  3041. = cc1_plugin::invoker<R, A, B, C, \
  3042. D, E>::invoke<plugin_ ## N>; \
  3043. current_context->add_callback (# N, fun); \
  3044. }
  3045. #define GCC_METHOD7(R, N, A, B, C, D, E, F, G) \
  3046. { \
  3047. cc1_plugin::callback_ftype *fun \
  3048. = cc1_plugin::invoker<R, A, B, C, \
  3049. D, E, F, G>::invoke<plugin_ ## N>; \
  3050. current_context->add_callback (# N, fun); \
  3051. }
  3052. #include "gcc-cp-fe.def"
  3053. #undef GCC_METHOD0
  3054. #undef GCC_METHOD1
  3055. #undef GCC_METHOD2
  3056. #undef GCC_METHOD3
  3057. #undef GCC_METHOD4
  3058. #undef GCC_METHOD5
  3059. #undef GCC_METHOD7
  3060. return 0;
  3061. }