pexpr.pas 98 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680
  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. if try_to_consume(_COMMA) then
  619. p2:=ccallparanode.create(comp_expr(true),nil)
  620. else
  621. p2:=nil;
  622. p2:=ccallparanode.create(p1,p2);
  623. statement_syssym:=geninlinenode(l,false,p2);
  624. consume(_RKLAMMER);
  625. end;
  626. end;
  627. in_initialize_x:
  628. begin
  629. statement_syssym:=inline_initialize;
  630. end;
  631. in_finalize_x:
  632. begin
  633. statement_syssym:=inline_finalize;
  634. end;
  635. in_copy_x:
  636. begin
  637. statement_syssym:=inline_copy;
  638. end;
  639. in_concat_x :
  640. begin
  641. consume(_LKLAMMER);
  642. in_args:=true;
  643. { Translate to x:=x+y[+z]. The addnode will do the
  644. type checking }
  645. p2:=nil;
  646. repeat
  647. p1:=comp_expr(true);
  648. if p2<>nil then
  649. p2:=caddnode.create(addn,p2,p1)
  650. else
  651. begin
  652. { Force string type if it isn't yet }
  653. if not(
  654. (p1.resulttype.def.deftype=stringdef) or
  655. is_chararray(p1.resulttype.def) or
  656. is_char(p1.resulttype.def)
  657. ) then
  658. inserttypeconv(p1,cshortstringtype);
  659. p2:=p1;
  660. end;
  661. until not try_to_consume(_COMMA);
  662. consume(_RKLAMMER);
  663. statement_syssym:=p2;
  664. end;
  665. in_read_x,
  666. in_readln_x :
  667. begin
  668. if try_to_consume(_LKLAMMER) then
  669. begin
  670. paras:=parse_paras(false,_RKLAMMER);
  671. consume(_RKLAMMER);
  672. end
  673. else
  674. paras:=nil;
  675. p1:=geninlinenode(l,false,paras);
  676. statement_syssym := p1;
  677. end;
  678. in_setlength_x:
  679. begin
  680. statement_syssym := inline_setlength;
  681. end;
  682. in_length_x:
  683. begin
  684. consume(_LKLAMMER);
  685. in_args:=true;
  686. p1:=comp_expr(true);
  687. p2:=geninlinenode(l,false,p1);
  688. consume(_RKLAMMER);
  689. statement_syssym:=p2;
  690. end;
  691. in_write_x,
  692. in_writeln_x :
  693. begin
  694. if try_to_consume(_LKLAMMER) then
  695. begin
  696. paras:=parse_paras(true,_RKLAMMER);
  697. consume(_RKLAMMER);
  698. end
  699. else
  700. paras:=nil;
  701. p1 := geninlinenode(l,false,paras);
  702. statement_syssym := p1;
  703. end;
  704. in_str_x_string :
  705. begin
  706. consume(_LKLAMMER);
  707. paras:=parse_paras(true,_RKLAMMER);
  708. consume(_RKLAMMER);
  709. p1 := geninlinenode(l,false,paras);
  710. statement_syssym := p1;
  711. end;
  712. in_val_x:
  713. Begin
  714. consume(_LKLAMMER);
  715. in_args := true;
  716. p1:= ccallparanode.create(comp_expr(true), nil);
  717. consume(_COMMA);
  718. p2 := ccallparanode.create(comp_expr(true),p1);
  719. if try_to_consume(_COMMA) then
  720. p2 := ccallparanode.create(comp_expr(true),p2);
  721. consume(_RKLAMMER);
  722. p2 := geninlinenode(l,false,p2);
  723. statement_syssym := p2;
  724. End;
  725. in_include_x_y,
  726. in_exclude_x_y :
  727. begin
  728. consume(_LKLAMMER);
  729. in_args:=true;
  730. p1:=comp_expr(true);
  731. consume(_COMMA);
  732. p2:=comp_expr(true);
  733. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,nil)));
  734. consume(_RKLAMMER);
  735. end;
  736. in_assert_x_y :
  737. begin
  738. consume(_LKLAMMER);
  739. in_args:=true;
  740. p1:=comp_expr(true);
  741. if try_to_consume(_COMMA) then
  742. p2:=comp_expr(true)
  743. else
  744. begin
  745. { then insert an empty string }
  746. p2:=cstringconstnode.createstr('',st_default);
  747. end;
  748. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,nil)));
  749. consume(_RKLAMMER);
  750. end;
  751. else
  752. internalerror(15);
  753. end;
  754. in_args:=prev_in_args;
  755. end;
  756. function maybe_load_methodpointer(st:tsymtable;var p1:tnode):boolean;
  757. begin
  758. maybe_load_methodpointer:=false;
  759. if not assigned(p1) then
  760. begin
  761. case st.symtabletype of
  762. withsymtable :
  763. begin
  764. if (st.defowner.deftype=objectdef) then
  765. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  766. end;
  767. objectsymtable :
  768. begin
  769. p1:=load_self_node;
  770. { We are calling a member }
  771. maybe_load_methodpointer:=true;
  772. end;
  773. end;
  774. end;
  775. end;
  776. { reads the parameter for a subroutine call }
  777. procedure do_proc_call(sym:tsym;st:tsymtable;obj:tobjectdef;getaddr:boolean;var again : boolean;var p1:tnode;callflags:tcallnodeflags);
  778. var
  779. membercall,
  780. prevafterassn : boolean;
  781. i : integer;
  782. para,p2 : tnode;
  783. currpara : tparavarsym;
  784. aprocdef : tprocdef;
  785. begin
  786. prevafterassn:=afterassignment;
  787. afterassignment:=false;
  788. membercall:=false;
  789. aprocdef:=nil;
  790. { when it is a call to a member we need to load the
  791. methodpointer first }
  792. membercall:=maybe_load_methodpointer(st,p1);
  793. { When we are expecting a procvar we also need
  794. to get the address in some cases }
  795. if assigned(getprocvardef) then
  796. begin
  797. if (block_type=bt_const) or
  798. getaddr then
  799. begin
  800. aprocdef:=Tprocsym(sym).search_procdef_byprocvardef(getprocvardef);
  801. getaddr:=true;
  802. end
  803. else
  804. if (m_tp_procvar in aktmodeswitches) or
  805. (m_mac_procvar in aktmodeswitches) then
  806. begin
  807. aprocdef:=Tprocsym(sym).search_procdef_byprocvardef(getprocvardef);
  808. if assigned(aprocdef) then
  809. getaddr:=true;
  810. end;
  811. end;
  812. { only need to get the address of the procedure? }
  813. if getaddr then
  814. begin
  815. { Retrieve info which procvar to call. For tp_procvar the
  816. aprocdef is already loaded above so we can reuse it }
  817. if not assigned(aprocdef) and
  818. assigned(getprocvardef) then
  819. aprocdef:=Tprocsym(sym).search_procdef_byprocvardef(getprocvardef);
  820. { generate a methodcallnode or proccallnode }
  821. { we shouldn't convert things like @tcollection.load }
  822. p2:=cloadnode.create_procvar(sym,aprocdef,st);
  823. if assigned(p1) then
  824. begin
  825. if (p1.nodetype<>typen) then
  826. tloadnode(p2).set_mp(p1)
  827. else
  828. p1.free;
  829. end;
  830. p1:=p2;
  831. { no postfix operators }
  832. again:=false;
  833. end
  834. else
  835. begin
  836. para:=nil;
  837. if anon_inherited then
  838. begin
  839. if not assigned(current_procinfo) then
  840. internalerror(200305054);
  841. for i:=0 to current_procinfo.procdef.paras.count-1 do
  842. begin
  843. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  844. if not(vo_is_hidden_para in currpara.varoptions) then
  845. para:=ccallparanode.create(cloadnode.create(currpara,currpara.owner),para);
  846. end;
  847. end
  848. else
  849. begin
  850. if try_to_consume(_LKLAMMER) then
  851. begin
  852. para:=parse_paras(false,_RKLAMMER);
  853. consume(_RKLAMMER);
  854. end;
  855. end;
  856. { indicate if this call was generated by a member and
  857. no explicit self is used, this is needed to determine
  858. how to handle a destructor call (PFV) }
  859. if membercall then
  860. include(callflags,cnf_member_call);
  861. if assigned(obj) then
  862. begin
  863. if (st.symtabletype<>objectsymtable) then
  864. internalerror(200310031);
  865. p1:=ccallnode.create(para,tprocsym(sym),obj.symtable,p1,callflags);
  866. end
  867. else
  868. p1:=ccallnode.create(para,tprocsym(sym),st,p1,callflags);
  869. end;
  870. afterassignment:=prevafterassn;
  871. end;
  872. procedure handle_procvar(pv : tprocvardef;var p2 : tnode);
  873. var
  874. hp,hp2 : tnode;
  875. hpp : ^tnode;
  876. currprocdef : tprocdef;
  877. begin
  878. if not assigned(pv) then
  879. internalerror(200301121);
  880. if (m_tp_procvar in aktmodeswitches) or
  881. (m_mac_procvar in aktmodeswitches) then
  882. begin
  883. hp:=p2;
  884. hpp:=@p2;
  885. while assigned(hp) and
  886. (hp.nodetype=typeconvn) do
  887. begin
  888. hp:=ttypeconvnode(hp).left;
  889. { save orignal address of the old tree so we can replace the node }
  890. hpp:=@hp;
  891. end;
  892. if (hp.nodetype=calln) and
  893. { a procvar can't have parameters! }
  894. not assigned(tcallnode(hp).left) then
  895. begin
  896. currprocdef:=tcallnode(hp).symtableprocentry.search_procdef_byprocvardef(pv);
  897. if assigned(currprocdef) then
  898. begin
  899. hp2:=cloadnode.create_procvar(tprocsym(tcallnode(hp).symtableprocentry),currprocdef,tcallnode(hp).symtableproc);
  900. if (po_methodpointer in pv.procoptions) then
  901. tloadnode(hp2).set_mp(tcallnode(hp).get_load_methodpointer);
  902. hp.destroy;
  903. { replace the old callnode with the new loadnode }
  904. hpp^:=hp2;
  905. end;
  906. end;
  907. end;
  908. end;
  909. { the following procedure handles the access to a property symbol }
  910. procedure handle_propertysym(sym : tsym;st : tsymtable;var p1 : tnode);
  911. var
  912. paras : tnode;
  913. p2 : tnode;
  914. membercall : boolean;
  915. callflags : tcallnodeflags;
  916. begin
  917. paras:=nil;
  918. { property parameters? read them only if the property really }
  919. { has parameters }
  920. if (ppo_hasparameters in tpropertysym(sym).propoptions) then
  921. begin
  922. if try_to_consume(_LECKKLAMMER) then
  923. begin
  924. paras:=parse_paras(false,_RECKKLAMMER);
  925. consume(_RECKKLAMMER);
  926. end;
  927. end;
  928. { indexed property }
  929. if (ppo_indexed in tpropertysym(sym).propoptions) then
  930. begin
  931. p2:=cordconstnode.create(tpropertysym(sym).index,tpropertysym(sym).indextype,true);
  932. paras:=ccallparanode.create(p2,paras);
  933. end;
  934. { we need only a write property if a := follows }
  935. { if not(afterassignment) and not(in_args) then }
  936. if token=_ASSIGNMENT then
  937. begin
  938. { write property: }
  939. if not tpropertysym(sym).writeaccess.empty then
  940. begin
  941. case tpropertysym(sym).writeaccess.firstsym^.sym.typ of
  942. procsym :
  943. begin
  944. callflags:=[];
  945. { generate the method call }
  946. membercall:=maybe_load_methodpointer(st,p1);
  947. if membercall then
  948. include(callflags,cnf_member_call);
  949. p1:=ccallnode.create(paras,tprocsym(tpropertysym(sym).writeaccess.firstsym^.sym),st,p1,callflags);
  950. paras:=nil;
  951. consume(_ASSIGNMENT);
  952. { read the expression }
  953. if tpropertysym(sym).proptype.def.deftype=procvardef then
  954. getprocvardef:=tprocvardef(tpropertysym(sym).proptype.def);
  955. p2:=comp_expr(true);
  956. if assigned(getprocvardef) then
  957. handle_procvar(getprocvardef,p2);
  958. tcallnode(p1).left:=ccallparanode.create(p2,tcallnode(p1).left);
  959. { mark as property, both the tcallnode and the real call block }
  960. include(p1.flags,nf_isproperty);
  961. getprocvardef:=nil;
  962. end;
  963. fieldvarsym :
  964. begin
  965. { generate access code }
  966. symlist_to_node(p1,st,tpropertysym(sym).writeaccess);
  967. include(p1.flags,nf_isproperty);
  968. consume(_ASSIGNMENT);
  969. { read the expression }
  970. p2:=comp_expr(true);
  971. p1:=cassignmentnode.create(p1,p2);
  972. end
  973. else
  974. begin
  975. p1:=cerrornode.create;
  976. Message(parser_e_no_procedure_to_access_property);
  977. end;
  978. end;
  979. end
  980. else
  981. begin
  982. p1:=cerrornode.create;
  983. Message(parser_e_no_procedure_to_access_property);
  984. end;
  985. end
  986. else
  987. begin
  988. { read property: }
  989. if not tpropertysym(sym).readaccess.empty then
  990. begin
  991. case tpropertysym(sym).readaccess.firstsym^.sym.typ of
  992. fieldvarsym :
  993. begin
  994. { generate access code }
  995. symlist_to_node(p1,st,tpropertysym(sym).readaccess);
  996. include(p1.flags,nf_isproperty);
  997. end;
  998. procsym :
  999. begin
  1000. callflags:=[];
  1001. { generate the method call }
  1002. membercall:=maybe_load_methodpointer(st,p1);
  1003. if membercall then
  1004. include(callflags,cnf_member_call);
  1005. p1:=ccallnode.create(paras,tprocsym(tpropertysym(sym).readaccess.firstsym^.sym),st,p1,callflags);
  1006. paras:=nil;
  1007. include(p1.flags,nf_isproperty);
  1008. end
  1009. else
  1010. begin
  1011. p1:=cerrornode.create;
  1012. Message(type_e_mismatch);
  1013. end;
  1014. end;
  1015. end
  1016. else
  1017. begin
  1018. { error, no function to read property }
  1019. p1:=cerrornode.create;
  1020. Message(parser_e_no_procedure_to_access_property);
  1021. end;
  1022. end;
  1023. { release paras if not used }
  1024. if assigned(paras) then
  1025. paras.free;
  1026. end;
  1027. { the ID token has to be consumed before calling this function }
  1028. procedure do_member_read(classh:tobjectdef;getaddr : boolean;sym : tsym;var p1 : tnode;var again : boolean;callflags:tcallnodeflags);
  1029. var
  1030. static_name : string;
  1031. isclassref : boolean;
  1032. srsymtable : tsymtable;
  1033. begin
  1034. if sym=nil then
  1035. begin
  1036. { pattern is still valid unless
  1037. there is another ID just after the ID of sym }
  1038. Message1(sym_e_id_no_member,orgpattern);
  1039. p1.free;
  1040. p1:=cerrornode.create;
  1041. { try to clean up }
  1042. again:=false;
  1043. end
  1044. else
  1045. begin
  1046. if assigned(p1) then
  1047. begin
  1048. if not assigned(p1.resulttype.def) then
  1049. do_resulttypepass(p1);
  1050. isclassref:=(p1.resulttype.def.deftype=classrefdef);
  1051. end
  1052. else
  1053. isclassref:=false;
  1054. { we assume, that only procsyms and varsyms are in an object }
  1055. { symbol table, for classes, properties are allowed }
  1056. case sym.typ of
  1057. procsym:
  1058. begin
  1059. do_proc_call(sym,sym.owner,classh,
  1060. (getaddr and not(token in [_CARET,_POINT])),
  1061. again,p1,callflags);
  1062. { we need to know which procedure is called }
  1063. do_resulttypepass(p1);
  1064. { calling using classref? }
  1065. if isclassref and
  1066. (p1.nodetype=calln) and
  1067. assigned(tcallnode(p1).procdefinition) and
  1068. not(po_classmethod in tcallnode(p1).procdefinition.procoptions) and
  1069. not(tcallnode(p1).procdefinition.proctypeoption=potype_constructor) then
  1070. Message(parser_e_only_class_methods_via_class_ref);
  1071. end;
  1072. fieldvarsym:
  1073. begin
  1074. if (sp_static in sym.symoptions) then
  1075. begin
  1076. static_name:=lower(sym.owner.name^)+'_'+sym.name;
  1077. searchsym(static_name,sym,srsymtable);
  1078. check_hints(sym,sym.symoptions);
  1079. p1.free;
  1080. p1:=cloadnode.create(sym,srsymtable);
  1081. end
  1082. else
  1083. begin
  1084. if isclassref then
  1085. Message(parser_e_only_class_methods_via_class_ref);
  1086. p1:=csubscriptnode.create(sym,p1);
  1087. end;
  1088. end;
  1089. propertysym:
  1090. begin
  1091. if isclassref then
  1092. Message(parser_e_only_class_methods_via_class_ref);
  1093. handle_propertysym(sym,sym.owner,p1);
  1094. end;
  1095. else internalerror(16);
  1096. end;
  1097. end;
  1098. end;
  1099. {****************************************************************************
  1100. Factor
  1101. ****************************************************************************}
  1102. {$ifdef fpc}
  1103. {$maxfpuregisters 0}
  1104. {$endif fpc}
  1105. function factor(getaddr : boolean) : tnode;
  1106. {---------------------------------------------
  1107. Factor_read_id
  1108. ---------------------------------------------}
  1109. procedure factor_read_id(var p1:tnode;var again:boolean);
  1110. var
  1111. pc : pchar;
  1112. len : longint;
  1113. srsym : tsym;
  1114. possible_error : boolean;
  1115. srsymtable : tsymtable;
  1116. storesymtablestack : tsymtable;
  1117. htype : ttype;
  1118. static_name : string;
  1119. begin
  1120. { allow post fix operators }
  1121. again:=true;
  1122. consume_sym(srsym,srsymtable);
  1123. { Access to funcret or need to call the function? }
  1124. if (srsym.typ in [absolutevarsym,localvarsym,paravarsym]) and
  1125. (vo_is_funcret in tabstractvarsym(srsym).varoptions) and
  1126. (
  1127. (token=_LKLAMMER) or
  1128. (
  1129. (
  1130. (m_tp7 in aktmodeswitches) or
  1131. (m_delphi in aktmodeswitches)
  1132. ) and
  1133. (afterassignment or in_args) and
  1134. not(vo_is_result in tabstractvarsym(srsym).varoptions)
  1135. )
  1136. ) then
  1137. begin
  1138. storesymtablestack:=symtablestack;
  1139. symtablestack:=srsym.owner.next;
  1140. searchsym(srsym.name,srsym,srsymtable);
  1141. if not assigned(srsym) then
  1142. srsym:=generrorsym;
  1143. if (srsym.typ<>procsym) then
  1144. Message(parser_e_illegal_expression);
  1145. symtablestack:=storesymtablestack;
  1146. end;
  1147. begin
  1148. case srsym.typ of
  1149. absolutevarsym :
  1150. begin
  1151. if (tabsolutevarsym(srsym).abstyp=tovar) then
  1152. begin
  1153. p1:=nil;
  1154. symlist_to_node(p1,nil,tabsolutevarsym(srsym).ref);
  1155. p1:=ctypeconvnode.create(p1,tabsolutevarsym(srsym).vartype);
  1156. include(p1.flags,nf_absolute);
  1157. end
  1158. else
  1159. p1:=cloadnode.create(srsym,srsymtable);
  1160. end;
  1161. globalvarsym,
  1162. localvarsym,
  1163. paravarsym,
  1164. fieldvarsym :
  1165. begin
  1166. if (sp_static in srsym.symoptions) then
  1167. begin
  1168. static_name:=lower(srsym.owner.name^)+'_'+srsym.name;
  1169. searchsym(static_name,srsym,srsymtable);
  1170. check_hints(srsym,srsym.symoptions);
  1171. end
  1172. else
  1173. begin
  1174. { are we in a class method, we check here the
  1175. srsymtable, because a field in another object
  1176. also has objectsymtable. And withsymtable is
  1177. not possible for self in class methods (PFV) }
  1178. if (srsymtable.symtabletype=objectsymtable) and
  1179. assigned(current_procinfo) and
  1180. (po_classmethod in current_procinfo.procdef.procoptions) then
  1181. Message(parser_e_only_class_methods);
  1182. end;
  1183. case srsymtable.symtabletype of
  1184. objectsymtable :
  1185. begin
  1186. p1:=csubscriptnode.create(srsym,load_self_node);
  1187. node_tree_set_filepos(p1,aktfilepos);
  1188. end;
  1189. withsymtable :
  1190. begin
  1191. p1:=csubscriptnode.create(srsym,tnode(twithsymtable(srsymtable).withrefnode).getcopy);
  1192. node_tree_set_filepos(p1,aktfilepos);
  1193. end;
  1194. else
  1195. p1:=cloadnode.create(srsym,srsymtable);
  1196. end;
  1197. end;
  1198. typedconstsym :
  1199. begin
  1200. p1:=cloadnode.create(srsym,srsymtable);
  1201. end;
  1202. syssym :
  1203. begin
  1204. p1:=statement_syssym(tsyssym(srsym).number);
  1205. end;
  1206. typesym :
  1207. begin
  1208. htype.setsym(srsym);
  1209. if not assigned(htype.def) then
  1210. begin
  1211. again:=false;
  1212. end
  1213. else
  1214. begin
  1215. { We need to know if this unit uses Variants }
  1216. if (htype.def=cvarianttype.def) and
  1217. not(cs_compilesystem in aktmoduleswitches) then
  1218. current_module.flags:=current_module.flags or uf_uses_variants;
  1219. if try_to_consume(_LKLAMMER) then
  1220. begin
  1221. p1:=comp_expr(true);
  1222. consume(_RKLAMMER);
  1223. p1:=ctypeconvnode.create_explicit(p1,htype);
  1224. end
  1225. else { not LKLAMMER }
  1226. if (token=_POINT) and
  1227. is_object(htype.def) then
  1228. begin
  1229. consume(_POINT);
  1230. if assigned(current_procinfo) and
  1231. assigned(current_procinfo.procdef._class) and
  1232. not(getaddr) then
  1233. begin
  1234. if current_procinfo.procdef._class.is_related(tobjectdef(htype.def)) then
  1235. begin
  1236. p1:=ctypenode.create(htype);
  1237. { search also in inherited methods }
  1238. srsym:=searchsym_in_class(tobjectdef(htype.def),pattern);
  1239. check_hints(srsym,srsym.symoptions);
  1240. consume(_ID);
  1241. do_member_read(tobjectdef(htype.def),false,srsym,p1,again,[]);
  1242. end
  1243. else
  1244. begin
  1245. Message(parser_e_no_super_class);
  1246. again:=false;
  1247. end;
  1248. end
  1249. else
  1250. begin
  1251. { allows @TObject.Load }
  1252. { also allows static methods and variables }
  1253. p1:=ctypenode.create(htype);
  1254. { TP allows also @TMenu.Load if Load is only }
  1255. { defined in an anchestor class }
  1256. srsym:=search_class_member(tobjectdef(htype.def),pattern);
  1257. check_hints(srsym,srsym.symoptions);
  1258. if not assigned(srsym) then
  1259. Message1(sym_e_id_no_member,orgpattern)
  1260. else if not(getaddr) and not(sp_static in srsym.symoptions) then
  1261. Message(sym_e_only_static_in_static)
  1262. else
  1263. begin
  1264. consume(_ID);
  1265. do_member_read(tobjectdef(htype.def),getaddr,srsym,p1,again,[]);
  1266. end;
  1267. end;
  1268. end
  1269. else
  1270. begin
  1271. { class reference ? }
  1272. if is_class(htype.def) then
  1273. begin
  1274. if getaddr and (token=_POINT) then
  1275. begin
  1276. consume(_POINT);
  1277. { allows @Object.Method }
  1278. { also allows static methods and variables }
  1279. p1:=ctypenode.create(htype);
  1280. { TP allows also @TMenu.Load if Load is only }
  1281. { defined in an anchestor class }
  1282. srsym:=search_class_member(tobjectdef(htype.def),pattern);
  1283. check_hints(srsym,srsym.symoptions);
  1284. if not assigned(srsym) then
  1285. Message1(sym_e_id_no_member,orgpattern)
  1286. else
  1287. begin
  1288. consume(_ID);
  1289. do_member_read(tobjectdef(htype.def),getaddr,srsym,p1,again,[]);
  1290. end;
  1291. end
  1292. else
  1293. begin
  1294. p1:=ctypenode.create(htype);
  1295. { For a type block we simply return only
  1296. the type. For all other blocks we return
  1297. a loadvmt node }
  1298. if (block_type<>bt_type) then
  1299. p1:=cloadvmtaddrnode.create(p1);
  1300. end;
  1301. end
  1302. else
  1303. p1:=ctypenode.create(htype);
  1304. end;
  1305. end;
  1306. end;
  1307. enumsym :
  1308. begin
  1309. p1:=genenumnode(tenumsym(srsym));
  1310. end;
  1311. constsym :
  1312. begin
  1313. case tconstsym(srsym).consttyp of
  1314. constord :
  1315. begin
  1316. if tconstsym(srsym).consttype.def=nil then
  1317. internalerror(200403232);
  1318. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,tconstsym(srsym).consttype,true);
  1319. end;
  1320. conststring :
  1321. begin
  1322. len:=tconstsym(srsym).value.len;
  1323. if not(cs_ansistrings in aktlocalswitches) and (len>255) then
  1324. len:=255;
  1325. getmem(pc,len+1);
  1326. move(pchar(tconstsym(srsym).value.valueptr)^,pc^,len);
  1327. pc[len]:=#0;
  1328. p1:=cstringconstnode.createpchar(pc,len);
  1329. end;
  1330. constwstring :
  1331. p1:=cstringconstnode.createwstr(pcompilerwidestring(tconstsym(srsym).value.valueptr));
  1332. constreal :
  1333. p1:=crealconstnode.create(pbestreal(tconstsym(srsym).value.valueptr)^,pbestrealtype^);
  1334. constset :
  1335. p1:=csetconstnode.create(pconstset(tconstsym(srsym).value.valueptr),tconstsym(srsym).consttype);
  1336. constpointer :
  1337. p1:=cpointerconstnode.create(tconstsym(srsym).value.valueordptr,tconstsym(srsym).consttype);
  1338. constnil :
  1339. p1:=cnilnode.create;
  1340. constresourcestring:
  1341. begin
  1342. p1:=cloadnode.create(srsym,srsymtable);
  1343. do_resulttypepass(p1);
  1344. {$ifdef ansistring_bits}
  1345. case aktansistring_bits of
  1346. sb_16:
  1347. p1.resulttype:=cansistringtype16;
  1348. sb_32:
  1349. p1.resulttype:=cansistringtype32;
  1350. sb_64:
  1351. p1.resulttype:=cansistringtype64;
  1352. end;
  1353. {$else}
  1354. p1.resulttype:=cansistringtype;
  1355. {$endif}
  1356. end;
  1357. constguid :
  1358. p1:=cguidconstnode.create(pguid(tconstsym(srsym).value.valueptr)^);
  1359. else
  1360. internalerror(200507181);
  1361. end;
  1362. end;
  1363. procsym :
  1364. begin
  1365. { are we in a class method ? }
  1366. possible_error:=(srsymtable.symtabletype<>withsymtable) and
  1367. (srsym.owner.symtabletype=objectsymtable) and
  1368. not(is_interface(tdef(srsym.owner.defowner))) and
  1369. assigned(current_procinfo) and
  1370. (po_classmethod in current_procinfo.procdef.procoptions);
  1371. do_proc_call(srsym,srsymtable,nil,
  1372. (getaddr and not(token in [_CARET,_POINT])),
  1373. again,p1,[]);
  1374. { we need to know which procedure is called }
  1375. if possible_error then
  1376. begin
  1377. do_resulttypepass(p1);
  1378. if (p1.nodetype=calln) and
  1379. assigned(tcallnode(p1).procdefinition) and
  1380. not(tcallnode(p1).procdefinition.proctypeoption=potype_constructor) and
  1381. not(po_classmethod in tcallnode(p1).procdefinition.procoptions) then
  1382. Message(parser_e_only_class_methods);
  1383. end;
  1384. end;
  1385. propertysym :
  1386. begin
  1387. { access to property in a method }
  1388. { are we in a class method ? }
  1389. if (srsymtable.symtabletype=objectsymtable) and
  1390. assigned(current_procinfo) and
  1391. (po_classmethod in current_procinfo.procdef.procoptions) then
  1392. Message(parser_e_only_class_methods);
  1393. { no method pointer }
  1394. p1:=nil;
  1395. handle_propertysym(srsym,srsymtable,p1);
  1396. end;
  1397. labelsym :
  1398. begin
  1399. { Support @label }
  1400. if getaddr then
  1401. p1:=cloadnode.create(srsym,srsym.owner)
  1402. else
  1403. begin
  1404. consume(_COLON);
  1405. if tlabelsym(srsym).defined then
  1406. Message(sym_e_label_already_defined);
  1407. tlabelsym(srsym).defined:=true;
  1408. p1:=clabelnode.create(nil);
  1409. tlabelsym(srsym).code:=p1;
  1410. end;
  1411. end;
  1412. errorsym :
  1413. begin
  1414. p1:=cerrornode.create;
  1415. if try_to_consume(_LKLAMMER) then
  1416. begin
  1417. parse_paras(false,_RKLAMMER);
  1418. consume(_RKLAMMER);
  1419. end;
  1420. end;
  1421. else
  1422. begin
  1423. p1:=cerrornode.create;
  1424. Message(parser_e_illegal_expression);
  1425. end;
  1426. end; { end case }
  1427. end;
  1428. end;
  1429. {---------------------------------------------
  1430. Factor_Read_Set
  1431. ---------------------------------------------}
  1432. { Read a set between [] }
  1433. function factor_read_set:tnode;
  1434. var
  1435. p1,p2 : tnode;
  1436. lastp,
  1437. buildp : tarrayconstructornode;
  1438. begin
  1439. buildp:=nil;
  1440. { be sure that a least one arrayconstructn is used, also for an
  1441. empty [] }
  1442. if token=_RECKKLAMMER then
  1443. buildp:=carrayconstructornode.create(nil,buildp)
  1444. else
  1445. repeat
  1446. p1:=comp_expr(true);
  1447. if try_to_consume(_POINTPOINT) then
  1448. begin
  1449. p2:=comp_expr(true);
  1450. p1:=carrayconstructorrangenode.create(p1,p2);
  1451. end;
  1452. { insert at the end of the tree, to get the correct order }
  1453. if not assigned(buildp) then
  1454. begin
  1455. buildp:=carrayconstructornode.create(p1,nil);
  1456. lastp:=buildp;
  1457. end
  1458. else
  1459. begin
  1460. lastp.right:=carrayconstructornode.create(p1,nil);
  1461. lastp:=tarrayconstructornode(lastp.right);
  1462. end;
  1463. { there could be more elements }
  1464. until not try_to_consume(_COMMA);
  1465. factor_read_set:=buildp;
  1466. end;
  1467. {---------------------------------------------
  1468. PostFixOperators
  1469. ---------------------------------------------}
  1470. procedure postfixoperators(var p1:tnode;var again:boolean);
  1471. { tries to avoid syntax errors after invalid qualifiers }
  1472. procedure recoverconsume_postfixops;
  1473. begin
  1474. repeat
  1475. if not try_to_consume(_CARET) then
  1476. if try_to_consume(_POINT) then
  1477. try_to_consume(_ID)
  1478. else if try_to_consume(_LECKKLAMMER) then
  1479. begin
  1480. repeat
  1481. comp_expr(true);
  1482. until not try_to_consume(_COMMA);
  1483. consume(_RECKKLAMMER);
  1484. end
  1485. else
  1486. break;
  1487. until false;
  1488. end;
  1489. procedure handle_variantarray;
  1490. var
  1491. p4 : tnode;
  1492. newstatement : tstatementnode;
  1493. tempresultvariant,
  1494. temp : ttempcreatenode;
  1495. paras : tcallparanode;
  1496. newblock : tnode;
  1497. countindices : aint;
  1498. begin
  1499. { create statements with call initialize the arguments and
  1500. call fpc_dynarr_setlength }
  1501. newblock:=internalstatements(newstatement);
  1502. { get temp for array of indicies,
  1503. we set the real size later }
  1504. temp:=ctempcreatenode.create(sinttype,4,tt_persistent,false);
  1505. addstatement(newstatement,temp);
  1506. countindices:=0;
  1507. repeat
  1508. p4:=comp_expr(true);
  1509. addstatement(newstatement,cassignmentnode.create(
  1510. ctemprefnode.create_offset(temp,countindices*sinttype.def.size),p4));
  1511. inc(countindices);
  1512. until not try_to_consume(_COMMA);
  1513. { set real size }
  1514. temp.size:=countindices*sinttype.def.size;
  1515. consume(_RECKKLAMMER);
  1516. { we need only a write access if a := follows }
  1517. if token=_ASSIGNMENT then
  1518. begin
  1519. consume(_ASSIGNMENT);
  1520. p4:=comp_expr(true);
  1521. { create call to fpc_vararray_put }
  1522. paras:=ccallparanode.create(cordconstnode.create
  1523. (countindices,sinttype,true),
  1524. ccallparanode.create(caddrnode.create_internal
  1525. (ctemprefnode.create(temp)),
  1526. ccallparanode.create(ctypeconvnode.create_internal(p4,cvarianttype),
  1527. ccallparanode.create(p1
  1528. ,nil))));
  1529. addstatement(newstatement,ccallnode.createintern('fpc_vararray_put',paras));
  1530. addstatement(newstatement,ctempdeletenode.create(temp));
  1531. end
  1532. else
  1533. begin
  1534. { create temp for result }
  1535. tempresultvariant:=ctempcreatenode.create(cvarianttype,cvarianttype.def.size,tt_persistent,true);
  1536. addstatement(newstatement,tempresultvariant);
  1537. { create call to fpc_vararray_get }
  1538. paras:=ccallparanode.create(cordconstnode.create
  1539. (countindices,sinttype,true),
  1540. ccallparanode.create(caddrnode.create_internal
  1541. (ctemprefnode.create(temp)),
  1542. ccallparanode.create(p1,
  1543. ccallparanode.create(
  1544. ctemprefnode.create(tempresultvariant)
  1545. ,nil))));
  1546. addstatement(newstatement,ccallnode.createintern('fpc_vararray_get',paras));
  1547. addstatement(newstatement,ctempdeletenode.create(temp));
  1548. { the last statement should return the value as
  1549. location and type, this is done be referencing the
  1550. temp and converting it first from a persistent temp to
  1551. normal temp }
  1552. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempresultvariant));
  1553. addstatement(newstatement,ctemprefnode.create(tempresultvariant));
  1554. end;
  1555. p1:=newblock;
  1556. end;
  1557. var
  1558. store_static : boolean;
  1559. protsym : tpropertysym;
  1560. p2,p3 : tnode;
  1561. hsym : tsym;
  1562. classh : tobjectdef;
  1563. label
  1564. skipreckklammercheck;
  1565. begin
  1566. again:=true;
  1567. while again do
  1568. begin
  1569. { we need the resulttype }
  1570. do_resulttypepass(p1);
  1571. if codegenerror then
  1572. begin
  1573. recoverconsume_postfixops;
  1574. exit;
  1575. end;
  1576. { handle token }
  1577. case token of
  1578. _CARET:
  1579. begin
  1580. consume(_CARET);
  1581. { support tp/mac procvar^ if the procvar returns a
  1582. pointer type }
  1583. if ((m_tp_procvar in aktmodeswitches) or
  1584. (m_mac_procvar in aktmodeswitches)) and
  1585. (p1.resulttype.def.deftype=procvardef) and
  1586. (tprocvardef(p1.resulttype.def).rettype.def.deftype=pointerdef) then
  1587. begin
  1588. p1:=ccallnode.create_procvar(nil,p1);
  1589. resulttypepass(p1);
  1590. end;
  1591. if (p1.resulttype.def.deftype<>pointerdef) then
  1592. begin
  1593. { ^ as binary operator is a problem!!!! (FK) }
  1594. again:=false;
  1595. Message(parser_e_invalid_qualifier);
  1596. recoverconsume_postfixops;
  1597. p1.destroy;
  1598. p1:=cerrornode.create;
  1599. end
  1600. else
  1601. p1:=cderefnode.create(p1);
  1602. end;
  1603. _LECKKLAMMER:
  1604. begin
  1605. if is_class_or_interface(p1.resulttype.def) then
  1606. begin
  1607. { default property }
  1608. protsym:=search_default_property(tobjectdef(p1.resulttype.def));
  1609. if not(assigned(protsym)) then
  1610. begin
  1611. p1.destroy;
  1612. p1:=cerrornode.create;
  1613. again:=false;
  1614. message(parser_e_no_default_property_available);
  1615. end
  1616. else
  1617. begin
  1618. { The property symbol is referenced indirect }
  1619. inc(protsym.refs);
  1620. handle_propertysym(protsym,protsym.owner,p1);
  1621. end;
  1622. end
  1623. else
  1624. begin
  1625. consume(_LECKKLAMMER);
  1626. repeat
  1627. case p1.resulttype.def.deftype of
  1628. pointerdef:
  1629. begin
  1630. { support delphi autoderef }
  1631. if (tpointerdef(p1.resulttype.def).pointertype.def.deftype=arraydef) and
  1632. (m_autoderef in aktmodeswitches) then
  1633. begin
  1634. p1:=cderefnode.create(p1);
  1635. end;
  1636. p2:=comp_expr(true);
  1637. p1:=cvecnode.create(p1,p2);
  1638. end;
  1639. variantdef:
  1640. begin
  1641. handle_variantarray;
  1642. { the RECKKLAMMER is already read }
  1643. goto skipreckklammercheck;
  1644. end;
  1645. stringdef :
  1646. begin
  1647. p2:=comp_expr(true);
  1648. p1:=cvecnode.create(p1,p2);
  1649. end;
  1650. arraydef :
  1651. begin
  1652. p2:=comp_expr(true);
  1653. { support SEG:OFS for go32v2 Mem[] }
  1654. if (target_info.system in [system_i386_go32v2,system_i386_watcom]) and
  1655. (p1.nodetype=loadn) and
  1656. assigned(tloadnode(p1).symtableentry) and
  1657. assigned(tloadnode(p1).symtableentry.owner.name) and
  1658. (tloadnode(p1).symtableentry.owner.name^='SYSTEM') and
  1659. ((tloadnode(p1).symtableentry.name='MEM') or
  1660. (tloadnode(p1).symtableentry.name='MEMW') or
  1661. (tloadnode(p1).symtableentry.name='MEML')) then
  1662. begin
  1663. if try_to_consume(_COLON) then
  1664. begin
  1665. p3:=caddnode.create(muln,cordconstnode.create($10,s32inttype,false),p2);
  1666. p2:=comp_expr(true);
  1667. p2:=caddnode.create(addn,p2,p3);
  1668. p1:=cvecnode.create(p1,p2);
  1669. include(tvecnode(p1).flags,nf_memseg);
  1670. include(tvecnode(p1).flags,nf_memindex);
  1671. end
  1672. else
  1673. begin
  1674. p1:=cvecnode.create(p1,p2);
  1675. include(tvecnode(p1).flags,nf_memindex);
  1676. end;
  1677. end
  1678. else
  1679. p1:=cvecnode.create(p1,p2);
  1680. end;
  1681. else
  1682. begin
  1683. Message(parser_e_invalid_qualifier);
  1684. p1.destroy;
  1685. p1:=cerrornode.create;
  1686. comp_expr(true);
  1687. again:=false;
  1688. end;
  1689. end;
  1690. do_resulttypepass(p1);
  1691. until not try_to_consume(_COMMA);
  1692. consume(_RECKKLAMMER);
  1693. { handle_variantarray eats the RECKKLAMMER and jumps here }
  1694. skipreckklammercheck:
  1695. end;
  1696. end;
  1697. _POINT :
  1698. begin
  1699. consume(_POINT);
  1700. if (p1.resulttype.def.deftype=pointerdef) and
  1701. (m_autoderef in aktmodeswitches) then
  1702. begin
  1703. p1:=cderefnode.create(p1);
  1704. do_resulttypepass(p1);
  1705. end;
  1706. case p1.resulttype.def.deftype of
  1707. recorddef:
  1708. begin
  1709. if token=_ID then
  1710. begin
  1711. hsym:=tsym(trecorddef(p1.resulttype.def).symtable.search(pattern));
  1712. check_hints(hsym,hsym.symoptions);
  1713. if assigned(hsym) and
  1714. (hsym.typ=fieldvarsym) then
  1715. p1:=csubscriptnode.create(hsym,p1)
  1716. else
  1717. begin
  1718. Message1(sym_e_illegal_field,pattern);
  1719. p1.destroy;
  1720. p1:=cerrornode.create;
  1721. end;
  1722. end;
  1723. consume(_ID);
  1724. end;
  1725. variantdef:
  1726. begin
  1727. end;
  1728. classrefdef:
  1729. begin
  1730. if token=_ID then
  1731. begin
  1732. classh:=tobjectdef(tclassrefdef(p1.resulttype.def).pointertype.def);
  1733. hsym:=searchsym_in_class(classh,pattern);
  1734. check_hints(hsym,hsym.symoptions);
  1735. if hsym=nil then
  1736. begin
  1737. Message1(sym_e_id_no_member,orgpattern);
  1738. p1.destroy;
  1739. p1:=cerrornode.create;
  1740. { try to clean up }
  1741. consume(_ID);
  1742. end
  1743. else
  1744. begin
  1745. consume(_ID);
  1746. do_member_read(classh,getaddr,hsym,p1,again,[]);
  1747. end;
  1748. end
  1749. else { Error }
  1750. Consume(_ID);
  1751. end;
  1752. objectdef:
  1753. begin
  1754. if token=_ID then
  1755. begin
  1756. store_static:=allow_only_static;
  1757. allow_only_static:=false;
  1758. classh:=tobjectdef(p1.resulttype.def);
  1759. hsym:=searchsym_in_class(classh,pattern);
  1760. check_hints(hsym,hsym.symoptions);
  1761. allow_only_static:=store_static;
  1762. if hsym=nil then
  1763. begin
  1764. Message1(sym_e_id_no_member,orgpattern);
  1765. p1.destroy;
  1766. p1:=cerrornode.create;
  1767. { try to clean up }
  1768. consume(_ID);
  1769. end
  1770. else
  1771. begin
  1772. consume(_ID);
  1773. do_member_read(classh,getaddr,hsym,p1,again,[]);
  1774. end;
  1775. end
  1776. else { Error }
  1777. Consume(_ID);
  1778. end;
  1779. pointerdef:
  1780. begin
  1781. Message(parser_e_invalid_qualifier);
  1782. if tpointerdef(p1.resulttype.def).pointertype.def.deftype in [recorddef,objectdef,classrefdef] then
  1783. Message(parser_h_maybe_deref_caret_missing);
  1784. end;
  1785. else
  1786. begin
  1787. Message(parser_e_invalid_qualifier);
  1788. p1.destroy;
  1789. p1:=cerrornode.create;
  1790. { Error }
  1791. consume(_ID);
  1792. end;
  1793. end;
  1794. end;
  1795. else
  1796. begin
  1797. { is this a procedure variable ? }
  1798. if assigned(p1.resulttype.def) and
  1799. (p1.resulttype.def.deftype=procvardef) then
  1800. begin
  1801. if assigned(getprocvardef) and
  1802. equal_defs(p1.resulttype.def,getprocvardef) then
  1803. again:=false
  1804. else
  1805. begin
  1806. if try_to_consume(_LKLAMMER) then
  1807. begin
  1808. p2:=parse_paras(false,_RKLAMMER);
  1809. consume(_RKLAMMER);
  1810. p1:=ccallnode.create_procvar(p2,p1);
  1811. { proc():= is never possible }
  1812. if token=_ASSIGNMENT then
  1813. begin
  1814. Message(parser_e_illegal_expression);
  1815. p1.free;
  1816. p1:=cerrornode.create;
  1817. again:=false;
  1818. end;
  1819. end
  1820. else
  1821. again:=false;
  1822. end;
  1823. end
  1824. else
  1825. again:=false;
  1826. end;
  1827. end;
  1828. end; { while again }
  1829. end;
  1830. {---------------------------------------------
  1831. Factor (Main)
  1832. ---------------------------------------------}
  1833. var
  1834. l : longint;
  1835. ic : int64;
  1836. qc : qword;
  1837. {$ifndef cpu64}
  1838. card : cardinal;
  1839. {$endif cpu64}
  1840. oldp1,
  1841. p1 : tnode;
  1842. code : integer;
  1843. again : boolean;
  1844. sym : tsym;
  1845. pd : tprocdef;
  1846. classh : tobjectdef;
  1847. d : bestreal;
  1848. hs,hsorg : string;
  1849. htype : ttype;
  1850. filepos : tfileposinfo;
  1851. {---------------------------------------------
  1852. Helpers
  1853. ---------------------------------------------}
  1854. procedure check_tokenpos;
  1855. begin
  1856. if (p1<>oldp1) then
  1857. begin
  1858. if assigned(p1) then
  1859. p1.fileinfo:=filepos;
  1860. oldp1:=p1;
  1861. filepos:=akttokenpos;
  1862. end;
  1863. end;
  1864. begin
  1865. oldp1:=nil;
  1866. p1:=nil;
  1867. filepos:=akttokenpos;
  1868. again:=false;
  1869. if token=_ID then
  1870. begin
  1871. again:=true;
  1872. { Handle references to self }
  1873. if (idtoken=_SELF) and
  1874. not(block_type in [bt_const,bt_type]) and
  1875. assigned(current_procinfo) and
  1876. assigned(current_procinfo.procdef._class) then
  1877. begin
  1878. p1:=load_self_node;
  1879. consume(_ID);
  1880. again:=true;
  1881. end
  1882. else
  1883. factor_read_id(p1,again);
  1884. if again then
  1885. begin
  1886. check_tokenpos;
  1887. { handle post fix operators }
  1888. postfixoperators(p1,again);
  1889. end;
  1890. end
  1891. else
  1892. case token of
  1893. _RETURN :
  1894. begin
  1895. consume(_RETURN);
  1896. p1 := cexitnode.create(comp_expr(true));
  1897. end;
  1898. _INHERITED :
  1899. begin
  1900. again:=true;
  1901. consume(_INHERITED);
  1902. if assigned(current_procinfo) and
  1903. assigned(current_procinfo.procdef._class) then
  1904. begin
  1905. classh:=current_procinfo.procdef._class.childof;
  1906. { if inherited; only then we need the method with
  1907. the same name }
  1908. if token in endtokens then
  1909. begin
  1910. hs:=current_procinfo.procdef.procsym.name;
  1911. hsorg:=current_procinfo.procdef.procsym.realname;
  1912. anon_inherited:=true;
  1913. { For message methods we need to search using the message
  1914. number or string }
  1915. pd:=tprocsym(current_procinfo.procdef.procsym).first_procdef;
  1916. if (po_msgint in pd.procoptions) then
  1917. sym:=searchsym_in_class_by_msgint(classh,pd.messageinf.i)
  1918. else
  1919. if (po_msgstr in pd.procoptions) then
  1920. sym:=searchsym_in_class_by_msgstr(classh,pd.messageinf.str)
  1921. else
  1922. sym:=searchsym_in_class(classh,hs);
  1923. end
  1924. else
  1925. begin
  1926. hs:=pattern;
  1927. hsorg:=orgpattern;
  1928. consume(_ID);
  1929. anon_inherited:=false;
  1930. sym:=searchsym_in_class(classh,hs);
  1931. end;
  1932. if assigned(sym) then
  1933. begin
  1934. check_hints(sym,sym.symoptions);
  1935. { load the procdef from the inherited class and
  1936. not from self }
  1937. if sym.typ in [procsym,propertysym] then
  1938. begin
  1939. if (sym.typ = procsym) then
  1940. begin
  1941. htype.setdef(classh);
  1942. if (po_classmethod in current_procinfo.procdef.procoptions) or
  1943. (po_staticmethod in current_procinfo.procdef.procoptions) then
  1944. htype.setdef(tclassrefdef.create(htype));
  1945. p1:=ctypenode.create(htype);
  1946. end;
  1947. end
  1948. else
  1949. begin
  1950. Message(parser_e_methode_id_expected);
  1951. p1:=cerrornode.create;
  1952. end;
  1953. do_member_read(classh,false,sym,p1,again,[cnf_inherited,cnf_anon_inherited]);
  1954. end
  1955. else
  1956. begin
  1957. if anon_inherited then
  1958. begin
  1959. { For message methods we need to call DefaultHandler }
  1960. if (po_msgint in pd.procoptions) or
  1961. (po_msgstr in pd.procoptions) then
  1962. begin
  1963. sym:=searchsym_in_class(classh,'DEFAULTHANDLER');
  1964. if not assigned(sym) or
  1965. (sym.typ<>procsym) then
  1966. internalerror(200303171);
  1967. p1:=nil;
  1968. do_proc_call(sym,sym.owner,classh,false,again,p1,[]);
  1969. end
  1970. else
  1971. begin
  1972. { we need to ignore the inherited; }
  1973. p1:=cnothingnode.create;
  1974. end;
  1975. end
  1976. else
  1977. begin
  1978. Message1(sym_e_id_no_member,hsorg);
  1979. p1:=cerrornode.create;
  1980. end;
  1981. again:=false;
  1982. end;
  1983. { turn auto inheriting off }
  1984. anon_inherited:=false;
  1985. end
  1986. else
  1987. begin
  1988. Message(parser_e_generic_methods_only_in_methods);
  1989. again:=false;
  1990. p1:=cerrornode.create;
  1991. end;
  1992. postfixoperators(p1,again);
  1993. end;
  1994. _INTCONST :
  1995. begin
  1996. {$ifdef cpu64}
  1997. { when already running under 64bit must read int64 constant, because reading
  1998. cardinal first will also succeed (code=0) for values > maxcardinal, because
  1999. range checking is off by default (PFV) }
  2000. val(pattern,ic,code);
  2001. if code=0 then
  2002. begin
  2003. consume(_INTCONST);
  2004. int_to_type(ic,htype);
  2005. p1:=cordconstnode.create(ic,htype,true);
  2006. end
  2007. else
  2008. begin
  2009. { try qword next }
  2010. val(pattern,qc,code);
  2011. if code=0 then
  2012. begin
  2013. consume(_INTCONST);
  2014. htype:=u64inttype;
  2015. p1:=cordconstnode.create(qc,htype,true);
  2016. end;
  2017. end;
  2018. {$else}
  2019. { try cardinal first }
  2020. val(pattern,card,code);
  2021. if code=0 then
  2022. begin
  2023. consume(_INTCONST);
  2024. int_to_type(card,htype);
  2025. p1:=cordconstnode.create(card,htype,true);
  2026. end
  2027. else
  2028. begin
  2029. { then longint }
  2030. val(pattern,l,code);
  2031. if code = 0 then
  2032. begin
  2033. consume(_INTCONST);
  2034. int_to_type(l,htype);
  2035. p1:=cordconstnode.create(l,htype,true);
  2036. end
  2037. else
  2038. begin
  2039. { then int64 }
  2040. val(pattern,ic,code);
  2041. if code=0 then
  2042. begin
  2043. consume(_INTCONST);
  2044. int_to_type(ic,htype);
  2045. p1:=cordconstnode.create(ic,htype,true);
  2046. end
  2047. else
  2048. begin
  2049. { try qword next }
  2050. val(pattern,qc,code);
  2051. if code=0 then
  2052. begin
  2053. consume(_INTCONST);
  2054. htype:=u64inttype;
  2055. p1:=cordconstnode.create(tconstexprint(qc),htype,true);
  2056. end;
  2057. end;
  2058. end;
  2059. end;
  2060. {$endif}
  2061. if code<>0 then
  2062. begin
  2063. { finally float }
  2064. val(pattern,d,code);
  2065. if code<>0 then
  2066. begin
  2067. Message(parser_e_invalid_integer);
  2068. consume(_INTCONST);
  2069. l:=1;
  2070. p1:=cordconstnode.create(l,sinttype,true);
  2071. end
  2072. else
  2073. begin
  2074. consume(_INTCONST);
  2075. p1:=crealconstnode.create(d,pbestrealtype^);
  2076. end;
  2077. end;
  2078. end;
  2079. _REALNUMBER :
  2080. begin
  2081. val(pattern,d,code);
  2082. if code<>0 then
  2083. begin
  2084. Message(parser_e_error_in_real);
  2085. d:=1.0;
  2086. end;
  2087. consume(_REALNUMBER);
  2088. p1:=crealconstnode.create(d,pbestrealtype^);
  2089. end;
  2090. _STRING :
  2091. begin
  2092. string_dec(htype);
  2093. { STRING can be also a type cast }
  2094. if try_to_consume(_LKLAMMER) then
  2095. begin
  2096. p1:=comp_expr(true);
  2097. consume(_RKLAMMER);
  2098. p1:=ctypeconvnode.create_explicit(p1,htype);
  2099. { handle postfix operators here e.g. string(a)[10] }
  2100. again:=true;
  2101. postfixoperators(p1,again);
  2102. end
  2103. else
  2104. p1:=ctypenode.create(htype);
  2105. end;
  2106. _FILE :
  2107. begin
  2108. htype:=cfiletype;
  2109. consume(_FILE);
  2110. { FILE can be also a type cast }
  2111. if try_to_consume(_LKLAMMER) then
  2112. begin
  2113. p1:=comp_expr(true);
  2114. consume(_RKLAMMER);
  2115. p1:=ctypeconvnode.create_explicit(p1,htype);
  2116. { handle postfix operators here e.g. string(a)[10] }
  2117. again:=true;
  2118. postfixoperators(p1,again);
  2119. end
  2120. else
  2121. begin
  2122. p1:=ctypenode.create(htype);
  2123. end;
  2124. end;
  2125. _CSTRING :
  2126. begin
  2127. p1:=cstringconstnode.createstr(pattern,st_default);
  2128. consume(_CSTRING);
  2129. end;
  2130. _CCHAR :
  2131. begin
  2132. p1:=cordconstnode.create(ord(pattern[1]),cchartype,true);
  2133. consume(_CCHAR);
  2134. end;
  2135. _CWSTRING:
  2136. begin
  2137. p1:=cstringconstnode.createwstr(patternw);
  2138. consume(_CWSTRING);
  2139. end;
  2140. _CWCHAR:
  2141. begin
  2142. p1:=cordconstnode.create(ord(getcharwidestring(patternw,0)),cwidechartype,true);
  2143. consume(_CWCHAR);
  2144. end;
  2145. _KLAMMERAFFE :
  2146. begin
  2147. consume(_KLAMMERAFFE);
  2148. got_addrn:=true;
  2149. { support both @<x> and @(<x>) }
  2150. if try_to_consume(_LKLAMMER) then
  2151. begin
  2152. p1:=factor(true);
  2153. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2154. begin
  2155. again:=true;
  2156. postfixoperators(p1,again);
  2157. end;
  2158. consume(_RKLAMMER);
  2159. end
  2160. else
  2161. p1:=factor(true);
  2162. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2163. begin
  2164. again:=true;
  2165. postfixoperators(p1,again);
  2166. end;
  2167. got_addrn:=false;
  2168. p1:=caddrnode.create(p1);
  2169. if cs_typed_addresses in aktlocalswitches then
  2170. include(p1.flags,nf_typedaddr);
  2171. { Store the procvar that we are expecting, the
  2172. addrn will use the information to find the correct
  2173. procdef or it will return an error }
  2174. if assigned(getprocvardef) and
  2175. (taddrnode(p1).left.nodetype = loadn) then
  2176. taddrnode(p1).getprocvardef:=getprocvardef;
  2177. end;
  2178. _LKLAMMER :
  2179. begin
  2180. consume(_LKLAMMER);
  2181. p1:=comp_expr(true);
  2182. consume(_RKLAMMER);
  2183. { it's not a good solution }
  2184. { but (a+b)^ makes some problems }
  2185. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2186. begin
  2187. again:=true;
  2188. postfixoperators(p1,again);
  2189. end;
  2190. end;
  2191. _LECKKLAMMER :
  2192. begin
  2193. consume(_LECKKLAMMER);
  2194. p1:=factor_read_set;
  2195. consume(_RECKKLAMMER);
  2196. end;
  2197. _PLUS :
  2198. begin
  2199. consume(_PLUS);
  2200. p1:=factor(false);
  2201. { we must generate a new node to do 0+<p1> otherwise the + will
  2202. not be checked }
  2203. p1:=caddnode.create(addn,genintconstnode(0),p1);
  2204. end;
  2205. _MINUS :
  2206. begin
  2207. consume(_MINUS);
  2208. if (token = _INTCONST) then
  2209. begin
  2210. { ugly hack, but necessary to be able to parse }
  2211. { -9223372036854775808 as int64 (JM) }
  2212. pattern := '-'+pattern;
  2213. p1:=sub_expr(oppower,false);
  2214. { -1 ** 4 should be - (1 ** 4) and not
  2215. (-1) ** 4
  2216. This was the reason of tw0869.pp test failure PM }
  2217. if p1.nodetype=starstarn then
  2218. begin
  2219. if tbinarynode(p1).left.nodetype=ordconstn then
  2220. begin
  2221. tordconstnode(tbinarynode(p1).left).value:=-tordconstnode(tbinarynode(p1).left).value;
  2222. p1:=cunaryminusnode.create(p1);
  2223. end
  2224. else if tbinarynode(p1).left.nodetype=realconstn then
  2225. begin
  2226. trealconstnode(tbinarynode(p1).left).value_real:=-trealconstnode(tbinarynode(p1).left).value_real;
  2227. p1:=cunaryminusnode.create(p1);
  2228. end
  2229. else
  2230. internalerror(20021029);
  2231. end;
  2232. end
  2233. else
  2234. begin
  2235. p1:=sub_expr(oppower,false);
  2236. p1:=cunaryminusnode.create(p1);
  2237. end;
  2238. end;
  2239. _OP_NOT :
  2240. begin
  2241. consume(_OP_NOT);
  2242. p1:=factor(false);
  2243. p1:=cnotnode.create(p1);
  2244. end;
  2245. _TRUE :
  2246. begin
  2247. consume(_TRUE);
  2248. p1:=cordconstnode.create(1,booltype,false);
  2249. end;
  2250. _FALSE :
  2251. begin
  2252. consume(_FALSE);
  2253. p1:=cordconstnode.create(0,booltype,false);
  2254. end;
  2255. _NIL :
  2256. begin
  2257. consume(_NIL);
  2258. p1:=cnilnode.create;
  2259. { It's really ugly code nil^, but delphi allows it }
  2260. if token in [_CARET] then
  2261. begin
  2262. again:=true;
  2263. postfixoperators(p1,again);
  2264. end;
  2265. end;
  2266. else
  2267. begin
  2268. p1:=cerrornode.create;
  2269. consume(token);
  2270. Message(parser_e_illegal_expression);
  2271. end;
  2272. end;
  2273. { generate error node if no node is created }
  2274. if not assigned(p1) then
  2275. begin
  2276. {$ifdef EXTDEBUG}
  2277. Comment(V_Warning,'factor: p1=nil');
  2278. {$endif}
  2279. p1:=cerrornode.create;
  2280. end;
  2281. { get the resulttype for the node }
  2282. if (not assigned(p1.resulttype.def)) then
  2283. do_resulttypepass(p1);
  2284. factor:=p1;
  2285. check_tokenpos;
  2286. end;
  2287. {$ifdef fpc}
  2288. {$maxfpuregisters default}
  2289. {$endif fpc}
  2290. {****************************************************************************
  2291. Sub_Expr
  2292. ****************************************************************************}
  2293. const
  2294. { Warning these stay be ordered !! }
  2295. operator_levels:array[Toperator_precedence] of set of Ttoken=
  2296. ([_LT,_LTE,_GT,_GTE,_EQUAL,_UNEQUAL,_OP_IN],
  2297. [_PLUS,_MINUS,_OP_OR,_PIPE,_OP_XOR],
  2298. [_CARET,_SYMDIF,_STARSTAR,_STAR,_SLASH,
  2299. _OP_AS,_OP_IS,_OP_AND,_AMPERSAND,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR],
  2300. [_STARSTAR] );
  2301. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):tnode;
  2302. {Reads a subexpression while the operators are of the current precedence
  2303. level, or any higher level. Replaces the old term, simpl_expr and
  2304. simpl2_expr.}
  2305. var
  2306. p1,p2 : tnode;
  2307. oldt : Ttoken;
  2308. filepos : tfileposinfo;
  2309. begin
  2310. if pred_level=highest_precedence then
  2311. p1:=factor(false)
  2312. else
  2313. p1:=sub_expr(succ(pred_level),true);
  2314. repeat
  2315. if (token in operator_levels[pred_level]) and
  2316. ((token<>_EQUAL) or accept_equal) then
  2317. begin
  2318. oldt:=token;
  2319. filepos:=akttokenpos;
  2320. consume(token);
  2321. if pred_level=highest_precedence then
  2322. p2:=factor(false)
  2323. else
  2324. p2:=sub_expr(succ(pred_level),true);
  2325. case oldt of
  2326. _PLUS :
  2327. p1:=caddnode.create(addn,p1,p2);
  2328. _MINUS :
  2329. p1:=caddnode.create(subn,p1,p2);
  2330. _STAR :
  2331. p1:=caddnode.create(muln,p1,p2);
  2332. _SLASH :
  2333. p1:=caddnode.create(slashn,p1,p2);
  2334. _EQUAL :
  2335. p1:=caddnode.create(equaln,p1,p2);
  2336. _GT :
  2337. p1:=caddnode.create(gtn,p1,p2);
  2338. _LT :
  2339. p1:=caddnode.create(ltn,p1,p2);
  2340. _GTE :
  2341. p1:=caddnode.create(gten,p1,p2);
  2342. _LTE :
  2343. p1:=caddnode.create(lten,p1,p2);
  2344. _SYMDIF :
  2345. p1:=caddnode.create(symdifn,p1,p2);
  2346. _STARSTAR :
  2347. p1:=caddnode.create(starstarn,p1,p2);
  2348. _OP_AS :
  2349. p1:=casnode.create(p1,p2);
  2350. _OP_IN :
  2351. p1:=cinnode.create(p1,p2);
  2352. _OP_IS :
  2353. p1:=cisnode.create(p1,p2);
  2354. _OP_OR,
  2355. _PIPE {macpas only} :
  2356. p1:=caddnode.create(orn,p1,p2);
  2357. _OP_AND,
  2358. _AMPERSAND {macpas only} :
  2359. p1:=caddnode.create(andn,p1,p2);
  2360. _OP_DIV :
  2361. p1:=cmoddivnode.create(divn,p1,p2);
  2362. _OP_NOT :
  2363. p1:=cnotnode.create(p1);
  2364. _OP_MOD :
  2365. p1:=cmoddivnode.create(modn,p1,p2);
  2366. _OP_SHL :
  2367. p1:=cshlshrnode.create(shln,p1,p2);
  2368. _OP_SHR :
  2369. p1:=cshlshrnode.create(shrn,p1,p2);
  2370. _OP_XOR :
  2371. p1:=caddnode.create(xorn,p1,p2);
  2372. _ASSIGNMENT :
  2373. p1:=cassignmentnode.create(p1,p2);
  2374. _CARET :
  2375. p1:=caddnode.create(caretn,p1,p2);
  2376. _UNEQUAL :
  2377. p1:=caddnode.create(unequaln,p1,p2);
  2378. end;
  2379. p1.fileinfo:=filepos;
  2380. end
  2381. else
  2382. break;
  2383. until false;
  2384. sub_expr:=p1;
  2385. end;
  2386. function comp_expr(accept_equal : boolean):tnode;
  2387. var
  2388. oldafterassignment : boolean;
  2389. p1 : tnode;
  2390. begin
  2391. oldafterassignment:=afterassignment;
  2392. afterassignment:=true;
  2393. p1:=sub_expr(opcompare,accept_equal);
  2394. { get the resulttype for this expression }
  2395. if not assigned(p1.resulttype.def) then
  2396. do_resulttypepass(p1);
  2397. afterassignment:=oldafterassignment;
  2398. comp_expr:=p1;
  2399. end;
  2400. function expr : tnode;
  2401. var
  2402. p1,p2 : tnode;
  2403. oldafterassignment : boolean;
  2404. oldp1 : tnode;
  2405. filepos : tfileposinfo;
  2406. begin
  2407. oldafterassignment:=afterassignment;
  2408. p1:=sub_expr(opcompare,true);
  2409. { get the resulttype for this expression }
  2410. if not assigned(p1.resulttype.def) then
  2411. do_resulttypepass(p1);
  2412. filepos:=akttokenpos;
  2413. if token in [_ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  2414. afterassignment:=true;
  2415. oldp1:=p1;
  2416. case token of
  2417. _POINTPOINT :
  2418. begin
  2419. consume(_POINTPOINT);
  2420. p2:=sub_expr(opcompare,true);
  2421. p1:=crangenode.create(p1,p2);
  2422. end;
  2423. _ASSIGNMENT :
  2424. begin
  2425. consume(_ASSIGNMENT);
  2426. if (p1.resulttype.def.deftype=procvardef) then
  2427. getprocvardef:=tprocvardef(p1.resulttype.def);
  2428. p2:=sub_expr(opcompare,true);
  2429. if assigned(getprocvardef) then
  2430. handle_procvar(getprocvardef,p2);
  2431. getprocvardef:=nil;
  2432. p1:=cassignmentnode.create(p1,p2);
  2433. end;
  2434. _PLUSASN :
  2435. begin
  2436. consume(_PLUSASN);
  2437. p2:=sub_expr(opcompare,true);
  2438. p1:=gen_c_style_operator(addn,p1,p2);
  2439. end;
  2440. _MINUSASN :
  2441. begin
  2442. consume(_MINUSASN);
  2443. p2:=sub_expr(opcompare,true);
  2444. p1:=gen_c_style_operator(subn,p1,p2);
  2445. end;
  2446. _STARASN :
  2447. begin
  2448. consume(_STARASN );
  2449. p2:=sub_expr(opcompare,true);
  2450. p1:=gen_c_style_operator(muln,p1,p2);
  2451. end;
  2452. _SLASHASN :
  2453. begin
  2454. consume(_SLASHASN );
  2455. p2:=sub_expr(opcompare,true);
  2456. p1:=gen_c_style_operator(slashn,p1,p2);
  2457. end;
  2458. end;
  2459. { get the resulttype for this expression }
  2460. if not assigned(p1.resulttype.def) then
  2461. do_resulttypepass(p1);
  2462. afterassignment:=oldafterassignment;
  2463. if p1<>oldp1 then
  2464. p1.fileinfo:=filepos;
  2465. expr:=p1;
  2466. end;
  2467. function get_intconst:TConstExprInt;
  2468. {Reads an expression, tries to evalute it and check if it is an integer
  2469. constant. Then the constant is returned.}
  2470. var
  2471. p:tnode;
  2472. begin
  2473. result:=0;
  2474. p:=comp_expr(true);
  2475. if not codegenerror then
  2476. begin
  2477. if (p.nodetype<>ordconstn) or
  2478. not(is_integer(p.resulttype.def)) then
  2479. Message(parser_e_illegal_expression)
  2480. else
  2481. result:=tordconstnode(p).value;
  2482. end;
  2483. p.free;
  2484. end;
  2485. function get_stringconst:string;
  2486. {Reads an expression, tries to evaluate it and checks if it is a string
  2487. constant. Then the constant is returned.}
  2488. var
  2489. p:tnode;
  2490. begin
  2491. get_stringconst:='';
  2492. p:=comp_expr(true);
  2493. if p.nodetype<>stringconstn then
  2494. begin
  2495. if (p.nodetype=ordconstn) and is_char(p.resulttype.def) then
  2496. get_stringconst:=char(tordconstnode(p).value)
  2497. else
  2498. Message(parser_e_illegal_expression);
  2499. end
  2500. else
  2501. get_stringconst:=strpas(tstringconstnode(p).value_str);
  2502. p.free;
  2503. end;
  2504. end.