pexpr.pas 89 KB

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