pexpr.pas 98 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701
  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;
  24. { reads a whole expression }
  25. function expr : tnode;
  26. { reads an expression without assignements and .. }
  27. function comp_expr(accept_equal : boolean):tnode;
  28. { reads a single factor }
  29. function factor(getaddr : boolean) : tnode;
  30. procedure string_dec(var t: ttype);
  31. procedure symlist_to_node(var p1:tnode;st:tsymtable;pl:tsymlist);
  32. function node_to_symlist(p1:tnode):tsymlist;
  33. function parse_paras(__colon : boolean;end_of_paras : ttoken) : tnode;
  34. { the ID token has to be consumed before calling this function }
  35. procedure do_member_read(classh:tobjectdef;getaddr : boolean;sym : tsym;var p1 : tnode;var again : boolean;callflags:tcallnodeflags);
  36. function get_intconst:TConstExprInt;
  37. function get_stringconst:string;
  38. implementation
  39. uses
  40. { common }
  41. cutils,
  42. { global }
  43. verbose,
  44. systems,widestr,
  45. { symtable }
  46. symconst,symtable,symsym,defutil,defcmp,
  47. { module }
  48. fmodule,ppu,
  49. { pass 1 }
  50. pass_1,htypechk,
  51. nmat,nadd,nmem,nset,ncnv,ninl,ncon,nld,nflw,nbas,nutils,
  52. { parser }
  53. scanner,
  54. pbase,pinline,
  55. { codegen }
  56. cgbase,procinfo,cpuinfo
  57. ;
  58. { sub_expr(opmultiply) is need to get -1 ** 4 to be
  59. read as - (1**4) and not (-1)**4 PM }
  60. type
  61. Toperator_precedence=(opcompare,opaddition,opmultiply,oppower);
  62. const
  63. highest_precedence = oppower;
  64. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):tnode;forward;
  65. const
  66. { true, if the inherited call is anonymous }
  67. anon_inherited : boolean = false;
  68. procedure string_dec(var t: ttype);
  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. t:=cshortstringtype;
  76. consume(_STRING);
  77. if try_to_consume(_LECKKLAMMER) then
  78. begin
  79. p:=comp_expr(true);
  80. if not is_constintnode(p) then
  81. begin
  82. Message(parser_e_illegal_expression);
  83. { error recovery }
  84. consume(_RECKKLAMMER);
  85. end
  86. else
  87. begin
  88. if (tordconstnode(p).value<=0) then
  89. begin
  90. Message(parser_e_invalid_string_size);
  91. tordconstnode(p).value:=255;
  92. end;
  93. consume(_RECKKLAMMER);
  94. if tordconstnode(p).value>255 then
  95. begin
  96. { longstring is currently unsupported (CEC)! }
  97. { t.setdef(tstringdef.createlong(tordconstnode(p).value))}
  98. Message(parser_e_invalid_string_size);
  99. tordconstnode(p).value:=255;
  100. t.setdef(tstringdef.createshort(tordconstnode(p).value));
  101. end
  102. else
  103. if tordconstnode(p).value<>255 then
  104. t.setdef(tstringdef.createshort(tordconstnode(p).value));
  105. end;
  106. p.free;
  107. end
  108. else
  109. begin
  110. if cs_ansistrings in aktlocalswitches then
  111. {$ifdef ansistring_bits}
  112. case aktansistring_bits of
  113. sb_16:
  114. t:=cansistringtype16;
  115. sb_32:
  116. t:=cansistringtype32;
  117. sb_64:
  118. t:=cansistringtype64;
  119. end
  120. {$else}
  121. t:=cansistringtype
  122. {$endif}
  123. else
  124. t:=cshortstringtype;
  125. end;
  126. end;
  127. procedure symlist_to_node(var p1:tnode;st:tsymtable;pl:tsymlist);
  128. var
  129. plist : psymlistitem;
  130. begin
  131. plist:=pl.firstsym;
  132. while assigned(plist) do
  133. begin
  134. case plist^.sltype of
  135. sl_load :
  136. begin
  137. if not assigned(st) then
  138. st:=plist^.sym.owner;
  139. { p1 can already contain the loadnode of
  140. the class variable. When there is no tree yet we
  141. may need to load it for with or objects }
  142. if not assigned(p1) then
  143. begin
  144. case st.symtabletype of
  145. withsymtable :
  146. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  147. objectsymtable :
  148. p1:=load_self_node;
  149. end;
  150. end;
  151. if assigned(p1) then
  152. p1:=csubscriptnode.create(plist^.sym,p1)
  153. else
  154. p1:=cloadnode.create(plist^.sym,st);
  155. end;
  156. sl_subscript :
  157. p1:=csubscriptnode.create(plist^.sym,p1);
  158. sl_typeconv :
  159. p1:=ctypeconvnode.create_explicit(p1,plist^.tt);
  160. sl_absolutetype :
  161. begin
  162. p1:=ctypeconvnode.create(p1,plist^.tt);
  163. include(p1.flags,nf_absolute);
  164. end;
  165. sl_vec :
  166. p1:=cvecnode.create(p1,cordconstnode.create(plist^.value,plist^.valuett,true));
  167. else
  168. internalerror(200110205);
  169. end;
  170. plist:=plist^.next;
  171. end;
  172. end;
  173. function node_to_symlist(p1:tnode):tsymlist;
  174. var
  175. sl : tsymlist;
  176. procedure addnode(p:tnode);
  177. begin
  178. case p.nodetype of
  179. subscriptn :
  180. begin
  181. addnode(tsubscriptnode(p).left);
  182. sl.addsym(sl_subscript,tsubscriptnode(p).vs);
  183. end;
  184. typeconvn :
  185. begin
  186. addnode(ttypeconvnode(p).left);
  187. if nf_absolute in ttypeconvnode(p).flags then
  188. sl.addtype(sl_absolutetype,ttypeconvnode(p).totype)
  189. else
  190. sl.addtype(sl_typeconv,ttypeconvnode(p).totype);
  191. end;
  192. vecn :
  193. begin
  194. addnode(tvecnode(p).left);
  195. if tvecnode(p).right.nodetype=ordconstn then
  196. sl.addconst(sl_vec,tordconstnode(tvecnode(p).right).value,tvecnode(p).right.resulttype)
  197. else
  198. begin
  199. Message(parser_e_illegal_expression);
  200. { recovery }
  201. sl.addconst(sl_vec,0,tvecnode(p).right.resulttype);
  202. end;
  203. end;
  204. loadn :
  205. sl.addsym(sl_load,tloadnode(p).symtableentry);
  206. else
  207. internalerror(200310282);
  208. end;
  209. end;
  210. begin
  211. sl:=tsymlist.create;
  212. addnode(p1);
  213. result:=sl;
  214. end;
  215. function parse_paras(__colon : boolean;end_of_paras : ttoken) : tnode;
  216. var
  217. p1,p2 : tnode;
  218. prev_in_args : boolean;
  219. old_allow_array_constructor : boolean;
  220. begin
  221. if token=end_of_paras then
  222. begin
  223. parse_paras:=nil;
  224. exit;
  225. end;
  226. { save old values }
  227. prev_in_args:=in_args;
  228. old_allow_array_constructor:=allow_array_constructor;
  229. { set para parsing values }
  230. in_args:=true;
  231. inc(parsing_para_level);
  232. allow_array_constructor:=true;
  233. p2:=nil;
  234. repeat
  235. p1:=comp_expr(true);
  236. p2:=ccallparanode.create(p1,p2);
  237. { it's for the str(l:5,s); }
  238. if __colon and (token=_COLON) then
  239. begin
  240. consume(_COLON);
  241. p1:=comp_expr(true);
  242. p2:=ccallparanode.create(p1,p2);
  243. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  244. if try_to_consume(_COLON) then
  245. begin
  246. p1:=comp_expr(true);
  247. p2:=ccallparanode.create(p1,p2);
  248. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  249. end
  250. end;
  251. until not try_to_consume(_COMMA);
  252. allow_array_constructor:=old_allow_array_constructor;
  253. dec(parsing_para_level);
  254. in_args:=prev_in_args;
  255. parse_paras:=p2;
  256. end;
  257. function gen_c_style_operator(ntyp:tnodetype;p1,p2:tnode) : tnode;
  258. var
  259. hp : tnode;
  260. htype : ttype;
  261. temp : ttempcreatenode;
  262. newstatement : tstatementnode;
  263. begin
  264. { Properties are not allowed, because the write can
  265. be different from the read }
  266. if (nf_isproperty in p1.flags) then
  267. begin
  268. Message(type_e_variable_id_expected);
  269. { We can continue with the loading,
  270. it'll not create errors. Only the expected
  271. result can be wrong }
  272. end;
  273. hp:=p1;
  274. while assigned(hp) and
  275. (hp.nodetype in [derefn,subscriptn,vecn,typeconvn]) do
  276. hp:=tunarynode(hp).left;
  277. if not assigned(hp) then
  278. internalerror(200410121);
  279. if (hp.nodetype=calln) then
  280. begin
  281. resulttypepass(p1);
  282. result:=internalstatements(newstatement);
  283. htype.setdef(tpointerdef.create(p1.resulttype));
  284. temp:=ctempcreatenode.create(htype,sizeof(aint),tt_persistent,false);
  285. addstatement(newstatement,temp);
  286. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(temp),caddrnode.create_internal(p1)));
  287. addstatement(newstatement,cassignmentnode.create(
  288. cderefnode.create(ctemprefnode.create(temp)),
  289. caddnode.create(ntyp,
  290. cderefnode.create(ctemprefnode.create(temp)),
  291. p2)));
  292. addstatement(newstatement,ctempdeletenode.create(temp));
  293. end
  294. else
  295. result:=cassignmentnode.create(p1,caddnode.create(ntyp,p1.getcopy,p2));
  296. end;
  297. function statement_syssym(l : longint) : tnode;
  298. var
  299. p1,p2,paras : tnode;
  300. err,
  301. prev_in_args : boolean;
  302. begin
  303. prev_in_args:=in_args;
  304. case l of
  305. in_new_x :
  306. begin
  307. if afterassignment or in_args then
  308. statement_syssym:=new_function
  309. else
  310. statement_syssym:=new_dispose_statement(true);
  311. end;
  312. in_dispose_x :
  313. begin
  314. statement_syssym:=new_dispose_statement(false);
  315. end;
  316. in_ord_x :
  317. begin
  318. consume(_LKLAMMER);
  319. in_args:=true;
  320. p1:=comp_expr(true);
  321. consume(_RKLAMMER);
  322. p1:=geninlinenode(in_ord_x,false,p1);
  323. statement_syssym := p1;
  324. end;
  325. in_exit :
  326. begin
  327. if try_to_consume(_LKLAMMER) then
  328. begin
  329. if not (m_mac in aktmodeswitches) then
  330. begin
  331. if not(try_to_consume(_RKLAMMER)) then
  332. begin
  333. p1:=comp_expr(true);
  334. consume(_RKLAMMER);
  335. if (block_type=bt_except) then
  336. begin
  337. Message(parser_e_exit_with_argument_not__possible);
  338. { recovery }
  339. p1.free;
  340. p1:=nil;
  341. end
  342. else if (not assigned(current_procinfo) or
  343. is_void(current_procinfo.procdef.rettype.def)) then
  344. begin
  345. Message(parser_e_void_function);
  346. { recovery }
  347. p1.free;
  348. p1:=nil;
  349. end;
  350. end
  351. else
  352. p1:=nil;
  353. end
  354. else
  355. begin
  356. if not (current_procinfo.procdef.procsym.name = pattern) then
  357. Message(parser_e_macpas_exit_wrong_param);
  358. consume(_ID);
  359. consume(_RKLAMMER);
  360. p1:=nil;
  361. end
  362. end
  363. else
  364. p1:=nil;
  365. statement_syssym:=cexitnode.create(p1);
  366. end;
  367. in_break :
  368. begin
  369. if not (m_mac in aktmodeswitches) then
  370. statement_syssym:=cbreaknode.create
  371. else
  372. begin
  373. Message1(sym_e_id_not_found, orgpattern);
  374. statement_syssym:=cerrornode.create;
  375. end;
  376. end;
  377. in_continue :
  378. begin
  379. if not (m_mac in aktmodeswitches) then
  380. statement_syssym:=ccontinuenode.create
  381. else
  382. begin
  383. Message1(sym_e_id_not_found, orgpattern);
  384. statement_syssym:=cerrornode.create;
  385. end;
  386. end;
  387. in_leave :
  388. begin
  389. if m_mac in aktmodeswitches then
  390. statement_syssym:=cbreaknode.create
  391. else
  392. begin
  393. Message1(sym_e_id_not_found, orgpattern);
  394. statement_syssym:=cerrornode.create;
  395. end;
  396. end;
  397. in_cycle :
  398. begin
  399. if m_mac in aktmodeswitches then
  400. statement_syssym:=ccontinuenode.create
  401. else
  402. begin
  403. Message1(sym_e_id_not_found, orgpattern);
  404. statement_syssym:=cerrornode.create;
  405. end;
  406. end;
  407. in_typeof_x :
  408. begin
  409. consume(_LKLAMMER);
  410. in_args:=true;
  411. p1:=comp_expr(true);
  412. consume(_RKLAMMER);
  413. if p1.nodetype=typen then
  414. ttypenode(p1).allowed:=true;
  415. { Allow classrefdef, which is required for
  416. Typeof(self) in static class methods }
  417. if (p1.resulttype.def.deftype = objectdef) or
  418. (assigned(current_procinfo) and
  419. ((po_classmethod in current_procinfo.procdef.procoptions) or
  420. (po_staticmethod in current_procinfo.procdef.procoptions)) and
  421. (p1.resulttype.def.deftype=classrefdef)) then
  422. statement_syssym:=geninlinenode(in_typeof_x,false,p1)
  423. else
  424. begin
  425. Message(parser_e_class_id_expected);
  426. p1.destroy;
  427. statement_syssym:=cerrornode.create;
  428. end;
  429. end;
  430. in_sizeof_x :
  431. begin
  432. consume(_LKLAMMER);
  433. in_args:=true;
  434. p1:=comp_expr(true);
  435. consume(_RKLAMMER);
  436. if (p1.nodetype<>typen) and
  437. (
  438. (is_object(p1.resulttype.def) and
  439. (oo_has_constructor in tobjectdef(p1.resulttype.def).objectoptions)) or
  440. is_open_array(p1.resulttype.def) or
  441. is_array_of_const(p1.resulttype.def) or
  442. is_open_string(p1.resulttype.def)
  443. ) then
  444. statement_syssym:=geninlinenode(in_sizeof_x,false,p1)
  445. else
  446. begin
  447. statement_syssym:=cordconstnode.create(p1.resulttype.def.size,sinttype,true);
  448. { p1 not needed !}
  449. p1.destroy;
  450. end;
  451. end;
  452. in_typeinfo_x :
  453. begin
  454. consume(_LKLAMMER);
  455. in_args:=true;
  456. p1:=comp_expr(true);
  457. { When reading a class type it is parsed as loadvmtaddrn,
  458. typeinfo only needs the type so we remove the loadvmtaddrn }
  459. if p1.nodetype=loadvmtaddrn then
  460. begin
  461. p2:=tloadvmtaddrnode(p1).left;
  462. tloadvmtaddrnode(p1).left:=nil;
  463. p1.free;
  464. p1:=p2;
  465. end;
  466. if p1.nodetype=typen then
  467. ttypenode(p1).allowed:=true
  468. else
  469. begin
  470. p1.destroy;
  471. p1:=cerrornode.create;
  472. Message(parser_e_illegal_parameter_list);
  473. end;
  474. consume(_RKLAMMER);
  475. p2:=geninlinenode(in_typeinfo_x,false,p1);
  476. statement_syssym:=p2;
  477. end;
  478. in_assigned_x :
  479. begin
  480. err:=false;
  481. consume(_LKLAMMER);
  482. in_args:=true;
  483. p1:=comp_expr(true);
  484. { When reading a class type it is parsed as loadvmtaddrn,
  485. typeinfo only needs the type so we remove the loadvmtaddrn }
  486. if p1.nodetype=loadvmtaddrn then
  487. begin
  488. p2:=tloadvmtaddrnode(p1).left;
  489. tloadvmtaddrnode(p1).left:=nil;
  490. p1.free;
  491. p1:=p2;
  492. end;
  493. if not codegenerror then
  494. begin
  495. case p1.resulttype.def.deftype of
  496. procdef, { procvar }
  497. pointerdef,
  498. procvardef,
  499. classrefdef : ;
  500. objectdef :
  501. if not is_class_or_interface(p1.resulttype.def) then
  502. begin
  503. Message(parser_e_illegal_parameter_list);
  504. err:=true;
  505. end;
  506. arraydef :
  507. if not is_dynamic_array(p1.resulttype.def) then
  508. begin
  509. Message(parser_e_illegal_parameter_list);
  510. err:=true;
  511. end;
  512. else
  513. begin
  514. Message(parser_e_illegal_parameter_list);
  515. err:=true;
  516. end;
  517. end;
  518. end
  519. else
  520. err:=true;
  521. if not err then
  522. begin
  523. p2:=ccallparanode.create(p1,nil);
  524. p2:=geninlinenode(in_assigned_x,false,p2);
  525. end
  526. else
  527. begin
  528. p1.free;
  529. p2:=cerrornode.create;
  530. end;
  531. consume(_RKLAMMER);
  532. statement_syssym:=p2;
  533. end;
  534. in_addr_x :
  535. begin
  536. consume(_LKLAMMER);
  537. in_args:=true;
  538. p1:=comp_expr(true);
  539. p1:=caddrnode.create(p1);
  540. if cs_typed_addresses in aktlocalswitches then
  541. include(p1.flags,nf_typedaddr);
  542. consume(_RKLAMMER);
  543. statement_syssym:=p1;
  544. end;
  545. in_ofs_x :
  546. begin
  547. consume(_LKLAMMER);
  548. in_args:=true;
  549. p1:=comp_expr(true);
  550. p1:=caddrnode.create(p1);
  551. do_resulttypepass(p1);
  552. { Ofs() returns a cardinal/qword, not a pointer }
  553. p1.resulttype:=uinttype;
  554. consume(_RKLAMMER);
  555. statement_syssym:=p1;
  556. end;
  557. in_seg_x :
  558. begin
  559. consume(_LKLAMMER);
  560. in_args:=true;
  561. p1:=comp_expr(true);
  562. p1:=geninlinenode(in_seg_x,false,p1);
  563. consume(_RKLAMMER);
  564. statement_syssym:=p1;
  565. end;
  566. in_high_x,
  567. in_low_x :
  568. begin
  569. consume(_LKLAMMER);
  570. in_args:=true;
  571. p1:=comp_expr(true);
  572. p2:=geninlinenode(l,false,p1);
  573. consume(_RKLAMMER);
  574. statement_syssym:=p2;
  575. end;
  576. in_succ_x,
  577. in_pred_x :
  578. begin
  579. consume(_LKLAMMER);
  580. in_args:=true;
  581. p1:=comp_expr(true);
  582. p2:=geninlinenode(l,false,p1);
  583. consume(_RKLAMMER);
  584. statement_syssym:=p2;
  585. end;
  586. in_inc_x,
  587. in_dec_x :
  588. begin
  589. consume(_LKLAMMER);
  590. in_args:=true;
  591. p1:=comp_expr(true);
  592. if try_to_consume(_COMMA) then
  593. p2:=ccallparanode.create(comp_expr(true),nil)
  594. else
  595. p2:=nil;
  596. p2:=ccallparanode.create(p1,p2);
  597. statement_syssym:=geninlinenode(l,false,p2);
  598. consume(_RKLAMMER);
  599. end;
  600. in_slice_x:
  601. begin
  602. if not(in_args) then
  603. begin
  604. message(parser_e_illegal_slice);
  605. consume(_LKLAMMER);
  606. in_args:=true;
  607. comp_expr(true).free;
  608. if try_to_consume(_COMMA) then
  609. comp_expr(true).free;
  610. statement_syssym:=cerrornode.create;
  611. consume(_RKLAMMER);
  612. end
  613. else
  614. begin
  615. consume(_LKLAMMER);
  616. in_args:=true;
  617. p1:=comp_expr(true);
  618. Consume(_COMMA);
  619. if not(codegenerror) then
  620. p2:=ccallparanode.create(comp_expr(true),nil)
  621. else
  622. p2:=cerrornode.create;
  623. p2:=ccallparanode.create(p1,p2);
  624. statement_syssym:=geninlinenode(l,false,p2);
  625. consume(_RKLAMMER);
  626. end;
  627. end;
  628. in_initialize_x:
  629. begin
  630. statement_syssym:=inline_initialize;
  631. end;
  632. in_finalize_x:
  633. begin
  634. statement_syssym:=inline_finalize;
  635. end;
  636. in_copy_x:
  637. begin
  638. statement_syssym:=inline_copy;
  639. end;
  640. in_concat_x :
  641. begin
  642. consume(_LKLAMMER);
  643. in_args:=true;
  644. { Translate to x:=x+y[+z]. The addnode will do the
  645. type checking }
  646. p2:=nil;
  647. repeat
  648. p1:=comp_expr(true);
  649. if p2<>nil then
  650. p2:=caddnode.create(addn,p2,p1)
  651. else
  652. begin
  653. { Force string type if it isn't yet }
  654. if not(
  655. (p1.resulttype.def.deftype=stringdef) or
  656. is_chararray(p1.resulttype.def) or
  657. is_char(p1.resulttype.def)
  658. ) then
  659. inserttypeconv(p1,cshortstringtype);
  660. p2:=p1;
  661. end;
  662. until not try_to_consume(_COMMA);
  663. consume(_RKLAMMER);
  664. statement_syssym:=p2;
  665. end;
  666. in_read_x,
  667. in_readln_x :
  668. begin
  669. if try_to_consume(_LKLAMMER) then
  670. begin
  671. paras:=parse_paras(false,_RKLAMMER);
  672. consume(_RKLAMMER);
  673. end
  674. else
  675. paras:=nil;
  676. p1:=geninlinenode(l,false,paras);
  677. statement_syssym := p1;
  678. end;
  679. in_setlength_x:
  680. begin
  681. statement_syssym := inline_setlength;
  682. end;
  683. in_length_x:
  684. begin
  685. consume(_LKLAMMER);
  686. in_args:=true;
  687. p1:=comp_expr(true);
  688. p2:=geninlinenode(l,false,p1);
  689. consume(_RKLAMMER);
  690. statement_syssym:=p2;
  691. end;
  692. in_write_x,
  693. in_writeln_x :
  694. begin
  695. if try_to_consume(_LKLAMMER) then
  696. begin
  697. paras:=parse_paras(true,_RKLAMMER);
  698. consume(_RKLAMMER);
  699. end
  700. else
  701. paras:=nil;
  702. p1 := geninlinenode(l,false,paras);
  703. statement_syssym := p1;
  704. end;
  705. in_str_x_string :
  706. begin
  707. consume(_LKLAMMER);
  708. paras:=parse_paras(true,_RKLAMMER);
  709. consume(_RKLAMMER);
  710. p1 := geninlinenode(l,false,paras);
  711. statement_syssym := p1;
  712. end;
  713. in_val_x:
  714. Begin
  715. consume(_LKLAMMER);
  716. in_args := true;
  717. p1:= ccallparanode.create(comp_expr(true), nil);
  718. consume(_COMMA);
  719. p2 := ccallparanode.create(comp_expr(true),p1);
  720. if try_to_consume(_COMMA) then
  721. p2 := ccallparanode.create(comp_expr(true),p2);
  722. consume(_RKLAMMER);
  723. p2 := geninlinenode(l,false,p2);
  724. statement_syssym := p2;
  725. End;
  726. in_include_x_y,
  727. in_exclude_x_y :
  728. begin
  729. consume(_LKLAMMER);
  730. in_args:=true;
  731. p1:=comp_expr(true);
  732. consume(_COMMA);
  733. p2:=comp_expr(true);
  734. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,nil)));
  735. consume(_RKLAMMER);
  736. end;
  737. in_assert_x_y :
  738. begin
  739. consume(_LKLAMMER);
  740. in_args:=true;
  741. p1:=comp_expr(true);
  742. if try_to_consume(_COMMA) then
  743. p2:=comp_expr(true)
  744. else
  745. begin
  746. { then insert an empty string }
  747. p2:=cstringconstnode.createstr('',st_default);
  748. end;
  749. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,nil)));
  750. consume(_RKLAMMER);
  751. end;
  752. else
  753. internalerror(15);
  754. end;
  755. in_args:=prev_in_args;
  756. end;
  757. function maybe_load_methodpointer(st:tsymtable;var p1:tnode):boolean;
  758. begin
  759. maybe_load_methodpointer:=false;
  760. if not assigned(p1) then
  761. begin
  762. case st.symtabletype of
  763. withsymtable :
  764. begin
  765. if (st.defowner.deftype=objectdef) then
  766. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  767. end;
  768. objectsymtable :
  769. begin
  770. p1:=load_self_node;
  771. { We are calling a member }
  772. maybe_load_methodpointer:=true;
  773. end;
  774. end;
  775. end;
  776. end;
  777. { reads the parameter for a subroutine call }
  778. procedure do_proc_call(sym:tsym;st:tsymtable;obj:tobjectdef;getaddr:boolean;var again : boolean;var p1:tnode;callflags:tcallnodeflags);
  779. var
  780. membercall,
  781. prevafterassn : boolean;
  782. i : integer;
  783. para,p2 : tnode;
  784. currpara : tparavarsym;
  785. aprocdef : tprocdef;
  786. begin
  787. prevafterassn:=afterassignment;
  788. afterassignment:=false;
  789. membercall:=false;
  790. aprocdef:=nil;
  791. { when it is a call to a member we need to load the
  792. methodpointer first }
  793. membercall:=maybe_load_methodpointer(st,p1);
  794. { When we are expecting a procvar we also need
  795. to get the address in some cases }
  796. if assigned(getprocvardef) then
  797. begin
  798. if (block_type=bt_const) or
  799. getaddr then
  800. begin
  801. aprocdef:=Tprocsym(sym).search_procdef_byprocvardef(getprocvardef);
  802. getaddr:=true;
  803. end
  804. else
  805. if (m_tp_procvar in aktmodeswitches) or
  806. (m_mac_procvar in aktmodeswitches) then
  807. begin
  808. aprocdef:=Tprocsym(sym).search_procdef_byprocvardef(getprocvardef);
  809. if assigned(aprocdef) then
  810. getaddr:=true;
  811. end;
  812. end;
  813. { only need to get the address of the procedure? }
  814. if getaddr then
  815. begin
  816. { Retrieve info which procvar to call. For tp_procvar the
  817. aprocdef is already loaded above so we can reuse it }
  818. if not assigned(aprocdef) and
  819. assigned(getprocvardef) then
  820. aprocdef:=Tprocsym(sym).search_procdef_byprocvardef(getprocvardef);
  821. { generate a methodcallnode or proccallnode }
  822. { we shouldn't convert things like @tcollection.load }
  823. p2:=cloadnode.create_procvar(sym,aprocdef,st);
  824. if assigned(p1) then
  825. begin
  826. { for loading methodpointer of an inherited function
  827. we use self as instance and load the address of
  828. the function directly and not through the vmt (PFV) }
  829. if (cnf_inherited in callflags) then
  830. begin
  831. include(p2.flags,nf_inherited);
  832. p1.free;
  833. p1:=load_self_node;
  834. end;
  835. if (p1.nodetype<>typen) then
  836. tloadnode(p2).set_mp(p1)
  837. else
  838. p1.free;
  839. end;
  840. p1:=p2;
  841. { no postfix operators }
  842. again:=false;
  843. end
  844. else
  845. begin
  846. para:=nil;
  847. if anon_inherited then
  848. begin
  849. if not assigned(current_procinfo) then
  850. internalerror(200305054);
  851. for i:=0 to current_procinfo.procdef.paras.count-1 do
  852. begin
  853. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  854. if not(vo_is_hidden_para in currpara.varoptions) then
  855. para:=ccallparanode.create(cloadnode.create(currpara,currpara.owner),para);
  856. end;
  857. end
  858. else
  859. begin
  860. if try_to_consume(_LKLAMMER) then
  861. begin
  862. para:=parse_paras(false,_RKLAMMER);
  863. consume(_RKLAMMER);
  864. end;
  865. end;
  866. { indicate if this call was generated by a member and
  867. no explicit self is used, this is needed to determine
  868. how to handle a destructor call (PFV) }
  869. if membercall then
  870. include(callflags,cnf_member_call);
  871. if assigned(obj) then
  872. begin
  873. if (st.symtabletype<>objectsymtable) then
  874. internalerror(200310031);
  875. p1:=ccallnode.create(para,tprocsym(sym),obj.symtable,p1,callflags);
  876. end
  877. else
  878. p1:=ccallnode.create(para,tprocsym(sym),st,p1,callflags);
  879. end;
  880. afterassignment:=prevafterassn;
  881. end;
  882. procedure handle_procvar(pv : tprocvardef;var p2 : tnode);
  883. var
  884. hp,hp2 : tnode;
  885. hpp : ^tnode;
  886. currprocdef : tprocdef;
  887. begin
  888. if not assigned(pv) then
  889. internalerror(200301121);
  890. if (m_tp_procvar in aktmodeswitches) or
  891. (m_mac_procvar in aktmodeswitches) then
  892. begin
  893. hp:=p2;
  894. hpp:=@p2;
  895. while assigned(hp) and
  896. (hp.nodetype=typeconvn) do
  897. begin
  898. hp:=ttypeconvnode(hp).left;
  899. { save orignal address of the old tree so we can replace the node }
  900. hpp:=@hp;
  901. end;
  902. if (hp.nodetype=calln) and
  903. { a procvar can't have parameters! }
  904. not assigned(tcallnode(hp).left) then
  905. begin
  906. currprocdef:=tcallnode(hp).symtableprocentry.search_procdef_byprocvardef(pv);
  907. if assigned(currprocdef) then
  908. begin
  909. hp2:=cloadnode.create_procvar(tprocsym(tcallnode(hp).symtableprocentry),currprocdef,tcallnode(hp).symtableproc);
  910. if (po_methodpointer in pv.procoptions) then
  911. tloadnode(hp2).set_mp(tcallnode(hp).get_load_methodpointer);
  912. hp.destroy;
  913. { replace the old callnode with the new loadnode }
  914. hpp^:=hp2;
  915. end;
  916. end;
  917. end;
  918. end;
  919. { the following procedure handles the access to a property symbol }
  920. procedure handle_propertysym(sym : tsym;st : tsymtable;var p1 : tnode);
  921. var
  922. paras : tnode;
  923. p2 : tnode;
  924. membercall : boolean;
  925. callflags : tcallnodeflags;
  926. begin
  927. paras:=nil;
  928. { property parameters? read them only if the property really }
  929. { has parameters }
  930. if (ppo_hasparameters in tpropertysym(sym).propoptions) then
  931. begin
  932. if try_to_consume(_LECKKLAMMER) then
  933. begin
  934. paras:=parse_paras(false,_RECKKLAMMER);
  935. consume(_RECKKLAMMER);
  936. end;
  937. end;
  938. { indexed property }
  939. if (ppo_indexed in tpropertysym(sym).propoptions) then
  940. begin
  941. p2:=cordconstnode.create(tpropertysym(sym).index,tpropertysym(sym).indextype,true);
  942. paras:=ccallparanode.create(p2,paras);
  943. end;
  944. { we need only a write property if a := follows }
  945. { if not(afterassignment) and not(in_args) then }
  946. if token=_ASSIGNMENT then
  947. begin
  948. { write property: }
  949. if not tpropertysym(sym).writeaccess.empty then
  950. begin
  951. case tpropertysym(sym).writeaccess.firstsym^.sym.typ of
  952. procsym :
  953. begin
  954. callflags:=[];
  955. { generate the method call }
  956. membercall:=maybe_load_methodpointer(st,p1);
  957. if membercall then
  958. include(callflags,cnf_member_call);
  959. p1:=ccallnode.create(paras,tprocsym(tpropertysym(sym).writeaccess.firstsym^.sym),st,p1,callflags);
  960. paras:=nil;
  961. consume(_ASSIGNMENT);
  962. { read the expression }
  963. if tpropertysym(sym).proptype.def.deftype=procvardef then
  964. getprocvardef:=tprocvardef(tpropertysym(sym).proptype.def);
  965. p2:=comp_expr(true);
  966. if assigned(getprocvardef) then
  967. handle_procvar(getprocvardef,p2);
  968. tcallnode(p1).left:=ccallparanode.create(p2,tcallnode(p1).left);
  969. { mark as property, both the tcallnode and the real call block }
  970. include(p1.flags,nf_isproperty);
  971. getprocvardef:=nil;
  972. end;
  973. fieldvarsym :
  974. begin
  975. { generate access code }
  976. symlist_to_node(p1,st,tpropertysym(sym).writeaccess);
  977. include(p1.flags,nf_isproperty);
  978. consume(_ASSIGNMENT);
  979. { read the expression }
  980. p2:=comp_expr(true);
  981. p1:=cassignmentnode.create(p1,p2);
  982. end
  983. else
  984. begin
  985. p1:=cerrornode.create;
  986. Message(parser_e_no_procedure_to_access_property);
  987. end;
  988. end;
  989. end
  990. else
  991. begin
  992. p1:=cerrornode.create;
  993. Message(parser_e_no_procedure_to_access_property);
  994. end;
  995. end
  996. else
  997. begin
  998. { read property: }
  999. if not tpropertysym(sym).readaccess.empty then
  1000. begin
  1001. case tpropertysym(sym).readaccess.firstsym^.sym.typ of
  1002. fieldvarsym :
  1003. begin
  1004. { generate access code }
  1005. symlist_to_node(p1,st,tpropertysym(sym).readaccess);
  1006. include(p1.flags,nf_isproperty);
  1007. end;
  1008. procsym :
  1009. begin
  1010. callflags:=[];
  1011. { generate the method call }
  1012. membercall:=maybe_load_methodpointer(st,p1);
  1013. if membercall then
  1014. include(callflags,cnf_member_call);
  1015. p1:=ccallnode.create(paras,tprocsym(tpropertysym(sym).readaccess.firstsym^.sym),st,p1,callflags);
  1016. paras:=nil;
  1017. include(p1.flags,nf_isproperty);
  1018. end
  1019. else
  1020. begin
  1021. p1:=cerrornode.create;
  1022. Message(type_e_mismatch);
  1023. end;
  1024. end;
  1025. end
  1026. else
  1027. begin
  1028. { error, no function to read property }
  1029. p1:=cerrornode.create;
  1030. Message(parser_e_no_procedure_to_access_property);
  1031. end;
  1032. end;
  1033. { release paras if not used }
  1034. if assigned(paras) then
  1035. paras.free;
  1036. end;
  1037. { the ID token has to be consumed before calling this function }
  1038. procedure do_member_read(classh:tobjectdef;getaddr : boolean;sym : tsym;var p1 : tnode;var again : boolean;callflags:tcallnodeflags);
  1039. var
  1040. static_name : string;
  1041. isclassref : boolean;
  1042. srsymtable : tsymtable;
  1043. begin
  1044. if sym=nil then
  1045. begin
  1046. { pattern is still valid unless
  1047. there is another ID just after the ID of sym }
  1048. Message1(sym_e_id_no_member,orgpattern);
  1049. p1.free;
  1050. p1:=cerrornode.create;
  1051. { try to clean up }
  1052. again:=false;
  1053. end
  1054. else
  1055. begin
  1056. if assigned(p1) then
  1057. begin
  1058. if not assigned(p1.resulttype.def) then
  1059. do_resulttypepass(p1);
  1060. isclassref:=(p1.resulttype.def.deftype=classrefdef);
  1061. end
  1062. else
  1063. isclassref:=false;
  1064. { we assume, that only procsyms and varsyms are in an object }
  1065. { symbol table, for classes, properties are allowed }
  1066. case sym.typ of
  1067. procsym:
  1068. begin
  1069. do_proc_call(sym,sym.owner,classh,
  1070. (getaddr and not(token in [_CARET,_POINT])),
  1071. again,p1,callflags);
  1072. { we need to know which procedure is called }
  1073. do_resulttypepass(p1);
  1074. { calling using classref? }
  1075. if isclassref and
  1076. (p1.nodetype=calln) and
  1077. assigned(tcallnode(p1).procdefinition) and
  1078. not(po_classmethod in tcallnode(p1).procdefinition.procoptions) and
  1079. not(tcallnode(p1).procdefinition.proctypeoption=potype_constructor) then
  1080. Message(parser_e_only_class_methods_via_class_ref);
  1081. end;
  1082. fieldvarsym:
  1083. begin
  1084. if (sp_static in sym.symoptions) then
  1085. begin
  1086. static_name:=lower(sym.owner.name^)+'_'+sym.name;
  1087. searchsym(static_name,sym,srsymtable);
  1088. if assigned(sym) then
  1089. check_hints(sym,sym.symoptions);
  1090. p1.free;
  1091. p1:=cloadnode.create(sym,srsymtable);
  1092. end
  1093. else
  1094. begin
  1095. if isclassref then
  1096. Message(parser_e_only_class_methods_via_class_ref);
  1097. p1:=csubscriptnode.create(sym,p1);
  1098. end;
  1099. end;
  1100. propertysym:
  1101. begin
  1102. if isclassref then
  1103. Message(parser_e_only_class_methods_via_class_ref);
  1104. handle_propertysym(sym,sym.owner,p1);
  1105. end;
  1106. else internalerror(16);
  1107. end;
  1108. end;
  1109. end;
  1110. {****************************************************************************
  1111. Factor
  1112. ****************************************************************************}
  1113. {$ifdef fpc}
  1114. {$maxfpuregisters 0}
  1115. {$endif fpc}
  1116. function factor(getaddr : boolean) : tnode;
  1117. {---------------------------------------------
  1118. Factor_read_id
  1119. ---------------------------------------------}
  1120. procedure factor_read_id(var p1:tnode;var again:boolean);
  1121. var
  1122. pc : pchar;
  1123. len : longint;
  1124. srsym : tsym;
  1125. possible_error : boolean;
  1126. srsymtable : tsymtable;
  1127. storesymtablestack : tsymtable;
  1128. htype : ttype;
  1129. static_name : string;
  1130. begin
  1131. { allow post fix operators }
  1132. again:=true;
  1133. consume_sym(srsym,srsymtable);
  1134. { Access to funcret or need to call the function? }
  1135. if (srsym.typ in [absolutevarsym,localvarsym,paravarsym]) and
  1136. (vo_is_funcret in tabstractvarsym(srsym).varoptions) and
  1137. (
  1138. (token=_LKLAMMER) or
  1139. (
  1140. (
  1141. (m_tp7 in aktmodeswitches) or
  1142. (m_delphi in aktmodeswitches)
  1143. ) and
  1144. (afterassignment or in_args) and
  1145. not(vo_is_result in tabstractvarsym(srsym).varoptions)
  1146. )
  1147. ) then
  1148. begin
  1149. storesymtablestack:=symtablestack;
  1150. symtablestack:=srsym.owner.next;
  1151. searchsym(srsym.name,srsym,srsymtable);
  1152. if not assigned(srsym) then
  1153. srsym:=generrorsym;
  1154. if (srsym.typ<>procsym) then
  1155. Message(parser_e_illegal_expression);
  1156. symtablestack:=storesymtablestack;
  1157. end;
  1158. begin
  1159. case srsym.typ of
  1160. absolutevarsym :
  1161. begin
  1162. if (tabsolutevarsym(srsym).abstyp=tovar) then
  1163. begin
  1164. p1:=nil;
  1165. symlist_to_node(p1,nil,tabsolutevarsym(srsym).ref);
  1166. p1:=ctypeconvnode.create(p1,tabsolutevarsym(srsym).vartype);
  1167. include(p1.flags,nf_absolute);
  1168. end
  1169. else
  1170. p1:=cloadnode.create(srsym,srsymtable);
  1171. end;
  1172. globalvarsym,
  1173. localvarsym,
  1174. paravarsym,
  1175. fieldvarsym :
  1176. begin
  1177. if (sp_static in srsym.symoptions) then
  1178. begin
  1179. static_name:=lower(srsym.owner.name^)+'_'+srsym.name;
  1180. searchsym(static_name,srsym,srsymtable);
  1181. if assigned(srsym) then
  1182. check_hints(srsym,srsym.symoptions);
  1183. end
  1184. else
  1185. begin
  1186. { are we in a class method, we check here the
  1187. srsymtable, because a field in another object
  1188. also has objectsymtable. And withsymtable is
  1189. not possible for self in class methods (PFV) }
  1190. if (srsymtable.symtabletype=objectsymtable) and
  1191. assigned(current_procinfo) and
  1192. (po_classmethod in current_procinfo.procdef.procoptions) then
  1193. Message(parser_e_only_class_methods);
  1194. end;
  1195. case srsymtable.symtabletype of
  1196. objectsymtable :
  1197. begin
  1198. p1:=csubscriptnode.create(srsym,load_self_node);
  1199. node_tree_set_filepos(p1,aktfilepos);
  1200. end;
  1201. withsymtable :
  1202. begin
  1203. p1:=csubscriptnode.create(srsym,tnode(twithsymtable(srsymtable).withrefnode).getcopy);
  1204. node_tree_set_filepos(p1,aktfilepos);
  1205. end;
  1206. else
  1207. p1:=cloadnode.create(srsym,srsymtable);
  1208. end;
  1209. end;
  1210. typedconstsym :
  1211. begin
  1212. p1:=cloadnode.create(srsym,srsymtable);
  1213. end;
  1214. syssym :
  1215. begin
  1216. p1:=statement_syssym(tsyssym(srsym).number);
  1217. end;
  1218. typesym :
  1219. begin
  1220. htype.setsym(srsym);
  1221. if not assigned(htype.def) then
  1222. begin
  1223. again:=false;
  1224. end
  1225. else
  1226. begin
  1227. { We need to know if this unit uses Variants }
  1228. if (htype.def=cvarianttype.def) and
  1229. not(cs_compilesystem in aktmoduleswitches) then
  1230. current_module.flags:=current_module.flags or uf_uses_variants;
  1231. if try_to_consume(_LKLAMMER) then
  1232. begin
  1233. p1:=comp_expr(true);
  1234. consume(_RKLAMMER);
  1235. p1:=ctypeconvnode.create_explicit(p1,htype);
  1236. end
  1237. else { not LKLAMMER }
  1238. if (token=_POINT) and
  1239. is_object(htype.def) then
  1240. begin
  1241. consume(_POINT);
  1242. if assigned(current_procinfo) and
  1243. assigned(current_procinfo.procdef._class) and
  1244. not(getaddr) then
  1245. begin
  1246. if current_procinfo.procdef._class.is_related(tobjectdef(htype.def)) then
  1247. begin
  1248. p1:=ctypenode.create(htype);
  1249. { search also in inherited methods }
  1250. srsym:=searchsym_in_class(tobjectdef(htype.def),pattern);
  1251. if assigned(srsym) then
  1252. check_hints(srsym,srsym.symoptions);
  1253. consume(_ID);
  1254. do_member_read(tobjectdef(htype.def),false,srsym,p1,again,[]);
  1255. end
  1256. else
  1257. begin
  1258. Message(parser_e_no_super_class);
  1259. again:=false;
  1260. end;
  1261. end
  1262. else
  1263. begin
  1264. { allows @TObject.Load }
  1265. { also allows static methods and variables }
  1266. p1:=ctypenode.create(htype);
  1267. { TP allows also @TMenu.Load if Load is only }
  1268. { defined in an anchestor class }
  1269. srsym:=search_class_member(tobjectdef(htype.def),pattern);
  1270. if assigned(srsym) then
  1271. begin
  1272. check_hints(srsym,srsym.symoptions);
  1273. if not(getaddr) and not(sp_static in srsym.symoptions) then
  1274. Message(sym_e_only_static_in_static)
  1275. else
  1276. begin
  1277. consume(_ID);
  1278. do_member_read(tobjectdef(htype.def),getaddr,srsym,p1,again,[]);
  1279. end;
  1280. end
  1281. else
  1282. Message1(sym_e_id_no_member,orgpattern);
  1283. end;
  1284. end
  1285. else
  1286. begin
  1287. { class reference ? }
  1288. if is_class(htype.def) then
  1289. begin
  1290. if getaddr and (token=_POINT) then
  1291. begin
  1292. consume(_POINT);
  1293. { allows @Object.Method }
  1294. { also allows static methods and variables }
  1295. p1:=ctypenode.create(htype);
  1296. { TP allows also @TMenu.Load if Load is only }
  1297. { defined in an anchestor class }
  1298. srsym:=search_class_member(tobjectdef(htype.def),pattern);
  1299. if assigned(srsym) then
  1300. begin
  1301. check_hints(srsym,srsym.symoptions);
  1302. consume(_ID);
  1303. do_member_read(tobjectdef(htype.def),getaddr,srsym,p1,again,[]);
  1304. end
  1305. else
  1306. begin
  1307. Message1(sym_e_id_no_member,orgpattern);
  1308. consume(_ID);
  1309. end;
  1310. end
  1311. else
  1312. begin
  1313. p1:=ctypenode.create(htype);
  1314. { For a type block we simply return only
  1315. the type. For all other blocks we return
  1316. a loadvmt node }
  1317. if (block_type<>bt_type) then
  1318. p1:=cloadvmtaddrnode.create(p1);
  1319. end;
  1320. end
  1321. else
  1322. p1:=ctypenode.create(htype);
  1323. end;
  1324. end;
  1325. end;
  1326. enumsym :
  1327. begin
  1328. p1:=genenumnode(tenumsym(srsym));
  1329. end;
  1330. constsym :
  1331. begin
  1332. case tconstsym(srsym).consttyp of
  1333. constord :
  1334. begin
  1335. if tconstsym(srsym).consttype.def=nil then
  1336. internalerror(200403232);
  1337. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,tconstsym(srsym).consttype,true);
  1338. end;
  1339. conststring :
  1340. begin
  1341. len:=tconstsym(srsym).value.len;
  1342. if not(cs_ansistrings in aktlocalswitches) and (len>255) then
  1343. len:=255;
  1344. getmem(pc,len+1);
  1345. move(pchar(tconstsym(srsym).value.valueptr)^,pc^,len);
  1346. pc[len]:=#0;
  1347. p1:=cstringconstnode.createpchar(pc,len);
  1348. end;
  1349. constwstring :
  1350. p1:=cstringconstnode.createwstr(pcompilerwidestring(tconstsym(srsym).value.valueptr));
  1351. constreal :
  1352. p1:=crealconstnode.create(pbestreal(tconstsym(srsym).value.valueptr)^,pbestrealtype^);
  1353. constset :
  1354. p1:=csetconstnode.create(pconstset(tconstsym(srsym).value.valueptr),tconstsym(srsym).consttype);
  1355. constpointer :
  1356. p1:=cpointerconstnode.create(tconstsym(srsym).value.valueordptr,tconstsym(srsym).consttype);
  1357. constnil :
  1358. p1:=cnilnode.create;
  1359. constresourcestring:
  1360. begin
  1361. p1:=cloadnode.create(srsym,srsymtable);
  1362. do_resulttypepass(p1);
  1363. {$ifdef ansistring_bits}
  1364. case aktansistring_bits of
  1365. sb_16:
  1366. p1.resulttype:=cansistringtype16;
  1367. sb_32:
  1368. p1.resulttype:=cansistringtype32;
  1369. sb_64:
  1370. p1.resulttype:=cansistringtype64;
  1371. end;
  1372. {$else}
  1373. p1.resulttype:=cansistringtype;
  1374. {$endif}
  1375. end;
  1376. constguid :
  1377. p1:=cguidconstnode.create(pguid(tconstsym(srsym).value.valueptr)^);
  1378. else
  1379. internalerror(200507181);
  1380. end;
  1381. end;
  1382. procsym :
  1383. begin
  1384. { are we in a class method ? }
  1385. possible_error:=(srsymtable.symtabletype<>withsymtable) and
  1386. (srsym.owner.symtabletype=objectsymtable) and
  1387. not(is_interface(tdef(srsym.owner.defowner))) and
  1388. assigned(current_procinfo) and
  1389. (po_classmethod in current_procinfo.procdef.procoptions);
  1390. do_proc_call(srsym,srsymtable,nil,
  1391. (getaddr and not(token in [_CARET,_POINT])),
  1392. again,p1,[]);
  1393. { we need to know which procedure is called }
  1394. if possible_error then
  1395. begin
  1396. do_resulttypepass(p1);
  1397. if (p1.nodetype=calln) and
  1398. assigned(tcallnode(p1).procdefinition) and
  1399. not(tcallnode(p1).procdefinition.proctypeoption=potype_constructor) and
  1400. not(po_classmethod in tcallnode(p1).procdefinition.procoptions) then
  1401. Message(parser_e_only_class_methods);
  1402. end;
  1403. end;
  1404. propertysym :
  1405. begin
  1406. { access to property in a method }
  1407. { are we in a class method ? }
  1408. if (srsymtable.symtabletype=objectsymtable) and
  1409. assigned(current_procinfo) and
  1410. (po_classmethod in current_procinfo.procdef.procoptions) then
  1411. Message(parser_e_only_class_methods);
  1412. { no method pointer }
  1413. p1:=nil;
  1414. handle_propertysym(srsym,srsymtable,p1);
  1415. end;
  1416. labelsym :
  1417. begin
  1418. { Support @label }
  1419. if getaddr then
  1420. p1:=cloadnode.create(srsym,srsym.owner)
  1421. else
  1422. begin
  1423. consume(_COLON);
  1424. if tlabelsym(srsym).defined then
  1425. Message(sym_e_label_already_defined);
  1426. tlabelsym(srsym).defined:=true;
  1427. p1:=clabelnode.create(nil);
  1428. tlabelsym(srsym).code:=p1;
  1429. end;
  1430. end;
  1431. errorsym :
  1432. begin
  1433. p1:=cerrornode.create;
  1434. if try_to_consume(_LKLAMMER) then
  1435. begin
  1436. parse_paras(false,_RKLAMMER);
  1437. consume(_RKLAMMER);
  1438. end;
  1439. end;
  1440. else
  1441. begin
  1442. p1:=cerrornode.create;
  1443. Message(parser_e_illegal_expression);
  1444. end;
  1445. end; { end case }
  1446. end;
  1447. end;
  1448. {---------------------------------------------
  1449. Factor_Read_Set
  1450. ---------------------------------------------}
  1451. { Read a set between [] }
  1452. function factor_read_set:tnode;
  1453. var
  1454. p1,p2 : tnode;
  1455. lastp,
  1456. buildp : tarrayconstructornode;
  1457. begin
  1458. buildp:=nil;
  1459. { be sure that a least one arrayconstructn is used, also for an
  1460. empty [] }
  1461. if token=_RECKKLAMMER then
  1462. buildp:=carrayconstructornode.create(nil,buildp)
  1463. else
  1464. repeat
  1465. p1:=comp_expr(true);
  1466. if try_to_consume(_POINTPOINT) then
  1467. begin
  1468. p2:=comp_expr(true);
  1469. p1:=carrayconstructorrangenode.create(p1,p2);
  1470. end;
  1471. { insert at the end of the tree, to get the correct order }
  1472. if not assigned(buildp) then
  1473. begin
  1474. buildp:=carrayconstructornode.create(p1,nil);
  1475. lastp:=buildp;
  1476. end
  1477. else
  1478. begin
  1479. lastp.right:=carrayconstructornode.create(p1,nil);
  1480. lastp:=tarrayconstructornode(lastp.right);
  1481. end;
  1482. { there could be more elements }
  1483. until not try_to_consume(_COMMA);
  1484. factor_read_set:=buildp;
  1485. end;
  1486. {---------------------------------------------
  1487. PostFixOperators
  1488. ---------------------------------------------}
  1489. procedure postfixoperators(var p1:tnode;var again:boolean);
  1490. { tries to avoid syntax errors after invalid qualifiers }
  1491. procedure recoverconsume_postfixops;
  1492. begin
  1493. repeat
  1494. if not try_to_consume(_CARET) then
  1495. if try_to_consume(_POINT) then
  1496. try_to_consume(_ID)
  1497. else if try_to_consume(_LECKKLAMMER) then
  1498. begin
  1499. repeat
  1500. comp_expr(true);
  1501. until not try_to_consume(_COMMA);
  1502. consume(_RECKKLAMMER);
  1503. end
  1504. else
  1505. break;
  1506. until false;
  1507. end;
  1508. procedure handle_variantarray;
  1509. var
  1510. p4 : tnode;
  1511. newstatement : tstatementnode;
  1512. tempresultvariant,
  1513. temp : ttempcreatenode;
  1514. paras : tcallparanode;
  1515. newblock : tnode;
  1516. countindices : aint;
  1517. begin
  1518. { create statements with call initialize the arguments and
  1519. call fpc_dynarr_setlength }
  1520. newblock:=internalstatements(newstatement);
  1521. { get temp for array of indicies,
  1522. we set the real size later }
  1523. temp:=ctempcreatenode.create(sinttype,4,tt_persistent,false);
  1524. addstatement(newstatement,temp);
  1525. countindices:=0;
  1526. repeat
  1527. p4:=comp_expr(true);
  1528. addstatement(newstatement,cassignmentnode.create(
  1529. ctemprefnode.create_offset(temp,countindices*sinttype.def.size),p4));
  1530. inc(countindices);
  1531. until not try_to_consume(_COMMA);
  1532. { set real size }
  1533. temp.size:=countindices*sinttype.def.size;
  1534. consume(_RECKKLAMMER);
  1535. { we need only a write access if a := follows }
  1536. if token=_ASSIGNMENT then
  1537. begin
  1538. consume(_ASSIGNMENT);
  1539. p4:=comp_expr(true);
  1540. { create call to fpc_vararray_put }
  1541. paras:=ccallparanode.create(cordconstnode.create
  1542. (countindices,sinttype,true),
  1543. ccallparanode.create(caddrnode.create_internal
  1544. (ctemprefnode.create(temp)),
  1545. ccallparanode.create(ctypeconvnode.create_internal(p4,cvarianttype),
  1546. ccallparanode.create(p1
  1547. ,nil))));
  1548. addstatement(newstatement,ccallnode.createintern('fpc_vararray_put',paras));
  1549. addstatement(newstatement,ctempdeletenode.create(temp));
  1550. end
  1551. else
  1552. begin
  1553. { create temp for result }
  1554. tempresultvariant:=ctempcreatenode.create(cvarianttype,cvarianttype.def.size,tt_persistent,true);
  1555. addstatement(newstatement,tempresultvariant);
  1556. { create call to fpc_vararray_get }
  1557. paras:=ccallparanode.create(cordconstnode.create
  1558. (countindices,sinttype,true),
  1559. ccallparanode.create(caddrnode.create_internal
  1560. (ctemprefnode.create(temp)),
  1561. ccallparanode.create(p1,
  1562. ccallparanode.create(
  1563. ctemprefnode.create(tempresultvariant)
  1564. ,nil))));
  1565. addstatement(newstatement,ccallnode.createintern('fpc_vararray_get',paras));
  1566. addstatement(newstatement,ctempdeletenode.create(temp));
  1567. { the last statement should return the value as
  1568. location and type, this is done be referencing the
  1569. temp and converting it first from a persistent temp to
  1570. normal temp }
  1571. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempresultvariant));
  1572. addstatement(newstatement,ctemprefnode.create(tempresultvariant));
  1573. end;
  1574. p1:=newblock;
  1575. end;
  1576. var
  1577. store_static : boolean;
  1578. protsym : tpropertysym;
  1579. p2,p3 : tnode;
  1580. hsym : tsym;
  1581. classh : tobjectdef;
  1582. label
  1583. skipreckklammercheck;
  1584. begin
  1585. again:=true;
  1586. while again do
  1587. begin
  1588. { we need the resulttype }
  1589. do_resulttypepass(p1);
  1590. if codegenerror then
  1591. begin
  1592. recoverconsume_postfixops;
  1593. exit;
  1594. end;
  1595. { handle token }
  1596. case token of
  1597. _CARET:
  1598. begin
  1599. consume(_CARET);
  1600. { support tp/mac procvar^ if the procvar returns a
  1601. pointer type }
  1602. if ((m_tp_procvar in aktmodeswitches) or
  1603. (m_mac_procvar in aktmodeswitches)) and
  1604. (p1.resulttype.def.deftype=procvardef) and
  1605. (tprocvardef(p1.resulttype.def).rettype.def.deftype=pointerdef) then
  1606. begin
  1607. p1:=ccallnode.create_procvar(nil,p1);
  1608. resulttypepass(p1);
  1609. end;
  1610. if (p1.resulttype.def.deftype<>pointerdef) then
  1611. begin
  1612. { ^ as binary operator is a problem!!!! (FK) }
  1613. again:=false;
  1614. Message(parser_e_invalid_qualifier);
  1615. recoverconsume_postfixops;
  1616. p1.destroy;
  1617. p1:=cerrornode.create;
  1618. end
  1619. else
  1620. p1:=cderefnode.create(p1);
  1621. end;
  1622. _LECKKLAMMER:
  1623. begin
  1624. if is_class_or_interface(p1.resulttype.def) then
  1625. begin
  1626. { default property }
  1627. protsym:=search_default_property(tobjectdef(p1.resulttype.def));
  1628. if not(assigned(protsym)) then
  1629. begin
  1630. p1.destroy;
  1631. p1:=cerrornode.create;
  1632. again:=false;
  1633. message(parser_e_no_default_property_available);
  1634. end
  1635. else
  1636. begin
  1637. { The property symbol is referenced indirect }
  1638. inc(protsym.refs);
  1639. handle_propertysym(protsym,protsym.owner,p1);
  1640. end;
  1641. end
  1642. else
  1643. begin
  1644. consume(_LECKKLAMMER);
  1645. repeat
  1646. case p1.resulttype.def.deftype of
  1647. pointerdef:
  1648. begin
  1649. { support delphi autoderef }
  1650. if (tpointerdef(p1.resulttype.def).pointertype.def.deftype=arraydef) and
  1651. (m_autoderef in aktmodeswitches) then
  1652. begin
  1653. p1:=cderefnode.create(p1);
  1654. end;
  1655. p2:=comp_expr(true);
  1656. p1:=cvecnode.create(p1,p2);
  1657. end;
  1658. variantdef:
  1659. begin
  1660. handle_variantarray;
  1661. { the RECKKLAMMER is already read }
  1662. goto skipreckklammercheck;
  1663. end;
  1664. stringdef :
  1665. begin
  1666. p2:=comp_expr(true);
  1667. p1:=cvecnode.create(p1,p2);
  1668. end;
  1669. arraydef :
  1670. begin
  1671. p2:=comp_expr(true);
  1672. { support SEG:OFS for go32v2 Mem[] }
  1673. if (target_info.system in [system_i386_go32v2,system_i386_watcom]) and
  1674. (p1.nodetype=loadn) and
  1675. assigned(tloadnode(p1).symtableentry) and
  1676. assigned(tloadnode(p1).symtableentry.owner.name) and
  1677. (tloadnode(p1).symtableentry.owner.name^='SYSTEM') and
  1678. ((tloadnode(p1).symtableentry.name='MEM') or
  1679. (tloadnode(p1).symtableentry.name='MEMW') or
  1680. (tloadnode(p1).symtableentry.name='MEML')) then
  1681. begin
  1682. if try_to_consume(_COLON) then
  1683. begin
  1684. p3:=caddnode.create(muln,cordconstnode.create($10,s32inttype,false),p2);
  1685. p2:=comp_expr(true);
  1686. p2:=caddnode.create(addn,p2,p3);
  1687. p1:=cvecnode.create(p1,p2);
  1688. include(tvecnode(p1).flags,nf_memseg);
  1689. include(tvecnode(p1).flags,nf_memindex);
  1690. end
  1691. else
  1692. begin
  1693. p1:=cvecnode.create(p1,p2);
  1694. include(tvecnode(p1).flags,nf_memindex);
  1695. end;
  1696. end
  1697. else
  1698. p1:=cvecnode.create(p1,p2);
  1699. end;
  1700. else
  1701. begin
  1702. Message(parser_e_invalid_qualifier);
  1703. p1.destroy;
  1704. p1:=cerrornode.create;
  1705. comp_expr(true);
  1706. again:=false;
  1707. end;
  1708. end;
  1709. do_resulttypepass(p1);
  1710. until not try_to_consume(_COMMA);
  1711. consume(_RECKKLAMMER);
  1712. { handle_variantarray eats the RECKKLAMMER and jumps here }
  1713. skipreckklammercheck:
  1714. end;
  1715. end;
  1716. _POINT :
  1717. begin
  1718. consume(_POINT);
  1719. if (p1.resulttype.def.deftype=pointerdef) and
  1720. (m_autoderef in aktmodeswitches) then
  1721. begin
  1722. p1:=cderefnode.create(p1);
  1723. do_resulttypepass(p1);
  1724. end;
  1725. case p1.resulttype.def.deftype of
  1726. recorddef:
  1727. begin
  1728. if token=_ID then
  1729. begin
  1730. hsym:=tsym(trecorddef(p1.resulttype.def).symtable.search(pattern));
  1731. if assigned(hsym) and
  1732. (hsym.typ=fieldvarsym) then
  1733. begin
  1734. check_hints(hsym,hsym.symoptions);
  1735. p1:=csubscriptnode.create(hsym,p1)
  1736. end
  1737. else
  1738. begin
  1739. Message1(sym_e_illegal_field,pattern);
  1740. p1.destroy;
  1741. p1:=cerrornode.create;
  1742. end;
  1743. end;
  1744. consume(_ID);
  1745. end;
  1746. variantdef:
  1747. begin
  1748. end;
  1749. classrefdef:
  1750. begin
  1751. if token=_ID then
  1752. begin
  1753. classh:=tobjectdef(tclassrefdef(p1.resulttype.def).pointertype.def);
  1754. hsym:=searchsym_in_class(classh,pattern);
  1755. if hsym=nil then
  1756. begin
  1757. Message1(sym_e_id_no_member,orgpattern);
  1758. p1.destroy;
  1759. p1:=cerrornode.create;
  1760. { try to clean up }
  1761. consume(_ID);
  1762. end
  1763. else
  1764. begin
  1765. check_hints(hsym,hsym.symoptions);
  1766. consume(_ID);
  1767. do_member_read(classh,getaddr,hsym,p1,again,[]);
  1768. end;
  1769. end
  1770. else { Error }
  1771. Consume(_ID);
  1772. end;
  1773. objectdef:
  1774. begin
  1775. if token=_ID then
  1776. begin
  1777. store_static:=allow_only_static;
  1778. allow_only_static:=false;
  1779. classh:=tobjectdef(p1.resulttype.def);
  1780. hsym:=searchsym_in_class(classh,pattern);
  1781. allow_only_static:=store_static;
  1782. if hsym=nil then
  1783. begin
  1784. Message1(sym_e_id_no_member,orgpattern);
  1785. p1.destroy;
  1786. p1:=cerrornode.create;
  1787. { try to clean up }
  1788. consume(_ID);
  1789. end
  1790. else
  1791. begin
  1792. check_hints(hsym,hsym.symoptions);
  1793. consume(_ID);
  1794. do_member_read(classh,getaddr,hsym,p1,again,[]);
  1795. end;
  1796. end
  1797. else { Error }
  1798. Consume(_ID);
  1799. end;
  1800. pointerdef:
  1801. begin
  1802. Message(parser_e_invalid_qualifier);
  1803. if tpointerdef(p1.resulttype.def).pointertype.def.deftype in [recorddef,objectdef,classrefdef] then
  1804. Message(parser_h_maybe_deref_caret_missing);
  1805. end;
  1806. else
  1807. begin
  1808. Message(parser_e_invalid_qualifier);
  1809. p1.destroy;
  1810. p1:=cerrornode.create;
  1811. { Error }
  1812. consume(_ID);
  1813. end;
  1814. end;
  1815. end;
  1816. else
  1817. begin
  1818. { is this a procedure variable ? }
  1819. if assigned(p1.resulttype.def) and
  1820. (p1.resulttype.def.deftype=procvardef) then
  1821. begin
  1822. if assigned(getprocvardef) and
  1823. equal_defs(p1.resulttype.def,getprocvardef) then
  1824. again:=false
  1825. else
  1826. begin
  1827. if try_to_consume(_LKLAMMER) then
  1828. begin
  1829. p2:=parse_paras(false,_RKLAMMER);
  1830. consume(_RKLAMMER);
  1831. p1:=ccallnode.create_procvar(p2,p1);
  1832. { proc():= is never possible }
  1833. if token=_ASSIGNMENT then
  1834. begin
  1835. Message(parser_e_illegal_expression);
  1836. p1.free;
  1837. p1:=cerrornode.create;
  1838. again:=false;
  1839. end;
  1840. end
  1841. else
  1842. again:=false;
  1843. end;
  1844. end
  1845. else
  1846. again:=false;
  1847. end;
  1848. end;
  1849. end; { while again }
  1850. end;
  1851. {---------------------------------------------
  1852. Factor (Main)
  1853. ---------------------------------------------}
  1854. var
  1855. l : longint;
  1856. ic : int64;
  1857. qc : qword;
  1858. {$ifndef cpu64}
  1859. card : cardinal;
  1860. {$endif cpu64}
  1861. oldp1,
  1862. p1 : tnode;
  1863. code : integer;
  1864. again : boolean;
  1865. sym : tsym;
  1866. pd : tprocdef;
  1867. classh : tobjectdef;
  1868. d : bestreal;
  1869. hs,hsorg : string;
  1870. htype : ttype;
  1871. filepos : tfileposinfo;
  1872. {---------------------------------------------
  1873. Helpers
  1874. ---------------------------------------------}
  1875. procedure check_tokenpos;
  1876. begin
  1877. if (p1<>oldp1) then
  1878. begin
  1879. if assigned(p1) then
  1880. p1.fileinfo:=filepos;
  1881. oldp1:=p1;
  1882. filepos:=akttokenpos;
  1883. end;
  1884. end;
  1885. begin
  1886. oldp1:=nil;
  1887. p1:=nil;
  1888. filepos:=akttokenpos;
  1889. again:=false;
  1890. if token=_ID then
  1891. begin
  1892. again:=true;
  1893. { Handle references to self }
  1894. if (idtoken=_SELF) and
  1895. not(block_type in [bt_const,bt_type]) and
  1896. assigned(current_procinfo) and
  1897. assigned(current_procinfo.procdef._class) then
  1898. begin
  1899. p1:=load_self_node;
  1900. consume(_ID);
  1901. again:=true;
  1902. end
  1903. else
  1904. factor_read_id(p1,again);
  1905. if again then
  1906. begin
  1907. check_tokenpos;
  1908. { handle post fix operators }
  1909. postfixoperators(p1,again);
  1910. end;
  1911. end
  1912. else
  1913. case token of
  1914. _RETURN :
  1915. begin
  1916. consume(_RETURN);
  1917. p1 := cexitnode.create(comp_expr(true));
  1918. end;
  1919. _INHERITED :
  1920. begin
  1921. again:=true;
  1922. consume(_INHERITED);
  1923. if assigned(current_procinfo) and
  1924. assigned(current_procinfo.procdef._class) then
  1925. begin
  1926. classh:=current_procinfo.procdef._class.childof;
  1927. { if inherited; only then we need the method with
  1928. the same name }
  1929. if token in endtokens then
  1930. begin
  1931. hs:=current_procinfo.procdef.procsym.name;
  1932. hsorg:=current_procinfo.procdef.procsym.realname;
  1933. anon_inherited:=true;
  1934. { For message methods we need to search using the message
  1935. number or string }
  1936. pd:=tprocsym(current_procinfo.procdef.procsym).first_procdef;
  1937. if (po_msgint in pd.procoptions) then
  1938. sym:=searchsym_in_class_by_msgint(classh,pd.messageinf.i)
  1939. else
  1940. if (po_msgstr in pd.procoptions) then
  1941. sym:=searchsym_in_class_by_msgstr(classh,pd.messageinf.str)
  1942. else
  1943. sym:=searchsym_in_class(classh,hs);
  1944. end
  1945. else
  1946. begin
  1947. hs:=pattern;
  1948. hsorg:=orgpattern;
  1949. consume(_ID);
  1950. anon_inherited:=false;
  1951. sym:=searchsym_in_class(classh,hs);
  1952. end;
  1953. if assigned(sym) then
  1954. begin
  1955. check_hints(sym,sym.symoptions);
  1956. { load the procdef from the inherited class and
  1957. not from self }
  1958. if sym.typ in [procsym,propertysym] then
  1959. begin
  1960. if (sym.typ = procsym) then
  1961. begin
  1962. htype.setdef(classh);
  1963. if (po_classmethod in current_procinfo.procdef.procoptions) or
  1964. (po_staticmethod in current_procinfo.procdef.procoptions) then
  1965. htype.setdef(tclassrefdef.create(htype));
  1966. p1:=ctypenode.create(htype);
  1967. end;
  1968. end
  1969. else
  1970. begin
  1971. Message(parser_e_methode_id_expected);
  1972. p1:=cerrornode.create;
  1973. end;
  1974. do_member_read(classh,getaddr,sym,p1,again,[cnf_inherited,cnf_anon_inherited]);
  1975. end
  1976. else
  1977. begin
  1978. if anon_inherited then
  1979. begin
  1980. { For message methods we need to call DefaultHandler }
  1981. if (po_msgint in pd.procoptions) or
  1982. (po_msgstr in pd.procoptions) then
  1983. begin
  1984. sym:=searchsym_in_class(classh,'DEFAULTHANDLER');
  1985. if not assigned(sym) or
  1986. (sym.typ<>procsym) then
  1987. internalerror(200303171);
  1988. p1:=nil;
  1989. do_proc_call(sym,sym.owner,classh,false,again,p1,[]);
  1990. end
  1991. else
  1992. begin
  1993. { we need to ignore the inherited; }
  1994. p1:=cnothingnode.create;
  1995. end;
  1996. end
  1997. else
  1998. begin
  1999. Message1(sym_e_id_no_member,hsorg);
  2000. p1:=cerrornode.create;
  2001. end;
  2002. again:=false;
  2003. end;
  2004. { turn auto inheriting off }
  2005. anon_inherited:=false;
  2006. end
  2007. else
  2008. begin
  2009. Message(parser_e_generic_methods_only_in_methods);
  2010. again:=false;
  2011. p1:=cerrornode.create;
  2012. end;
  2013. postfixoperators(p1,again);
  2014. end;
  2015. _INTCONST :
  2016. begin
  2017. {$ifdef cpu64}
  2018. { when already running under 64bit must read int64 constant, because reading
  2019. cardinal first will also succeed (code=0) for values > maxcardinal, because
  2020. range checking is off by default (PFV) }
  2021. val(pattern,ic,code);
  2022. if code=0 then
  2023. begin
  2024. consume(_INTCONST);
  2025. int_to_type(ic,htype);
  2026. p1:=cordconstnode.create(ic,htype,true);
  2027. end
  2028. else
  2029. begin
  2030. { try qword next }
  2031. val(pattern,qc,code);
  2032. if code=0 then
  2033. begin
  2034. consume(_INTCONST);
  2035. htype:=u64inttype;
  2036. p1:=cordconstnode.create(qc,htype,true);
  2037. end;
  2038. end;
  2039. {$else}
  2040. { try cardinal first }
  2041. val(pattern,card,code);
  2042. if code=0 then
  2043. begin
  2044. consume(_INTCONST);
  2045. int_to_type(card,htype);
  2046. p1:=cordconstnode.create(card,htype,true);
  2047. end
  2048. else
  2049. begin
  2050. { then longint }
  2051. val(pattern,l,code);
  2052. if code = 0 then
  2053. begin
  2054. consume(_INTCONST);
  2055. int_to_type(l,htype);
  2056. p1:=cordconstnode.create(l,htype,true);
  2057. end
  2058. else
  2059. begin
  2060. { then int64 }
  2061. val(pattern,ic,code);
  2062. if code=0 then
  2063. begin
  2064. consume(_INTCONST);
  2065. int_to_type(ic,htype);
  2066. p1:=cordconstnode.create(ic,htype,true);
  2067. end
  2068. else
  2069. begin
  2070. { try qword next }
  2071. val(pattern,qc,code);
  2072. if code=0 then
  2073. begin
  2074. consume(_INTCONST);
  2075. htype:=u64inttype;
  2076. p1:=cordconstnode.create(tconstexprint(qc),htype,true);
  2077. end;
  2078. end;
  2079. end;
  2080. end;
  2081. {$endif}
  2082. if code<>0 then
  2083. begin
  2084. { finally float }
  2085. val(pattern,d,code);
  2086. if code<>0 then
  2087. begin
  2088. Message(parser_e_invalid_integer);
  2089. consume(_INTCONST);
  2090. l:=1;
  2091. p1:=cordconstnode.create(l,sinttype,true);
  2092. end
  2093. else
  2094. begin
  2095. consume(_INTCONST);
  2096. p1:=crealconstnode.create(d,pbestrealtype^);
  2097. end;
  2098. end;
  2099. end;
  2100. _REALNUMBER :
  2101. begin
  2102. val(pattern,d,code);
  2103. if code<>0 then
  2104. begin
  2105. Message(parser_e_error_in_real);
  2106. d:=1.0;
  2107. end;
  2108. consume(_REALNUMBER);
  2109. p1:=crealconstnode.create(d,pbestrealtype^);
  2110. end;
  2111. _STRING :
  2112. begin
  2113. string_dec(htype);
  2114. { STRING can be also a type cast }
  2115. if try_to_consume(_LKLAMMER) then
  2116. begin
  2117. p1:=comp_expr(true);
  2118. consume(_RKLAMMER);
  2119. p1:=ctypeconvnode.create_explicit(p1,htype);
  2120. { handle postfix operators here e.g. string(a)[10] }
  2121. again:=true;
  2122. postfixoperators(p1,again);
  2123. end
  2124. else
  2125. p1:=ctypenode.create(htype);
  2126. end;
  2127. _FILE :
  2128. begin
  2129. htype:=cfiletype;
  2130. consume(_FILE);
  2131. { FILE can be also a type cast }
  2132. if try_to_consume(_LKLAMMER) then
  2133. begin
  2134. p1:=comp_expr(true);
  2135. consume(_RKLAMMER);
  2136. p1:=ctypeconvnode.create_explicit(p1,htype);
  2137. { handle postfix operators here e.g. string(a)[10] }
  2138. again:=true;
  2139. postfixoperators(p1,again);
  2140. end
  2141. else
  2142. begin
  2143. p1:=ctypenode.create(htype);
  2144. end;
  2145. end;
  2146. _CSTRING :
  2147. begin
  2148. p1:=cstringconstnode.createstr(pattern,st_default);
  2149. consume(_CSTRING);
  2150. end;
  2151. _CCHAR :
  2152. begin
  2153. p1:=cordconstnode.create(ord(pattern[1]),cchartype,true);
  2154. consume(_CCHAR);
  2155. end;
  2156. _CWSTRING:
  2157. begin
  2158. p1:=cstringconstnode.createwstr(patternw);
  2159. consume(_CWSTRING);
  2160. end;
  2161. _CWCHAR:
  2162. begin
  2163. p1:=cordconstnode.create(ord(getcharwidestring(patternw,0)),cwidechartype,true);
  2164. consume(_CWCHAR);
  2165. end;
  2166. _KLAMMERAFFE :
  2167. begin
  2168. consume(_KLAMMERAFFE);
  2169. got_addrn:=true;
  2170. { support both @<x> and @(<x>) }
  2171. if try_to_consume(_LKLAMMER) then
  2172. begin
  2173. p1:=factor(true);
  2174. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2175. begin
  2176. again:=true;
  2177. postfixoperators(p1,again);
  2178. end;
  2179. consume(_RKLAMMER);
  2180. end
  2181. else
  2182. p1:=factor(true);
  2183. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2184. begin
  2185. again:=true;
  2186. postfixoperators(p1,again);
  2187. end;
  2188. got_addrn:=false;
  2189. p1:=caddrnode.create(p1);
  2190. if cs_typed_addresses in aktlocalswitches then
  2191. include(p1.flags,nf_typedaddr);
  2192. { Store the procvar that we are expecting, the
  2193. addrn will use the information to find the correct
  2194. procdef or it will return an error }
  2195. if assigned(getprocvardef) and
  2196. (taddrnode(p1).left.nodetype = loadn) then
  2197. taddrnode(p1).getprocvardef:=getprocvardef;
  2198. end;
  2199. _LKLAMMER :
  2200. begin
  2201. consume(_LKLAMMER);
  2202. p1:=comp_expr(true);
  2203. consume(_RKLAMMER);
  2204. { it's not a good solution }
  2205. { but (a+b)^ makes some problems }
  2206. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2207. begin
  2208. again:=true;
  2209. postfixoperators(p1,again);
  2210. end;
  2211. end;
  2212. _LECKKLAMMER :
  2213. begin
  2214. consume(_LECKKLAMMER);
  2215. p1:=factor_read_set;
  2216. consume(_RECKKLAMMER);
  2217. end;
  2218. _PLUS :
  2219. begin
  2220. consume(_PLUS);
  2221. p1:=factor(false);
  2222. { we must generate a new node to do 0+<p1> otherwise the + will
  2223. not be checked }
  2224. p1:=caddnode.create(addn,genintconstnode(0),p1);
  2225. end;
  2226. _MINUS :
  2227. begin
  2228. consume(_MINUS);
  2229. if (token = _INTCONST) then
  2230. begin
  2231. { ugly hack, but necessary to be able to parse }
  2232. { -9223372036854775808 as int64 (JM) }
  2233. pattern := '-'+pattern;
  2234. p1:=sub_expr(oppower,false);
  2235. { -1 ** 4 should be - (1 ** 4) and not
  2236. (-1) ** 4
  2237. This was the reason of tw0869.pp test failure PM }
  2238. if p1.nodetype=starstarn then
  2239. begin
  2240. if tbinarynode(p1).left.nodetype=ordconstn then
  2241. begin
  2242. tordconstnode(tbinarynode(p1).left).value:=-tordconstnode(tbinarynode(p1).left).value;
  2243. p1:=cunaryminusnode.create(p1);
  2244. end
  2245. else if tbinarynode(p1).left.nodetype=realconstn then
  2246. begin
  2247. trealconstnode(tbinarynode(p1).left).value_real:=-trealconstnode(tbinarynode(p1).left).value_real;
  2248. p1:=cunaryminusnode.create(p1);
  2249. end
  2250. else
  2251. internalerror(20021029);
  2252. end;
  2253. end
  2254. else
  2255. begin
  2256. p1:=sub_expr(oppower,false);
  2257. p1:=cunaryminusnode.create(p1);
  2258. end;
  2259. end;
  2260. _OP_NOT :
  2261. begin
  2262. consume(_OP_NOT);
  2263. p1:=factor(false);
  2264. p1:=cnotnode.create(p1);
  2265. end;
  2266. _TRUE :
  2267. begin
  2268. consume(_TRUE);
  2269. p1:=cordconstnode.create(1,booltype,false);
  2270. end;
  2271. _FALSE :
  2272. begin
  2273. consume(_FALSE);
  2274. p1:=cordconstnode.create(0,booltype,false);
  2275. end;
  2276. _NIL :
  2277. begin
  2278. consume(_NIL);
  2279. p1:=cnilnode.create;
  2280. { It's really ugly code nil^, but delphi allows it }
  2281. if token in [_CARET] then
  2282. begin
  2283. again:=true;
  2284. postfixoperators(p1,again);
  2285. end;
  2286. end;
  2287. else
  2288. begin
  2289. p1:=cerrornode.create;
  2290. consume(token);
  2291. Message(parser_e_illegal_expression);
  2292. end;
  2293. end;
  2294. { generate error node if no node is created }
  2295. if not assigned(p1) then
  2296. begin
  2297. {$ifdef EXTDEBUG}
  2298. Comment(V_Warning,'factor: p1=nil');
  2299. {$endif}
  2300. p1:=cerrornode.create;
  2301. end;
  2302. { get the resulttype for the node }
  2303. if (not assigned(p1.resulttype.def)) then
  2304. do_resulttypepass(p1);
  2305. factor:=p1;
  2306. check_tokenpos;
  2307. end;
  2308. {$ifdef fpc}
  2309. {$maxfpuregisters default}
  2310. {$endif fpc}
  2311. {****************************************************************************
  2312. Sub_Expr
  2313. ****************************************************************************}
  2314. const
  2315. { Warning these stay be ordered !! }
  2316. operator_levels:array[Toperator_precedence] of set of Ttoken=
  2317. ([_LT,_LTE,_GT,_GTE,_EQUAL,_UNEQUAL,_OP_IN],
  2318. [_PLUS,_MINUS,_OP_OR,_PIPE,_OP_XOR],
  2319. [_CARET,_SYMDIF,_STARSTAR,_STAR,_SLASH,
  2320. _OP_AS,_OP_IS,_OP_AND,_AMPERSAND,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR],
  2321. [_STARSTAR] );
  2322. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):tnode;
  2323. {Reads a subexpression while the operators are of the current precedence
  2324. level, or any higher level. Replaces the old term, simpl_expr and
  2325. simpl2_expr.}
  2326. var
  2327. p1,p2 : tnode;
  2328. oldt : Ttoken;
  2329. filepos : tfileposinfo;
  2330. begin
  2331. if pred_level=highest_precedence then
  2332. p1:=factor(false)
  2333. else
  2334. p1:=sub_expr(succ(pred_level),true);
  2335. repeat
  2336. if (token in operator_levels[pred_level]) and
  2337. ((token<>_EQUAL) or accept_equal) then
  2338. begin
  2339. oldt:=token;
  2340. filepos:=akttokenpos;
  2341. consume(token);
  2342. if pred_level=highest_precedence then
  2343. p2:=factor(false)
  2344. else
  2345. p2:=sub_expr(succ(pred_level),true);
  2346. case oldt of
  2347. _PLUS :
  2348. p1:=caddnode.create(addn,p1,p2);
  2349. _MINUS :
  2350. p1:=caddnode.create(subn,p1,p2);
  2351. _STAR :
  2352. p1:=caddnode.create(muln,p1,p2);
  2353. _SLASH :
  2354. p1:=caddnode.create(slashn,p1,p2);
  2355. _EQUAL :
  2356. p1:=caddnode.create(equaln,p1,p2);
  2357. _GT :
  2358. p1:=caddnode.create(gtn,p1,p2);
  2359. _LT :
  2360. p1:=caddnode.create(ltn,p1,p2);
  2361. _GTE :
  2362. p1:=caddnode.create(gten,p1,p2);
  2363. _LTE :
  2364. p1:=caddnode.create(lten,p1,p2);
  2365. _SYMDIF :
  2366. p1:=caddnode.create(symdifn,p1,p2);
  2367. _STARSTAR :
  2368. p1:=caddnode.create(starstarn,p1,p2);
  2369. _OP_AS :
  2370. p1:=casnode.create(p1,p2);
  2371. _OP_IN :
  2372. p1:=cinnode.create(p1,p2);
  2373. _OP_IS :
  2374. p1:=cisnode.create(p1,p2);
  2375. _OP_OR,
  2376. _PIPE {macpas only} :
  2377. p1:=caddnode.create(orn,p1,p2);
  2378. _OP_AND,
  2379. _AMPERSAND {macpas only} :
  2380. p1:=caddnode.create(andn,p1,p2);
  2381. _OP_DIV :
  2382. p1:=cmoddivnode.create(divn,p1,p2);
  2383. _OP_NOT :
  2384. p1:=cnotnode.create(p1);
  2385. _OP_MOD :
  2386. p1:=cmoddivnode.create(modn,p1,p2);
  2387. _OP_SHL :
  2388. p1:=cshlshrnode.create(shln,p1,p2);
  2389. _OP_SHR :
  2390. p1:=cshlshrnode.create(shrn,p1,p2);
  2391. _OP_XOR :
  2392. p1:=caddnode.create(xorn,p1,p2);
  2393. _ASSIGNMENT :
  2394. p1:=cassignmentnode.create(p1,p2);
  2395. _CARET :
  2396. p1:=caddnode.create(caretn,p1,p2);
  2397. _UNEQUAL :
  2398. p1:=caddnode.create(unequaln,p1,p2);
  2399. end;
  2400. p1.fileinfo:=filepos;
  2401. end
  2402. else
  2403. break;
  2404. until false;
  2405. sub_expr:=p1;
  2406. end;
  2407. function comp_expr(accept_equal : boolean):tnode;
  2408. var
  2409. oldafterassignment : boolean;
  2410. p1 : tnode;
  2411. begin
  2412. oldafterassignment:=afterassignment;
  2413. afterassignment:=true;
  2414. p1:=sub_expr(opcompare,accept_equal);
  2415. { get the resulttype for this expression }
  2416. if not assigned(p1.resulttype.def) then
  2417. do_resulttypepass(p1);
  2418. afterassignment:=oldafterassignment;
  2419. comp_expr:=p1;
  2420. end;
  2421. function expr : tnode;
  2422. var
  2423. p1,p2 : tnode;
  2424. oldafterassignment : boolean;
  2425. oldp1 : tnode;
  2426. filepos : tfileposinfo;
  2427. begin
  2428. oldafterassignment:=afterassignment;
  2429. p1:=sub_expr(opcompare,true);
  2430. { get the resulttype for this expression }
  2431. if not assigned(p1.resulttype.def) then
  2432. do_resulttypepass(p1);
  2433. filepos:=akttokenpos;
  2434. if token in [_ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  2435. afterassignment:=true;
  2436. oldp1:=p1;
  2437. case token of
  2438. _POINTPOINT :
  2439. begin
  2440. consume(_POINTPOINT);
  2441. p2:=sub_expr(opcompare,true);
  2442. p1:=crangenode.create(p1,p2);
  2443. end;
  2444. _ASSIGNMENT :
  2445. begin
  2446. consume(_ASSIGNMENT);
  2447. if (p1.resulttype.def.deftype=procvardef) then
  2448. getprocvardef:=tprocvardef(p1.resulttype.def);
  2449. p2:=sub_expr(opcompare,true);
  2450. if assigned(getprocvardef) then
  2451. handle_procvar(getprocvardef,p2);
  2452. getprocvardef:=nil;
  2453. p1:=cassignmentnode.create(p1,p2);
  2454. end;
  2455. _PLUSASN :
  2456. begin
  2457. consume(_PLUSASN);
  2458. p2:=sub_expr(opcompare,true);
  2459. p1:=gen_c_style_operator(addn,p1,p2);
  2460. end;
  2461. _MINUSASN :
  2462. begin
  2463. consume(_MINUSASN);
  2464. p2:=sub_expr(opcompare,true);
  2465. p1:=gen_c_style_operator(subn,p1,p2);
  2466. end;
  2467. _STARASN :
  2468. begin
  2469. consume(_STARASN );
  2470. p2:=sub_expr(opcompare,true);
  2471. p1:=gen_c_style_operator(muln,p1,p2);
  2472. end;
  2473. _SLASHASN :
  2474. begin
  2475. consume(_SLASHASN );
  2476. p2:=sub_expr(opcompare,true);
  2477. p1:=gen_c_style_operator(slashn,p1,p2);
  2478. end;
  2479. end;
  2480. { get the resulttype for this expression }
  2481. if not assigned(p1.resulttype.def) then
  2482. do_resulttypepass(p1);
  2483. afterassignment:=oldafterassignment;
  2484. if p1<>oldp1 then
  2485. p1.fileinfo:=filepos;
  2486. expr:=p1;
  2487. end;
  2488. function get_intconst:TConstExprInt;
  2489. {Reads an expression, tries to evalute it and check if it is an integer
  2490. constant. Then the constant is returned.}
  2491. var
  2492. p:tnode;
  2493. begin
  2494. result:=0;
  2495. p:=comp_expr(true);
  2496. if not codegenerror then
  2497. begin
  2498. if (p.nodetype<>ordconstn) or
  2499. not(is_integer(p.resulttype.def)) then
  2500. Message(parser_e_illegal_expression)
  2501. else
  2502. result:=tordconstnode(p).value;
  2503. end;
  2504. p.free;
  2505. end;
  2506. function get_stringconst:string;
  2507. {Reads an expression, tries to evaluate it and checks if it is a string
  2508. constant. Then the constant is returned.}
  2509. var
  2510. p:tnode;
  2511. begin
  2512. get_stringconst:='';
  2513. p:=comp_expr(true);
  2514. if p.nodetype<>stringconstn then
  2515. begin
  2516. if (p.nodetype=ordconstn) and is_char(p.resulttype.def) then
  2517. get_stringconst:=char(tordconstnode(p).value)
  2518. else
  2519. Message(parser_e_illegal_expression);
  2520. end
  2521. else
  2522. get_stringconst:=strpas(tstringconstnode(p).value_str);
  2523. p.free;
  2524. end;
  2525. end.