pexpr.pas 98 KB

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