pexpr.pas 118 KB

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