pexpr.pas 89 KB

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