pexpr.pas 97 KB

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