pexpr.pas 98 KB

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