pexpr.pas 97 KB

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