pexpr.pas 97 KB

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