pexpr.pas 90 KB

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