pexpr.pas 121 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Does parsing of expression for Free Pascal
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit pexpr;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. symtype,symdef,symbase,
  22. node,ncal,
  23. tokens,globtype,globals,constexp;
  24. { reads a whole expression }
  25. function expr(dotypecheck:boolean) : tnode;
  26. { reads an expression without assignements and .. }
  27. function comp_expr(accept_equal,typeonly:boolean):tnode;
  28. { reads a single factor }
  29. function factor(getaddr,typeonly:boolean) : tnode;
  30. procedure string_dec(var def: tdef; allowtypedef: boolean);
  31. function parse_paras(__colon,__namedpara : boolean;end_of_paras : ttoken) : tnode;
  32. { the ID token has to be consumed before calling this function }
  33. procedure do_member_read(structh:tabstractrecorddef;getaddr:boolean;sym:tsym;var p1:tnode;var again:boolean;callflags:tcallnodeflags);
  34. function get_intconst:TConstExprInt;
  35. function get_stringconst:string;
  36. implementation
  37. uses
  38. { common }
  39. cutils,
  40. { global }
  41. verbose,
  42. systems,widestr,
  43. { symtable }
  44. symconst,symtable,symsym,defutil,defcmp,
  45. { module }
  46. fmodule,ppu,
  47. { pass 1 }
  48. pass_1,htypechk,
  49. nmat,nadd,nmem,nset,ncnv,ninl,ncon,nld,nflw,nbas,nutils,
  50. { parser }
  51. scanner,
  52. pbase,pinline,ptype,pgenutil,
  53. { codegen }
  54. cgbase,procinfo,cpuinfo
  55. ;
  56. { sub_expr(opmultiply) is need to get -1 ** 4 to be
  57. read as - (1**4) and not (-1)**4 PM }
  58. type
  59. Toperator_precedence=(opcompare,opaddition,opmultiply,oppower);
  60. const
  61. highest_precedence = oppower;
  62. function sub_expr(pred_level:Toperator_precedence;accept_equal,typeonly:boolean):tnode;forward;
  63. const
  64. { true, if the inherited call is anonymous }
  65. anon_inherited : boolean = false;
  66. { last def found, only used by anon. inherited calls to insert proper type casts }
  67. srdef : tdef = nil;
  68. procedure string_dec(var def:tdef; allowtypedef: boolean);
  69. { reads a string type with optional length }
  70. { and returns a pointer to the string }
  71. { definition }
  72. var
  73. p : tnode;
  74. begin
  75. def:=cshortstringtype;
  76. consume(_STRING);
  77. if (token=_LECKKLAMMER) then
  78. begin
  79. if not(allowtypedef) then
  80. Message(parser_e_no_local_para_def);
  81. consume(_LECKKLAMMER);
  82. p:=comp_expr(true,false);
  83. if not is_constintnode(p) then
  84. begin
  85. Message(parser_e_illegal_expression);
  86. { error recovery }
  87. consume(_RECKKLAMMER);
  88. end
  89. else
  90. begin
  91. if (tordconstnode(p).value<=0) then
  92. begin
  93. Message(parser_e_invalid_string_size);
  94. tordconstnode(p).value:=255;
  95. end;
  96. consume(_RECKKLAMMER);
  97. if tordconstnode(p).value>255 then
  98. begin
  99. { longstring is currently unsupported (CEC)! }
  100. { t:=tstringdef.createlong(tordconstnode(p).value))}
  101. Message(parser_e_invalid_string_size);
  102. tordconstnode(p).value:=255;
  103. def:=tstringdef.createshort(int64(tordconstnode(p).value));
  104. end
  105. else
  106. if tordconstnode(p).value<>255 then
  107. def:=tstringdef.createshort(int64(tordconstnode(p).value));
  108. end;
  109. p.free;
  110. end
  111. else
  112. begin
  113. if cs_ansistrings in current_settings.localswitches then
  114. def:=cansistringtype
  115. else
  116. def:=cshortstringtype;
  117. end;
  118. end;
  119. function parse_paras(__colon,__namedpara : boolean;end_of_paras : ttoken) : tnode;
  120. var
  121. p1,p2,argname : tnode;
  122. prev_in_args,
  123. old_named_args_allowed,
  124. old_allow_array_constructor : boolean;
  125. begin
  126. if token=end_of_paras then
  127. begin
  128. parse_paras:=nil;
  129. exit;
  130. end;
  131. { save old values }
  132. prev_in_args:=in_args;
  133. old_allow_array_constructor:=allow_array_constructor;
  134. old_named_args_allowed:=named_args_allowed;
  135. { set para parsing values }
  136. in_args:=true;
  137. named_args_allowed:=false;
  138. allow_array_constructor:=true;
  139. p2:=nil;
  140. repeat
  141. if __namedpara then
  142. begin
  143. if token=_COMMA then
  144. begin
  145. { empty parameter }
  146. p2:=ccallparanode.create(cnothingnode.create,p2);
  147. end
  148. else
  149. begin
  150. named_args_allowed:=true;
  151. p1:=comp_expr(true,false);
  152. named_args_allowed:=false;
  153. if found_arg_name then
  154. begin
  155. argname:=p1;
  156. p1:=comp_expr(true,false);
  157. p2:=ccallparanode.create(p1,p2);
  158. tcallparanode(p2).parametername:=argname;
  159. end
  160. else
  161. p2:=ccallparanode.create(p1,p2);
  162. found_arg_name:=false;
  163. end;
  164. end
  165. else
  166. begin
  167. p1:=comp_expr(true,false);
  168. p2:=ccallparanode.create(p1,p2);
  169. end;
  170. { it's for the str(l:5,s); }
  171. if __colon and (token=_COLON) then
  172. begin
  173. consume(_COLON);
  174. p1:=comp_expr(true,false);
  175. p2:=ccallparanode.create(p1,p2);
  176. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  177. if try_to_consume(_COLON) then
  178. begin
  179. p1:=comp_expr(true,false);
  180. p2:=ccallparanode.create(p1,p2);
  181. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  182. end
  183. end;
  184. until not try_to_consume(_COMMA);
  185. allow_array_constructor:=old_allow_array_constructor;
  186. in_args:=prev_in_args;
  187. named_args_allowed:=old_named_args_allowed;
  188. parse_paras:=p2;
  189. end;
  190. function gen_c_style_operator(ntyp:tnodetype;p1,p2:tnode) : tnode;
  191. var
  192. hp : tnode;
  193. hdef : tdef;
  194. temp : ttempcreatenode;
  195. newstatement : tstatementnode;
  196. begin
  197. { Properties are not allowed, because the write can
  198. be different from the read }
  199. if (nf_isproperty in p1.flags) then
  200. begin
  201. Message(type_e_variable_id_expected);
  202. { We can continue with the loading,
  203. it'll not create errors. Only the expected
  204. result can be wrong }
  205. end;
  206. hp:=p1;
  207. while assigned(hp) and
  208. (hp.nodetype in [derefn,subscriptn,vecn,typeconvn]) do
  209. hp:=tunarynode(hp).left;
  210. if not assigned(hp) then
  211. internalerror(200410121);
  212. if (hp.nodetype=calln) then
  213. begin
  214. typecheckpass(p1);
  215. result:=internalstatements(newstatement);
  216. hdef:=tpointerdef.create(p1.resultdef);
  217. temp:=ctempcreatenode.create(hdef,sizeof(pint),tt_persistent,false);
  218. addstatement(newstatement,temp);
  219. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(temp),caddrnode.create_internal(p1)));
  220. addstatement(newstatement,cassignmentnode.create(
  221. cderefnode.create(ctemprefnode.create(temp)),
  222. caddnode.create(ntyp,
  223. cderefnode.create(ctemprefnode.create(temp)),
  224. p2)));
  225. addstatement(newstatement,ctempdeletenode.create(temp));
  226. end
  227. else
  228. result:=cassignmentnode.create(p1,caddnode.create(ntyp,p1.getcopy,p2));
  229. end;
  230. function statement_syssym(l : byte) : tnode;
  231. var
  232. p1,p2,paras : tnode;
  233. err,
  234. prev_in_args : boolean;
  235. begin
  236. prev_in_args:=in_args;
  237. case l of
  238. in_new_x :
  239. begin
  240. if afterassignment or in_args then
  241. statement_syssym:=new_function
  242. else
  243. statement_syssym:=new_dispose_statement(true);
  244. end;
  245. in_dispose_x :
  246. begin
  247. statement_syssym:=new_dispose_statement(false);
  248. end;
  249. in_ord_x :
  250. begin
  251. consume(_LKLAMMER);
  252. in_args:=true;
  253. p1:=comp_expr(true,false);
  254. consume(_RKLAMMER);
  255. p1:=geninlinenode(in_ord_x,false,p1);
  256. statement_syssym := p1;
  257. end;
  258. in_exit :
  259. begin
  260. if try_to_consume(_LKLAMMER) then
  261. begin
  262. if not (m_mac in current_settings.modeswitches) then
  263. begin
  264. if not(try_to_consume(_RKLAMMER)) then
  265. begin
  266. p1:=comp_expr(true,false);
  267. consume(_RKLAMMER);
  268. if (not assigned(current_procinfo) or
  269. is_void(current_procinfo.procdef.returndef)) then
  270. begin
  271. Message(parser_e_void_function);
  272. { recovery }
  273. p1.free;
  274. p1:=nil;
  275. end;
  276. end
  277. else
  278. p1:=nil;
  279. end
  280. else
  281. begin
  282. if not (current_procinfo.procdef.procsym.name = pattern) then
  283. Message(parser_e_macpas_exit_wrong_param);
  284. consume(_ID);
  285. consume(_RKLAMMER);
  286. p1:=nil;
  287. end
  288. end
  289. else
  290. p1:=nil;
  291. statement_syssym:=cexitnode.create(p1);
  292. end;
  293. in_break :
  294. begin
  295. statement_syssym:=cbreaknode.create
  296. end;
  297. in_continue :
  298. begin
  299. statement_syssym:=ccontinuenode.create
  300. end;
  301. in_leave :
  302. begin
  303. if m_mac in current_settings.modeswitches then
  304. statement_syssym:=cbreaknode.create
  305. else
  306. begin
  307. Message1(sym_e_id_not_found, orgpattern);
  308. statement_syssym:=cerrornode.create;
  309. end;
  310. end;
  311. in_cycle :
  312. begin
  313. if m_mac in current_settings.modeswitches then
  314. statement_syssym:=ccontinuenode.create
  315. else
  316. begin
  317. Message1(sym_e_id_not_found, orgpattern);
  318. statement_syssym:=cerrornode.create;
  319. end;
  320. end;
  321. in_typeof_x :
  322. begin
  323. consume(_LKLAMMER);
  324. in_args:=true;
  325. p1:=comp_expr(true,false);
  326. consume(_RKLAMMER);
  327. if p1.nodetype=typen then
  328. ttypenode(p1).allowed:=true;
  329. { Allow classrefdef, which is required for
  330. Typeof(self) in static class methods }
  331. if not(is_objc_class_or_protocol(p1.resultdef)) and
  332. ((p1.resultdef.typ = objectdef) or
  333. (assigned(current_procinfo) and
  334. ((po_classmethod in current_procinfo.procdef.procoptions) or
  335. (po_staticmethod in current_procinfo.procdef.procoptions)) and
  336. (p1.resultdef.typ=classrefdef))) then
  337. statement_syssym:=geninlinenode(in_typeof_x,false,p1)
  338. else
  339. begin
  340. Message(parser_e_class_id_expected);
  341. p1.destroy;
  342. statement_syssym:=cerrornode.create;
  343. end;
  344. end;
  345. in_sizeof_x,
  346. in_bitsizeof_x :
  347. begin
  348. consume(_LKLAMMER);
  349. in_args:=true;
  350. p1:=comp_expr(true,false);
  351. consume(_RKLAMMER);
  352. if (p1.nodetype<>typen) and
  353. (
  354. (is_object(p1.resultdef) and
  355. (oo_has_constructor in tobjectdef(p1.resultdef).objectoptions)) or
  356. is_open_array(p1.resultdef) or
  357. is_array_of_const(p1.resultdef) or
  358. is_open_string(p1.resultdef)
  359. ) then
  360. begin
  361. statement_syssym:=geninlinenode(in_sizeof_x,false,p1);
  362. { no packed bit support for these things }
  363. if (l = in_bitsizeof_x) then
  364. statement_syssym:=caddnode.create(muln,statement_syssym,cordconstnode.create(8,sinttype,true));
  365. end
  366. else
  367. begin
  368. { allow helpers for SizeOf and BitSizeOf }
  369. if p1.nodetype=typen then
  370. ttypenode(p1).helperallowed:=true;
  371. if (p1.resultdef.typ=forwarddef) then
  372. Message1(type_e_type_is_not_completly_defined,tforwarddef(p1.resultdef).tosymname^);
  373. if (l = in_sizeof_x) or
  374. (not((p1.nodetype = vecn) and
  375. is_packed_array(tvecnode(p1).left.resultdef)) and
  376. not((p1.nodetype = subscriptn) and
  377. is_packed_record_or_object(tsubscriptnode(p1).left.resultdef))) then
  378. begin
  379. statement_syssym:=cordconstnode.create(p1.resultdef.size,sinttype,true);
  380. if (l = in_bitsizeof_x) then
  381. statement_syssym:=caddnode.create(muln,statement_syssym,cordconstnode.create(8,sinttype,true));
  382. end
  383. else
  384. statement_syssym:=cordconstnode.create(p1.resultdef.packedbitsize,sinttype,true);
  385. { p1 not needed !}
  386. p1.destroy;
  387. end;
  388. end;
  389. in_typeinfo_x,
  390. in_objc_encode_x :
  391. begin
  392. if (l=in_typeinfo_x) or
  393. (m_objectivec1 in current_settings.modeswitches) then
  394. begin
  395. consume(_LKLAMMER);
  396. in_args:=true;
  397. p1:=comp_expr(true,false);
  398. { When reading a class type it is parsed as loadvmtaddrn,
  399. typeinfo only needs the type so we remove the loadvmtaddrn }
  400. if p1.nodetype=loadvmtaddrn then
  401. begin
  402. p2:=tloadvmtaddrnode(p1).left;
  403. tloadvmtaddrnode(p1).left:=nil;
  404. p1.free;
  405. p1:=p2;
  406. end;
  407. if p1.nodetype=typen then
  408. begin
  409. ttypenode(p1).allowed:=true;
  410. { allow helpers for TypeInfo }
  411. if l=in_typeinfo_x then
  412. ttypenode(p1).helperallowed:=true;
  413. end;
  414. { else
  415. begin
  416. p1.destroy;
  417. p1:=cerrornode.create;
  418. Message(parser_e_illegal_parameter_list);
  419. end;}
  420. consume(_RKLAMMER);
  421. p2:=geninlinenode(l,false,p1);
  422. statement_syssym:=p2;
  423. end
  424. else
  425. begin
  426. Message1(sym_e_id_not_found, orgpattern);
  427. statement_syssym:=cerrornode.create;
  428. end;
  429. end;
  430. in_unaligned_x :
  431. begin
  432. err:=false;
  433. consume(_LKLAMMER);
  434. in_args:=true;
  435. p1:=comp_expr(true,false);
  436. p2:=ccallparanode.create(p1,nil);
  437. p2:=geninlinenode(in_unaligned_x,false,p2);
  438. consume(_RKLAMMER);
  439. statement_syssym:=p2;
  440. end;
  441. in_assigned_x :
  442. begin
  443. err:=false;
  444. consume(_LKLAMMER);
  445. in_args:=true;
  446. p1:=comp_expr(true,false);
  447. { When reading a class type it is parsed as loadvmtaddrn,
  448. typeinfo only needs the type so we remove the loadvmtaddrn }
  449. if p1.nodetype=loadvmtaddrn then
  450. begin
  451. p2:=tloadvmtaddrnode(p1).left;
  452. tloadvmtaddrnode(p1).left:=nil;
  453. p1.free;
  454. p1:=p2;
  455. end;
  456. if not codegenerror then
  457. begin
  458. case p1.resultdef.typ of
  459. procdef, { procvar }
  460. pointerdef,
  461. procvardef,
  462. classrefdef : ;
  463. objectdef :
  464. if not is_implicit_pointer_object_type(p1.resultdef) then
  465. begin
  466. Message(parser_e_illegal_parameter_list);
  467. err:=true;
  468. end;
  469. arraydef :
  470. if not is_dynamic_array(p1.resultdef) then
  471. begin
  472. Message(parser_e_illegal_parameter_list);
  473. err:=true;
  474. end;
  475. else
  476. begin
  477. Message(parser_e_illegal_parameter_list);
  478. err:=true;
  479. end;
  480. end;
  481. end
  482. else
  483. err:=true;
  484. if not err then
  485. begin
  486. p2:=ccallparanode.create(p1,nil);
  487. p2:=geninlinenode(in_assigned_x,false,p2);
  488. end
  489. else
  490. begin
  491. p1.free;
  492. p2:=cerrornode.create;
  493. end;
  494. consume(_RKLAMMER);
  495. statement_syssym:=p2;
  496. end;
  497. in_addr_x :
  498. begin
  499. consume(_LKLAMMER);
  500. in_args:=true;
  501. p1:=comp_expr(true,false);
  502. p1:=caddrnode.create(p1);
  503. consume(_RKLAMMER);
  504. statement_syssym:=p1;
  505. end;
  506. in_ofs_x :
  507. begin
  508. consume(_LKLAMMER);
  509. in_args:=true;
  510. p1:=comp_expr(true,false);
  511. p1:=caddrnode.create(p1);
  512. do_typecheckpass(p1);
  513. { Ofs() returns a cardinal/qword, not a pointer }
  514. p1.resultdef:=uinttype;
  515. consume(_RKLAMMER);
  516. statement_syssym:=p1;
  517. end;
  518. in_seg_x :
  519. begin
  520. consume(_LKLAMMER);
  521. in_args:=true;
  522. p1:=comp_expr(true,false);
  523. p1:=geninlinenode(in_seg_x,false,p1);
  524. consume(_RKLAMMER);
  525. statement_syssym:=p1;
  526. end;
  527. in_high_x,
  528. in_low_x :
  529. begin
  530. consume(_LKLAMMER);
  531. in_args:=true;
  532. p1:=comp_expr(true,false);
  533. p2:=geninlinenode(l,false,p1);
  534. consume(_RKLAMMER);
  535. statement_syssym:=p2;
  536. end;
  537. in_succ_x,
  538. in_pred_x :
  539. begin
  540. consume(_LKLAMMER);
  541. in_args:=true;
  542. p1:=comp_expr(true,false);
  543. p2:=geninlinenode(l,false,p1);
  544. consume(_RKLAMMER);
  545. statement_syssym:=p2;
  546. end;
  547. in_inc_x,
  548. in_dec_x :
  549. begin
  550. consume(_LKLAMMER);
  551. in_args:=true;
  552. p1:=comp_expr(true,false);
  553. if try_to_consume(_COMMA) then
  554. p2:=ccallparanode.create(comp_expr(true,false),nil)
  555. else
  556. p2:=nil;
  557. p2:=ccallparanode.create(p1,p2);
  558. statement_syssym:=geninlinenode(l,false,p2);
  559. consume(_RKLAMMER);
  560. end;
  561. in_slice_x:
  562. begin
  563. if not(in_args) then
  564. begin
  565. message(parser_e_illegal_slice);
  566. consume(_LKLAMMER);
  567. in_args:=true;
  568. comp_expr(true,false).free;
  569. if try_to_consume(_COMMA) then
  570. comp_expr(true,false).free;
  571. statement_syssym:=cerrornode.create;
  572. consume(_RKLAMMER);
  573. end
  574. else
  575. begin
  576. consume(_LKLAMMER);
  577. in_args:=true;
  578. p1:=comp_expr(true,false);
  579. Consume(_COMMA);
  580. if not(codegenerror) then
  581. p2:=ccallparanode.create(comp_expr(true,false),nil)
  582. else
  583. p2:=cerrornode.create;
  584. p2:=ccallparanode.create(p1,p2);
  585. statement_syssym:=geninlinenode(l,false,p2);
  586. consume(_RKLAMMER);
  587. end;
  588. end;
  589. in_initialize_x:
  590. begin
  591. statement_syssym:=inline_initialize;
  592. end;
  593. in_finalize_x:
  594. begin
  595. statement_syssym:=inline_finalize;
  596. end;
  597. in_copy_x:
  598. begin
  599. statement_syssym:=inline_copy;
  600. end;
  601. in_concat_x :
  602. begin
  603. consume(_LKLAMMER);
  604. in_args:=true;
  605. { Translate to x:=x+y[+z]. The addnode will do the
  606. type checking }
  607. p2:=nil;
  608. repeat
  609. p1:=comp_expr(true,false);
  610. if p2<>nil then
  611. p2:=caddnode.create(addn,p2,p1)
  612. else
  613. begin
  614. { Force string type if it isn't yet }
  615. if not(
  616. (p1.resultdef.typ=stringdef) or
  617. is_chararray(p1.resultdef) or
  618. is_char(p1.resultdef)
  619. ) then
  620. inserttypeconv(p1,cshortstringtype);
  621. p2:=p1;
  622. end;
  623. until not try_to_consume(_COMMA);
  624. consume(_RKLAMMER);
  625. statement_syssym:=p2;
  626. end;
  627. in_read_x,
  628. in_readln_x,
  629. in_readstr_x:
  630. begin
  631. if try_to_consume(_LKLAMMER) then
  632. begin
  633. paras:=parse_paras(false,false,_RKLAMMER);
  634. consume(_RKLAMMER);
  635. end
  636. else
  637. paras:=nil;
  638. p1:=geninlinenode(l,false,paras);
  639. statement_syssym := p1;
  640. end;
  641. in_setlength_x:
  642. begin
  643. statement_syssym := inline_setlength;
  644. end;
  645. in_objc_selector_x:
  646. begin
  647. if (m_objectivec1 in current_settings.modeswitches) then
  648. begin
  649. consume(_LKLAMMER);
  650. in_args:=true;
  651. { don't turn procsyms into calls (getaddr = true) }
  652. p1:=factor(true,false);
  653. p2:=geninlinenode(l,false,p1);
  654. consume(_RKLAMMER);
  655. statement_syssym:=p2;
  656. end
  657. else
  658. begin
  659. Message1(sym_e_id_not_found, orgpattern);
  660. statement_syssym:=cerrornode.create;
  661. end;
  662. end;
  663. in_length_x:
  664. begin
  665. consume(_LKLAMMER);
  666. in_args:=true;
  667. p1:=comp_expr(true,false);
  668. p2:=geninlinenode(l,false,p1);
  669. consume(_RKLAMMER);
  670. statement_syssym:=p2;
  671. end;
  672. in_write_x,
  673. in_writeln_x,
  674. in_writestr_x :
  675. begin
  676. if try_to_consume(_LKLAMMER) then
  677. begin
  678. paras:=parse_paras(true,false,_RKLAMMER);
  679. consume(_RKLAMMER);
  680. end
  681. else
  682. paras:=nil;
  683. p1 := geninlinenode(l,false,paras);
  684. statement_syssym := p1;
  685. end;
  686. in_str_x_string :
  687. begin
  688. consume(_LKLAMMER);
  689. paras:=parse_paras(true,false,_RKLAMMER);
  690. consume(_RKLAMMER);
  691. p1 := geninlinenode(l,false,paras);
  692. statement_syssym := p1;
  693. end;
  694. in_val_x:
  695. Begin
  696. consume(_LKLAMMER);
  697. in_args := true;
  698. p1:= ccallparanode.create(comp_expr(true,false), nil);
  699. consume(_COMMA);
  700. p2 := ccallparanode.create(comp_expr(true,false),p1);
  701. if try_to_consume(_COMMA) then
  702. p2 := ccallparanode.create(comp_expr(true,false),p2);
  703. consume(_RKLAMMER);
  704. p2 := geninlinenode(l,false,p2);
  705. statement_syssym := p2;
  706. End;
  707. in_include_x_y,
  708. in_exclude_x_y :
  709. begin
  710. consume(_LKLAMMER);
  711. in_args:=true;
  712. p1:=comp_expr(true,false);
  713. consume(_COMMA);
  714. p2:=comp_expr(true,false);
  715. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,nil)));
  716. consume(_RKLAMMER);
  717. end;
  718. in_pack_x_y_z,
  719. in_unpack_x_y_z :
  720. begin
  721. consume(_LKLAMMER);
  722. in_args:=true;
  723. p1:=comp_expr(true,false);
  724. consume(_COMMA);
  725. p2:=comp_expr(true,false);
  726. consume(_COMMA);
  727. paras:=comp_expr(true,false);
  728. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,ccallparanode.create(paras,nil))));
  729. consume(_RKLAMMER);
  730. end;
  731. in_assert_x_y :
  732. begin
  733. consume(_LKLAMMER);
  734. in_args:=true;
  735. p1:=comp_expr(true,false);
  736. if try_to_consume(_COMMA) then
  737. p2:=comp_expr(true,false)
  738. else
  739. begin
  740. { then insert an empty string }
  741. p2:=cstringconstnode.createstr('');
  742. end;
  743. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,nil)));
  744. consume(_RKLAMMER);
  745. end;
  746. in_get_frame:
  747. begin
  748. statement_syssym:=geninlinenode(l,false,nil);
  749. end;
  750. (*
  751. in_get_caller_frame:
  752. begin
  753. if try_to_consume(_LKLAMMER) then
  754. begin
  755. {You used to call get_caller_frame as get_caller_frame(get_frame),
  756. however, as a stack frame may not exist, it does more harm than
  757. good, so ignore it.}
  758. in_args:=true;
  759. p1:=comp_expr(true,false);
  760. p1.destroy;
  761. consume(_RKLAMMER);
  762. end;
  763. statement_syssym:=geninlinenode(l,false,nil);
  764. end;
  765. *)
  766. else
  767. internalerror(15);
  768. end;
  769. in_args:=prev_in_args;
  770. end;
  771. function maybe_load_methodpointer(st:TSymtable;var p1:tnode):boolean;
  772. begin
  773. maybe_load_methodpointer:=false;
  774. if not assigned(p1) then
  775. begin
  776. case st.symtabletype of
  777. withsymtable :
  778. begin
  779. if (st.defowner.typ=objectdef) then
  780. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  781. end;
  782. ObjectSymtable,
  783. recordsymtable:
  784. begin
  785. { We are calling from the static class method which has no self node }
  786. if assigned(current_procinfo) and current_procinfo.procdef.no_self_node then
  787. p1:=cloadvmtaddrnode.create(ctypenode.create(current_procinfo.procdef.struct))
  788. else
  789. p1:=load_self_node;
  790. { We are calling a member }
  791. maybe_load_methodpointer:=true;
  792. end;
  793. end;
  794. end;
  795. end;
  796. { reads the parameter for a subroutine call }
  797. procedure do_proc_call(sym:tsym;st:TSymtable;obj:tabstractrecorddef;getaddr:boolean;var again : boolean;var p1:tnode;callflags:tcallnodeflags);
  798. var
  799. membercall,
  800. prevafterassn : boolean;
  801. i : integer;
  802. para,p2 : tnode;
  803. currpara : tparavarsym;
  804. aprocdef : tprocdef;
  805. begin
  806. prevafterassn:=afterassignment;
  807. afterassignment:=false;
  808. membercall:=false;
  809. aprocdef:=nil;
  810. { when it is a call to a member we need to load the
  811. methodpointer first
  812. }
  813. membercall:=maybe_load_methodpointer(st,p1);
  814. { When we are expecting a procvar we also need
  815. to get the address in some cases }
  816. if assigned(getprocvardef) then
  817. begin
  818. if (block_type=bt_const) or
  819. getaddr then
  820. begin
  821. aprocdef:=Tprocsym(sym).Find_procdef_byprocvardef(getprocvardef);
  822. getaddr:=true;
  823. end
  824. else
  825. if (m_tp_procvar in current_settings.modeswitches) or
  826. (m_mac_procvar in current_settings.modeswitches) then
  827. begin
  828. aprocdef:=Tprocsym(sym).Find_procdef_byprocvardef(getprocvardef);
  829. if assigned(aprocdef) then
  830. getaddr:=true;
  831. end;
  832. end;
  833. { only need to get the address of the procedure? }
  834. if getaddr then
  835. begin
  836. { Retrieve info which procvar to call. For tp_procvar the
  837. aprocdef is already loaded above so we can reuse it }
  838. if not assigned(aprocdef) and
  839. assigned(getprocvardef) then
  840. aprocdef:=Tprocsym(sym).Find_procdef_byprocvardef(getprocvardef);
  841. { generate a methodcallnode or proccallnode }
  842. { we shouldn't convert things like @tcollection.load }
  843. p2:=cloadnode.create_procvar(sym,aprocdef,st);
  844. if assigned(p1) then
  845. begin
  846. { for loading methodpointer of an inherited function
  847. we use self as instance and load the address of
  848. the function directly and not through the vmt (PFV) }
  849. if (cnf_inherited in callflags) then
  850. begin
  851. include(p2.flags,nf_inherited);
  852. p1.free;
  853. p1:=load_self_node;
  854. end;
  855. if (p1.nodetype<>typen) then
  856. tloadnode(p2).set_mp(p1)
  857. else
  858. p1.free;
  859. end;
  860. p1:=p2;
  861. { no postfix operators }
  862. again:=false;
  863. end
  864. else
  865. begin
  866. para:=nil;
  867. if anon_inherited then
  868. begin
  869. if not assigned(current_procinfo) then
  870. internalerror(200305054);
  871. for i:=0 to current_procinfo.procdef.paras.count-1 do
  872. begin
  873. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  874. if not(vo_is_hidden_para in currpara.varoptions) then
  875. begin
  876. { inheritance by msgint? }
  877. if assigned(srdef) then
  878. { anonymous inherited via msgid calls only require a var parameter for
  879. both methods, so we need some type casting here }
  880. para:=ccallparanode.create(ctypeconvnode.create_internal(ctypeconvnode.create_internal(
  881. cloadnode.create(currpara,currpara.owner),cformaltype),tparavarsym(tprocdef(srdef).paras[i]).vardef),
  882. para)
  883. else
  884. para:=ccallparanode.create(cloadnode.create(currpara,currpara.owner),para);
  885. end;
  886. end;
  887. end
  888. else
  889. begin
  890. if try_to_consume(_LKLAMMER) then
  891. begin
  892. para:=parse_paras(false,false,_RKLAMMER);
  893. consume(_RKLAMMER);
  894. end;
  895. end;
  896. { indicate if this call was generated by a member and
  897. no explicit self is used, this is needed to determine
  898. how to handle a destructor call (PFV) }
  899. if membercall then
  900. include(callflags,cnf_member_call);
  901. if assigned(obj) then
  902. begin
  903. if not (st.symtabletype in [ObjectSymtable,recordsymtable]) then
  904. internalerror(200310031);
  905. p1:=ccallnode.create(para,tprocsym(sym),obj.symtable,p1,callflags);
  906. end
  907. else
  908. p1:=ccallnode.create(para,tprocsym(sym),st,p1,callflags);
  909. end;
  910. afterassignment:=prevafterassn;
  911. end;
  912. procedure handle_procvar(pv : tprocvardef;var p2 : tnode);
  913. var
  914. hp,hp2 : tnode;
  915. hpp : ^tnode;
  916. currprocdef : tprocdef;
  917. begin
  918. if not assigned(pv) then
  919. internalerror(200301121);
  920. if (m_tp_procvar in current_settings.modeswitches) or
  921. (m_mac_procvar in current_settings.modeswitches) then
  922. begin
  923. hp:=p2;
  924. hpp:=@p2;
  925. while assigned(hp) and
  926. (hp.nodetype=typeconvn) do
  927. begin
  928. hp:=ttypeconvnode(hp).left;
  929. { save orignal address of the old tree so we can replace the node }
  930. hpp:=@hp;
  931. end;
  932. if (hp.nodetype=calln) and
  933. { a procvar can't have parameters! }
  934. not assigned(tcallnode(hp).left) then
  935. begin
  936. currprocdef:=tcallnode(hp).symtableprocentry.Find_procdef_byprocvardef(pv);
  937. if assigned(currprocdef) then
  938. begin
  939. hp2:=cloadnode.create_procvar(tprocsym(tcallnode(hp).symtableprocentry),currprocdef,tcallnode(hp).symtableproc);
  940. if (po_methodpointer in pv.procoptions) then
  941. tloadnode(hp2).set_mp(tcallnode(hp).methodpointer.getcopy);
  942. hp.destroy;
  943. { replace the old callnode with the new loadnode }
  944. hpp^:=hp2;
  945. end;
  946. end;
  947. end;
  948. end;
  949. { checks whether sym is a static field and if so, translates the access
  950. to the appropriate node tree }
  951. function handle_staticfield_access(sym: tsym; nested: boolean; var p1: tnode): boolean;
  952. var
  953. static_name: shortstring;
  954. srsymtable: tsymtable;
  955. begin
  956. result:=false;
  957. { generate access code }
  958. if (sp_static in sym.symoptions) then
  959. begin
  960. result:=true;
  961. if not nested then
  962. static_name:=lower(sym.owner.name^)+'_'+sym.name
  963. else
  964. static_name:=lower(generate_nested_name(sym.owner,'_'))+'_'+sym.name;
  965. if sym.owner.defowner.typ=objectdef then
  966. searchsym_in_class(tobjectdef(sym.owner.defowner),tobjectdef(sym.owner.defowner),static_name,sym,srsymtable,true)
  967. else
  968. searchsym_in_record(trecorddef(sym.owner.defowner),static_name,sym,srsymtable);
  969. if assigned(sym) then
  970. check_hints(sym,sym.symoptions,sym.deprecatedmsg);
  971. p1.free;
  972. p1:=nil;
  973. { static syms are always stored as absolutevarsym to handle scope and storage properly }
  974. propaccesslist_to_node(p1,nil,tabsolutevarsym(sym).ref);
  975. end;
  976. end;
  977. { the following procedure handles the access to a property symbol }
  978. procedure handle_propertysym(propsym : tpropertysym;st : TSymtable;var p1 : tnode);
  979. var
  980. paras : tnode;
  981. p2 : tnode;
  982. membercall : boolean;
  983. callflags : tcallnodeflags;
  984. propaccesslist : tpropaccesslist;
  985. sym: tsym;
  986. begin
  987. { property parameters? read them only if the property really }
  988. { has parameters }
  989. paras:=nil;
  990. if (ppo_hasparameters in propsym.propoptions) then
  991. begin
  992. if try_to_consume(_LECKKLAMMER) then
  993. begin
  994. paras:=parse_paras(false,false,_RECKKLAMMER);
  995. consume(_RECKKLAMMER);
  996. end;
  997. end;
  998. { indexed property }
  999. if (ppo_indexed in propsym.propoptions) then
  1000. begin
  1001. p2:=cordconstnode.create(propsym.index,propsym.indexdef,true);
  1002. paras:=ccallparanode.create(p2,paras);
  1003. end;
  1004. { we need only a write property if a := follows }
  1005. { if not(afterassignment) and not(in_args) then }
  1006. if token=_ASSIGNMENT then
  1007. begin
  1008. if getpropaccesslist(propsym,palt_write,propaccesslist) then
  1009. begin
  1010. sym:=propaccesslist.firstsym^.sym;
  1011. case sym.typ of
  1012. procsym :
  1013. begin
  1014. callflags:=[];
  1015. { generate the method call }
  1016. membercall:=maybe_load_methodpointer(st,p1);
  1017. if membercall then
  1018. include(callflags,cnf_member_call);
  1019. p1:=ccallnode.create(paras,tprocsym(sym),st,p1,callflags);
  1020. addsymref(sym);
  1021. paras:=nil;
  1022. consume(_ASSIGNMENT);
  1023. { read the expression }
  1024. if propsym.propdef.typ=procvardef then
  1025. getprocvardef:=tprocvardef(propsym.propdef);
  1026. p2:=comp_expr(true,false);
  1027. if assigned(getprocvardef) then
  1028. handle_procvar(getprocvardef,p2);
  1029. tcallnode(p1).left:=ccallparanode.create(p2,tcallnode(p1).left);
  1030. { mark as property, both the tcallnode and the real call block }
  1031. include(p1.flags,nf_isproperty);
  1032. getprocvardef:=nil;
  1033. end;
  1034. fieldvarsym :
  1035. begin
  1036. { generate access code }
  1037. if not handle_staticfield_access(sym,false,p1) then
  1038. propaccesslist_to_node(p1,st,propaccesslist);
  1039. include(p1.flags,nf_isproperty);
  1040. consume(_ASSIGNMENT);
  1041. { read the expression }
  1042. p2:=comp_expr(true,false);
  1043. p1:=cassignmentnode.create(p1,p2);
  1044. end
  1045. else
  1046. begin
  1047. p1:=cerrornode.create;
  1048. Message(parser_e_no_procedure_to_access_property);
  1049. end;
  1050. end;
  1051. end
  1052. else
  1053. begin
  1054. p1:=cerrornode.create;
  1055. Message(parser_e_no_procedure_to_access_property);
  1056. end;
  1057. end
  1058. else
  1059. begin
  1060. if getpropaccesslist(propsym,palt_read,propaccesslist) then
  1061. begin
  1062. sym := propaccesslist.firstsym^.sym;
  1063. case sym.typ of
  1064. fieldvarsym :
  1065. begin
  1066. { generate access code }
  1067. if not handle_staticfield_access(sym,false,p1) then
  1068. propaccesslist_to_node(p1,st,propaccesslist);
  1069. include(p1.flags,nf_isproperty);
  1070. end;
  1071. procsym :
  1072. begin
  1073. callflags:=[];
  1074. { generate the method call }
  1075. membercall:=maybe_load_methodpointer(st,p1);
  1076. if membercall then
  1077. include(callflags,cnf_member_call);
  1078. p1:=ccallnode.create(paras,tprocsym(sym),st,p1,callflags);
  1079. paras:=nil;
  1080. include(p1.flags,nf_isproperty);
  1081. end
  1082. else
  1083. begin
  1084. p1:=cerrornode.create;
  1085. Message(type_e_mismatch);
  1086. end;
  1087. end;
  1088. end
  1089. else
  1090. begin
  1091. { error, no function to read property }
  1092. p1:=cerrornode.create;
  1093. Message(parser_e_no_procedure_to_access_property);
  1094. end;
  1095. end;
  1096. { release paras if not used }
  1097. if assigned(paras) then
  1098. paras.free;
  1099. end;
  1100. { the ID token has to be consumed before calling this function }
  1101. procedure do_member_read(structh:tabstractrecorddef;getaddr:boolean;sym:tsym;var p1:tnode;var again:boolean;callflags:tcallnodeflags);
  1102. var
  1103. isclassref : boolean;
  1104. begin
  1105. if sym=nil then
  1106. begin
  1107. { pattern is still valid unless
  1108. there is another ID just after the ID of sym }
  1109. Message1(sym_e_id_no_member,orgpattern);
  1110. p1.free;
  1111. p1:=cerrornode.create;
  1112. { try to clean up }
  1113. again:=false;
  1114. end
  1115. else
  1116. begin
  1117. if assigned(p1) then
  1118. begin
  1119. if not assigned(p1.resultdef) then
  1120. do_typecheckpass(p1);
  1121. isclassref:=(p1.resultdef.typ=classrefdef);
  1122. end
  1123. else
  1124. isclassref:=false;
  1125. { we assume, that only procsyms and varsyms are in an object }
  1126. { symbol table, for classes, properties are allowed }
  1127. case sym.typ of
  1128. procsym:
  1129. begin
  1130. do_proc_call(sym,sym.owner,structh,
  1131. (getaddr and not(token in [_CARET,_POINT])),
  1132. again,p1,callflags);
  1133. { we need to know which procedure is called }
  1134. do_typecheckpass(p1);
  1135. { calling using classref? }
  1136. if isclassref and
  1137. (p1.nodetype=calln) and
  1138. assigned(tcallnode(p1).procdefinition) and
  1139. not(po_classmethod in tcallnode(p1).procdefinition.procoptions) and
  1140. not(tcallnode(p1).procdefinition.proctypeoption=potype_constructor) then
  1141. Message(parser_e_only_class_members_via_class_ref);
  1142. end;
  1143. fieldvarsym:
  1144. begin
  1145. if not handle_staticfield_access(sym,true,p1) then
  1146. begin
  1147. if isclassref then
  1148. if assigned(p1) and
  1149. (
  1150. is_self_node(p1) or
  1151. (assigned(current_procinfo) and (current_procinfo.procdef.no_self_node) and
  1152. (current_procinfo.procdef.struct=structh))) then
  1153. Message(parser_e_only_class_members)
  1154. else
  1155. Message(parser_e_only_class_members_via_class_ref);
  1156. p1:=csubscriptnode.create(sym,p1);
  1157. end;
  1158. end;
  1159. propertysym:
  1160. begin
  1161. if isclassref and not (sp_static in sym.symoptions) then
  1162. Message(parser_e_only_class_members_via_class_ref);
  1163. handle_propertysym(tpropertysym(sym),sym.owner,p1);
  1164. end;
  1165. typesym:
  1166. begin
  1167. p1.free;
  1168. if try_to_consume(_LKLAMMER) then
  1169. begin
  1170. p1:=comp_expr(true,false);
  1171. consume(_RKLAMMER);
  1172. p1:=ctypeconvnode.create_explicit(p1,ttypesym(sym).typedef);
  1173. end
  1174. else
  1175. begin
  1176. p1:=ctypenode.create(ttypesym(sym).typedef);
  1177. if (is_class(ttypesym(sym).typedef) or is_objcclass(ttypesym(sym).typedef)) and
  1178. not(block_type in [bt_type,bt_const_type,bt_var_type]) then
  1179. p1:=cloadvmtaddrnode.create(p1);
  1180. end;
  1181. end;
  1182. constsym:
  1183. begin
  1184. p1.free;
  1185. p1:=genconstsymtree(tconstsym(sym));
  1186. end;
  1187. staticvarsym:
  1188. begin
  1189. { typed constant is a staticvarsym
  1190. now they are absolutevarsym }
  1191. p1.free;
  1192. p1:=cloadnode.create(sym,sym.Owner);
  1193. end;
  1194. absolutevarsym:
  1195. begin
  1196. p1.free;
  1197. p1:=nil;
  1198. { typed constants are absolutebarsyms now to handle storage properly }
  1199. propaccesslist_to_node(p1,nil,tabsolutevarsym(sym).ref);
  1200. end
  1201. else
  1202. internalerror(16);
  1203. end;
  1204. end;
  1205. end;
  1206. function handle_factor_typenode(hdef:tdef;getaddr:boolean;var again:boolean):tnode;
  1207. var
  1208. srsym : tsym;
  1209. srsymtable : tsymtable;
  1210. begin
  1211. if try_to_consume(_LKLAMMER) then
  1212. begin
  1213. result:=comp_expr(true,false);
  1214. consume(_RKLAMMER);
  1215. { type casts to class helpers aren't allowed }
  1216. if is_objectpascal_helper(hdef) then
  1217. Message(parser_e_no_category_as_types)
  1218. { recovery by not creating a conversion node }
  1219. else
  1220. result:=ctypeconvnode.create_explicit(result,hdef);
  1221. end
  1222. else { not LKLAMMER }
  1223. if (token=_POINT) and
  1224. (is_object(hdef) or is_record(hdef)) then
  1225. begin
  1226. consume(_POINT);
  1227. { handles calling methods declared in parent objects
  1228. using "parentobject.methodname()" }
  1229. if assigned(current_structdef) and
  1230. not(getaddr) and
  1231. current_structdef.is_related(hdef) then
  1232. begin
  1233. result:=ctypenode.create(hdef);
  1234. { search also in inherited methods }
  1235. searchsym_in_class(tobjectdef(hdef),tobjectdef(current_structdef),pattern,srsym,srsymtable,true);
  1236. if assigned(srsym) then
  1237. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1238. consume(_ID);
  1239. do_member_read(tabstractrecorddef(hdef),false,srsym,result,again,[]);
  1240. end
  1241. else
  1242. begin
  1243. { handles:
  1244. * @TObject.Load
  1245. * static methods and variables }
  1246. result:=ctypenode.create(hdef);
  1247. { TP allows also @TMenu.Load if Load is only }
  1248. { defined in an anchestor class }
  1249. srsym:=search_struct_member(tabstractrecorddef(hdef),pattern);
  1250. if assigned(srsym) then
  1251. begin
  1252. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1253. consume(_ID);
  1254. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,result,again,[]);
  1255. end
  1256. else
  1257. Message1(sym_e_id_no_member,orgpattern);
  1258. end;
  1259. end
  1260. else
  1261. begin
  1262. { Normally here would be the check against the usage
  1263. of "TClassHelper.Something", but as that might be
  1264. used inside of system symbols like sizeof and
  1265. typeinfo this check is put into ttypenode.pass_1
  1266. (for "TClassHelper" alone) and tcallnode.pass_1
  1267. (for "TClassHelper.Something") }
  1268. { class reference ? }
  1269. if is_class(hdef) or
  1270. is_objcclass(hdef) then
  1271. begin
  1272. if getaddr and (token=_POINT) then
  1273. begin
  1274. consume(_POINT);
  1275. { allows @Object.Method }
  1276. { also allows static methods and variables }
  1277. result:=ctypenode.create(hdef);
  1278. { TP allows also @TMenu.Load if Load is only }
  1279. { defined in an anchestor class }
  1280. srsym:=search_struct_member(tobjectdef(hdef),pattern);
  1281. if assigned(srsym) then
  1282. begin
  1283. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1284. consume(_ID);
  1285. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,result,again,[]);
  1286. end
  1287. else
  1288. begin
  1289. Message1(sym_e_id_no_member,orgpattern);
  1290. consume(_ID);
  1291. end;
  1292. end
  1293. else
  1294. begin
  1295. result:=ctypenode.create(hdef);
  1296. { For a type block we simply return only
  1297. the type. For all other blocks we return
  1298. a loadvmt node }
  1299. if not(block_type in [bt_type,bt_const_type,bt_var_type]) then
  1300. result:=cloadvmtaddrnode.create(result);
  1301. end;
  1302. end
  1303. else
  1304. result:=ctypenode.create(hdef);
  1305. end;
  1306. end;
  1307. {****************************************************************************
  1308. Factor
  1309. ****************************************************************************}
  1310. {---------------------------------------------
  1311. PostFixOperators
  1312. ---------------------------------------------}
  1313. { returns whether or not p1 has been changed }
  1314. function postfixoperators(var p1:tnode;var again:boolean;getaddr:boolean): boolean;
  1315. { tries to avoid syntax errors after invalid qualifiers }
  1316. procedure recoverconsume_postfixops;
  1317. begin
  1318. repeat
  1319. if not try_to_consume(_CARET) then
  1320. if try_to_consume(_POINT) then
  1321. try_to_consume(_ID)
  1322. else if try_to_consume(_LECKKLAMMER) then
  1323. begin
  1324. repeat
  1325. comp_expr(true,false);
  1326. until not try_to_consume(_COMMA);
  1327. consume(_RECKKLAMMER);
  1328. end
  1329. else if try_to_consume(_LKLAMMER) then
  1330. begin
  1331. repeat
  1332. comp_expr(true,false);
  1333. until not try_to_consume(_COMMA);
  1334. consume(_RKLAMMER);
  1335. end
  1336. else
  1337. break;
  1338. until false;
  1339. end;
  1340. procedure handle_variantarray;
  1341. var
  1342. p4 : tnode;
  1343. newstatement : tstatementnode;
  1344. tempresultvariant,
  1345. temp : ttempcreatenode;
  1346. paras : tcallparanode;
  1347. newblock : tnode;
  1348. countindices : aint;
  1349. begin
  1350. { create statements with call initialize the arguments and
  1351. call fpc_dynarr_setlength }
  1352. newblock:=internalstatements(newstatement);
  1353. { get temp for array of indicies,
  1354. we set the real size later }
  1355. temp:=ctempcreatenode.create(s32inttype,4,tt_persistent,false);
  1356. addstatement(newstatement,temp);
  1357. countindices:=0;
  1358. repeat
  1359. p4:=comp_expr(true,false);
  1360. addstatement(newstatement,cassignmentnode.create(
  1361. ctemprefnode.create_offset(temp,countindices*s32inttype.size),p4));
  1362. inc(countindices);
  1363. until not try_to_consume(_COMMA);
  1364. { set real size }
  1365. temp.size:=countindices*s32inttype.size;
  1366. consume(_RECKKLAMMER);
  1367. { we need only a write access if a := follows }
  1368. if token=_ASSIGNMENT then
  1369. begin
  1370. consume(_ASSIGNMENT);
  1371. p4:=comp_expr(true,false);
  1372. { create call to fpc_vararray_put }
  1373. paras:=ccallparanode.create(cordconstnode.create
  1374. (countindices,s32inttype,true),
  1375. ccallparanode.create(caddrnode.create_internal
  1376. (ctemprefnode.create(temp)),
  1377. ccallparanode.create(ctypeconvnode.create_internal(p4,cvarianttype),
  1378. ccallparanode.create(ctypeconvnode.create_internal(p1,cvarianttype)
  1379. ,nil))));
  1380. addstatement(newstatement,ccallnode.createintern('fpc_vararray_put',paras));
  1381. addstatement(newstatement,ctempdeletenode.create(temp));
  1382. end
  1383. else
  1384. begin
  1385. { create temp for result }
  1386. tempresultvariant:=ctempcreatenode.create(cvarianttype,cvarianttype.size,tt_persistent,true);
  1387. addstatement(newstatement,tempresultvariant);
  1388. { create call to fpc_vararray_get }
  1389. paras:=ccallparanode.create(cordconstnode.create
  1390. (countindices,s32inttype,true),
  1391. ccallparanode.create(caddrnode.create_internal
  1392. (ctemprefnode.create(temp)),
  1393. ccallparanode.create(p1,
  1394. ccallparanode.create(
  1395. ctemprefnode.create(tempresultvariant)
  1396. ,nil))));
  1397. addstatement(newstatement,ccallnode.createintern('fpc_vararray_get',paras));
  1398. addstatement(newstatement,ctempdeletenode.create(temp));
  1399. { the last statement should return the value as
  1400. location and type, this is done be referencing the
  1401. temp and converting it first from a persistent temp to
  1402. normal temp }
  1403. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempresultvariant));
  1404. addstatement(newstatement,ctemprefnode.create(tempresultvariant));
  1405. end;
  1406. p1:=newblock;
  1407. end;
  1408. var
  1409. protsym : tpropertysym;
  1410. p2,p3 : tnode;
  1411. srsym : tsym;
  1412. srsymtable : TSymtable;
  1413. structh : tabstractrecorddef;
  1414. { shouldn't be used that often, so the extra overhead is ok to save
  1415. stack space }
  1416. dispatchstring : ansistring;
  1417. nodechanged : boolean;
  1418. calltype: tdispcalltype;
  1419. label
  1420. skipreckklammercheck;
  1421. begin
  1422. result:=false;
  1423. again:=true;
  1424. while again do
  1425. begin
  1426. { we need the resultdef }
  1427. do_typecheckpass_changed(p1,nodechanged);
  1428. result:=result or nodechanged;
  1429. if codegenerror then
  1430. begin
  1431. recoverconsume_postfixops;
  1432. exit;
  1433. end;
  1434. { handle token }
  1435. case token of
  1436. _CARET:
  1437. begin
  1438. consume(_CARET);
  1439. { support tp/mac procvar^ if the procvar returns a
  1440. pointer type }
  1441. if ((m_tp_procvar in current_settings.modeswitches) or
  1442. (m_mac_procvar in current_settings.modeswitches)) and
  1443. (p1.resultdef.typ=procvardef) and
  1444. (tprocvardef(p1.resultdef).returndef.typ=pointerdef) then
  1445. begin
  1446. p1:=ccallnode.create_procvar(nil,p1);
  1447. typecheckpass(p1);
  1448. end;
  1449. if (p1.resultdef.typ<>pointerdef) then
  1450. begin
  1451. { ^ as binary operator is a problem!!!! (FK) }
  1452. again:=false;
  1453. Message(parser_e_invalid_qualifier);
  1454. recoverconsume_postfixops;
  1455. p1.destroy;
  1456. p1:=cerrornode.create;
  1457. end
  1458. else
  1459. p1:=cderefnode.create(p1);
  1460. end;
  1461. _LECKKLAMMER:
  1462. begin
  1463. if is_class_or_interface_or_object(p1.resultdef) or
  1464. is_dispinterface(p1.resultdef) or is_record(p1.resultdef) then
  1465. begin
  1466. { default property }
  1467. protsym:=search_default_property(tabstractrecorddef(p1.resultdef));
  1468. if not(assigned(protsym)) then
  1469. begin
  1470. p1.destroy;
  1471. p1:=cerrornode.create;
  1472. again:=false;
  1473. message(parser_e_no_default_property_available);
  1474. end
  1475. else
  1476. begin
  1477. { The property symbol is referenced indirect }
  1478. protsym.IncRefCount;
  1479. handle_propertysym(protsym,protsym.owner,p1);
  1480. end;
  1481. end
  1482. else
  1483. begin
  1484. consume(_LECKKLAMMER);
  1485. repeat
  1486. { in all of the cases below, p1 is changed }
  1487. case p1.resultdef.typ of
  1488. pointerdef:
  1489. begin
  1490. { support delphi autoderef }
  1491. if (tpointerdef(p1.resultdef).pointeddef.typ=arraydef) and
  1492. (m_autoderef in current_settings.modeswitches) then
  1493. p1:=cderefnode.create(p1);
  1494. p2:=comp_expr(true,false);
  1495. { Support Pbytevar[0..9] which returns array [0..9].}
  1496. if try_to_consume(_POINTPOINT) then
  1497. p2:=crangenode.create(p2,comp_expr(true,false));
  1498. p1:=cvecnode.create(p1,p2);
  1499. end;
  1500. variantdef:
  1501. begin
  1502. handle_variantarray;
  1503. { the RECKKLAMMER is already read }
  1504. goto skipreckklammercheck;
  1505. end;
  1506. stringdef :
  1507. begin
  1508. p2:=comp_expr(true,false);
  1509. { Support string[0..9] which returns array [0..9] of char.}
  1510. if try_to_consume(_POINTPOINT) then
  1511. p2:=crangenode.create(p2,comp_expr(true,false));
  1512. p1:=cvecnode.create(p1,p2);
  1513. end;
  1514. arraydef:
  1515. begin
  1516. p2:=comp_expr(true,false);
  1517. { support SEG:OFS for go32v2 Mem[] }
  1518. if (target_info.system in [system_i386_go32v2,system_i386_watcom]) and
  1519. (p1.nodetype=loadn) and
  1520. assigned(tloadnode(p1).symtableentry) and
  1521. assigned(tloadnode(p1).symtableentry.owner.name) and
  1522. (tloadnode(p1).symtableentry.owner.name^='SYSTEM') and
  1523. ((tloadnode(p1).symtableentry.name='MEM') or
  1524. (tloadnode(p1).symtableentry.name='MEMW') or
  1525. (tloadnode(p1).symtableentry.name='MEML')) then
  1526. begin
  1527. if try_to_consume(_COLON) then
  1528. begin
  1529. p3:=caddnode.create(muln,cordconstnode.create($10,s32inttype,false),p2);
  1530. p2:=comp_expr(true,false);
  1531. p2:=caddnode.create(addn,p2,p3);
  1532. if try_to_consume(_POINTPOINT) then
  1533. { Support mem[$a000:$0000..$07ff] which returns array [0..$7ff] of memtype.}
  1534. p2:=crangenode.create(p2,caddnode.create(addn,comp_expr(true,false),p3.getcopy));
  1535. p1:=cvecnode.create(p1,p2);
  1536. include(tvecnode(p1).flags,nf_memseg);
  1537. include(tvecnode(p1).flags,nf_memindex);
  1538. end
  1539. else
  1540. begin
  1541. if try_to_consume(_POINTPOINT) then
  1542. { Support mem[$80000000..$80000002] which returns array [0..2] of memtype.}
  1543. p2:=crangenode.create(p2,comp_expr(true,false));
  1544. p1:=cvecnode.create(p1,p2);
  1545. include(tvecnode(p1).flags,nf_memindex);
  1546. end;
  1547. end
  1548. else
  1549. begin
  1550. if try_to_consume(_POINTPOINT) then
  1551. { Support arrayvar[0..9] which returns array [0..9] of arraytype.}
  1552. p2:=crangenode.create(p2,comp_expr(true,false));
  1553. p1:=cvecnode.create(p1,p2);
  1554. end;
  1555. end;
  1556. else
  1557. begin
  1558. if p1.resultdef.typ<>undefineddef then
  1559. Message(parser_e_invalid_qualifier);
  1560. p1.destroy;
  1561. p1:=cerrornode.create;
  1562. comp_expr(true,false);
  1563. again:=false;
  1564. end;
  1565. end;
  1566. do_typecheckpass(p1);
  1567. until not try_to_consume(_COMMA);
  1568. consume(_RECKKLAMMER);
  1569. { handle_variantarray eats the RECKKLAMMER and jumps here }
  1570. skipreckklammercheck:
  1571. end;
  1572. end;
  1573. _POINT :
  1574. begin
  1575. consume(_POINT);
  1576. if (p1.resultdef.typ=pointerdef) and
  1577. (m_autoderef in current_settings.modeswitches) and
  1578. { don't auto-deref objc.id, because then the code
  1579. below for supporting id.anyobjcmethod isn't triggered }
  1580. (p1.resultdef<>objc_idtype) then
  1581. begin
  1582. p1:=cderefnode.create(p1);
  1583. do_typecheckpass(p1);
  1584. end;
  1585. { procvar.<something> can never mean anything so always
  1586. try to call it in case it returns a record/object/... }
  1587. maybe_call_procvar(p1,false);
  1588. case p1.resultdef.typ of
  1589. recorddef:
  1590. begin
  1591. if token=_ID then
  1592. begin
  1593. structh:=tabstractrecorddef(p1.resultdef);
  1594. searchsym_in_record(structh,pattern,srsym,srsymtable);
  1595. if assigned(srsym) then
  1596. begin
  1597. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1598. consume(_ID);
  1599. do_member_read(structh,getaddr,srsym,p1,again,[]);
  1600. end
  1601. else
  1602. begin
  1603. Message1(sym_e_id_no_member,orgpattern);
  1604. p1.destroy;
  1605. p1:=cerrornode.create;
  1606. { try to clean up }
  1607. consume(_ID);
  1608. end;
  1609. end
  1610. else
  1611. consume(_ID);
  1612. end;
  1613. enumdef:
  1614. begin
  1615. if token=_ID then
  1616. begin
  1617. srsym:=tsym(tenumdef(p1.resultdef).symtable.Find(pattern));
  1618. p1.destroy;
  1619. if assigned(srsym) and (srsym.typ=enumsym) then
  1620. begin
  1621. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1622. p1:=genenumnode(tenumsym(srsym));
  1623. end
  1624. else
  1625. begin
  1626. Message1(sym_e_id_no_member,orgpattern);
  1627. p1:=cerrornode.create;
  1628. end;
  1629. end;
  1630. consume(_ID);
  1631. end;
  1632. variantdef:
  1633. begin
  1634. { dispatch call? }
  1635. { lhs := v.ident[parameters] -> property get
  1636. lhs := v.ident(parameters) -> method call
  1637. v.ident[parameters] := rhs -> property put
  1638. v.ident(parameters) := rhs -> also property put }
  1639. if token=_ID then
  1640. begin
  1641. dispatchstring:=orgpattern;
  1642. consume(_ID);
  1643. calltype:=dct_method;
  1644. if try_to_consume(_LKLAMMER) then
  1645. begin
  1646. p2:=parse_paras(false,true,_RKLAMMER);
  1647. consume(_RKLAMMER);
  1648. end
  1649. else if try_to_consume(_LECKKLAMMER) then
  1650. begin
  1651. p2:=parse_paras(false,true,_RECKKLAMMER);
  1652. consume(_RECKKLAMMER);
  1653. calltype:=dct_propget;
  1654. end
  1655. else
  1656. p2:=nil;
  1657. { property setter? }
  1658. if (token=_ASSIGNMENT) and not(afterassignment) then
  1659. begin
  1660. consume(_ASSIGNMENT);
  1661. { read the expression }
  1662. p3:=comp_expr(true,false);
  1663. { concat value parameter too }
  1664. p2:=ccallparanode.create(p3,p2);
  1665. p1:=translate_disp_call(p1,p2,dct_propput,dispatchstring,0,voidtype);
  1666. end
  1667. else
  1668. { this is only an approximation
  1669. setting useresult if not necessary is only a waste of time, no more, no less (FK) }
  1670. if afterassignment or in_args or (token<>_SEMICOLON) then
  1671. p1:=translate_disp_call(p1,p2,calltype,dispatchstring,0,cvarianttype)
  1672. else
  1673. p1:=translate_disp_call(p1,p2,calltype,dispatchstring,0,voidtype);
  1674. end
  1675. else { Error }
  1676. Consume(_ID);
  1677. end;
  1678. classrefdef:
  1679. begin
  1680. if token=_ID then
  1681. begin
  1682. structh:=tobjectdef(tclassrefdef(p1.resultdef).pointeddef);
  1683. searchsym_in_class(tobjectdef(structh),tobjectdef(structh),pattern,srsym,srsymtable,true);
  1684. if assigned(srsym) then
  1685. begin
  1686. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1687. consume(_ID);
  1688. do_member_read(structh,getaddr,srsym,p1,again,[]);
  1689. end
  1690. else
  1691. begin
  1692. Message1(sym_e_id_no_member,orgpattern);
  1693. p1.destroy;
  1694. p1:=cerrornode.create;
  1695. { try to clean up }
  1696. consume(_ID);
  1697. end;
  1698. end
  1699. else { Error }
  1700. Consume(_ID);
  1701. end;
  1702. objectdef:
  1703. begin
  1704. if token=_ID then
  1705. begin
  1706. structh:=tobjectdef(p1.resultdef);
  1707. searchsym_in_class(tobjectdef(structh),tobjectdef(structh),pattern,srsym,srsymtable,true);
  1708. if assigned(srsym) then
  1709. begin
  1710. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1711. consume(_ID);
  1712. do_member_read(structh,getaddr,srsym,p1,again,[]);
  1713. end
  1714. else
  1715. begin
  1716. Message1(sym_e_id_no_member,orgpattern);
  1717. p1.destroy;
  1718. p1:=cerrornode.create;
  1719. { try to clean up }
  1720. consume(_ID);
  1721. end;
  1722. end
  1723. else { Error }
  1724. Consume(_ID);
  1725. end;
  1726. pointerdef:
  1727. begin
  1728. if (p1.resultdef=objc_idtype) then
  1729. begin
  1730. { objc's id type can be used to call any
  1731. Objective-C method of any Objective-C class
  1732. type that's currently in scope }
  1733. if search_objc_method(pattern,srsym,srsymtable) then
  1734. begin
  1735. consume(_ID);
  1736. do_proc_call(srsym,srsymtable,nil,
  1737. (getaddr and not(token in [_CARET,_POINT])),
  1738. again,p1,[cnf_objc_id_call]);
  1739. { we need to know which procedure is called }
  1740. do_typecheckpass(p1);
  1741. end
  1742. else
  1743. begin
  1744. consume(_ID);
  1745. Message(parser_e_methode_id_expected);
  1746. end;
  1747. end
  1748. else
  1749. begin
  1750. Message(parser_e_invalid_qualifier);
  1751. if tpointerdef(p1.resultdef).pointeddef.typ in [recorddef,objectdef,classrefdef] then
  1752. Message(parser_h_maybe_deref_caret_missing);
  1753. end
  1754. end;
  1755. else
  1756. begin
  1757. if p1.resultdef.typ<>undefineddef then
  1758. Message(parser_e_invalid_qualifier);
  1759. p1.destroy;
  1760. p1:=cerrornode.create;
  1761. { Error }
  1762. consume(_ID);
  1763. end;
  1764. end;
  1765. end;
  1766. else
  1767. begin
  1768. { is this a procedure variable ? }
  1769. if assigned(p1.resultdef) and
  1770. (p1.resultdef.typ=procvardef) then
  1771. begin
  1772. { Typenode for typecasting or expecting a procvar }
  1773. if (p1.nodetype=typen) or
  1774. (
  1775. assigned(getprocvardef) and
  1776. equal_defs(p1.resultdef,getprocvardef)
  1777. ) then
  1778. begin
  1779. if try_to_consume(_LKLAMMER) then
  1780. begin
  1781. p1:=comp_expr(true,false);
  1782. consume(_RKLAMMER);
  1783. p1:=ctypeconvnode.create_explicit(p1,p1.resultdef);
  1784. end
  1785. else
  1786. again:=false
  1787. end
  1788. else
  1789. begin
  1790. if try_to_consume(_LKLAMMER) then
  1791. begin
  1792. p2:=parse_paras(false,false,_RKLAMMER);
  1793. consume(_RKLAMMER);
  1794. p1:=ccallnode.create_procvar(p2,p1);
  1795. { proc():= is never possible }
  1796. if token=_ASSIGNMENT then
  1797. begin
  1798. Message(parser_e_illegal_expression);
  1799. p1.free;
  1800. p1:=cerrornode.create;
  1801. again:=false;
  1802. end;
  1803. end
  1804. else
  1805. again:=false;
  1806. end;
  1807. end
  1808. else
  1809. again:=false;
  1810. end;
  1811. end;
  1812. { we only try again if p1 was changed }
  1813. if again or
  1814. (p1.nodetype=errorn) then
  1815. result:=true;
  1816. end; { while again }
  1817. end;
  1818. function is_member_read(sym: tsym; st: tsymtable; var p1: tnode;
  1819. out memberparentdef: tdef): boolean;
  1820. var
  1821. hdef : tdef;
  1822. begin
  1823. result:=true;
  1824. memberparentdef:=nil;
  1825. case st.symtabletype of
  1826. ObjectSymtable,
  1827. recordsymtable:
  1828. begin
  1829. memberparentdef:=tdef(st.defowner);
  1830. exit;
  1831. end;
  1832. WithSymtable:
  1833. begin
  1834. if assigned(p1) then
  1835. internalerror(2007012002);
  1836. hdef:=tnode(twithsymtable(st).withrefnode).resultdef;
  1837. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  1838. if not(hdef.typ in [objectdef,classrefdef]) then
  1839. exit;
  1840. if (hdef.typ=classrefdef) then
  1841. hdef:=tclassrefdef(hdef).pointeddef;
  1842. memberparentdef:=hdef;
  1843. end;
  1844. else
  1845. result:=false;
  1846. end;
  1847. end;
  1848. {$maxfpuregisters 0}
  1849. function factor(getaddr,typeonly:boolean) : tnode;
  1850. {---------------------------------------------
  1851. Factor_read_id
  1852. ---------------------------------------------}
  1853. procedure factor_read_id(out p1:tnode;var again:boolean);
  1854. var
  1855. srsym : tsym;
  1856. srsymtable : TSymtable;
  1857. hdef : tdef;
  1858. orgstoredpattern,
  1859. storedpattern : string;
  1860. callflags: tcallnodeflags;
  1861. t : ttoken;
  1862. unit_found : boolean;
  1863. begin
  1864. { allow post fix operators }
  1865. again:=true;
  1866. { first check for identifier }
  1867. if token<>_ID then
  1868. begin
  1869. srsym:=generrorsym;
  1870. srsymtable:=nil;
  1871. consume(_ID);
  1872. end
  1873. else
  1874. begin
  1875. if typeonly then
  1876. searchsym_type(pattern,srsym,srsymtable)
  1877. else
  1878. searchsym(pattern,srsym,srsymtable);
  1879. { handle unit specification like System.Writeln }
  1880. unit_found:=try_consume_unitsym(srsym,srsymtable,t);
  1881. storedpattern:=pattern;
  1882. orgstoredpattern:=orgpattern;
  1883. consume(t);
  1884. { named parameter support }
  1885. found_arg_name:=false;
  1886. if not(unit_found) and
  1887. named_args_allowed and
  1888. (token=_ASSIGNMENT) then
  1889. begin
  1890. found_arg_name:=true;
  1891. p1:=cstringconstnode.createstr(storedpattern);
  1892. consume(_ASSIGNMENT);
  1893. exit;
  1894. end;
  1895. { check hints, but only if it isn't a potential generic symbol;
  1896. that is checked in sub_expr if it isn't a generic }
  1897. if assigned(srsym) and
  1898. not (
  1899. (srsym.typ=typesym) and
  1900. (ttypesym(srsym).typedef.typ in [recorddef,objectdef,arraydef,procvardef,undefineddef]) and
  1901. not (sp_generic_para in srsym.symoptions) and
  1902. (token in [_LT, _LSHARPBRACKET])
  1903. ) then
  1904. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1905. { if nothing found give error and return errorsym }
  1906. if not assigned(srsym) then
  1907. begin
  1908. identifier_not_found(orgstoredpattern);
  1909. srsym:=generrorsym;
  1910. srsymtable:=nil;
  1911. end;
  1912. end;
  1913. { Access to funcret or need to call the function? }
  1914. if (srsym.typ in [absolutevarsym,localvarsym,paravarsym]) and
  1915. (vo_is_funcret in tabstractvarsym(srsym).varoptions) and
  1916. { result(x) is not allowed }
  1917. not(vo_is_result in tabstractvarsym(srsym).varoptions) and
  1918. (
  1919. (token=_LKLAMMER) or
  1920. (
  1921. (
  1922. (m_tp7 in current_settings.modeswitches) or
  1923. (m_delphi in current_settings.modeswitches)
  1924. ) and
  1925. (afterassignment or in_args)
  1926. )
  1927. ) then
  1928. begin
  1929. hdef:=tdef(srsym.owner.defowner);
  1930. if assigned(hdef) and
  1931. (hdef.typ=procdef) then
  1932. srsym:=tprocdef(hdef).procsym
  1933. else
  1934. begin
  1935. Message(parser_e_illegal_expression);
  1936. srsym:=generrorsym;
  1937. end;
  1938. srsymtable:=srsym.owner;
  1939. end;
  1940. begin
  1941. case srsym.typ of
  1942. absolutevarsym :
  1943. begin
  1944. if (tabsolutevarsym(srsym).abstyp=tovar) then
  1945. begin
  1946. p1:=nil;
  1947. propaccesslist_to_node(p1,nil,tabsolutevarsym(srsym).ref);
  1948. p1:=ctypeconvnode.create(p1,tabsolutevarsym(srsym).vardef);
  1949. include(p1.flags,nf_absolute);
  1950. end
  1951. else
  1952. p1:=cloadnode.create(srsym,srsymtable);
  1953. end;
  1954. staticvarsym,
  1955. localvarsym,
  1956. paravarsym,
  1957. fieldvarsym :
  1958. begin
  1959. { check if we are reading a field of an object/class/ }
  1960. { record. is_member_read() will deal with withsymtables }
  1961. { if needed. }
  1962. p1:=nil;
  1963. if is_member_read(srsym,srsymtable,p1,hdef) then
  1964. begin
  1965. { if the field was originally found in an }
  1966. { objectsymtable, it means it's part of self
  1967. if only method from which it was called is
  1968. not class static }
  1969. if (srsymtable.symtabletype in [ObjectSymtable,recordsymtable]) then
  1970. if assigned(current_procinfo) and current_procinfo.procdef.no_self_node then
  1971. p1:=cloadvmtaddrnode.create(ctypenode.create(current_procinfo.procdef.struct))
  1972. else
  1973. p1:=load_self_node;
  1974. { now, if the field itself is part of an objectsymtab }
  1975. { (it can be even if it was found in a withsymtable, }
  1976. { e.g., "with classinstance do field := 5"), then }
  1977. { let do_member_read handle it }
  1978. if (srsym.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  1979. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,p1,again,[])
  1980. else
  1981. { otherwise it's a regular record subscript }
  1982. p1:=csubscriptnode.create(srsym,p1);
  1983. end
  1984. else
  1985. { regular non-field load }
  1986. p1:=cloadnode.create(srsym,srsymtable);
  1987. end;
  1988. syssym :
  1989. begin
  1990. p1:=statement_syssym(tsyssym(srsym).number);
  1991. end;
  1992. typesym :
  1993. begin
  1994. hdef:=ttypesym(srsym).typedef;
  1995. if not assigned(hdef) then
  1996. begin
  1997. again:=false;
  1998. end
  1999. else
  2000. begin
  2001. { We need to know if this unit uses Variants }
  2002. if (hdef=cvarianttype) and
  2003. not(cs_compilesystem in current_settings.moduleswitches) then
  2004. current_module.flags:=current_module.flags or uf_uses_variants;
  2005. p1:=handle_factor_typenode(hdef,getaddr,again);
  2006. end;
  2007. end;
  2008. enumsym :
  2009. begin
  2010. p1:=genenumnode(tenumsym(srsym));
  2011. end;
  2012. constsym :
  2013. begin
  2014. if tconstsym(srsym).consttyp=constresourcestring then
  2015. begin
  2016. p1:=cloadnode.create(srsym,srsymtable);
  2017. do_typecheckpass(p1);
  2018. p1.resultdef:=cansistringtype;
  2019. end
  2020. else
  2021. p1:=genconstsymtree(tconstsym(srsym));
  2022. end;
  2023. procsym :
  2024. begin
  2025. p1:=nil;
  2026. { check if it's a method/class method }
  2027. if is_member_read(srsym,srsymtable,p1,hdef) then
  2028. begin
  2029. { not srsymtable.symtabletype since that can be }
  2030. { withsymtable as well }
  2031. if (srsym.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  2032. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,p1,again,[])
  2033. else
  2034. { no procsyms in records (yet) }
  2035. internalerror(2007012006);
  2036. end
  2037. else
  2038. begin
  2039. { regular procedure/function call }
  2040. if not unit_found then
  2041. callflags:=[]
  2042. else
  2043. callflags:=[cnf_unit_specified];
  2044. do_proc_call(srsym,srsymtable,nil,
  2045. (getaddr and not(token in [_CARET,_POINT,_LECKKLAMMER])),
  2046. again,p1,callflags);
  2047. end;
  2048. end;
  2049. propertysym :
  2050. begin
  2051. p1:=nil;
  2052. { property of a class/object? }
  2053. if is_member_read(srsym,srsymtable,p1,hdef) then
  2054. begin
  2055. if (srsymtable.symtabletype in [ObjectSymtable,recordsymtable]) then
  2056. if assigned(current_procinfo) and current_procinfo.procdef.no_self_node then
  2057. { no self node in static class methods }
  2058. p1:=cloadvmtaddrnode.create(ctypenode.create(hdef))
  2059. else
  2060. p1:=load_self_node;
  2061. { not srsymtable.symtabletype since that can be }
  2062. { withsymtable as well }
  2063. if (srsym.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  2064. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,p1,again,[])
  2065. else
  2066. { no propertysyms in records (yet) }
  2067. internalerror(2009111510);
  2068. end
  2069. else
  2070. { no method pointer }
  2071. begin
  2072. handle_propertysym(tpropertysym(srsym),srsymtable,p1);
  2073. end;
  2074. end;
  2075. labelsym :
  2076. begin
  2077. { Support @label }
  2078. if getaddr then
  2079. begin
  2080. if srsym.owner<>current_procinfo.procdef.localst then
  2081. CGMessage(parser_e_label_outside_proc);
  2082. p1:=cloadnode.create(srsym,srsym.owner)
  2083. end
  2084. else
  2085. begin
  2086. consume(_COLON);
  2087. if tlabelsym(srsym).defined then
  2088. Message(sym_e_label_already_defined);
  2089. if symtablestack.top.symtablelevel<>srsymtable.symtablelevel then
  2090. begin
  2091. tlabelsym(srsym).nonlocal:=true;
  2092. exclude(current_procinfo.procdef.procoptions,po_inline);
  2093. end;
  2094. if tlabelsym(srsym).nonlocal and
  2095. (current_procinfo.procdef.proctypeoption in [potype_unitinit,potype_unitfinalize]) then
  2096. Message(sym_e_interprocgoto_into_init_final_code_not_allowed);
  2097. tlabelsym(srsym).defined:=true;
  2098. p1:=clabelnode.create(nil,tlabelsym(srsym));
  2099. tlabelsym(srsym).code:=p1;
  2100. end;
  2101. end;
  2102. errorsym :
  2103. begin
  2104. p1:=cerrornode.create;
  2105. if try_to_consume(_LKLAMMER) then
  2106. begin
  2107. parse_paras(false,false,_RKLAMMER);
  2108. consume(_RKLAMMER);
  2109. end;
  2110. end;
  2111. else
  2112. begin
  2113. p1:=cerrornode.create;
  2114. Message(parser_e_illegal_expression);
  2115. end;
  2116. end; { end case }
  2117. end;
  2118. end;
  2119. {---------------------------------------------
  2120. Factor_Read_Set
  2121. ---------------------------------------------}
  2122. { Read a set between [] }
  2123. function factor_read_set:tnode;
  2124. var
  2125. p1,p2 : tnode;
  2126. lastp,
  2127. buildp : tarrayconstructornode;
  2128. old_allow_array_constructor : boolean;
  2129. begin
  2130. buildp:=nil;
  2131. { be sure that a least one arrayconstructn is used, also for an
  2132. empty [] }
  2133. if token=_RECKKLAMMER then
  2134. buildp:=carrayconstructornode.create(nil,buildp)
  2135. else
  2136. repeat
  2137. { nested array constructors are not allowed, see also tests/webtbs/tw17213.pp }
  2138. old_allow_array_constructor:=allow_array_constructor;
  2139. allow_array_constructor:=false;
  2140. p1:=comp_expr(true,false);
  2141. if try_to_consume(_POINTPOINT) then
  2142. begin
  2143. p2:=comp_expr(true,false);
  2144. p1:=carrayconstructorrangenode.create(p1,p2);
  2145. end;
  2146. { insert at the end of the tree, to get the correct order }
  2147. if not assigned(buildp) then
  2148. begin
  2149. buildp:=carrayconstructornode.create(p1,nil);
  2150. lastp:=buildp;
  2151. end
  2152. else
  2153. begin
  2154. lastp.right:=carrayconstructornode.create(p1,nil);
  2155. lastp:=tarrayconstructornode(lastp.right);
  2156. end;
  2157. allow_array_constructor:=old_allow_array_constructor;
  2158. { there could be more elements }
  2159. until not try_to_consume(_COMMA);
  2160. factor_read_set:=buildp;
  2161. end;
  2162. {---------------------------------------------
  2163. Factor (Main)
  2164. ---------------------------------------------}
  2165. var
  2166. l : longint;
  2167. ic : int64;
  2168. qc : qword;
  2169. p1 : tnode;
  2170. code : integer;
  2171. srsym : tsym;
  2172. srsymtable : TSymtable;
  2173. pd : tprocdef;
  2174. hclassdef : tobjectdef;
  2175. d : bestreal;
  2176. cur : currency;
  2177. hs,hsorg : string;
  2178. hdef : tdef;
  2179. filepos : tfileposinfo;
  2180. again,
  2181. updatefpos,
  2182. nodechanged : boolean;
  2183. begin
  2184. { can't keep a copy of p1 and compare pointers afterwards, because
  2185. p1 may be freed and reallocated in the same place! }
  2186. updatefpos:=false;
  2187. p1:=nil;
  2188. filepos:=current_tokenpos;
  2189. again:=false;
  2190. if token=_ID then
  2191. begin
  2192. again:=true;
  2193. { Handle references to self }
  2194. if (idtoken=_SELF) and
  2195. not(block_type in [bt_const,bt_type,bt_const_type,bt_var_type]) and
  2196. assigned(current_structdef) then
  2197. begin
  2198. p1:=load_self_node;
  2199. consume(_ID);
  2200. again:=true;
  2201. end
  2202. else
  2203. factor_read_id(p1,again);
  2204. if assigned(p1) then
  2205. begin
  2206. { factor_read_id will set the filepos to after the id,
  2207. and in case of _SELF the filepos will already be the
  2208. same as filepos (so setting it again doesn't hurt). }
  2209. p1.fileinfo:=filepos;
  2210. filepos:=current_tokenpos;
  2211. end;
  2212. { handle post fix operators }
  2213. updatefpos:=postfixoperators(p1,again,getaddr);
  2214. end
  2215. else
  2216. begin
  2217. updatefpos:=true;
  2218. case token of
  2219. _RETURN :
  2220. begin
  2221. consume(_RETURN);
  2222. if not(token in [_SEMICOLON,_ELSE,_END]) then
  2223. p1 := cexitnode.create(comp_expr(true,false))
  2224. else
  2225. p1 := cexitnode.create(nil);
  2226. end;
  2227. _INHERITED :
  2228. begin
  2229. again:=true;
  2230. consume(_INHERITED);
  2231. if assigned(current_procinfo) and
  2232. assigned(current_structdef) and
  2233. (current_structdef.typ=objectdef) then
  2234. begin
  2235. { for record helpers in mode Delphi "inherited" is not
  2236. allowed }
  2237. if is_objectpascal_helper(current_structdef) and
  2238. (m_delphi in current_settings.modeswitches) and
  2239. is_record(tobjectdef(current_structdef).extendeddef) then
  2240. Message(parser_e_inherited_not_in_record);
  2241. hclassdef:=tobjectdef(current_structdef).childof;
  2242. { Objective-C categories *replace* methods in the class
  2243. they extend, or add methods to it. So calling an
  2244. inherited method always calls the method inherited from
  2245. the parent of the extended class }
  2246. if is_objccategory(current_structdef) then
  2247. hclassdef:=hclassdef.childof;
  2248. { if inherited; only then we need the method with
  2249. the same name }
  2250. if token <> _ID then
  2251. begin
  2252. hs:=current_procinfo.procdef.procsym.name;
  2253. hsorg:=current_procinfo.procdef.procsym.realname;
  2254. anon_inherited:=true;
  2255. { For message methods we need to search using the message
  2256. number or string }
  2257. pd:=tprocdef(tprocsym(current_procinfo.procdef.procsym).ProcdefList[0]);
  2258. srdef:=nil;
  2259. if (po_msgint in pd.procoptions) then
  2260. searchsym_in_class_by_msgint(hclassdef,pd.messageinf.i,srdef,srsym,srsymtable)
  2261. else
  2262. if (po_msgstr in pd.procoptions) then
  2263. searchsym_in_class_by_msgstr(hclassdef,pd.messageinf.str^,srsym,srsymtable)
  2264. else
  2265. { helpers have their own ways of dealing with inherited }
  2266. if is_objectpascal_helper(current_structdef) then
  2267. searchsym_in_helper(tobjectdef(current_structdef),tobjectdef(current_structdef),hs,srsym,srsymtable,true)
  2268. else
  2269. searchsym_in_class(hclassdef,tobjectdef(current_structdef),hs,srsym,srsymtable,true);
  2270. end
  2271. else
  2272. begin
  2273. hs:=pattern;
  2274. hsorg:=orgpattern;
  2275. consume(_ID);
  2276. anon_inherited:=false;
  2277. { helpers have their own ways of dealing with inherited }
  2278. if is_objectpascal_helper(current_structdef) then
  2279. searchsym_in_helper(tobjectdef(current_structdef),tobjectdef(current_structdef),hs,srsym,srsymtable,true)
  2280. else
  2281. searchsym_in_class(hclassdef,tobjectdef(current_structdef),hs,srsym,srsymtable,true);
  2282. end;
  2283. if assigned(srsym) then
  2284. begin
  2285. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  2286. { load the procdef from the inherited class and
  2287. not from self }
  2288. case srsym.typ of
  2289. procsym:
  2290. begin
  2291. if is_objectpascal_helper(current_structdef) then
  2292. begin
  2293. { for a helper load the procdef either from the
  2294. extended type, from the parent helper or from
  2295. the extended type of the parent helper
  2296. depending on the def the found symbol belongs
  2297. to }
  2298. if (srsym.Owner.defowner.typ=objectdef) and
  2299. is_objectpascal_helper(tobjectdef(srsym.Owner.defowner)) then
  2300. if current_structdef.is_related(tdef(srsym.Owner.defowner)) and
  2301. assigned(tobjectdef(current_structdef).childof) then
  2302. hdef:=tobjectdef(current_structdef).childof
  2303. else
  2304. hdef:=tobjectdef(srsym.Owner.defowner).extendeddef
  2305. else
  2306. hdef:=tdef(srsym.Owner.defowner);
  2307. end
  2308. else
  2309. hdef:=hclassdef;
  2310. if (po_classmethod in current_procinfo.procdef.procoptions) or
  2311. (po_staticmethod in current_procinfo.procdef.procoptions) then
  2312. hdef:=tclassrefdef.create(hdef);
  2313. p1:=ctypenode.create(hdef);
  2314. { we need to allow helpers here }
  2315. ttypenode(p1).helperallowed:=true;
  2316. end;
  2317. propertysym:
  2318. ;
  2319. else
  2320. begin
  2321. Message(parser_e_methode_id_expected);
  2322. p1:=cerrornode.create;
  2323. end;
  2324. end;
  2325. do_member_read(hclassdef,getaddr,srsym,p1,again,[cnf_inherited,cnf_anon_inherited]);
  2326. end
  2327. else
  2328. begin
  2329. if anon_inherited then
  2330. begin
  2331. { For message methods we need to call DefaultHandler }
  2332. if (po_msgint in pd.procoptions) or
  2333. (po_msgstr in pd.procoptions) then
  2334. begin
  2335. searchsym_in_class(hclassdef,hclassdef,'DEFAULTHANDLER',srsym,srsymtable,true);
  2336. if not assigned(srsym) or
  2337. (srsym.typ<>procsym) then
  2338. internalerror(200303171);
  2339. p1:=nil;
  2340. do_proc_call(srsym,srsym.owner,hclassdef,false,again,p1,[]);
  2341. end
  2342. else
  2343. begin
  2344. { we need to ignore the inherited; }
  2345. p1:=cnothingnode.create;
  2346. end;
  2347. end
  2348. else
  2349. begin
  2350. Message1(sym_e_id_no_member,hsorg);
  2351. p1:=cerrornode.create;
  2352. end;
  2353. again:=false;
  2354. end;
  2355. { turn auto inheriting off }
  2356. anon_inherited:=false;
  2357. end
  2358. else
  2359. begin
  2360. { in case of records we use a more clear error message }
  2361. if assigned(current_structdef) and
  2362. (current_structdef.typ=recorddef) then
  2363. Message(parser_e_inherited_not_in_record)
  2364. else
  2365. Message(parser_e_generic_methods_only_in_methods);
  2366. again:=false;
  2367. p1:=cerrornode.create;
  2368. end;
  2369. postfixoperators(p1,again,getaddr);
  2370. end;
  2371. _INTCONST :
  2372. begin
  2373. {Try first wether the value fits in an int64.}
  2374. val(pattern,ic,code);
  2375. if code=0 then
  2376. begin
  2377. consume(_INTCONST);
  2378. int_to_type(ic,hdef);
  2379. p1:=cordconstnode.create(ic,hdef,true);
  2380. end
  2381. else
  2382. begin
  2383. { try qword next }
  2384. val(pattern,qc,code);
  2385. if code=0 then
  2386. begin
  2387. consume(_INTCONST);
  2388. int_to_type(qc,hdef);
  2389. p1:=cordconstnode.create(qc,hdef,true);
  2390. end;
  2391. end;
  2392. if code<>0 then
  2393. begin
  2394. { finally float }
  2395. val(pattern,d,code);
  2396. if code<>0 then
  2397. begin
  2398. Message(parser_e_invalid_integer);
  2399. consume(_INTCONST);
  2400. l:=1;
  2401. p1:=cordconstnode.create(l,sinttype,true);
  2402. end
  2403. else
  2404. begin
  2405. consume(_INTCONST);
  2406. p1:=crealconstnode.create(d,pbestrealtype^);
  2407. end;
  2408. end
  2409. else
  2410. { the necessary range checking has already been done by val }
  2411. tordconstnode(p1).rangecheck:=false;
  2412. end;
  2413. _REALNUMBER :
  2414. begin
  2415. val(pattern,d,code);
  2416. if code<>0 then
  2417. begin
  2418. Message(parser_e_error_in_real);
  2419. d:=1.0;
  2420. end;
  2421. consume(_REALNUMBER);
  2422. {$ifdef FPC_REAL2REAL_FIXED}
  2423. if current_settings.fputype=fpu_none then
  2424. Message(parser_e_unsupported_real);
  2425. if (current_settings.minfpconstprec=s32real) and
  2426. (d = single(d)) then
  2427. p1:=crealconstnode.create(d,s32floattype)
  2428. else if (current_settings.minfpconstprec=s64real) and
  2429. (d = double(d)) then
  2430. p1:=crealconstnode.create(d,s64floattype)
  2431. else
  2432. {$endif FPC_REAL2REAL_FIXED}
  2433. p1:=crealconstnode.create(d,pbestrealtype^);
  2434. {$ifdef FPC_HAS_STR_CURRENCY}
  2435. val(pattern,cur,code);
  2436. if code=0 then
  2437. trealconstnode(p1).value_currency:=cur;
  2438. {$endif FPC_HAS_STR_CURRENCY}
  2439. end;
  2440. _STRING :
  2441. begin
  2442. string_dec(hdef,true);
  2443. { STRING can be also a type cast }
  2444. if try_to_consume(_LKLAMMER) then
  2445. begin
  2446. p1:=comp_expr(true,false);
  2447. consume(_RKLAMMER);
  2448. p1:=ctypeconvnode.create_explicit(p1,hdef);
  2449. { handle postfix operators here e.g. string(a)[10] }
  2450. again:=true;
  2451. postfixoperators(p1,again,getaddr);
  2452. end
  2453. else
  2454. p1:=ctypenode.create(hdef);
  2455. end;
  2456. _FILE :
  2457. begin
  2458. hdef:=cfiletype;
  2459. consume(_FILE);
  2460. { FILE can be also a type cast }
  2461. if try_to_consume(_LKLAMMER) then
  2462. begin
  2463. p1:=comp_expr(true,false);
  2464. consume(_RKLAMMER);
  2465. p1:=ctypeconvnode.create_explicit(p1,hdef);
  2466. { handle postfix operators here e.g. string(a)[10] }
  2467. again:=true;
  2468. postfixoperators(p1,again,getaddr);
  2469. end
  2470. else
  2471. begin
  2472. p1:=ctypenode.create(hdef);
  2473. end;
  2474. end;
  2475. _CSTRING :
  2476. begin
  2477. p1:=cstringconstnode.createpchar(ansistring2pchar(cstringpattern),length(cstringpattern));
  2478. consume(_CSTRING);
  2479. end;
  2480. _CCHAR :
  2481. begin
  2482. p1:=cordconstnode.create(ord(pattern[1]),cchartype,true);
  2483. consume(_CCHAR);
  2484. end;
  2485. _CWSTRING:
  2486. begin
  2487. p1:=cstringconstnode.createwstr(patternw);
  2488. consume(_CWSTRING);
  2489. end;
  2490. _CWCHAR:
  2491. begin
  2492. p1:=cordconstnode.create(ord(getcharwidestring(patternw,0)),cwidechartype,true);
  2493. consume(_CWCHAR);
  2494. end;
  2495. _KLAMMERAFFE :
  2496. begin
  2497. consume(_KLAMMERAFFE);
  2498. got_addrn:=true;
  2499. { support both @<x> and @(<x>) }
  2500. if try_to_consume(_LKLAMMER) then
  2501. begin
  2502. p1:=factor(true,false);
  2503. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2504. begin
  2505. again:=true;
  2506. postfixoperators(p1,again,getaddr);
  2507. end
  2508. else
  2509. consume(_RKLAMMER);
  2510. end
  2511. else
  2512. p1:=factor(true,false);
  2513. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2514. begin
  2515. again:=true;
  2516. postfixoperators(p1,again,getaddr);
  2517. end;
  2518. got_addrn:=false;
  2519. p1:=caddrnode.create(p1);
  2520. p1.fileinfo:=filepos;
  2521. if cs_typed_addresses in current_settings.localswitches then
  2522. include(p1.flags,nf_typedaddr);
  2523. { Store the procvar that we are expecting, the
  2524. addrn will use the information to find the correct
  2525. procdef or it will return an error }
  2526. if assigned(getprocvardef) and
  2527. (taddrnode(p1).left.nodetype = loadn) then
  2528. taddrnode(p1).getprocvardef:=getprocvardef;
  2529. end;
  2530. _LKLAMMER :
  2531. begin
  2532. consume(_LKLAMMER);
  2533. p1:=comp_expr(true,false);
  2534. consume(_RKLAMMER);
  2535. { it's not a good solution }
  2536. { but (a+b)^ makes some problems }
  2537. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2538. begin
  2539. again:=true;
  2540. postfixoperators(p1,again,getaddr);
  2541. end;
  2542. end;
  2543. _LECKKLAMMER :
  2544. begin
  2545. consume(_LECKKLAMMER);
  2546. p1:=factor_read_set;
  2547. consume(_RECKKLAMMER);
  2548. end;
  2549. _PLUS :
  2550. begin
  2551. consume(_PLUS);
  2552. p1:=factor(false,false);
  2553. p1:=cunaryplusnode.create(p1);
  2554. end;
  2555. _MINUS :
  2556. begin
  2557. consume(_MINUS);
  2558. if (token = _INTCONST) and not(m_isolike_unary_minus in current_settings.modeswitches) then
  2559. begin
  2560. { ugly hack, but necessary to be able to parse }
  2561. { -9223372036854775808 as int64 (JM) }
  2562. pattern := '-'+pattern;
  2563. p1:=sub_expr(oppower,false,false);
  2564. { -1 ** 4 should be - (1 ** 4) and not
  2565. (-1) ** 4
  2566. This was the reason of tw0869.pp test failure PM }
  2567. if p1.nodetype=starstarn then
  2568. begin
  2569. if tbinarynode(p1).left.nodetype=ordconstn then
  2570. begin
  2571. tordconstnode(tbinarynode(p1).left).value:=-tordconstnode(tbinarynode(p1).left).value;
  2572. p1:=cunaryminusnode.create(p1);
  2573. end
  2574. else if tbinarynode(p1).left.nodetype=realconstn then
  2575. begin
  2576. trealconstnode(tbinarynode(p1).left).value_real:=-trealconstnode(tbinarynode(p1).left).value_real;
  2577. trealconstnode(tbinarynode(p1).left).value_currency:=-trealconstnode(tbinarynode(p1).left).value_currency;
  2578. p1:=cunaryminusnode.create(p1);
  2579. end
  2580. else
  2581. internalerror(20021029);
  2582. end;
  2583. end
  2584. else
  2585. begin
  2586. if m_isolike_unary_minus in current_settings.modeswitches then
  2587. p1:=sub_expr(opmultiply,false,false)
  2588. else
  2589. p1:=sub_expr(oppower,false,false);
  2590. p1:=cunaryminusnode.create(p1);
  2591. end;
  2592. end;
  2593. _OP_NOT :
  2594. begin
  2595. consume(_OP_NOT);
  2596. p1:=factor(false,false);
  2597. p1:=cnotnode.create(p1);
  2598. end;
  2599. _TRUE :
  2600. begin
  2601. consume(_TRUE);
  2602. p1:=cordconstnode.create(1,booltype,false);
  2603. end;
  2604. _FALSE :
  2605. begin
  2606. consume(_FALSE);
  2607. p1:=cordconstnode.create(0,booltype,false);
  2608. end;
  2609. _NIL :
  2610. begin
  2611. consume(_NIL);
  2612. p1:=cnilnode.create;
  2613. { It's really ugly code nil^, but delphi allows it }
  2614. if token in [_CARET] then
  2615. begin
  2616. again:=true;
  2617. postfixoperators(p1,again,getaddr);
  2618. end;
  2619. end;
  2620. _OBJCPROTOCOL:
  2621. begin
  2622. { The @protocol keyword is used in two ways in Objective-C:
  2623. 1) to declare protocols (~ Object Pascal interfaces)
  2624. 2) to obtain the metaclass (~ Object Pascal) "class of")
  2625. of a declared protocol
  2626. This code is for handling the second case. Because of 1),
  2627. we cannot simply use a system unit symbol.
  2628. }
  2629. consume(_OBJCPROTOCOL);
  2630. consume(_LKLAMMER);
  2631. p1:=factor(false,false);
  2632. consume(_RKLAMMER);
  2633. p1:=cinlinenode.create(in_objc_protocol_x,false,p1);
  2634. end;
  2635. else
  2636. begin
  2637. Message(parser_e_illegal_expression);
  2638. p1:=cerrornode.create;
  2639. { recover }
  2640. consume(token);
  2641. end;
  2642. end;
  2643. end;
  2644. { generate error node if no node is created }
  2645. if not assigned(p1) then
  2646. begin
  2647. {$ifdef EXTDEBUG}
  2648. Comment(V_Warning,'factor: p1=nil');
  2649. {$endif}
  2650. p1:=cerrornode.create;
  2651. updatefpos:=true;
  2652. end;
  2653. { get the resultdef for the node }
  2654. if (not assigned(p1.resultdef)) then
  2655. begin
  2656. do_typecheckpass_changed(p1,nodechanged);
  2657. updatefpos:=updatefpos or nodechanged;
  2658. end;
  2659. if assigned(p1) and
  2660. updatefpos then
  2661. p1.fileinfo:=filepos;
  2662. factor:=p1;
  2663. end;
  2664. {$maxfpuregisters default}
  2665. {****************************************************************************
  2666. Sub_Expr
  2667. ****************************************************************************}
  2668. const
  2669. { Warning these stay be ordered !! }
  2670. operator_levels:array[Toperator_precedence] of set of NOTOKEN..last_operator=
  2671. ([_LT,_LTE,_GT,_GTE,_EQ,_NE,_OP_IN],
  2672. [_PLUS,_MINUS,_OP_OR,_PIPE,_OP_XOR],
  2673. [_CARET,_SYMDIF,_STARSTAR,_STAR,_SLASH,
  2674. _OP_AS,_OP_IS,_OP_AND,_AMPERSAND,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR],
  2675. [_STARSTAR] );
  2676. function sub_expr(pred_level:Toperator_precedence;accept_equal,typeonly:boolean):tnode;
  2677. {Reads a subexpression while the operators are of the current precedence
  2678. level, or any higher level. Replaces the old term, simpl_expr and
  2679. simpl2_expr.}
  2680. function istypenode(n:tnode):boolean;inline;
  2681. { Checks whether the given node is a type node or a VMT node containing a
  2682. typenode. This is used in the code for inline specializations in the
  2683. _LT branch below }
  2684. begin
  2685. result:=assigned(n) and
  2686. (
  2687. (n.nodetype=typen) or
  2688. (
  2689. (n.nodetype=loadvmtaddrn) and
  2690. (tloadvmtaddrnode(n).left.nodetype=typen)
  2691. )
  2692. );
  2693. end;
  2694. function gettypedef(n:tnode):tdef;inline;
  2695. { This returns the typedef that belongs to the given typenode or
  2696. loadvmtaddrnode. n must not be Nil! }
  2697. begin
  2698. if n.nodetype=typen then
  2699. result:=ttypenode(n).typedef
  2700. else
  2701. result:=ttypenode(tloadvmtaddrnode(n).left).typedef;
  2702. end;
  2703. var
  2704. p1,p2 : tnode;
  2705. oldt : Ttoken;
  2706. filepos : tfileposinfo;
  2707. again,
  2708. isgeneric : boolean;
  2709. gendef,parseddef : tdef;
  2710. begin
  2711. if pred_level=highest_precedence then
  2712. p1:=factor(false,typeonly)
  2713. else
  2714. p1:=sub_expr(succ(pred_level),true,typeonly);
  2715. repeat
  2716. if (token in [NOTOKEN..last_operator]) and
  2717. (token in operator_levels[pred_level]) and
  2718. ((token<>_EQ) or accept_equal) then
  2719. begin
  2720. oldt:=token;
  2721. filepos:=current_tokenpos;
  2722. consume(token);
  2723. if pred_level=highest_precedence then
  2724. p2:=factor(false,false)
  2725. else
  2726. p2:=sub_expr(succ(pred_level),true,typeonly);
  2727. case oldt of
  2728. _PLUS :
  2729. p1:=caddnode.create(addn,p1,p2);
  2730. _MINUS :
  2731. p1:=caddnode.create(subn,p1,p2);
  2732. _STAR :
  2733. p1:=caddnode.create(muln,p1,p2);
  2734. _SLASH :
  2735. p1:=caddnode.create(slashn,p1,p2);
  2736. _EQ:
  2737. p1:=caddnode.create(equaln,p1,p2);
  2738. _GT :
  2739. p1:=caddnode.create(gtn,p1,p2);
  2740. _LT :
  2741. begin
  2742. isgeneric:=false;
  2743. if istypenode(p1) and istypenode(p2) and
  2744. (m_delphi in current_settings.modeswitches) and
  2745. (token in [_GT,_RSHARPBRACKET,_COMMA]) then
  2746. begin
  2747. { this is an inline specialization }
  2748. { retrieve the defs of two nodes }
  2749. gendef:=gettypedef(p1);
  2750. parseddef:=gettypedef(p2);
  2751. if gendef.typesym.typ<>typesym then
  2752. Internalerror(2011050301);
  2753. if parseddef.typesym.typ<>typesym then
  2754. Internalerror(2011051001);
  2755. { check the hints for parseddef }
  2756. check_hints(parseddef.typesym,parseddef.typesym.symoptions,parseddef.typesym.deprecatedmsg);
  2757. { generate the specialization }
  2758. generate_specialization(gendef,false,parseddef);
  2759. isgeneric:=true;
  2760. end;
  2761. if not isgeneric then
  2762. begin
  2763. { for potential generic types that are followed by a "<"
  2764. the hints are not checked }
  2765. if istypenode(p1) then
  2766. begin
  2767. gendef:=gettypedef(p1);
  2768. if gendef.typ in [objectdef,recorddef,arraydef,procvardef] then
  2769. check_hints(gendef.typesym,gendef.typesym.symoptions,gendef.typesym.deprecatedmsg);
  2770. end;
  2771. if istypenode(p2) and
  2772. (token in [_LT, _LSHARPBRACKET]) then
  2773. begin
  2774. gendef:=gettypedef(p2);
  2775. if gendef.typ in [objectdef,recorddef,arraydef,procvardef] then
  2776. check_hints(gendef.typesym,gendef.typesym.symoptions,gendef.typesym.deprecatedmsg);
  2777. end;
  2778. { create the comparison node for "<" }
  2779. p1:=caddnode.create(ltn,p1,p2)
  2780. end
  2781. else
  2782. begin
  2783. { we don't need the old left and right nodes anymore }
  2784. p1.Free;
  2785. p2.Free;
  2786. { in case of a class or a record the specialized generic
  2787. is always a classrefdef }
  2788. if is_class_or_interface_or_object(gendef) or
  2789. is_record(gendef) then
  2790. gendef:=tclassrefdef.create(gendef);
  2791. again:=false;
  2792. { handle potential typecasts, etc }
  2793. p1:=handle_factor_typenode(gendef,false,again);
  2794. { parse postfix operators }
  2795. if postfixoperators(p1,again,false) then
  2796. if assigned(p1) then
  2797. p1.fileinfo:=filepos
  2798. else
  2799. p1:=cerrornode.create;
  2800. end;
  2801. end;
  2802. _GTE :
  2803. p1:=caddnode.create(gten,p1,p2);
  2804. _LTE :
  2805. p1:=caddnode.create(lten,p1,p2);
  2806. _SYMDIF :
  2807. p1:=caddnode.create(symdifn,p1,p2);
  2808. _STARSTAR :
  2809. p1:=caddnode.create(starstarn,p1,p2);
  2810. _OP_AS :
  2811. p1:=casnode.create(p1,p2);
  2812. _OP_IN :
  2813. p1:=cinnode.create(p1,p2);
  2814. _OP_IS :
  2815. p1:=cisnode.create(p1,p2);
  2816. _OP_OR,
  2817. _PIPE {macpas only} :
  2818. begin
  2819. p1:=caddnode.create(orn,p1,p2);
  2820. if (oldt = _PIPE) then
  2821. include(p1.flags,nf_short_bool);
  2822. end;
  2823. _OP_AND,
  2824. _AMPERSAND {macpas only} :
  2825. begin
  2826. p1:=caddnode.create(andn,p1,p2);
  2827. if (oldt = _AMPERSAND) then
  2828. include(p1.flags,nf_short_bool);
  2829. end;
  2830. _OP_DIV :
  2831. p1:=cmoddivnode.create(divn,p1,p2);
  2832. _OP_NOT :
  2833. p1:=cnotnode.create(p1);
  2834. _OP_MOD :
  2835. p1:=cmoddivnode.create(modn,p1,p2);
  2836. _OP_SHL :
  2837. p1:=cshlshrnode.create(shln,p1,p2);
  2838. _OP_SHR :
  2839. p1:=cshlshrnode.create(shrn,p1,p2);
  2840. _OP_XOR :
  2841. p1:=caddnode.create(xorn,p1,p2);
  2842. _ASSIGNMENT :
  2843. p1:=cassignmentnode.create(p1,p2);
  2844. _NE :
  2845. p1:=caddnode.create(unequaln,p1,p2);
  2846. end;
  2847. p1.fileinfo:=filepos;
  2848. end
  2849. else
  2850. break;
  2851. until false;
  2852. sub_expr:=p1;
  2853. end;
  2854. function comp_expr(accept_equal,typeonly:boolean):tnode;
  2855. var
  2856. oldafterassignment : boolean;
  2857. p1 : tnode;
  2858. begin
  2859. oldafterassignment:=afterassignment;
  2860. afterassignment:=true;
  2861. p1:=sub_expr(opcompare,accept_equal,typeonly);
  2862. { get the resultdef for this expression }
  2863. if not assigned(p1.resultdef) then
  2864. do_typecheckpass(p1);
  2865. afterassignment:=oldafterassignment;
  2866. comp_expr:=p1;
  2867. end;
  2868. function expr(dotypecheck : boolean) : tnode;
  2869. var
  2870. p1,p2 : tnode;
  2871. filepos : tfileposinfo;
  2872. oldafterassignment,
  2873. updatefpos : boolean;
  2874. begin
  2875. oldafterassignment:=afterassignment;
  2876. p1:=sub_expr(opcompare,true,false);
  2877. { get the resultdef for this expression }
  2878. if not assigned(p1.resultdef) and
  2879. dotypecheck then
  2880. do_typecheckpass(p1);
  2881. filepos:=current_tokenpos;
  2882. if token in [_ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  2883. afterassignment:=true;
  2884. updatefpos:=true;
  2885. case token of
  2886. _POINTPOINT :
  2887. begin
  2888. consume(_POINTPOINT);
  2889. p2:=sub_expr(opcompare,true,false);
  2890. p1:=crangenode.create(p1,p2);
  2891. end;
  2892. _ASSIGNMENT :
  2893. begin
  2894. consume(_ASSIGNMENT);
  2895. if (p1.resultdef.typ=procvardef) then
  2896. getprocvardef:=tprocvardef(p1.resultdef);
  2897. p2:=sub_expr(opcompare,true,false);
  2898. if assigned(getprocvardef) then
  2899. handle_procvar(getprocvardef,p2);
  2900. getprocvardef:=nil;
  2901. p1:=cassignmentnode.create(p1,p2);
  2902. end;
  2903. _PLUSASN :
  2904. begin
  2905. consume(_PLUSASN);
  2906. p2:=sub_expr(opcompare,true,false);
  2907. p1:=gen_c_style_operator(addn,p1,p2);
  2908. end;
  2909. _MINUSASN :
  2910. begin
  2911. consume(_MINUSASN);
  2912. p2:=sub_expr(opcompare,true,false);
  2913. p1:=gen_c_style_operator(subn,p1,p2);
  2914. end;
  2915. _STARASN :
  2916. begin
  2917. consume(_STARASN );
  2918. p2:=sub_expr(opcompare,true,false);
  2919. p1:=gen_c_style_operator(muln,p1,p2);
  2920. end;
  2921. _SLASHASN :
  2922. begin
  2923. consume(_SLASHASN );
  2924. p2:=sub_expr(opcompare,true,false);
  2925. p1:=gen_c_style_operator(slashn,p1,p2);
  2926. end;
  2927. else
  2928. updatefpos:=false;
  2929. end;
  2930. { get the resultdef for this expression }
  2931. if not assigned(p1.resultdef) and
  2932. dotypecheck then
  2933. do_typecheckpass(p1);
  2934. afterassignment:=oldafterassignment;
  2935. if updatefpos then
  2936. p1.fileinfo:=filepos;
  2937. expr:=p1;
  2938. end;
  2939. function get_intconst:TConstExprInt;
  2940. {Reads an expression, tries to evalute it and check if it is an integer
  2941. constant. Then the constant is returned.}
  2942. var
  2943. p:tnode;
  2944. begin
  2945. result:=0;
  2946. p:=comp_expr(true,false);
  2947. if not codegenerror then
  2948. begin
  2949. if (p.nodetype<>ordconstn) or
  2950. not(is_integer(p.resultdef)) then
  2951. Message(parser_e_illegal_expression)
  2952. else
  2953. result:=tordconstnode(p).value;
  2954. end;
  2955. p.free;
  2956. end;
  2957. function get_stringconst:string;
  2958. {Reads an expression, tries to evaluate it and checks if it is a string
  2959. constant. Then the constant is returned.}
  2960. var
  2961. p:tnode;
  2962. begin
  2963. get_stringconst:='';
  2964. p:=comp_expr(true,false);
  2965. if p.nodetype<>stringconstn then
  2966. begin
  2967. if (p.nodetype=ordconstn) and is_char(p.resultdef) then
  2968. get_stringconst:=char(int64(tordconstnode(p).value))
  2969. else
  2970. Message(parser_e_illegal_expression);
  2971. end
  2972. else
  2973. get_stringconst:=strpas(tstringconstnode(p).value_str);
  2974. p.free;
  2975. end;
  2976. end.