pexpr.pas 88 KB

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