pexpr.pas 95 KB

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