pexpr.pas 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449
  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);
  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 (p2.nodetype=calln) then
  605. doconv(pv,p2)
  606. else
  607. if (p2.nodetype=typeconvn) and
  608. (ttypeconvnode(p2).left.nodetype=calln) then
  609. doconv(pv,ttypeconvnode(p2).left);
  610. end;
  611. { the following procedure handles the access to a property symbol }
  612. procedure handle_propertysym(sym : tsym;st : tsymtable;var p1 : tnode);
  613. var
  614. paras : tnode;
  615. p2 : tnode;
  616. plist : psymlistitem;
  617. begin
  618. paras:=nil;
  619. { property parameters? read them only if the property really }
  620. { has parameters }
  621. if ppo_hasparameters in tpropertysym(sym).propoptions then
  622. begin
  623. if token=_LECKKLAMMER then
  624. begin
  625. consume(_LECKKLAMMER);
  626. paras:=parse_paras(false,true);
  627. consume(_RECKKLAMMER);
  628. end;
  629. { indexed property }
  630. if (ppo_indexed in tpropertysym(sym).propoptions) then
  631. begin
  632. p2:=cordconstnode.create(tpropertysym(sym).index,tpropertysym(sym).indextype);
  633. paras:=ccallparanode.create(p2,paras);
  634. end;
  635. end;
  636. { we need only a write property if a := follows }
  637. { if not(afterassignment) and not(in_args) then }
  638. if token=_ASSIGNMENT then
  639. begin
  640. { write property: }
  641. if not tpropertysym(sym).writeaccess.empty then
  642. begin
  643. case tpropertysym(sym).writeaccess.firstsym^.sym.typ of
  644. procsym :
  645. begin
  646. { generate the method call }
  647. p1:=ccallnode.create(paras,
  648. tprocsym(tpropertysym(sym).writeaccess.firstsym^.sym),st,p1);
  649. { we know the procedure to call, so
  650. force the usage of that procedure }
  651. tcallnode(p1).procdefinition:=tprocdef(tpropertysym(sym).writeaccess.def);
  652. consume(_ASSIGNMENT);
  653. { read the expression }
  654. getprocvar:=(tpropertysym(sym).proptype.def.deftype=procvardef);
  655. p2:=comp_expr(true);
  656. if getprocvar then
  657. handle_procvar(tprocvardef(tpropertysym(sym).proptype.def),p2);
  658. tcallnode(p1).left:=ccallparanode.create(p2,tcallnode(p1).left);
  659. include(tcallnode(p1).flags,nf_isproperty);
  660. getprocvar:=false;
  661. end;
  662. varsym :
  663. begin
  664. if assigned(paras) then
  665. message(parser_e_no_paras_allowed);
  666. { subscribed access? }
  667. plist:=tpropertysym(sym).writeaccess.firstsym;
  668. while assigned(plist) do
  669. begin
  670. if p1=nil then
  671. p1:=cloadnode.create(tvarsym(plist^.sym),st)
  672. else
  673. p1:=csubscriptnode.create(tvarsym(plist^.sym),p1);
  674. plist:=plist^.next;
  675. end;
  676. include(tcallnode(p1).flags,nf_isproperty);
  677. consume(_ASSIGNMENT);
  678. { read the expression }
  679. p2:=comp_expr(true);
  680. p1:=cassignmentnode.create(p1,p2);
  681. end
  682. else
  683. begin
  684. p1:=cerrornode.create;
  685. Message(parser_e_no_procedure_to_access_property);
  686. end;
  687. end;
  688. end
  689. else
  690. begin
  691. p1:=cerrornode.create;
  692. Message(parser_e_no_procedure_to_access_property);
  693. end;
  694. end
  695. else
  696. begin
  697. { read property: }
  698. if not tpropertysym(sym).readaccess.empty then
  699. begin
  700. case tpropertysym(sym).readaccess.firstsym^.sym.typ of
  701. varsym :
  702. begin
  703. if assigned(paras) then
  704. message(parser_e_no_paras_allowed);
  705. { subscribed access? }
  706. plist:=tpropertysym(sym).readaccess.firstsym;
  707. while assigned(plist) do
  708. begin
  709. if p1=nil then
  710. p1:=cloadnode.create(tvarsym(plist^.sym),st)
  711. else
  712. p1:=csubscriptnode.create(tvarsym(plist^.sym),p1);
  713. plist:=plist^.next;
  714. end;
  715. include(p1.flags,nf_isproperty);
  716. end;
  717. procsym :
  718. begin
  719. { generate the method call }
  720. p1:=ccallnode.create(paras,tprocsym(tpropertysym(sym).readaccess.firstsym^.sym),st,p1);
  721. { we know the procedure to call, so
  722. force the usage of that procedure }
  723. tcallnode(p1).procdefinition:=tprocdef(tpropertysym(sym).readaccess.def);
  724. include(p1.flags,nf_isproperty);
  725. end
  726. else
  727. begin
  728. p1:=cerrornode.create;
  729. Message(type_e_mismatch);
  730. end;
  731. end;
  732. end
  733. else
  734. begin
  735. { error, no function to read property }
  736. p1:=cerrornode.create;
  737. Message(parser_e_no_procedure_to_access_property);
  738. end;
  739. end;
  740. end;
  741. { the ID token has to be consumed before calling this function }
  742. procedure do_member_read(getaddr : boolean;sym : tsym;var p1 : tnode;var again : boolean);
  743. var
  744. static_name : string;
  745. isclassref : boolean;
  746. srsymtable : tsymtable;
  747. objdef : tobjectdef;
  748. begin
  749. if sym=nil then
  750. begin
  751. { pattern is still valid unless
  752. there is another ID just after the ID of sym }
  753. Message1(sym_e_id_no_member,pattern);
  754. p1.free;
  755. p1:=cerrornode.create;
  756. { try to clean up }
  757. again:=false;
  758. end
  759. else
  760. begin
  761. if assigned(p1) then
  762. begin
  763. if not assigned(p1.resulttype.def) then
  764. do_resulttypepass(p1);
  765. isclassref:=(p1.resulttype.def.deftype=classrefdef);
  766. end
  767. else
  768. isclassref:=false;
  769. objdef:=tobjectdef(sym.owner.defowner);
  770. { check protected and private members }
  771. { please leave this code as it is, }
  772. { it has now the same behaviaor as TP/Delphi }
  773. if (sp_private in sym.symoptions) and
  774. (objdef.owner.symtabletype=globalsymtable) and
  775. (objdef.owner.unitid<>0) then
  776. Message(parser_e_cant_access_private_member);
  777. if (sp_protected in sym.symoptions) and
  778. (objdef.owner.symtabletype=globalsymtable) and
  779. (objdef.owner.unitid<>0) then
  780. begin
  781. if assigned(aktprocsym.definition._class) then
  782. begin
  783. if not aktprocsym.definition._class.is_related(objdef) then
  784. Message(parser_e_cant_access_protected_member);
  785. end
  786. else
  787. Message(parser_e_cant_access_protected_member);
  788. end;
  789. { we assume, that only procsyms and varsyms are in an object }
  790. { symbol table, for classes, properties are allowed }
  791. case sym.typ of
  792. procsym:
  793. begin
  794. do_proc_call(sym,sym.owner,
  795. getaddr or
  796. (getprocvar and
  797. ((block_type=bt_const) or
  798. ((m_tp_procvar in aktmodeswitches) and
  799. proc_to_procvar_equal(tprocsym(sym).definition,getprocvardef)
  800. )
  801. )
  802. ),again,tcallnode(p1));
  803. if (block_type=bt_const) and
  804. getprocvar then
  805. handle_procvar(getprocvardef,p1);
  806. { we need to know which procedure is called }
  807. do_resulttypepass(p1);
  808. { now we know the real method e.g. we can check for a class method }
  809. if isclassref and
  810. assigned(tcallnode(p1).procdefinition) and
  811. not(po_classmethod in tcallnode(p1).procdefinition.procoptions) and
  812. not(tcallnode(p1).procdefinition.proctypeoption=potype_constructor) then
  813. Message(parser_e_only_class_methods_via_class_ref);
  814. end;
  815. varsym:
  816. begin
  817. if isclassref then
  818. Message(parser_e_only_class_methods_via_class_ref);
  819. if (sp_static in sym.symoptions) then
  820. begin
  821. static_name:=lower(sym.owner.name^)+'_'+sym.name;
  822. searchsym(static_name,sym,srsymtable);
  823. p1.free;
  824. p1:=cloadnode.create(tvarsym(sym),srsymtable);
  825. end
  826. else
  827. p1:=csubscriptnode.create(tvarsym(sym),p1);
  828. end;
  829. propertysym:
  830. begin
  831. if isclassref then
  832. Message(parser_e_only_class_methods_via_class_ref);
  833. handle_propertysym(sym,sym.owner,p1);
  834. end;
  835. else internalerror(16);
  836. end;
  837. end;
  838. end;
  839. {****************************************************************************
  840. Factor
  841. ****************************************************************************}
  842. {$ifdef fpc}
  843. {$maxfpuregisters 0}
  844. {$endif fpc}
  845. function factor(getaddr : boolean) : tnode;
  846. {---------------------------------------------
  847. Is_func_ret
  848. ---------------------------------------------}
  849. function is_func_ret(var p1:tnode;var sym : tsym;var srsymtable:tsymtable) : boolean;
  850. var
  851. p : pprocinfo;
  852. storesymtablestack : tsymtable;
  853. begin
  854. is_func_ret:=false;
  855. if not assigned(procinfo) or
  856. ((sym.typ<>funcretsym) and ((procinfo^.flags and pi_operator)=0)) then
  857. exit;
  858. p:=procinfo;
  859. while assigned(p) do
  860. begin
  861. { is this an access to a function result? Accessing _RESULT is
  862. always allowed and funcretn is generated }
  863. if assigned(p^.funcretsym) and
  864. ((tfuncretsym(sym)=p^.resultfuncretsym) or
  865. ((tfuncretsym(sym)=p^.funcretsym) or
  866. ((tvarsym(sym)=otsym) and ((p^.flags and pi_operator)<>0))) and
  867. (not is_void(p^.returntype.def)) and
  868. (token<>_LKLAMMER) and
  869. (not ((m_tp in aktmodeswitches) and (afterassignment or in_args)))
  870. ) then
  871. begin
  872. if ((tvarsym(sym)=otsym) and
  873. ((p^.flags and pi_operator)<>0)) then
  874. inc(otsym.refs);
  875. p1:=cfuncretnode.create(p);
  876. is_func_ret:=true;
  877. if p^.funcret_state=vs_declared then
  878. begin
  879. p^.funcret_state:=vs_declared_and_first_found;
  880. include(p1.flags,nf_is_first_funcret);
  881. end;
  882. exit;
  883. end;
  884. p:=p^.parent;
  885. end;
  886. { we must use the function call, update the
  887. sym to be the procsym }
  888. if (sym.typ=funcretsym) then
  889. begin
  890. storesymtablestack:=symtablestack;
  891. symtablestack:=sym.owner.next;
  892. searchsym(sym.name,sym,srsymtable);
  893. if not assigned(sym) then
  894. sym:=generrorsym;
  895. if (sym.typ<>procsym) then
  896. Message(cg_e_illegal_expression);
  897. symtablestack:=storesymtablestack;
  898. end;
  899. end;
  900. {---------------------------------------------
  901. Factor_read_id
  902. ---------------------------------------------}
  903. procedure factor_read_id(var p1:tnode;var again:boolean);
  904. var
  905. pc : pchar;
  906. len : longint;
  907. srsym : tsym;
  908. possible_error : boolean;
  909. srsymtable : tsymtable;
  910. htype : ttype;
  911. static_name : string;
  912. begin
  913. { allow post fix operators }
  914. again:=true;
  915. consume_sym(srsym,srsymtable);
  916. if not is_func_ret(p1,srsym,srsymtable) then
  917. begin
  918. { check semantics of private }
  919. if (srsym.typ in [propertysym,procsym,varsym]) and
  920. (srsym.owner.symtabletype=objectsymtable) then
  921. begin
  922. if (sp_private in srsym.symoptions) and
  923. (tobjectdef(srsym.owner.defowner).owner.symtabletype=globalsymtable) and
  924. (tobjectdef(srsym.owner.defowner).owner.unitid<>0) then
  925. Message(parser_e_cant_access_private_member);
  926. end;
  927. case srsym.typ of
  928. absolutesym :
  929. begin
  930. p1:=cloadnode.create(tvarsym(srsym),srsymtable);
  931. end;
  932. varsym :
  933. begin
  934. { are we in a class method ? }
  935. if (srsym.owner.symtabletype=objectsymtable) and
  936. assigned(aktprocsym) and
  937. (po_classmethod in aktprocsym.definition.procoptions) then
  938. Message(parser_e_only_class_methods);
  939. if (sp_static in srsym.symoptions) then
  940. begin
  941. static_name:=lower(srsym.owner.name^)+'_'+srsym.name;
  942. searchsym(static_name,srsym,srsymtable);
  943. end;
  944. p1:=cloadnode.create(tvarsym(srsym),srsymtable);
  945. if tvarsym(srsym).varstate=vs_declared then
  946. begin
  947. include(p1.flags,nf_first);
  948. { set special between first loaded until checked in resulttypepass }
  949. tvarsym(srsym).varstate:=vs_declared_and_first_found;
  950. end;
  951. end;
  952. typedconstsym :
  953. begin
  954. p1:=cloadnode.create(srsym,srsymtable);
  955. end;
  956. syssym :
  957. p1:=statement_syssym(tsyssym(srsym).number);
  958. typesym :
  959. begin
  960. htype.setsym(srsym);
  961. if not assigned(htype.def) then
  962. begin
  963. again:=false;
  964. end
  965. else
  966. begin
  967. if token=_LKLAMMER then
  968. begin
  969. consume(_LKLAMMER);
  970. p1:=comp_expr(true);
  971. consume(_RKLAMMER);
  972. p1:=ctypeconvnode.create(p1,htype);
  973. include(p1.flags,nf_explizit);
  974. end
  975. else { not LKLAMMER }
  976. if (token=_POINT) and
  977. is_object(htype.def) then
  978. begin
  979. consume(_POINT);
  980. if assigned(procinfo) and
  981. assigned(procinfo^._class) and
  982. not(getaddr) then
  983. begin
  984. if procinfo^._class.is_related(tobjectdef(htype.def)) then
  985. begin
  986. p1:=ctypenode.create(htype);
  987. { search also in inherited methods }
  988. repeat
  989. srsym:=tvarsym(tobjectdef(htype.def).symtable.search(pattern));
  990. if assigned(srsym) then
  991. break;
  992. htype.def:=tobjectdef(htype.def).childof;
  993. until not assigned(htype.def);
  994. consume(_ID);
  995. do_member_read(false,srsym,p1,again);
  996. end
  997. else
  998. begin
  999. Message(parser_e_no_super_class);
  1000. again:=false;
  1001. end;
  1002. end
  1003. else
  1004. begin
  1005. { allows @TObject.Load }
  1006. { also allows static methods and variables }
  1007. p1:=ctypenode.create(htype);
  1008. { TP allows also @TMenu.Load if Load is only }
  1009. { defined in an anchestor class }
  1010. srsym:=tvarsym(search_class_member(tobjectdef(htype.def),pattern));
  1011. if not assigned(srsym) then
  1012. Message1(sym_e_id_no_member,pattern)
  1013. else if not(getaddr) and not(sp_static in srsym.symoptions) then
  1014. Message(sym_e_only_static_in_static)
  1015. else
  1016. begin
  1017. consume(_ID);
  1018. do_member_read(getaddr,srsym,p1,again);
  1019. end;
  1020. end;
  1021. end
  1022. else
  1023. begin
  1024. { class reference ? }
  1025. if is_class(htype.def) then
  1026. begin
  1027. if getaddr and (token=_POINT) then
  1028. begin
  1029. consume(_POINT);
  1030. { allows @Object.Method }
  1031. { also allows static methods and variables }
  1032. p1:=ctypenode.create(htype);
  1033. { TP allows also @TMenu.Load if Load is only }
  1034. { defined in an anchestor class }
  1035. srsym:=tvarsym(search_class_member(tobjectdef(htype.def),pattern));
  1036. if not assigned(srsym) then
  1037. Message1(sym_e_id_no_member,pattern)
  1038. else
  1039. begin
  1040. consume(_ID);
  1041. do_member_read(getaddr,srsym,p1,again);
  1042. end;
  1043. end
  1044. else
  1045. begin
  1046. p1:=ctypenode.create(htype);
  1047. { For a type block we simply return only
  1048. the type. For all other blocks we return
  1049. a loadvmt node }
  1050. if (block_type<>bt_type) then
  1051. p1:=cloadvmtnode.create(p1);
  1052. end;
  1053. end
  1054. else
  1055. p1:=ctypenode.create(htype);
  1056. end;
  1057. end;
  1058. end;
  1059. enumsym :
  1060. begin
  1061. p1:=genenumnode(tenumsym(srsym));
  1062. end;
  1063. constsym :
  1064. begin
  1065. case tconstsym(srsym).consttyp of
  1066. constint :
  1067. begin
  1068. { do a very dirty trick to bootstrap this code }
  1069. if (tconstsym(srsym).value>=-(int64(2147483647)+int64(1))) and (tconstsym(srsym).value<=2147483647) then
  1070. p1:=cordconstnode.create(tconstsym(srsym).value,s32bittype)
  1071. else if (tconstsym(srsym).value > maxlongint) and (tconstsym(srsym).value <= int64(maxlongint)+int64(maxlongint)+1) then
  1072. p1:=cordconstnode.create(tconstsym(srsym).value,u32bittype)
  1073. else
  1074. p1:=cordconstnode.create(tconstsym(srsym).value,cs64bittype);
  1075. end;
  1076. conststring :
  1077. begin
  1078. len:=tconstsym(srsym).len;
  1079. if not(cs_ansistrings in aktlocalswitches) and (len>255) then
  1080. len:=255;
  1081. getmem(pc,len+1);
  1082. move(pchar(tpointerord(tconstsym(srsym).value))^,pc^,len);
  1083. pc[len]:=#0;
  1084. p1:=cstringconstnode.createpchar(pc,len);
  1085. end;
  1086. constchar :
  1087. p1:=cordconstnode.create(tconstsym(srsym).value,cchartype);
  1088. constreal :
  1089. p1:=crealconstnode.create(pbestreal(tpointerord(tconstsym(srsym).value))^,pbestrealtype^);
  1090. constbool :
  1091. p1:=cordconstnode.create(tconstsym(srsym).value,booltype);
  1092. constset :
  1093. p1:=csetconstnode.create(pconstset(tpointerord(tconstsym(srsym).value)),tconstsym(srsym).consttype);
  1094. constord :
  1095. p1:=cordconstnode.create(tconstsym(srsym).value,tconstsym(srsym).consttype);
  1096. constpointer :
  1097. p1:=cpointerconstnode.create(tconstsym(srsym).value,tconstsym(srsym).consttype);
  1098. constnil :
  1099. p1:=cnilnode.create;
  1100. constresourcestring:
  1101. begin
  1102. p1:=cloadnode.create(tvarsym(srsym),srsymtable);
  1103. do_resulttypepass(p1);
  1104. p1.resulttype:=cansistringtype;
  1105. end;
  1106. end;
  1107. end;
  1108. procsym :
  1109. begin
  1110. { are we in a class method ? }
  1111. possible_error:=(srsym.owner.symtabletype=objectsymtable) and
  1112. assigned(aktprocsym) and
  1113. (po_classmethod in aktprocsym.definition.procoptions);
  1114. do_proc_call(srsym,srsymtable,
  1115. getaddr or
  1116. (getprocvar and
  1117. ((block_type=bt_const) or
  1118. ((m_tp_procvar in aktmodeswitches) and
  1119. proc_to_procvar_equal(tprocsym(srsym).definition,getprocvardef)
  1120. )
  1121. )
  1122. ),again,p1);
  1123. if (block_type=bt_const) and
  1124. getprocvar then
  1125. handle_procvar(getprocvardef,p1);
  1126. { we need to know which procedure is called }
  1127. if possible_error then
  1128. begin
  1129. do_resulttypepass(p1);
  1130. if not(po_classmethod in tcallnode(p1).procdefinition.procoptions) then
  1131. Message(parser_e_only_class_methods);
  1132. end;
  1133. end;
  1134. propertysym :
  1135. begin
  1136. { access to property in a method }
  1137. { are we in a class method ? }
  1138. if (srsym.owner.symtabletype=objectsymtable) and
  1139. assigned(aktprocsym) and
  1140. (po_classmethod in aktprocsym.definition.procoptions) then
  1141. Message(parser_e_only_class_methods);
  1142. { no method pointer }
  1143. p1:=nil;
  1144. handle_propertysym(srsym,srsymtable,p1);
  1145. end;
  1146. labelsym :
  1147. begin
  1148. consume(_COLON);
  1149. if tlabelsym(srsym).defined then
  1150. Message(sym_e_label_already_defined);
  1151. tlabelsym(srsym).defined:=true;
  1152. p1:=clabelnode.create(tlabelsym(srsym),nil);
  1153. end;
  1154. errorsym :
  1155. begin
  1156. p1:=cerrornode.create;
  1157. if token=_LKLAMMER then
  1158. begin
  1159. consume(_LKLAMMER);
  1160. parse_paras(false,false);
  1161. consume(_RKLAMMER);
  1162. end;
  1163. end;
  1164. else
  1165. begin
  1166. p1:=cerrornode.create;
  1167. Message(cg_e_illegal_expression);
  1168. end;
  1169. end; { end case }
  1170. end;
  1171. end;
  1172. {---------------------------------------------
  1173. Factor_Read_Set
  1174. ---------------------------------------------}
  1175. { Read a set between [] }
  1176. function factor_read_set:tnode;
  1177. var
  1178. p1,p2 : tnode;
  1179. lastp,
  1180. buildp : tarrayconstructornode;
  1181. begin
  1182. buildp:=nil;
  1183. { be sure that a least one arrayconstructn is used, also for an
  1184. empty [] }
  1185. if token=_RECKKLAMMER then
  1186. buildp:=carrayconstructornode.create(nil,buildp)
  1187. else
  1188. begin
  1189. while true do
  1190. begin
  1191. p1:=comp_expr(true);
  1192. if token=_POINTPOINT then
  1193. begin
  1194. consume(_POINTPOINT);
  1195. p2:=comp_expr(true);
  1196. p1:=carrayconstructorrangenode.create(p1,p2);
  1197. end;
  1198. { insert at the end of the tree, to get the correct order }
  1199. if not assigned(buildp) then
  1200. begin
  1201. buildp:=carrayconstructornode.create(p1,nil);
  1202. lastp:=buildp;
  1203. end
  1204. else
  1205. begin
  1206. lastp.right:=carrayconstructornode.create(p1,nil);
  1207. lastp:=tarrayconstructornode(lastp.right);
  1208. end;
  1209. { there could be more elements }
  1210. if token=_COMMA then
  1211. consume(_COMMA)
  1212. else
  1213. break;
  1214. end;
  1215. end;
  1216. factor_read_set:=buildp;
  1217. end;
  1218. {---------------------------------------------
  1219. PostFixOperators
  1220. ---------------------------------------------}
  1221. procedure postfixoperators(var p1:tnode;var again:boolean);
  1222. { tries to avoid syntax errors after invalid qualifiers }
  1223. procedure recoverconsume_postfixops;
  1224. begin
  1225. while true do
  1226. begin
  1227. case token of
  1228. _CARET:
  1229. consume(_CARET);
  1230. _POINT:
  1231. begin
  1232. consume(_POINT);
  1233. if token=_ID then
  1234. consume(_ID);
  1235. end;
  1236. _LECKKLAMMER:
  1237. begin
  1238. consume(_LECKKLAMMER);
  1239. repeat
  1240. comp_expr(true);
  1241. if token=_COMMA then
  1242. consume(_COMMA)
  1243. else
  1244. break;
  1245. until false;
  1246. consume(_RECKKLAMMER);
  1247. end
  1248. else
  1249. break;
  1250. end;
  1251. end;
  1252. end;
  1253. var
  1254. store_static : boolean;
  1255. protsym : tpropertysym;
  1256. p2,p3 : tnode;
  1257. hsym : tsym;
  1258. classh : tobjectdef;
  1259. begin
  1260. again:=true;
  1261. while again do
  1262. begin
  1263. { we need the resulttype }
  1264. do_resulttypepass(p1);
  1265. if codegenerror then
  1266. begin
  1267. recoverconsume_postfixops;
  1268. exit;
  1269. end;
  1270. { handle token }
  1271. case token of
  1272. _CARET:
  1273. begin
  1274. consume(_CARET);
  1275. if (p1.resulttype.def.deftype<>pointerdef) then
  1276. begin
  1277. { ^ as binary operator is a problem!!!! (FK) }
  1278. again:=false;
  1279. Message(cg_e_invalid_qualifier);
  1280. recoverconsume_postfixops;
  1281. p1.destroy;
  1282. p1:=cerrornode.create;
  1283. end
  1284. else
  1285. begin
  1286. p1:=cderefnode.create(p1);
  1287. end;
  1288. end;
  1289. _LECKKLAMMER:
  1290. begin
  1291. if is_class_or_interface(p1.resulttype.def) then
  1292. begin
  1293. { default property }
  1294. protsym:=search_default_property(tobjectdef(p1.resulttype.def));
  1295. if not(assigned(protsym)) then
  1296. begin
  1297. p1.destroy;
  1298. p1:=cerrornode.create;
  1299. again:=false;
  1300. message(parser_e_no_default_property_available);
  1301. end
  1302. else
  1303. handle_propertysym(protsym,protsym.owner,p1);
  1304. end
  1305. else
  1306. begin
  1307. consume(_LECKKLAMMER);
  1308. repeat
  1309. case p1.resulttype.def.deftype of
  1310. pointerdef:
  1311. begin
  1312. { support delphi autoderef }
  1313. if (tpointerdef(p1.resulttype.def).pointertype.def.deftype=arraydef) and
  1314. (m_autoderef in aktmodeswitches) then
  1315. begin
  1316. p1:=cderefnode.create(p1);
  1317. end;
  1318. p2:=comp_expr(true);
  1319. p1:=cvecnode.create(p1,p2);
  1320. end;
  1321. stringdef :
  1322. begin
  1323. p2:=comp_expr(true);
  1324. p1:=cvecnode.create(p1,p2);
  1325. end;
  1326. arraydef :
  1327. begin
  1328. p2:=comp_expr(true);
  1329. { support SEG:OFS for go32v2 Mem[] }
  1330. if (target_info.target=target_i386_go32v2) and
  1331. (p1.nodetype=loadn) and
  1332. assigned(tloadnode(p1).symtableentry) and
  1333. assigned(tloadnode(p1).symtableentry.owner.name) and
  1334. (tloadnode(p1).symtableentry.owner.name^='SYSTEM') and
  1335. ((tloadnode(p1).symtableentry.name='MEM') or
  1336. (tloadnode(p1).symtableentry.name='MEMW') or
  1337. (tloadnode(p1).symtableentry.name='MEML')) then
  1338. begin
  1339. if (token=_COLON) then
  1340. begin
  1341. consume(_COLON);
  1342. p3:=caddnode.create(muln,cordconstnode.create($10,s32bittype),p2);
  1343. p2:=comp_expr(true);
  1344. p2:=caddnode.create(addn,p2,p3);
  1345. p1:=cvecnode.create(p1,p2);
  1346. include(tvecnode(p1).flags,nf_memseg);
  1347. include(tvecnode(p1).flags,nf_memindex);
  1348. end
  1349. else
  1350. begin
  1351. p1:=cvecnode.create(p1,p2);
  1352. include(tvecnode(p1).flags,nf_memindex);
  1353. end;
  1354. end
  1355. else
  1356. p1:=cvecnode.create(p1,p2);
  1357. end;
  1358. else
  1359. begin
  1360. Message(cg_e_invalid_qualifier);
  1361. p1.destroy;
  1362. p1:=cerrornode.create;
  1363. comp_expr(true);
  1364. again:=false;
  1365. end;
  1366. end;
  1367. do_resulttypepass(p1);
  1368. if token=_COMMA then
  1369. consume(_COMMA)
  1370. else
  1371. break;
  1372. until false;
  1373. consume(_RECKKLAMMER);
  1374. end;
  1375. end;
  1376. _POINT :
  1377. begin
  1378. consume(_POINT);
  1379. if (p1.resulttype.def.deftype=pointerdef) and
  1380. (m_autoderef in aktmodeswitches) then
  1381. begin
  1382. p1:=cderefnode.create(p1);
  1383. do_resulttypepass(p1);
  1384. end;
  1385. case p1.resulttype.def.deftype of
  1386. recorddef:
  1387. begin
  1388. hsym:=tsym(trecorddef(p1.resulttype.def).symtable.search(pattern));
  1389. if assigned(hsym) and
  1390. (hsym.typ=varsym) then
  1391. p1:=csubscriptnode.create(tvarsym(hsym),p1)
  1392. else
  1393. begin
  1394. Message1(sym_e_illegal_field,pattern);
  1395. p1.destroy;
  1396. p1:=cerrornode.create;
  1397. end;
  1398. consume(_ID);
  1399. end;
  1400. classrefdef:
  1401. begin
  1402. classh:=tobjectdef(tclassrefdef(p1.resulttype.def).pointertype.def);
  1403. hsym:=nil;
  1404. while assigned(classh) do
  1405. begin
  1406. hsym:=tsym(classh.symtable.search(pattern));
  1407. if assigned(hsym) then
  1408. break;
  1409. classh:=classh.childof;
  1410. end;
  1411. if hsym=nil then
  1412. begin
  1413. Message1(sym_e_id_no_member,pattern);
  1414. p1.destroy;
  1415. p1:=cerrornode.create;
  1416. { try to clean up }
  1417. consume(_ID);
  1418. end
  1419. else
  1420. begin
  1421. consume(_ID);
  1422. do_member_read(getaddr,hsym,p1,again);
  1423. end;
  1424. end;
  1425. objectdef:
  1426. begin
  1427. classh:=tobjectdef(p1.resulttype.def);
  1428. hsym:=nil;
  1429. store_static:=allow_only_static;
  1430. allow_only_static:=false;
  1431. while assigned(classh) do
  1432. begin
  1433. hsym:=tsym(classh.symtable.search(pattern));
  1434. if assigned(hsym) then
  1435. break;
  1436. classh:=classh.childof;
  1437. end;
  1438. allow_only_static:=store_static;
  1439. if hsym=nil then
  1440. begin
  1441. Message1(sym_e_id_no_member,pattern);
  1442. p1.destroy;
  1443. p1:=cerrornode.create;
  1444. { try to clean up }
  1445. consume(_ID);
  1446. end
  1447. else
  1448. begin
  1449. consume(_ID);
  1450. do_member_read(getaddr,hsym,p1,again);
  1451. end;
  1452. end;
  1453. pointerdef:
  1454. begin
  1455. Message(cg_e_invalid_qualifier);
  1456. if tpointerdef(p1.resulttype.def).pointertype.def.deftype in [recorddef,objectdef,classrefdef] then
  1457. Message(parser_h_maybe_deref_caret_missing);
  1458. end;
  1459. else
  1460. begin
  1461. Message(cg_e_invalid_qualifier);
  1462. p1.destroy;
  1463. p1:=cerrornode.create;
  1464. consume(_ID);
  1465. end;
  1466. end;
  1467. end;
  1468. else
  1469. begin
  1470. { is this a procedure variable ? }
  1471. if assigned(p1.resulttype.def) then
  1472. begin
  1473. if (p1.resulttype.def.deftype=procvardef) then
  1474. begin
  1475. if getprocvar and is_equal(p1.resulttype.def,getprocvardef) then
  1476. again:=false
  1477. else
  1478. if (token=_LKLAMMER) or
  1479. ((tprocvardef(p1.resulttype.def).para.empty) and
  1480. (not((token in [_ASSIGNMENT,_UNEQUAL,_EQUAL]))) and
  1481. (not afterassignment) and
  1482. (not in_args)) then
  1483. begin
  1484. { do this in a strange way }
  1485. { it's not a clean solution }
  1486. p2:=p1;
  1487. p1:=ccallnode.create(nil,nil,nil,nil);
  1488. tcallnode(p1).set_procvar(p2);
  1489. {$ifdef TEST_PROCSYMS}
  1490. p1.unit_specific:=unit_specific;
  1491. {$endif TEST_PROCSYMS}
  1492. {tcallnode(p1).symtableprocentry:=tprocsym(sym);}
  1493. if token=_LKLAMMER then
  1494. begin
  1495. consume(_LKLAMMER);
  1496. tcallnode(p1).left:=parse_paras(false,false);
  1497. consume(_RKLAMMER);
  1498. end;
  1499. { proc():= is never possible }
  1500. if token=_ASSIGNMENT then
  1501. begin
  1502. Message(cg_e_illegal_expression);
  1503. p1:=cerrornode.create;
  1504. again:=false;
  1505. end;
  1506. end
  1507. else
  1508. again:=false;
  1509. end
  1510. else
  1511. again:=false;
  1512. end
  1513. else
  1514. again:=false;
  1515. end;
  1516. end;
  1517. end; { while again }
  1518. end;
  1519. {---------------------------------------------
  1520. Factor (Main)
  1521. ---------------------------------------------}
  1522. var
  1523. l : longint;
  1524. card : cardinal;
  1525. ic : TConstExprInt;
  1526. oldp1,
  1527. p1,p2 : tnode;
  1528. code : integer;
  1529. {$ifdef TEST_PROCSYMS}
  1530. unit_specific,
  1531. {$endif TEST_PROCSYMS}
  1532. again : boolean;
  1533. sym : tsym;
  1534. classh : tobjectdef;
  1535. d : bestreal;
  1536. hs : string;
  1537. htype : ttype;
  1538. filepos : tfileposinfo;
  1539. {---------------------------------------------
  1540. Helpers
  1541. ---------------------------------------------}
  1542. procedure check_tokenpos;
  1543. begin
  1544. if (p1<>oldp1) then
  1545. begin
  1546. if assigned(p1) then
  1547. p1.set_tree_filepos(filepos);
  1548. oldp1:=p1;
  1549. filepos:=akttokenpos;
  1550. end;
  1551. end;
  1552. begin
  1553. oldp1:=nil;
  1554. p1:=nil;
  1555. filepos:=akttokenpos;
  1556. again:=false;
  1557. if token=_ID then
  1558. begin
  1559. if idtoken=_NEW then
  1560. begin
  1561. consume(_NEW);
  1562. consume(_LKLAMMER);
  1563. p1:=factor(false);
  1564. if p1.nodetype<>typen then
  1565. begin
  1566. Message(type_e_type_id_expected);
  1567. p1.destroy;
  1568. p1:=cerrornode.create;
  1569. do_resulttypepass(p1);
  1570. end;
  1571. if (p1.resulttype.def.deftype<>pointerdef) then
  1572. Message1(type_e_pointer_type_expected,p1.resulttype.def.typename)
  1573. else
  1574. if token=_RKLAMMER then
  1575. begin
  1576. if (tpointerdef(p1.resulttype.def).pointertype.def.deftype=objectdef) and
  1577. (oo_has_vmt in tobjectdef(tpointerdef(p1.resulttype.def).pointertype.def).objectoptions) then
  1578. Message(parser_w_use_extended_syntax_for_objects);
  1579. p2:=cnewnode.create(nil);
  1580. do_resulttypepass(p2);
  1581. p2.resulttype:=p1.resulttype;
  1582. p1.destroy;
  1583. p1:=p2;
  1584. consume(_RKLAMMER);
  1585. end
  1586. else
  1587. begin
  1588. p2:=chnewnode.create;
  1589. do_resulttypepass(p2);
  1590. p2.resulttype:=tpointerdef(p1.resulttype.def).pointertype;
  1591. consume(_COMMA);
  1592. afterassignment:=false;
  1593. { determines the current object defintion }
  1594. classh:=tobjectdef(p2.resulttype.def);
  1595. if classh.deftype<>objectdef then
  1596. Message(parser_e_pointer_to_class_expected)
  1597. else
  1598. begin
  1599. { check for an abstract class }
  1600. if (oo_has_abstract in classh.objectoptions) then
  1601. Message(sym_e_no_instance_of_abstract_object);
  1602. { search the constructor also in the symbol tables of
  1603. the parents }
  1604. sym:=nil;
  1605. while assigned(classh) do
  1606. begin
  1607. sym:=tsym(classh.symtable.search(pattern));
  1608. if assigned(sym) then
  1609. break;
  1610. classh:=classh.childof;
  1611. end;
  1612. consume(_ID);
  1613. do_member_read(false,sym,p2,again);
  1614. { we need to know which procedure is called }
  1615. do_resulttypepass(p2);
  1616. if (p2.nodetype<>calln) or
  1617. (assigned(tcallnode(p2).procdefinition) and
  1618. (tcallnode(p2).procdefinition.proctypeoption<>potype_constructor)) then
  1619. Message(parser_e_expr_have_to_be_constructor_call);
  1620. end;
  1621. p2:=cnewnode.create(p2);
  1622. do_resulttypepass(p2);
  1623. p2.resulttype:=p1.resulttype;
  1624. p1.destroy;
  1625. p1:=p2;
  1626. consume(_RKLAMMER);
  1627. end;
  1628. postfixoperators(p1,again);
  1629. end
  1630. else
  1631. begin
  1632. factor_read_id(p1,again);
  1633. if again then
  1634. begin
  1635. check_tokenpos;
  1636. { handle post fix operators }
  1637. postfixoperators(p1,again);
  1638. end;
  1639. end;
  1640. end
  1641. else
  1642. case token of
  1643. _SELF :
  1644. begin
  1645. again:=true;
  1646. consume(_SELF);
  1647. if not assigned(procinfo^._class) then
  1648. begin
  1649. p1:=cerrornode.create;
  1650. again:=false;
  1651. Message(parser_e_self_not_in_method);
  1652. end
  1653. else
  1654. begin
  1655. if (po_classmethod in aktprocsym.definition.procoptions) then
  1656. begin
  1657. { self in class methods is a class reference type }
  1658. htype.setdef(procinfo^._class);
  1659. p1:=cselfnode.create(tobjectdef(tclassrefdef.create(htype)));
  1660. end
  1661. else
  1662. p1:=cselfnode.create(procinfo^._class);
  1663. postfixoperators(p1,again);
  1664. end;
  1665. end;
  1666. _INHERITED :
  1667. begin
  1668. again:=true;
  1669. consume(_INHERITED);
  1670. if assigned(procinfo^._class) then
  1671. begin
  1672. { if inherited; only then we need the method with
  1673. the same name }
  1674. if token=_SEMICOLON then
  1675. begin
  1676. hs:=aktprocsym.name;
  1677. auto_inherited:=true
  1678. end
  1679. else
  1680. begin
  1681. hs:=pattern;
  1682. consume(_ID);
  1683. auto_inherited:=false;
  1684. end;
  1685. classh:=procinfo^._class.childof;
  1686. while assigned(classh) do
  1687. begin
  1688. sym:=tsym(tobjectdef(classh).symtable.search(hs));
  1689. if assigned(sym) then
  1690. begin
  1691. if sym.typ=procsym then
  1692. begin
  1693. htype.setdef(classh);
  1694. p1:=ctypenode.create(htype);
  1695. end;
  1696. do_member_read(false,sym,p1,again);
  1697. break;
  1698. end;
  1699. classh:=classh.childof;
  1700. end;
  1701. if classh=nil then
  1702. begin
  1703. Message1(sym_e_id_no_member,hs);
  1704. again:=false;
  1705. p1:=cerrornode.create;
  1706. end;
  1707. { turn auto inheriting off }
  1708. auto_inherited:=false;
  1709. end
  1710. else
  1711. begin
  1712. Message(parser_e_generic_methods_only_in_methods);
  1713. again:=false;
  1714. p1:=cerrornode.create;
  1715. end;
  1716. postfixoperators(p1,again);
  1717. end;
  1718. _INTCONST :
  1719. begin
  1720. { try cardinal first }
  1721. val(pattern,card,code);
  1722. if code<>0 then
  1723. begin
  1724. { then longint }
  1725. valint(pattern,l,code);
  1726. if code <> 0 then
  1727. begin
  1728. { then int64 }
  1729. val(pattern,ic,code);
  1730. if code<>0 then
  1731. begin
  1732. {finally float }
  1733. val(pattern,d,code);
  1734. if code<>0 then
  1735. begin
  1736. Message(cg_e_invalid_integer);
  1737. consume(_INTCONST);
  1738. l:=1;
  1739. p1:=cordconstnode.create(l,s32bittype);
  1740. end
  1741. else
  1742. begin
  1743. consume(_INTCONST);
  1744. p1:=crealconstnode.create(d,pbestrealtype^);
  1745. end;
  1746. end
  1747. else
  1748. begin
  1749. consume(_INTCONST);
  1750. p1:=cordconstnode.create(ic,cs64bittype);
  1751. end
  1752. end
  1753. else
  1754. begin
  1755. consume(_INTCONST);
  1756. p1:=cordconstnode.create(l,s32bittype)
  1757. end
  1758. end
  1759. else
  1760. begin
  1761. consume(_INTCONST);
  1762. { check whether the value isn't in the longint range as well }
  1763. { (longint is easier to perform calculations with) (JM) }
  1764. if card <= $7fffffff then
  1765. { no sign extension necessary, so not longint typecast (JM) }
  1766. p1:=cordconstnode.create(card,s32bittype)
  1767. else
  1768. p1:=cordconstnode.create(card,u32bittype)
  1769. end;
  1770. end;
  1771. _REALNUMBER :
  1772. begin
  1773. val(pattern,d,code);
  1774. if code<>0 then
  1775. begin
  1776. Message(parser_e_error_in_real);
  1777. d:=1.0;
  1778. end;
  1779. consume(_REALNUMBER);
  1780. p1:=crealconstnode.create(d,pbestrealtype^);
  1781. end;
  1782. _STRING :
  1783. begin
  1784. string_dec(htype);
  1785. { STRING can be also a type cast }
  1786. if token=_LKLAMMER then
  1787. begin
  1788. consume(_LKLAMMER);
  1789. p1:=comp_expr(true);
  1790. consume(_RKLAMMER);
  1791. p1:=ctypeconvnode.create(p1,htype);
  1792. include(p1.flags,nf_explizit);
  1793. { handle postfix operators here e.g. string(a)[10] }
  1794. again:=true;
  1795. postfixoperators(p1,again);
  1796. end
  1797. else
  1798. p1:=ctypenode.create(htype);
  1799. end;
  1800. _FILE :
  1801. begin
  1802. htype:=cfiletype;
  1803. consume(_FILE);
  1804. { FILE can be also a type cast }
  1805. if token=_LKLAMMER then
  1806. begin
  1807. consume(_LKLAMMER);
  1808. p1:=comp_expr(true);
  1809. consume(_RKLAMMER);
  1810. p1:=ctypeconvnode.create(p1,htype);
  1811. include(p1.flags,nf_explizit);
  1812. { handle postfix operators here e.g. string(a)[10] }
  1813. again:=true;
  1814. postfixoperators(p1,again);
  1815. end
  1816. else
  1817. begin
  1818. p1:=ctypenode.create(htype);
  1819. end;
  1820. end;
  1821. _CSTRING :
  1822. begin
  1823. p1:=cstringconstnode.createstr(pattern,st_default);
  1824. consume(_CSTRING);
  1825. end;
  1826. _CCHAR :
  1827. begin
  1828. p1:=cordconstnode.create(ord(pattern[1]),cchartype);
  1829. consume(_CCHAR);
  1830. end;
  1831. _CWSTRING:
  1832. begin
  1833. p1:=cstringconstnode.createwstr(patternw);
  1834. consume(_CWSTRING);
  1835. end;
  1836. _CWCHAR:
  1837. begin
  1838. p1:=cordconstnode.create(ord(getcharwidestring(patternw,0)),cwidechartype);
  1839. consume(_CWCHAR);
  1840. end;
  1841. _KLAMMERAFFE :
  1842. begin
  1843. consume(_KLAMMERAFFE);
  1844. got_addrn:=true;
  1845. { support both @<x> and @(<x>) }
  1846. if token=_LKLAMMER then
  1847. begin
  1848. consume(_LKLAMMER);
  1849. p1:=factor(true);
  1850. consume(_RKLAMMER);
  1851. if token in [_CARET,_POINT,_LECKKLAMMER] then
  1852. begin
  1853. again:=true;
  1854. postfixoperators(p1,again);
  1855. end;
  1856. end
  1857. else
  1858. p1:=factor(true);
  1859. got_addrn:=false;
  1860. p1:=caddrnode.create(p1);
  1861. end;
  1862. _LKLAMMER :
  1863. begin
  1864. consume(_LKLAMMER);
  1865. p1:=comp_expr(true);
  1866. consume(_RKLAMMER);
  1867. { it's not a good solution }
  1868. { but (a+b)^ makes some problems }
  1869. if token in [_CARET,_POINT,_LECKKLAMMER] then
  1870. begin
  1871. again:=true;
  1872. postfixoperators(p1,again);
  1873. end;
  1874. end;
  1875. _LECKKLAMMER :
  1876. begin
  1877. consume(_LECKKLAMMER);
  1878. p1:=factor_read_set;
  1879. consume(_RECKKLAMMER);
  1880. end;
  1881. _PLUS :
  1882. begin
  1883. consume(_PLUS);
  1884. p1:=factor(false);
  1885. end;
  1886. _MINUS :
  1887. begin
  1888. consume(_MINUS);
  1889. p1:=sub_expr(oppower,false);
  1890. p1:=cunaryminusnode.create(p1);
  1891. end;
  1892. _OP_NOT :
  1893. begin
  1894. consume(_OP_NOT);
  1895. p1:=factor(false);
  1896. p1:=cnotnode.create(p1);
  1897. end;
  1898. _TRUE :
  1899. begin
  1900. consume(_TRUE);
  1901. p1:=cordconstnode.create(1,booltype);
  1902. end;
  1903. _FALSE :
  1904. begin
  1905. consume(_FALSE);
  1906. p1:=cordconstnode.create(0,booltype);
  1907. end;
  1908. _NIL :
  1909. begin
  1910. consume(_NIL);
  1911. p1:=cnilnode.create;
  1912. end;
  1913. else
  1914. begin
  1915. p1:=cerrornode.create;
  1916. consume(token);
  1917. Message(cg_e_illegal_expression);
  1918. end;
  1919. end;
  1920. { generate error node if no node is created }
  1921. if not assigned(p1) then
  1922. begin
  1923. {$ifdef EXTDEBUG}
  1924. Comment(V_Warning,'factor: p1=nil');
  1925. {$endif}
  1926. p1:=cerrornode.create;
  1927. end;
  1928. { get the resulttype for the node }
  1929. if (not assigned(p1.resulttype.def)) then
  1930. do_resulttypepass(p1);
  1931. { tp7 procvar handling, but not if the next token
  1932. will be a := }
  1933. if (m_tp_procvar in aktmodeswitches) and
  1934. (token<>_ASSIGNMENT) then
  1935. check_tp_procvar(p1);
  1936. factor:=p1;
  1937. check_tokenpos;
  1938. end;
  1939. {$ifdef fpc}
  1940. {$maxfpuregisters default}
  1941. {$endif fpc}
  1942. {****************************************************************************
  1943. Sub_Expr
  1944. ****************************************************************************}
  1945. const
  1946. { Warning these stay be ordered !! }
  1947. operator_levels:array[Toperator_precedence] of set of Ttoken=
  1948. ([_LT,_LTE,_GT,_GTE,_EQUAL,_UNEQUAL,_OP_IN,_OP_IS],
  1949. [_PLUS,_MINUS,_OP_OR,_OP_XOR],
  1950. [_CARET,_SYMDIF,_STARSTAR,_STAR,_SLASH,
  1951. _OP_AS,_OP_AND,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR],
  1952. [_STARSTAR] );
  1953. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):tnode;
  1954. {Reads a subexpression while the operators are of the current precedence
  1955. level, or any higher level. Replaces the old term, simpl_expr and
  1956. simpl2_expr.}
  1957. var
  1958. p1,p2 : tnode;
  1959. oldt : Ttoken;
  1960. filepos : tfileposinfo;
  1961. begin
  1962. if pred_level=highest_precedence then
  1963. p1:=factor(false)
  1964. else
  1965. p1:=sub_expr(succ(pred_level),true);
  1966. repeat
  1967. if (token in operator_levels[pred_level]) and
  1968. ((token<>_EQUAL) or accept_equal) then
  1969. begin
  1970. oldt:=token;
  1971. filepos:=akttokenpos;
  1972. consume(token);
  1973. if pred_level=highest_precedence then
  1974. p2:=factor(false)
  1975. else
  1976. p2:=sub_expr(succ(pred_level),true);
  1977. case oldt of
  1978. _PLUS :
  1979. p1:=caddnode.create(addn,p1,p2);
  1980. _MINUS :
  1981. p1:=caddnode.create(subn,p1,p2);
  1982. _STAR :
  1983. p1:=caddnode.create(muln,p1,p2);
  1984. _SLASH :
  1985. p1:=caddnode.create(slashn,p1,p2);
  1986. _EQUAL :
  1987. p1:=caddnode.create(equaln,p1,p2);
  1988. _GT :
  1989. p1:=caddnode.create(gtn,p1,p2);
  1990. _LT :
  1991. p1:=caddnode.create(ltn,p1,p2);
  1992. _GTE :
  1993. p1:=caddnode.create(gten,p1,p2);
  1994. _LTE :
  1995. p1:=caddnode.create(lten,p1,p2);
  1996. _SYMDIF :
  1997. p1:=caddnode.create(symdifn,p1,p2);
  1998. _STARSTAR :
  1999. p1:=caddnode.create(starstarn,p1,p2);
  2000. _OP_AS :
  2001. p1:=casnode.create(p1,p2);
  2002. _OP_IN :
  2003. p1:=cinnode.create(p1,p2);
  2004. _OP_IS :
  2005. p1:=cisnode.create(p1,p2);
  2006. _OP_OR :
  2007. p1:=caddnode.create(orn,p1,p2);
  2008. _OP_AND :
  2009. p1:=caddnode.create(andn,p1,p2);
  2010. _OP_DIV :
  2011. p1:=cmoddivnode.create(divn,p1,p2);
  2012. _OP_NOT :
  2013. p1:=cnotnode.create(p1);
  2014. _OP_MOD :
  2015. p1:=cmoddivnode.create(modn,p1,p2);
  2016. _OP_SHL :
  2017. p1:=cshlshrnode.create(shln,p1,p2);
  2018. _OP_SHR :
  2019. p1:=cshlshrnode.create(shrn,p1,p2);
  2020. _OP_XOR :
  2021. p1:=caddnode.create(xorn,p1,p2);
  2022. _ASSIGNMENT :
  2023. p1:=cassignmentnode.create(p1,p2);
  2024. _CARET :
  2025. p1:=caddnode.create(caretn,p1,p2);
  2026. _UNEQUAL :
  2027. p1:=caddnode.create(unequaln,p1,p2);
  2028. end;
  2029. p1.set_tree_filepos(filepos);
  2030. end
  2031. else
  2032. break;
  2033. until false;
  2034. sub_expr:=p1;
  2035. end;
  2036. function comp_expr(accept_equal : boolean):tnode;
  2037. var
  2038. oldafterassignment : boolean;
  2039. p1 : tnode;
  2040. begin
  2041. oldafterassignment:=afterassignment;
  2042. afterassignment:=true;
  2043. p1:=sub_expr(opcompare,accept_equal);
  2044. { get the resulttype for this expression }
  2045. if not assigned(p1.resulttype.def) then
  2046. do_resulttypepass(p1);
  2047. afterassignment:=oldafterassignment;
  2048. comp_expr:=p1;
  2049. end;
  2050. function expr : tnode;
  2051. var
  2052. p1,p2 : tnode;
  2053. oldafterassignment : boolean;
  2054. oldp1 : tnode;
  2055. filepos : tfileposinfo;
  2056. begin
  2057. oldafterassignment:=afterassignment;
  2058. p1:=sub_expr(opcompare,true);
  2059. { get the resulttype for this expression }
  2060. if not assigned(p1.resulttype.def) then
  2061. do_resulttypepass(p1);
  2062. filepos:=akttokenpos;
  2063. if (m_tp_procvar in aktmodeswitches) and
  2064. (token<>_ASSIGNMENT) then
  2065. check_tp_procvar(p1);
  2066. if token in [_ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  2067. afterassignment:=true;
  2068. oldp1:=p1;
  2069. case token of
  2070. _POINTPOINT :
  2071. begin
  2072. consume(_POINTPOINT);
  2073. p2:=sub_expr(opcompare,true);
  2074. p1:=crangenode.create(p1,p2);
  2075. end;
  2076. _ASSIGNMENT :
  2077. begin
  2078. consume(_ASSIGNMENT);
  2079. if (m_tp_procvar in aktmodeswitches) and
  2080. (p1.resulttype.def.deftype=procvardef) then
  2081. begin
  2082. getprocvar:=true;
  2083. getprocvardef:=tprocvardef(p1.resulttype.def);
  2084. end;
  2085. p2:=sub_expr(opcompare,true);
  2086. if getprocvar then
  2087. handle_procvar(getprocvardef,p2);
  2088. getprocvar:=false;
  2089. p1:=cassignmentnode.create(p1,p2);
  2090. end;
  2091. _PLUSASN :
  2092. begin
  2093. consume(_PLUSASN);
  2094. p2:=sub_expr(opcompare,true);
  2095. p1:=cassignmentnode.create(p1,caddnode.create(addn,p1.getcopy,p2));
  2096. end;
  2097. _MINUSASN :
  2098. begin
  2099. consume(_MINUSASN);
  2100. p2:=sub_expr(opcompare,true);
  2101. p1:=cassignmentnode.create(p1,caddnode.create(subn,p1.getcopy,p2));
  2102. end;
  2103. _STARASN :
  2104. begin
  2105. consume(_STARASN );
  2106. p2:=sub_expr(opcompare,true);
  2107. p1:=cassignmentnode.create(p1,caddnode.create(muln,p1.getcopy,p2));
  2108. end;
  2109. _SLASHASN :
  2110. begin
  2111. consume(_SLASHASN );
  2112. p2:=sub_expr(opcompare,true);
  2113. p1:=cassignmentnode.create(p1,caddnode.create(slashn,p1.getcopy,p2));
  2114. end;
  2115. end;
  2116. { get the resulttype for this expression }
  2117. if not assigned(p1.resulttype.def) then
  2118. do_resulttypepass(p1);
  2119. afterassignment:=oldafterassignment;
  2120. if p1<>oldp1 then
  2121. p1.set_tree_filepos(filepos);
  2122. expr:=p1;
  2123. end;
  2124. {$ifdef int64funcresok}
  2125. function get_intconst:TConstExprInt;
  2126. {$else int64funcresok}
  2127. function get_intconst:longint;
  2128. {$endif int64funcresok}
  2129. {Reads an expression, tries to evalute it and check if it is an integer
  2130. constant. Then the constant is returned.}
  2131. var
  2132. p:tnode;
  2133. begin
  2134. p:=comp_expr(true);
  2135. if not codegenerror then
  2136. begin
  2137. if (p.nodetype<>ordconstn) or
  2138. not(is_integer(p.resulttype.def)) then
  2139. Message(cg_e_illegal_expression)
  2140. else
  2141. get_intconst:=tordconstnode(p).value;
  2142. end;
  2143. p.free;
  2144. end;
  2145. function get_stringconst:string;
  2146. {Reads an expression, tries to evaluate it and checks if it is a string
  2147. constant. Then the constant is returned.}
  2148. var
  2149. p:tnode;
  2150. begin
  2151. get_stringconst:='';
  2152. p:=comp_expr(true);
  2153. if p.nodetype<>stringconstn then
  2154. begin
  2155. if (p.nodetype=ordconstn) and is_char(p.resulttype.def) then
  2156. get_stringconst:=char(tordconstnode(p).value)
  2157. else
  2158. Message(cg_e_illegal_expression);
  2159. end
  2160. else
  2161. get_stringconst:=strpas(tstringconstnode(p).value_str);
  2162. p.free;
  2163. end;
  2164. end.
  2165. {
  2166. $Log$
  2167. Revision 1.36 2001-06-04 18:16:42 peter
  2168. * fixed tp procvar support in parameters of a called procvar
  2169. * typenode cleanup, no special handling needed anymore for bt_type
  2170. Revision 1.35 2001/06/04 11:45:35 peter
  2171. * parse const after .. using bt_const block to allow expressions, this
  2172. is Delphi compatible
  2173. Revision 1.34 2001/05/19 21:15:53 peter
  2174. * allow typenodes for typeinfo and typeof
  2175. * tp procvar fixes for properties
  2176. Revision 1.33 2001/05/19 12:23:59 peter
  2177. * fixed crash with auto dereferencing
  2178. Revision 1.32 2001/05/09 19:52:51 peter
  2179. * removed unused allow_type
  2180. Revision 1.31 2001/05/04 15:52:03 florian
  2181. * some Delphi incompatibilities fixed:
  2182. - out, dispose and new can be used as idenfiers now
  2183. - const p = apointerype(nil); is supported now
  2184. + support for const p = apointertype(pointer(1234)); added
  2185. Revision 1.30 2001/04/14 14:07:10 peter
  2186. * moved more code from pass_1 to det_resulttype
  2187. Revision 1.29 2001/04/13 23:50:24 peter
  2188. * fpc mode now requires @ also when left of assignment is an procvardef
  2189. Revision 1.28 2001/04/13 01:22:12 peter
  2190. * symtable change to classes
  2191. * range check generation and errors fixed, make cycle DEBUG=1 works
  2192. * memory leaks fixed
  2193. Revision 1.27 2001/04/04 22:43:52 peter
  2194. * remove unnecessary calls to firstpass
  2195. Revision 1.26 2001/04/02 21:20:33 peter
  2196. * resulttype rewrite
  2197. Revision 1.25 2001/03/11 22:58:50 peter
  2198. * getsym redesign, removed the globals srsym,srsymtable
  2199. Revision 1.24 2000/12/25 00:07:27 peter
  2200. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  2201. tlinkedlist objects)
  2202. Revision 1.23 2000/12/19 20:36:03 peter
  2203. * cardinal const expr fix from jonas
  2204. Revision 1.22 2000/12/17 14:00:18 peter
  2205. * fixed static variables
  2206. Revision 1.21 2000/12/15 13:26:01 jonas
  2207. * only return int64's from functions if it int64funcresok is defined
  2208. + added int64funcresok define to options.pas
  2209. Revision 1.20 2000/12/15 12:13:52 michael
  2210. + Fix from Peter
  2211. Revision 1.19 2000/12/07 17:19:42 jonas
  2212. * new constant handling: from now on, hex constants >$7fffffff are
  2213. parsed as unsigned constants (otherwise, $80000000 got sign extended
  2214. and became $ffffffff80000000), all constants in the longint range
  2215. become longints, all constants >$7fffffff and <=cardinal($ffffffff)
  2216. are cardinals and the rest are int64's.
  2217. * added lots of longint typecast to prevent range check errors in the
  2218. compiler and rtl
  2219. * type casts of symbolic ordinal constants are now preserved
  2220. * fixed bug where the original resulttype.def wasn't restored correctly
  2221. after doing a 64bit rangecheck
  2222. Revision 1.18 2000/11/29 00:30:36 florian
  2223. * unused units removed from uses clause
  2224. * some changes for widestrings
  2225. Revision 1.17 2000/11/09 17:46:55 florian
  2226. * System.TypeInfo fixed
  2227. + System.Finalize implemented
  2228. + some new keywords for interface support added
  2229. Revision 1.16 2000/11/06 20:30:55 peter
  2230. * more fixes to get make cycle working
  2231. Revision 1.15 2000/11/04 14:25:20 florian
  2232. + merged Attila's changes for interfaces, not tested yet
  2233. Revision 1.14 2000/10/31 22:02:49 peter
  2234. * symtable splitted, no real code changes
  2235. Revision 1.13 2000/10/26 23:40:54 peter
  2236. * fixed crash with call from type decl which is not allowed (merged)
  2237. Revision 1.12 2000/10/21 18:16:12 florian
  2238. * a lot of changes:
  2239. - basic dyn. array support
  2240. - basic C++ support
  2241. - some work for interfaces done
  2242. ....
  2243. Revision 1.11 2000/10/14 10:14:51 peter
  2244. * moehrendorf oct 2000 rewrite
  2245. Revision 1.10 2000/10/01 19:48:25 peter
  2246. * lot of compile updates for cg11
  2247. Revision 1.9 2000/09/24 21:19:50 peter
  2248. * delphi compile fixes
  2249. Revision 1.8 2000/09/24 15:06:22 peter
  2250. * use defines.inc
  2251. Revision 1.7 2000/08/27 16:11:51 peter
  2252. * moved some util functions from globals,cobjects to cutils
  2253. * splitted files into finput,fmodule
  2254. Revision 1.6 2000/08/20 15:12:49 peter
  2255. * auto derefence mode for array pointer (merged)
  2256. Revision 1.5 2000/08/16 18:33:53 peter
  2257. * splitted namedobjectitem.next into indexnext and listnext so it
  2258. can be used in both lists
  2259. * don't allow "word = word" type definitions (merged)
  2260. Revision 1.4 2000/08/16 13:06:06 florian
  2261. + support of 64 bit integer constants
  2262. Revision 1.3 2000/08/04 22:00:52 peter
  2263. * merges from fixes
  2264. Revision 1.2 2000/07/13 11:32:44 michael
  2265. + removed logs
  2266. }