pexpr.pas 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Does parsing of expression for Free Pascal
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit pexpr;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. symtype,
  23. node,
  24. globals,
  25. cpuinfo;
  26. { reads a whole expression }
  27. function expr : tnode;
  28. { reads an expression without assignements and .. }
  29. function comp_expr(accept_equal : boolean):tnode;
  30. { reads a single factor }
  31. function factor(getaddr : boolean) : tnode;
  32. procedure string_dec(var t: ttype);
  33. function parse_paras(__colon,in_prop_paras : boolean) : tnode;
  34. { the ID token has to be consumed before calling this function }
  35. procedure do_member_read(getaddr : boolean;sym : tsym;var p1 : tnode;var again : boolean);
  36. {$ifdef int64funcresok}
  37. function get_intconst:TConstExprInt;
  38. {$else int64funcresok}
  39. function get_intconst:longint;
  40. {$endif int64funcresok}
  41. function get_stringconst:string;
  42. implementation
  43. uses
  44. {$ifdef delphi}
  45. SysUtils,
  46. {$endif}
  47. { common }
  48. cutils,
  49. { global }
  50. globtype,tokens,verbose,
  51. systems,widestr,
  52. { symtable }
  53. symconst,symbase,symdef,symsym,symtable,defutil,defcmp,
  54. { pass 1 }
  55. pass_1,htypechk,
  56. nmat,nadd,ncal,nmem,nset,ncnv,ninl,ncon,nld,nflw,nbas,
  57. { parser }
  58. scanner,
  59. pbase,pinline,
  60. { codegen }
  61. cgbase
  62. ;
  63. { sub_expr(opmultiply) is need to get -1 ** 4 to be
  64. read as - (1**4) and not (-1)**4 PM }
  65. type
  66. Toperator_precedence=(opcompare,opaddition,opmultiply,oppower);
  67. const
  68. highest_precedence = oppower;
  69. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):tnode;forward;
  70. const
  71. got_addrn : boolean = false;
  72. anon_inherited : boolean = false;
  73. procedure string_dec(var t: ttype);
  74. { reads a string type with optional length }
  75. { and returns a pointer to the string }
  76. { definition }
  77. var
  78. p : tnode;
  79. begin
  80. t:=cshortstringtype;
  81. consume(_STRING);
  82. if token=_LECKKLAMMER then
  83. begin
  84. consume(_LECKKLAMMER);
  85. p:=comp_expr(true);
  86. if not is_constintnode(p) then
  87. begin
  88. Message(cg_e_illegal_expression);
  89. { error recovery }
  90. consume(_RECKKLAMMER);
  91. end
  92. else
  93. begin
  94. if (tordconstnode(p).value<=0) then
  95. begin
  96. Message(parser_e_invalid_string_size);
  97. tordconstnode(p).value:=255;
  98. end;
  99. consume(_RECKKLAMMER);
  100. if tordconstnode(p).value>255 then
  101. begin
  102. { longstring is currently unsupported (CEC)! }
  103. { t.setdef(tstringdef.createlong(tordconstnode(p).value))}
  104. Message(parser_e_invalid_string_size);
  105. tordconstnode(p).value:=255;
  106. t.setdef(tstringdef.createshort(tordconstnode(p).value));
  107. end
  108. else
  109. if tordconstnode(p).value<>255 then
  110. t.setdef(tstringdef.createshort(tordconstnode(p).value));
  111. end;
  112. p.free;
  113. end
  114. else
  115. begin
  116. if cs_ansistrings in aktlocalswitches then
  117. t:=cansistringtype
  118. else
  119. t:=cshortstringtype;
  120. end;
  121. end;
  122. function parse_paras(__colon,in_prop_paras : boolean) : tnode;
  123. var
  124. p1,p2 : tnode;
  125. end_of_paras : ttoken;
  126. prev_in_args : boolean;
  127. old_allow_array_constructor : boolean;
  128. begin
  129. if in_prop_paras then
  130. end_of_paras:=_RECKKLAMMER
  131. else
  132. end_of_paras:=_RKLAMMER;
  133. if token=end_of_paras then
  134. begin
  135. parse_paras:=nil;
  136. exit;
  137. end;
  138. { save old values }
  139. prev_in_args:=in_args;
  140. old_allow_array_constructor:=allow_array_constructor;
  141. { set para parsing values }
  142. in_args:=true;
  143. inc(parsing_para_level);
  144. allow_array_constructor:=true;
  145. p2:=nil;
  146. while true do
  147. begin
  148. p1:=comp_expr(true);
  149. p2:=ccallparanode.create(p1,p2);
  150. { it's for the str(l:5,s); }
  151. if __colon and (token=_COLON) then
  152. begin
  153. consume(_COLON);
  154. p1:=comp_expr(true);
  155. p2:=ccallparanode.create(p1,p2);
  156. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  157. if token=_COLON then
  158. begin
  159. consume(_COLON);
  160. p1:=comp_expr(true);
  161. p2:=ccallparanode.create(p1,p2);
  162. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  163. end
  164. end;
  165. if token=_COMMA then
  166. consume(_COMMA)
  167. else
  168. break;
  169. end;
  170. allow_array_constructor:=old_allow_array_constructor;
  171. dec(parsing_para_level);
  172. in_args:=prev_in_args;
  173. parse_paras:=p2;
  174. end;
  175. procedure check_tp_procvar(var p : tnode);
  176. var
  177. hp,
  178. p1 : tnode;
  179. begin
  180. if (m_tp_procvar in aktmodeswitches) and
  181. (token<>_ASSIGNMENT) and
  182. (not got_addrn) and
  183. (block_type=bt_general) then
  184. begin
  185. { ignore vecn,subscriptn }
  186. hp:=p;
  187. repeat
  188. case hp.nodetype of
  189. vecn :
  190. hp:=tvecnode(hp).left;
  191. subscriptn :
  192. hp:=tsubscriptnode(hp).left;
  193. else
  194. break;
  195. end;
  196. until false;
  197. if (hp.nodetype=loadn) then
  198. begin
  199. { get the resulttype of p }
  200. do_resulttypepass(p);
  201. { convert the procvar load to a call:
  202. - not expecting a procvar
  203. - the procvar does not get arguments, when it
  204. requires arguments the callnode will fail
  205. Note: When arguments were passed there was no loadn }
  206. if (getprocvardef=nil) and
  207. (p.resulttype.def.deftype=procvardef) and
  208. (tprocvardef(p.resulttype.def).minparacount=0) then
  209. begin
  210. p1:=ccallnode.create(nil,nil,nil,nil);
  211. tcallnode(p1).set_procvar(p);
  212. resulttypepass(p1);
  213. p:=p1;
  214. end;
  215. end;
  216. end;
  217. end;
  218. function statement_syssym(l : longint) : tnode;
  219. var
  220. p1,p2,paras : tnode;
  221. err,
  222. prev_in_args : boolean;
  223. begin
  224. prev_in_args:=in_args;
  225. case l of
  226. in_new_x :
  227. begin
  228. if afterassignment or in_args then
  229. statement_syssym:=new_function
  230. else
  231. statement_syssym:=new_dispose_statement(true);
  232. end;
  233. in_dispose_x :
  234. begin
  235. statement_syssym:=new_dispose_statement(false);
  236. end;
  237. in_ord_x :
  238. begin
  239. consume(_LKLAMMER);
  240. in_args:=true;
  241. p1:=comp_expr(true);
  242. consume(_RKLAMMER);
  243. p1:=geninlinenode(in_ord_x,false,p1);
  244. statement_syssym := p1;
  245. end;
  246. in_exit :
  247. begin
  248. if try_to_consume(_LKLAMMER) then
  249. begin
  250. p1:=comp_expr(true);
  251. consume(_RKLAMMER);
  252. if (block_type=bt_except) then
  253. Message(parser_e_exit_with_argument_not__possible);
  254. if is_void(current_procdef.rettype.def) then
  255. Message(parser_e_void_function);
  256. end
  257. else
  258. p1:=nil;
  259. statement_syssym:=cexitnode.create(p1);
  260. end;
  261. in_break :
  262. begin
  263. statement_syssym:=cbreaknode.create;
  264. end;
  265. in_continue :
  266. begin
  267. statement_syssym:=ccontinuenode.create;
  268. end;
  269. in_typeof_x :
  270. begin
  271. consume(_LKLAMMER);
  272. in_args:=true;
  273. p1:=comp_expr(true);
  274. consume(_RKLAMMER);
  275. if p1.nodetype=typen then
  276. ttypenode(p1).allowed:=true;
  277. if (p1.resulttype.def.deftype = objectdef) or
  278. ((p1.resulttype.def.deftype = classrefdef) and
  279. (p1.nodetype in [selfn,loadvmtn])) then
  280. statement_syssym:=geninlinenode(in_typeof_x,false,p1)
  281. else
  282. begin
  283. Message(type_e_mismatch);
  284. p1.destroy;
  285. statement_syssym:=cerrornode.create;
  286. end;
  287. end;
  288. in_sizeof_x :
  289. begin
  290. consume(_LKLAMMER);
  291. in_args:=true;
  292. p1:=comp_expr(true);
  293. consume(_RKLAMMER);
  294. if (p1.nodetype<>typen) and
  295. (
  296. (is_object(p1.resulttype.def) and
  297. (oo_has_constructor in tobjectdef(p1.resulttype.def).objectoptions)) or
  298. is_open_array(p1.resulttype.def) or
  299. is_open_string(p1.resulttype.def)
  300. ) then
  301. statement_syssym:=geninlinenode(in_sizeof_x,false,p1)
  302. else
  303. begin
  304. statement_syssym:=cordconstnode.create(p1.resulttype.def.size,s32bittype,true);
  305. { p1 not needed !}
  306. p1.destroy;
  307. end;
  308. end;
  309. in_typeinfo_x :
  310. begin
  311. consume(_LKLAMMER);
  312. in_args:=true;
  313. p1:=comp_expr(true);
  314. if p1.nodetype=typen then
  315. ttypenode(p1).allowed:=true
  316. else
  317. begin
  318. p1.destroy;
  319. p1:=cerrornode.create;
  320. Message(parser_e_illegal_parameter_list);
  321. end;
  322. consume(_RKLAMMER);
  323. p2:=ccallparanode.create(p1,nil);
  324. p2:=geninlinenode(in_typeinfo_x,false,p2);
  325. statement_syssym:=p2;
  326. end;
  327. in_assigned_x :
  328. begin
  329. err:=false;
  330. consume(_LKLAMMER);
  331. in_args:=true;
  332. p1:=comp_expr(true);
  333. if not codegenerror then
  334. begin
  335. { With tp procvars we allways need to load a
  336. procvar when it is passed }
  337. if (m_tp_procvar in aktmodeswitches) and
  338. (p1.nodetype=calln) then
  339. load_procvar_from_calln(p1);
  340. case p1.resulttype.def.deftype of
  341. pointerdef,
  342. procvardef,
  343. classrefdef : ;
  344. objectdef :
  345. if not is_class_or_interface(p1.resulttype.def) then
  346. begin
  347. Message(parser_e_illegal_parameter_list);
  348. err:=true;
  349. end;
  350. else
  351. begin
  352. Message(parser_e_illegal_parameter_list);
  353. err:=true;
  354. end;
  355. end;
  356. end
  357. else
  358. err:=true;
  359. if not err then
  360. begin
  361. p2:=ccallparanode.create(p1,nil);
  362. p2:=geninlinenode(in_assigned_x,false,p2);
  363. end
  364. else
  365. begin
  366. p1.free;
  367. p2:=cerrornode.create;
  368. end;
  369. consume(_RKLAMMER);
  370. statement_syssym:=p2;
  371. end;
  372. in_addr_x :
  373. begin
  374. consume(_LKLAMMER);
  375. in_args:=true;
  376. p1:=comp_expr(true);
  377. p1:=caddrnode.create(p1);
  378. consume(_RKLAMMER);
  379. statement_syssym:=p1;
  380. end;
  381. in_ofs_x :
  382. begin
  383. consume(_LKLAMMER);
  384. in_args:=true;
  385. p1:=comp_expr(true);
  386. p1:=caddrnode.create(p1);
  387. do_resulttypepass(p1);
  388. { Ofs() returns a cardinal, not a pointer }
  389. p1.resulttype:=u32bittype;
  390. consume(_RKLAMMER);
  391. statement_syssym:=p1;
  392. end;
  393. in_seg_x :
  394. begin
  395. consume(_LKLAMMER);
  396. in_args:=true;
  397. p1:=comp_expr(true);
  398. p1:=geninlinenode(in_seg_x,false,p1);
  399. consume(_RKLAMMER);
  400. statement_syssym:=p1;
  401. end;
  402. in_high_x,
  403. in_low_x :
  404. begin
  405. consume(_LKLAMMER);
  406. in_args:=true;
  407. p1:=comp_expr(true);
  408. p2:=geninlinenode(l,false,p1);
  409. consume(_RKLAMMER);
  410. statement_syssym:=p2;
  411. end;
  412. in_succ_x,
  413. in_pred_x :
  414. begin
  415. consume(_LKLAMMER);
  416. in_args:=true;
  417. p1:=comp_expr(true);
  418. p2:=geninlinenode(l,false,p1);
  419. consume(_RKLAMMER);
  420. statement_syssym:=p2;
  421. end;
  422. in_inc_x,
  423. in_dec_x :
  424. begin
  425. consume(_LKLAMMER);
  426. in_args:=true;
  427. p1:=comp_expr(true);
  428. if token=_COMMA then
  429. begin
  430. consume(_COMMA);
  431. p2:=ccallparanode.create(comp_expr(true),nil);
  432. end
  433. else
  434. p2:=nil;
  435. p2:=ccallparanode.create(p1,p2);
  436. statement_syssym:=geninlinenode(l,false,p2);
  437. consume(_RKLAMMER);
  438. end;
  439. in_finalize_x:
  440. begin
  441. statement_syssym:=inline_finalize;
  442. end;
  443. in_copy_x:
  444. begin
  445. statement_syssym:=inline_copy;
  446. end;
  447. in_concat_x :
  448. begin
  449. consume(_LKLAMMER);
  450. in_args:=true;
  451. p2:=nil;
  452. while true do
  453. begin
  454. p1:=comp_expr(true);
  455. set_varstate(p1,true);
  456. if not((p1.resulttype.def.deftype=stringdef) or
  457. ((p1.resulttype.def.deftype=orddef) and
  458. (torddef(p1.resulttype.def).typ=uchar))) then
  459. Message(parser_e_illegal_parameter_list);
  460. if p2<>nil then
  461. p2:=caddnode.create(addn,p2,p1)
  462. else
  463. p2:=p1;
  464. if token=_COMMA then
  465. consume(_COMMA)
  466. else
  467. break;
  468. end;
  469. consume(_RKLAMMER);
  470. statement_syssym:=p2;
  471. end;
  472. in_read_x,
  473. in_readln_x :
  474. begin
  475. if token=_LKLAMMER then
  476. begin
  477. consume(_LKLAMMER);
  478. paras:=parse_paras(false,false);
  479. consume(_RKLAMMER);
  480. end
  481. else
  482. paras:=nil;
  483. p1:=geninlinenode(l,false,paras);
  484. statement_syssym := p1;
  485. end;
  486. in_setlength_x:
  487. begin
  488. statement_syssym := inline_setlength;
  489. end;
  490. in_length_x:
  491. begin
  492. consume(_LKLAMMER);
  493. in_args:=true;
  494. p1:=comp_expr(true);
  495. p2:=geninlinenode(l,false,p1);
  496. consume(_RKLAMMER);
  497. statement_syssym:=p2;
  498. end;
  499. in_write_x,
  500. in_writeln_x :
  501. begin
  502. if token=_LKLAMMER then
  503. begin
  504. consume(_LKLAMMER);
  505. paras:=parse_paras(true,false);
  506. consume(_RKLAMMER);
  507. end
  508. else
  509. paras:=nil;
  510. p1 := geninlinenode(l,false,paras);
  511. statement_syssym := p1;
  512. end;
  513. in_str_x_string :
  514. begin
  515. consume(_LKLAMMER);
  516. paras:=parse_paras(true,false);
  517. consume(_RKLAMMER);
  518. p1 := geninlinenode(l,false,paras);
  519. statement_syssym := p1;
  520. end;
  521. in_val_x:
  522. Begin
  523. consume(_LKLAMMER);
  524. in_args := true;
  525. p1:= ccallparanode.create(comp_expr(true), nil);
  526. consume(_COMMA);
  527. p2 := ccallparanode.create(comp_expr(true),p1);
  528. if (token = _COMMA) then
  529. Begin
  530. consume(_COMMA);
  531. p2 := ccallparanode.create(comp_expr(true),p2)
  532. End;
  533. consume(_RKLAMMER);
  534. p2 := geninlinenode(l,false,p2);
  535. statement_syssym := p2;
  536. End;
  537. in_include_x_y,
  538. in_exclude_x_y :
  539. begin
  540. consume(_LKLAMMER);
  541. in_args:=true;
  542. p1:=comp_expr(true);
  543. consume(_COMMA);
  544. p2:=comp_expr(true);
  545. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,nil)));
  546. consume(_RKLAMMER);
  547. end;
  548. in_assert_x_y :
  549. begin
  550. consume(_LKLAMMER);
  551. in_args:=true;
  552. p1:=comp_expr(true);
  553. if token=_COMMA then
  554. begin
  555. consume(_COMMA);
  556. p2:=comp_expr(true);
  557. end
  558. else
  559. begin
  560. { then insert an empty string }
  561. p2:=cstringconstnode.createstr('',st_default);
  562. end;
  563. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,nil)));
  564. consume(_RKLAMMER);
  565. end;
  566. else
  567. internalerror(15);
  568. end;
  569. in_args:=prev_in_args;
  570. end;
  571. { reads the parameter for a subroutine call }
  572. procedure do_proc_call(sym:tsym;st:tsymtable;getaddr:boolean;var again : boolean;var p1:tnode);
  573. var
  574. prevafterassn : boolean;
  575. hs,hs1 : tvarsym;
  576. para,p2 : tnode;
  577. hst : tsymtable;
  578. aprocdef : tprocdef;
  579. begin
  580. prevafterassn:=afterassignment;
  581. afterassignment:=false;
  582. aprocdef:=nil;
  583. { When we are expecting a procvar we also need
  584. to get the address in some cases }
  585. if assigned(getprocvardef) then
  586. begin
  587. if (block_type=bt_const) or
  588. getaddr then
  589. begin
  590. aprocdef:=Tprocsym(sym).search_procdef_byprocvardef(getprocvardef);
  591. getaddr:=true;
  592. end
  593. else
  594. if (m_tp_procvar in aktmodeswitches) then
  595. begin
  596. aprocdef:=Tprocsym(sym).search_procdef_byprocvardef(getprocvardef);
  597. if assigned(aprocdef) then
  598. getaddr:=true;
  599. end;
  600. end;
  601. { want we only determine the address of }
  602. { a subroutine ? }
  603. if not(getaddr) then
  604. begin
  605. para:=nil;
  606. if anon_inherited then
  607. begin
  608. hst:=symtablestack;
  609. while assigned(hst) and (hst.symtabletype<>parasymtable) do
  610. hst:=hst.next;
  611. if assigned(hst) then
  612. begin
  613. hs:=tvarsym(hst.symindex.first);
  614. while assigned(hs) do
  615. begin
  616. if hs.typ<>varsym then
  617. internalerror(54382953);
  618. { if there is a localcopy then use that }
  619. if assigned(hs.localvarsym) then
  620. hs1:=hs.localvarsym
  621. else
  622. hs1:=hs;
  623. para:=ccallparanode.create(cloadnode.create(hs1,hs1.owner),para);
  624. hs:=tvarsym(hs.indexnext);
  625. end;
  626. end
  627. else
  628. internalerror(54382954);
  629. end
  630. else
  631. begin
  632. if try_to_consume(_LKLAMMER) then
  633. begin
  634. para:=parse_paras(false,false);
  635. consume(_RKLAMMER);
  636. end;
  637. end;
  638. p1:=ccallnode.create(para,tprocsym(sym),st,p1);
  639. end
  640. else
  641. begin
  642. { address operator @: }
  643. if not assigned(p1) then
  644. begin
  645. case st.symtabletype of
  646. withsymtable :
  647. begin
  648. if (st.defowner.deftype=objectdef) then
  649. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  650. end;
  651. objectsymtable :
  652. begin
  653. { we must provide a method pointer, if it isn't given, }
  654. { it is self }
  655. p1:=cselfnode.create(tobjectdef(st.defowner));
  656. end;
  657. end;
  658. end;
  659. { Retrieve info which procvar to call. For tp_procvar the
  660. aprocdef is already loaded above so we can reuse it }
  661. if not assigned(aprocdef) and
  662. assigned(getprocvardef) then
  663. aprocdef:=Tprocsym(sym).search_procdef_byprocvardef(getprocvardef);
  664. { generate a methodcallnode or proccallnode }
  665. { we shouldn't convert things like @tcollection.load }
  666. p2:=cloadnode.create_procvar(sym,aprocdef,st);
  667. if assigned(p1) then
  668. begin
  669. if (p1.nodetype<>typen) then
  670. tloadnode(p2).set_mp(p1)
  671. else
  672. p1.free;
  673. end;
  674. p1:=p2;
  675. { no postfix operators }
  676. again:=false;
  677. end;
  678. afterassignment:=prevafterassn;
  679. end;
  680. procedure handle_procvar(pv : tprocvardef;var p2 : tnode);
  681. var
  682. hp,hp2 : tnode;
  683. hpp : ^tnode;
  684. currprocdef : tprocdef;
  685. begin
  686. if not assigned(pv) then
  687. internalerror(200301121);
  688. if (m_tp_procvar in aktmodeswitches) then
  689. begin
  690. hp:=p2;
  691. hpp:=@p2;
  692. while assigned(hp) and
  693. (hp.nodetype=typeconvn) do
  694. begin
  695. hp:=ttypeconvnode(hp).left;
  696. { save orignal address of the old tree so we can replace the node }
  697. hpp:=@hp;
  698. end;
  699. if (hp.nodetype=calln) and
  700. { a procvar can't have parameters! }
  701. not assigned(tcallnode(hp).left) then
  702. begin
  703. currprocdef:=tcallnode(hp).symtableprocentry.search_procdef_byprocvardef(pv);
  704. if assigned(currprocdef) then
  705. begin
  706. hp2:=cloadnode.create_procvar(tprocsym(tcallnode(hp).symtableprocentry),currprocdef,tcallnode(hp).symtableproc);
  707. if (po_methodpointer in pv.procoptions) then
  708. tloadnode(hp2).set_mp(tnode(tcallnode(hp).methodpointer).getcopy);
  709. hp.destroy;
  710. { replace the old callnode with the new loadnode }
  711. hpp^:=hp2;
  712. end;
  713. end;
  714. end;
  715. end;
  716. { the following procedure handles the access to a property symbol }
  717. procedure handle_propertysym(sym : tsym;st : tsymtable;var p1 : tnode; getaddr: boolean);
  718. procedure symlist_to_node(var p1:tnode;pl:tsymlist);
  719. var
  720. plist : psymlistitem;
  721. begin
  722. plist:=pl.firstsym;
  723. while assigned(plist) do
  724. begin
  725. case plist^.sltype of
  726. sl_load :
  727. begin
  728. { p1 can already contain the loadnode of
  729. the class variable. Then we need to use a
  730. subscriptn. If no tree is found (with block), then
  731. generate a loadn }
  732. if assigned(p1) then
  733. p1:=csubscriptnode.create(plist^.sym,p1)
  734. else
  735. p1:=cloadnode.create(plist^.sym,st);
  736. end;
  737. sl_subscript :
  738. p1:=csubscriptnode.create(plist^.sym,p1);
  739. sl_vec :
  740. p1:=cvecnode.create(p1,cordconstnode.create(plist^.value,s32bittype,true));
  741. else
  742. internalerror(200110205);
  743. end;
  744. plist:=plist^.next;
  745. end;
  746. include(p1.flags,nf_isproperty);
  747. end;
  748. var
  749. paras : tnode;
  750. p2 : tnode;
  751. begin
  752. paras:=nil;
  753. { property parameters? read them only if the property really }
  754. { has parameters }
  755. if (ppo_hasparameters in tpropertysym(sym).propoptions) then
  756. begin
  757. if token=_LECKKLAMMER then
  758. begin
  759. consume(_LECKKLAMMER);
  760. paras:=parse_paras(false,true);
  761. consume(_RECKKLAMMER);
  762. end;
  763. end;
  764. { indexed property }
  765. if (ppo_indexed in tpropertysym(sym).propoptions) then
  766. begin
  767. p2:=cordconstnode.create(tpropertysym(sym).index,tpropertysym(sym).indextype,true);
  768. paras:=ccallparanode.create(p2,paras);
  769. end;
  770. { we need only a write property if a := follows }
  771. { if not(afterassignment) and not(in_args) then }
  772. if token=_ASSIGNMENT then
  773. begin
  774. { write property: }
  775. if not tpropertysym(sym).writeaccess.empty then
  776. begin
  777. case tpropertysym(sym).writeaccess.firstsym^.sym.typ of
  778. procsym :
  779. begin
  780. { generate the method call }
  781. p1:=ccallnode.create(paras,
  782. tprocsym(tpropertysym(sym).writeaccess.firstsym^.sym),st,p1);
  783. paras:=nil;
  784. consume(_ASSIGNMENT);
  785. { read the expression }
  786. if tpropertysym(sym).proptype.def.deftype=procvardef then
  787. getprocvardef:=tprocvardef(tpropertysym(sym).proptype.def);
  788. p2:=comp_expr(true);
  789. if assigned(getprocvardef) then
  790. handle_procvar(getprocvardef,p2);
  791. tcallnode(p1).left:=ccallparanode.create(p2,tcallnode(p1).left);
  792. include(tcallnode(p1).flags,nf_isproperty);
  793. getprocvardef:=nil;
  794. end;
  795. varsym :
  796. begin
  797. { generate access code }
  798. symlist_to_node(p1,tpropertysym(sym).writeaccess);
  799. consume(_ASSIGNMENT);
  800. { read the expression }
  801. p2:=comp_expr(true);
  802. p1:=cassignmentnode.create(p1,p2);
  803. end
  804. else
  805. begin
  806. p1:=cerrornode.create;
  807. Message(parser_e_no_procedure_to_access_property);
  808. end;
  809. end;
  810. end
  811. else
  812. begin
  813. p1:=cerrornode.create;
  814. Message(parser_e_no_procedure_to_access_property);
  815. end;
  816. end
  817. else
  818. begin
  819. { read property: }
  820. if not tpropertysym(sym).readaccess.empty then
  821. begin
  822. case tpropertysym(sym).readaccess.firstsym^.sym.typ of
  823. varsym :
  824. begin
  825. { generate access code }
  826. symlist_to_node(p1,tpropertysym(sym).readaccess);
  827. end;
  828. procsym :
  829. begin
  830. { generate the method call }
  831. p1:=ccallnode.create(paras,tprocsym(tpropertysym(sym).readaccess.firstsym^.sym),st,p1);
  832. paras:=nil;
  833. include(p1.flags,nf_isproperty);
  834. end
  835. else
  836. begin
  837. p1:=cerrornode.create;
  838. Message(type_e_mismatch);
  839. end;
  840. end;
  841. end
  842. else
  843. begin
  844. { error, no function to read property }
  845. p1:=cerrornode.create;
  846. Message(parser_e_no_procedure_to_access_property);
  847. end;
  848. end;
  849. { release paras if not used }
  850. if assigned(paras) then
  851. paras.free;
  852. end;
  853. { the ID token has to be consumed before calling this function }
  854. procedure do_member_read(getaddr : boolean;sym : tsym;var p1 : tnode;var again : boolean);
  855. var
  856. static_name : string;
  857. isclassref : boolean;
  858. srsymtable : tsymtable;
  859. begin
  860. if sym=nil then
  861. begin
  862. { pattern is still valid unless
  863. there is another ID just after the ID of sym }
  864. Message1(sym_e_id_no_member,pattern);
  865. p1.free;
  866. p1:=cerrornode.create;
  867. { try to clean up }
  868. again:=false;
  869. end
  870. else
  871. begin
  872. if assigned(p1) then
  873. begin
  874. if not assigned(p1.resulttype.def) then
  875. do_resulttypepass(p1);
  876. isclassref:=(p1.resulttype.def.deftype=classrefdef);
  877. end
  878. else
  879. isclassref:=false;
  880. { we assume, that only procsyms and varsyms are in an object }
  881. { symbol table, for classes, properties are allowed }
  882. case sym.typ of
  883. procsym:
  884. begin
  885. do_proc_call(sym,sym.owner,
  886. (getaddr and not(token in [_CARET,_POINT])),
  887. again,p1);
  888. { we need to know which procedure is called }
  889. do_resulttypepass(p1);
  890. { now we know the real method e.g. we can check for a class method }
  891. if isclassref and
  892. (p1.nodetype=calln) and
  893. assigned(tcallnode(p1).procdefinition) and
  894. not(po_classmethod in tcallnode(p1).procdefinition.procoptions) and
  895. not(tcallnode(p1).procdefinition.proctypeoption=potype_constructor) then
  896. Message(parser_e_only_class_methods_via_class_ref);
  897. end;
  898. varsym:
  899. begin
  900. if isclassref then
  901. Message(parser_e_only_class_methods_via_class_ref);
  902. if (sp_static in sym.symoptions) then
  903. begin
  904. static_name:=lower(sym.owner.name^)+'_'+sym.name;
  905. searchsym(static_name,sym,srsymtable);
  906. check_hints(sym);
  907. p1.free;
  908. p1:=cloadnode.create(sym,srsymtable);
  909. end
  910. else
  911. p1:=csubscriptnode.create(sym,p1);
  912. end;
  913. propertysym:
  914. begin
  915. if isclassref then
  916. Message(parser_e_only_class_methods_via_class_ref);
  917. handle_propertysym(sym,sym.owner,p1,getaddr);
  918. end;
  919. else internalerror(16);
  920. end;
  921. end;
  922. end;
  923. {****************************************************************************
  924. Factor
  925. ****************************************************************************}
  926. {$ifdef fpc}
  927. {$maxfpuregisters 0}
  928. {$endif fpc}
  929. function factor(getaddr : boolean) : tnode;
  930. {---------------------------------------------
  931. Factor_read_id
  932. ---------------------------------------------}
  933. procedure factor_read_id(var p1:tnode;var again:boolean);
  934. var
  935. pc : pchar;
  936. len : longint;
  937. srsym : tsym;
  938. possible_error : boolean;
  939. srsymtable : tsymtable;
  940. storesymtablestack : tsymtable;
  941. htype : ttype;
  942. static_name : string;
  943. begin
  944. { allow post fix operators }
  945. again:=true;
  946. consume_sym(srsym,srsymtable);
  947. { Access to funcret or need to call the function? }
  948. if (srsym.typ in [absolutesym,varsym]) and
  949. (vo_is_funcret in tvarsym(srsym).varoptions) and
  950. (
  951. (token=_LKLAMMER) or
  952. (not(m_fpc in aktmodeswitches) and
  953. (afterassignment or in_args) and
  954. not(vo_is_result in tvarsym(srsym).varoptions))
  955. ) then
  956. begin
  957. storesymtablestack:=symtablestack;
  958. symtablestack:=srsym.owner.next;
  959. searchsym(srsym.name,srsym,srsymtable);
  960. if not assigned(srsym) then
  961. srsym:=generrorsym;
  962. if (srsym.typ<>procsym) then
  963. Message(cg_e_illegal_expression);
  964. symtablestack:=storesymtablestack;
  965. end;
  966. begin
  967. { check semantics of private }
  968. if (srsym.typ in [propertysym,procsym,varsym]) and
  969. (srsym.owner.symtabletype=objectsymtable) then
  970. begin
  971. if (sp_private in srsym.symoptions) and
  972. (tobjectdef(srsym.owner.defowner).owner.symtabletype=globalsymtable) and
  973. (tobjectdef(srsym.owner.defowner).owner.unitid<>0) then
  974. Message(parser_e_cant_access_private_member);
  975. end;
  976. case srsym.typ of
  977. absolutesym :
  978. begin
  979. p1:=cloadnode.create(srsym,srsymtable);
  980. end;
  981. varsym :
  982. begin
  983. { are we in a class method, we check here the
  984. srsymtable, because a field in another object
  985. also has objectsymtable. And withsymtable is
  986. not possible for self in class methods (PFV) }
  987. if (srsymtable.symtabletype=objectsymtable) and
  988. assigned(current_procdef) and
  989. (po_classmethod in current_procdef.procoptions) then
  990. Message(parser_e_only_class_methods);
  991. if (sp_static in srsym.symoptions) then
  992. begin
  993. static_name:=lower(srsym.owner.name^)+'_'+srsym.name;
  994. searchsym(static_name,srsym,srsymtable);
  995. check_hints(srsym);
  996. end;
  997. p1:=cloadnode.create(srsym,srsymtable);
  998. if tvarsym(srsym).varstate=vs_declared then
  999. begin
  1000. include(p1.flags,nf_first_use);
  1001. { set special between first loaded until checked in resulttypepass }
  1002. tvarsym(srsym).varstate:=vs_declared_and_first_found;
  1003. end;
  1004. end;
  1005. typedconstsym :
  1006. begin
  1007. p1:=cloadnode.create(srsym,srsymtable);
  1008. end;
  1009. syssym :
  1010. begin
  1011. p1:=statement_syssym(tsyssym(srsym).number);
  1012. end;
  1013. typesym :
  1014. begin
  1015. htype.setsym(srsym);
  1016. if not assigned(htype.def) then
  1017. begin
  1018. again:=false;
  1019. end
  1020. else
  1021. begin
  1022. if token=_LKLAMMER then
  1023. begin
  1024. consume(_LKLAMMER);
  1025. p1:=comp_expr(true);
  1026. consume(_RKLAMMER);
  1027. p1:=ctypeconvnode.create_explicit(p1,htype);
  1028. end
  1029. else { not LKLAMMER }
  1030. if (token=_POINT) and
  1031. is_object(htype.def) then
  1032. begin
  1033. consume(_POINT);
  1034. if assigned(current_procdef) and
  1035. assigned(current_procdef._class) and
  1036. not(getaddr) then
  1037. begin
  1038. if current_procdef._class.is_related(tobjectdef(htype.def)) then
  1039. begin
  1040. p1:=ctypenode.create(htype);
  1041. { search also in inherited methods }
  1042. srsym:=searchsym_in_class(tobjectdef(htype.def),pattern);
  1043. check_hints(srsym);
  1044. consume(_ID);
  1045. do_member_read(false,srsym,p1,again);
  1046. end
  1047. else
  1048. begin
  1049. Message(parser_e_no_super_class);
  1050. again:=false;
  1051. end;
  1052. end
  1053. else
  1054. begin
  1055. { allows @TObject.Load }
  1056. { also allows static methods and variables }
  1057. p1:=ctypenode.create(htype);
  1058. { TP allows also @TMenu.Load if Load is only }
  1059. { defined in an anchestor class }
  1060. srsym:=search_class_member(tobjectdef(htype.def),pattern);
  1061. check_hints(srsym);
  1062. if not assigned(srsym) then
  1063. Message1(sym_e_id_no_member,pattern)
  1064. else if not(getaddr) and not(sp_static in srsym.symoptions) then
  1065. Message(sym_e_only_static_in_static)
  1066. else
  1067. begin
  1068. consume(_ID);
  1069. do_member_read(getaddr,srsym,p1,again);
  1070. end;
  1071. end;
  1072. end
  1073. else
  1074. begin
  1075. { class reference ? }
  1076. if is_class(htype.def) then
  1077. begin
  1078. if getaddr and (token=_POINT) then
  1079. begin
  1080. consume(_POINT);
  1081. { allows @Object.Method }
  1082. { also allows static methods and variables }
  1083. p1:=ctypenode.create(htype);
  1084. { TP allows also @TMenu.Load if Load is only }
  1085. { defined in an anchestor class }
  1086. srsym:=search_class_member(tobjectdef(htype.def),pattern);
  1087. check_hints(srsym);
  1088. if not assigned(srsym) then
  1089. Message1(sym_e_id_no_member,pattern)
  1090. else
  1091. begin
  1092. consume(_ID);
  1093. do_member_read(getaddr,srsym,p1,again);
  1094. end;
  1095. end
  1096. else
  1097. begin
  1098. p1:=ctypenode.create(htype);
  1099. { For a type block we simply return only
  1100. the type. For all other blocks we return
  1101. a loadvmt node }
  1102. if (block_type<>bt_type) then
  1103. p1:=cloadvmtnode.create(p1);
  1104. end;
  1105. end
  1106. else
  1107. p1:=ctypenode.create(htype);
  1108. end;
  1109. end;
  1110. end;
  1111. enumsym :
  1112. begin
  1113. p1:=genenumnode(tenumsym(srsym));
  1114. end;
  1115. constsym :
  1116. begin
  1117. case tconstsym(srsym).consttyp of
  1118. constint :
  1119. begin
  1120. { do a very dirty trick to bootstrap this code }
  1121. if (tconstsym(srsym).value.valueord>=-(int64(2147483647)+int64(1))) and
  1122. (tconstsym(srsym).value.valueord<=2147483647) then
  1123. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,s32bittype,true)
  1124. else if (tconstsym(srsym).value.valueord > maxlongint) and
  1125. (tconstsym(srsym).value.valueord <= int64(maxlongint)+int64(maxlongint)+1) then
  1126. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,u32bittype,true)
  1127. else
  1128. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,cs64bittype,true);
  1129. end;
  1130. conststring :
  1131. begin
  1132. len:=tconstsym(srsym).value.len;
  1133. if not(cs_ansistrings in aktlocalswitches) and (len>255) then
  1134. len:=255;
  1135. getmem(pc,len+1);
  1136. move(pchar(tconstsym(srsym).value.valueptr)^,pc^,len);
  1137. pc[len]:=#0;
  1138. p1:=cstringconstnode.createpchar(pc,len);
  1139. end;
  1140. constchar :
  1141. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,cchartype,true);
  1142. constreal :
  1143. p1:=crealconstnode.create(pbestreal(tconstsym(srsym).value.valueptr)^,pbestrealtype^);
  1144. constbool :
  1145. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,booltype,true);
  1146. constset :
  1147. p1:=csetconstnode.create(pconstset(tconstsym(srsym).value.valueptr),tconstsym(srsym).consttype);
  1148. constord :
  1149. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,tconstsym(srsym).consttype,true);
  1150. constpointer :
  1151. p1:=cpointerconstnode.create(tconstsym(srsym).value.valueordptr,tconstsym(srsym).consttype);
  1152. constnil :
  1153. p1:=cnilnode.create;
  1154. constresourcestring:
  1155. begin
  1156. p1:=cloadnode.create(srsym,srsymtable);
  1157. do_resulttypepass(p1);
  1158. p1.resulttype:=cansistringtype;
  1159. end;
  1160. constguid :
  1161. p1:=cguidconstnode.create(pguid(tconstsym(srsym).value.valueptr)^);
  1162. end;
  1163. end;
  1164. procsym :
  1165. begin
  1166. { are we in a class method ? }
  1167. possible_error:=(srsym.owner.symtabletype=objectsymtable) and
  1168. not(is_interface(tdef(srsym.owner.defowner))) and
  1169. assigned(current_procdef) and
  1170. (po_classmethod in current_procdef.procoptions);
  1171. do_proc_call(srsym,srsymtable,
  1172. (getaddr and not(token in [_CARET,_POINT])),
  1173. again,p1);
  1174. { we need to know which procedure is called }
  1175. if possible_error then
  1176. begin
  1177. do_resulttypepass(p1);
  1178. if not(po_classmethod in tcallnode(p1).procdefinition.procoptions) then
  1179. Message(parser_e_only_class_methods);
  1180. end;
  1181. end;
  1182. propertysym :
  1183. begin
  1184. { access to property in a method }
  1185. { are we in a class method ? }
  1186. if (srsym.owner.symtabletype=objectsymtable) and
  1187. assigned(current_procdef) and
  1188. (po_classmethod in current_procdef.procoptions) then
  1189. Message(parser_e_only_class_methods);
  1190. { no method pointer }
  1191. p1:=nil;
  1192. handle_propertysym(srsym,srsymtable,p1,getaddr);
  1193. end;
  1194. labelsym :
  1195. begin
  1196. consume(_COLON);
  1197. if tlabelsym(srsym).defined then
  1198. Message(sym_e_label_already_defined);
  1199. tlabelsym(srsym).defined:=true;
  1200. p1:=clabelnode.create(tlabelsym(srsym),nil);
  1201. end;
  1202. errorsym :
  1203. begin
  1204. p1:=cerrornode.create;
  1205. if token=_LKLAMMER then
  1206. begin
  1207. consume(_LKLAMMER);
  1208. parse_paras(false,false);
  1209. consume(_RKLAMMER);
  1210. end;
  1211. end;
  1212. else
  1213. begin
  1214. p1:=cerrornode.create;
  1215. Message(cg_e_illegal_expression);
  1216. end;
  1217. end; { end case }
  1218. end;
  1219. end;
  1220. {---------------------------------------------
  1221. Factor_Read_Set
  1222. ---------------------------------------------}
  1223. { Read a set between [] }
  1224. function factor_read_set:tnode;
  1225. var
  1226. p1,p2 : tnode;
  1227. lastp,
  1228. buildp : tarrayconstructornode;
  1229. begin
  1230. buildp:=nil;
  1231. { be sure that a least one arrayconstructn is used, also for an
  1232. empty [] }
  1233. if token=_RECKKLAMMER then
  1234. buildp:=carrayconstructornode.create(nil,buildp)
  1235. else
  1236. begin
  1237. while true do
  1238. begin
  1239. p1:=comp_expr(true);
  1240. if token=_POINTPOINT then
  1241. begin
  1242. consume(_POINTPOINT);
  1243. p2:=comp_expr(true);
  1244. p1:=carrayconstructorrangenode.create(p1,p2);
  1245. end;
  1246. { insert at the end of the tree, to get the correct order }
  1247. if not assigned(buildp) then
  1248. begin
  1249. buildp:=carrayconstructornode.create(p1,nil);
  1250. lastp:=buildp;
  1251. end
  1252. else
  1253. begin
  1254. lastp.right:=carrayconstructornode.create(p1,nil);
  1255. lastp:=tarrayconstructornode(lastp.right);
  1256. end;
  1257. { there could be more elements }
  1258. if token=_COMMA then
  1259. consume(_COMMA)
  1260. else
  1261. break;
  1262. end;
  1263. end;
  1264. factor_read_set:=buildp;
  1265. end;
  1266. {---------------------------------------------
  1267. PostFixOperators
  1268. ---------------------------------------------}
  1269. procedure postfixoperators(var p1:tnode;var again:boolean);
  1270. { tries to avoid syntax errors after invalid qualifiers }
  1271. procedure recoverconsume_postfixops;
  1272. begin
  1273. while true do
  1274. begin
  1275. case token of
  1276. _CARET:
  1277. consume(_CARET);
  1278. _POINT:
  1279. begin
  1280. consume(_POINT);
  1281. if token=_ID then
  1282. consume(_ID);
  1283. end;
  1284. _LECKKLAMMER:
  1285. begin
  1286. consume(_LECKKLAMMER);
  1287. repeat
  1288. comp_expr(true);
  1289. if token=_COMMA then
  1290. consume(_COMMA)
  1291. else
  1292. break;
  1293. until false;
  1294. consume(_RECKKLAMMER);
  1295. end
  1296. else
  1297. break;
  1298. end;
  1299. end;
  1300. end;
  1301. var
  1302. store_static : boolean;
  1303. protsym : tpropertysym;
  1304. p2,p3 : tnode;
  1305. hsym : tsym;
  1306. classh : tobjectdef;
  1307. begin
  1308. again:=true;
  1309. while again do
  1310. begin
  1311. { we need the resulttype }
  1312. do_resulttypepass(p1);
  1313. if codegenerror then
  1314. begin
  1315. recoverconsume_postfixops;
  1316. exit;
  1317. end;
  1318. { handle token }
  1319. case token of
  1320. _CARET:
  1321. begin
  1322. consume(_CARET);
  1323. if (p1.resulttype.def.deftype<>pointerdef) then
  1324. begin
  1325. { ^ as binary operator is a problem!!!! (FK) }
  1326. again:=false;
  1327. Message(cg_e_invalid_qualifier);
  1328. recoverconsume_postfixops;
  1329. p1.destroy;
  1330. p1:=cerrornode.create;
  1331. end
  1332. else
  1333. begin
  1334. p1:=cderefnode.create(p1);
  1335. end;
  1336. end;
  1337. _LECKKLAMMER:
  1338. begin
  1339. if is_class_or_interface(p1.resulttype.def) then
  1340. begin
  1341. { default property }
  1342. protsym:=search_default_property(tobjectdef(p1.resulttype.def));
  1343. if not(assigned(protsym)) then
  1344. begin
  1345. p1.destroy;
  1346. p1:=cerrornode.create;
  1347. again:=false;
  1348. message(parser_e_no_default_property_available);
  1349. end
  1350. else
  1351. begin
  1352. { The property symbol is referenced indirect }
  1353. inc(protsym.refs);
  1354. handle_propertysym(protsym,protsym.owner,p1,getaddr);
  1355. end;
  1356. end
  1357. else
  1358. begin
  1359. consume(_LECKKLAMMER);
  1360. repeat
  1361. case p1.resulttype.def.deftype of
  1362. pointerdef:
  1363. begin
  1364. { support delphi autoderef }
  1365. if (tpointerdef(p1.resulttype.def).pointertype.def.deftype=arraydef) and
  1366. (m_autoderef in aktmodeswitches) then
  1367. begin
  1368. p1:=cderefnode.create(p1);
  1369. end;
  1370. p2:=comp_expr(true);
  1371. p1:=cvecnode.create(p1,p2);
  1372. end;
  1373. stringdef :
  1374. begin
  1375. p2:=comp_expr(true);
  1376. p1:=cvecnode.create(p1,p2);
  1377. end;
  1378. arraydef :
  1379. begin
  1380. p2:=comp_expr(true);
  1381. { support SEG:OFS for go32v2 Mem[] }
  1382. if (target_info.system=system_i386_go32v2) and
  1383. (p1.nodetype=loadn) and
  1384. assigned(tloadnode(p1).symtableentry) and
  1385. assigned(tloadnode(p1).symtableentry.owner.name) and
  1386. (tloadnode(p1).symtableentry.owner.name^='SYSTEM') and
  1387. ((tloadnode(p1).symtableentry.name='MEM') or
  1388. (tloadnode(p1).symtableentry.name='MEMW') or
  1389. (tloadnode(p1).symtableentry.name='MEML')) then
  1390. begin
  1391. if (token=_COLON) then
  1392. begin
  1393. consume(_COLON);
  1394. p3:=caddnode.create(muln,cordconstnode.create($10,s32bittype,false),p2);
  1395. p2:=comp_expr(true);
  1396. p2:=caddnode.create(addn,p2,p3);
  1397. p1:=cvecnode.create(p1,p2);
  1398. include(tvecnode(p1).flags,nf_memseg);
  1399. include(tvecnode(p1).flags,nf_memindex);
  1400. end
  1401. else
  1402. begin
  1403. p1:=cvecnode.create(p1,p2);
  1404. include(tvecnode(p1).flags,nf_memindex);
  1405. end;
  1406. end
  1407. else
  1408. p1:=cvecnode.create(p1,p2);
  1409. end;
  1410. else
  1411. begin
  1412. Message(cg_e_invalid_qualifier);
  1413. p1.destroy;
  1414. p1:=cerrornode.create;
  1415. comp_expr(true);
  1416. again:=false;
  1417. end;
  1418. end;
  1419. do_resulttypepass(p1);
  1420. if token=_COMMA then
  1421. consume(_COMMA)
  1422. else
  1423. break;
  1424. until false;
  1425. consume(_RECKKLAMMER);
  1426. end;
  1427. end;
  1428. _POINT :
  1429. begin
  1430. consume(_POINT);
  1431. if (p1.resulttype.def.deftype=pointerdef) and
  1432. (m_autoderef in aktmodeswitches) then
  1433. begin
  1434. p1:=cderefnode.create(p1);
  1435. do_resulttypepass(p1);
  1436. end;
  1437. case p1.resulttype.def.deftype of
  1438. recorddef:
  1439. begin
  1440. hsym:=tsym(trecorddef(p1.resulttype.def).symtable.search(pattern));
  1441. check_hints(hsym);
  1442. if assigned(hsym) and
  1443. (hsym.typ=varsym) then
  1444. p1:=csubscriptnode.create(hsym,p1)
  1445. else
  1446. begin
  1447. Message1(sym_e_illegal_field,pattern);
  1448. p1.destroy;
  1449. p1:=cerrornode.create;
  1450. end;
  1451. consume(_ID);
  1452. end;
  1453. variantdef:
  1454. begin
  1455. end;
  1456. classrefdef:
  1457. begin
  1458. classh:=tobjectdef(tclassrefdef(p1.resulttype.def).pointertype.def);
  1459. hsym:=searchsym_in_class(classh,pattern);
  1460. check_hints(hsym);
  1461. if hsym=nil then
  1462. begin
  1463. Message1(sym_e_id_no_member,pattern);
  1464. p1.destroy;
  1465. p1:=cerrornode.create;
  1466. { try to clean up }
  1467. consume(_ID);
  1468. end
  1469. else
  1470. begin
  1471. consume(_ID);
  1472. do_member_read(getaddr,hsym,p1,again);
  1473. end;
  1474. end;
  1475. objectdef:
  1476. begin
  1477. store_static:=allow_only_static;
  1478. allow_only_static:=false;
  1479. classh:=tobjectdef(p1.resulttype.def);
  1480. hsym:=searchsym_in_class(classh,pattern);
  1481. check_hints(hsym);
  1482. allow_only_static:=store_static;
  1483. if hsym=nil then
  1484. begin
  1485. Message1(sym_e_id_no_member,pattern);
  1486. p1.destroy;
  1487. p1:=cerrornode.create;
  1488. { try to clean up }
  1489. consume(_ID);
  1490. end
  1491. else
  1492. begin
  1493. consume(_ID);
  1494. do_member_read(getaddr,hsym,p1,again);
  1495. end;
  1496. end;
  1497. pointerdef:
  1498. begin
  1499. Message(cg_e_invalid_qualifier);
  1500. if tpointerdef(p1.resulttype.def).pointertype.def.deftype in [recorddef,objectdef,classrefdef] then
  1501. Message(parser_h_maybe_deref_caret_missing);
  1502. end;
  1503. else
  1504. begin
  1505. Message(cg_e_invalid_qualifier);
  1506. p1.destroy;
  1507. p1:=cerrornode.create;
  1508. consume(_ID);
  1509. end;
  1510. end;
  1511. end;
  1512. else
  1513. begin
  1514. { is this a procedure variable ? }
  1515. if assigned(p1.resulttype.def) then
  1516. begin
  1517. if (p1.resulttype.def.deftype=procvardef) then
  1518. begin
  1519. if assigned(getprocvardef) and
  1520. equal_defs(p1.resulttype.def,getprocvardef) then
  1521. again:=false
  1522. else
  1523. if (token=_LKLAMMER) or
  1524. ((tprocvardef(p1.resulttype.def).para.empty) and
  1525. (not((token in [_ASSIGNMENT,_UNEQUAL,_EQUAL]))) and
  1526. (not afterassignment) and
  1527. (not in_args)) then
  1528. begin
  1529. { do this in a strange way }
  1530. { it's not a clean solution }
  1531. p2:=p1;
  1532. p1:=ccallnode.create(nil,nil,nil,nil);
  1533. tcallnode(p1).set_procvar(p2);
  1534. if try_to_consume(_LKLAMMER) then
  1535. begin
  1536. tcallnode(p1).left:=parse_paras(false,false);
  1537. consume(_RKLAMMER);
  1538. end;
  1539. { proc():= is never possible }
  1540. if token=_ASSIGNMENT then
  1541. begin
  1542. Message(cg_e_illegal_expression);
  1543. p1:=cerrornode.create;
  1544. again:=false;
  1545. end;
  1546. end
  1547. else
  1548. again:=false;
  1549. end
  1550. else
  1551. again:=false;
  1552. end
  1553. else
  1554. again:=false;
  1555. end;
  1556. end;
  1557. end; { while again }
  1558. end;
  1559. {---------------------------------------------
  1560. Factor (Main)
  1561. ---------------------------------------------}
  1562. var
  1563. l : longint;
  1564. card : cardinal;
  1565. ic : TConstExprInt;
  1566. oldp1,
  1567. p1 : tnode;
  1568. code : integer;
  1569. again : boolean;
  1570. sym : tsym;
  1571. pd : tprocdef;
  1572. classh : tobjectdef;
  1573. d : bestreal;
  1574. hs : string;
  1575. htype : ttype;
  1576. filepos : tfileposinfo;
  1577. {---------------------------------------------
  1578. Helpers
  1579. ---------------------------------------------}
  1580. procedure check_tokenpos;
  1581. begin
  1582. if (p1<>oldp1) then
  1583. begin
  1584. if assigned(p1) then
  1585. p1.set_tree_filepos(filepos);
  1586. oldp1:=p1;
  1587. filepos:=akttokenpos;
  1588. end;
  1589. end;
  1590. begin
  1591. oldp1:=nil;
  1592. p1:=nil;
  1593. filepos:=akttokenpos;
  1594. again:=false;
  1595. if token=_ID then
  1596. begin
  1597. factor_read_id(p1,again);
  1598. if again then
  1599. begin
  1600. check_tokenpos;
  1601. { handle post fix operators }
  1602. postfixoperators(p1,again);
  1603. end;
  1604. end
  1605. else
  1606. case token of
  1607. _SELF :
  1608. begin
  1609. again:=true;
  1610. consume(_SELF);
  1611. if not(assigned(current_procdef) and
  1612. assigned(current_procdef._class)) then
  1613. begin
  1614. p1:=cerrornode.create;
  1615. again:=false;
  1616. Message(parser_e_self_not_in_method);
  1617. end
  1618. else
  1619. begin
  1620. if (po_classmethod in current_procdef.procoptions) then
  1621. begin
  1622. { self in class methods is a class reference type }
  1623. htype.setdef(current_procdef._class);
  1624. p1:=cselfnode.create(tclassrefdef.create(htype));
  1625. end
  1626. else
  1627. p1:=cselfnode.create(current_procdef._class);
  1628. postfixoperators(p1,again);
  1629. end;
  1630. end;
  1631. _INHERITED :
  1632. begin
  1633. again:=true;
  1634. consume(_INHERITED);
  1635. if assigned(current_procdef._class) then
  1636. begin
  1637. classh:=current_procdef._class.childof;
  1638. { if inherited; only then we need the method with
  1639. the same name }
  1640. if token in endtokens then
  1641. begin
  1642. hs:=current_procdef.procsym.name;
  1643. anon_inherited:=true;
  1644. { For message methods we need to search using the message
  1645. number or string }
  1646. pd:=tprocsym(current_procdef.procsym).first_procdef;
  1647. if (po_msgint in pd.procoptions) then
  1648. sym:=searchsym_in_class_by_msgint(classh,pd.messageinf.i)
  1649. else
  1650. if (po_msgstr in pd.procoptions) then
  1651. sym:=searchsym_in_class_by_msgstr(classh,pd.messageinf.str)
  1652. else
  1653. sym:=searchsym_in_class(classh,hs);
  1654. end
  1655. else
  1656. begin
  1657. hs:=pattern;
  1658. consume(_ID);
  1659. anon_inherited:=false;
  1660. sym:=searchsym_in_class(classh,hs);
  1661. end;
  1662. if assigned(sym) then
  1663. begin
  1664. check_hints(sym);
  1665. if sym.typ=procsym then
  1666. begin
  1667. htype.setdef(classh);
  1668. p1:=ctypenode.create(htype);
  1669. end;
  1670. do_member_read(false,sym,p1,again);
  1671. { Add flag to indicate that inherited is used }
  1672. if p1.nodetype=calln then
  1673. include(p1.flags,nf_anon_inherited);
  1674. end
  1675. else
  1676. begin
  1677. if anon_inherited then
  1678. begin
  1679. { we didn't find a member in the parents call the
  1680. DefaultHandler }
  1681. sym:=searchsym_in_class(classh,'DEFAULTHANDLER');
  1682. if not assigned(sym) or
  1683. (sym.typ<>procsym) then
  1684. internalerror(200303171);
  1685. p1:=nil;
  1686. do_proc_call(sym,sym.owner,false,again,p1);
  1687. end
  1688. else
  1689. begin
  1690. Message1(sym_e_id_no_member,hs);
  1691. p1:=cerrornode.create;
  1692. end;
  1693. again:=false;
  1694. end;
  1695. { turn auto inheriting off }
  1696. anon_inherited:=false;
  1697. end
  1698. else
  1699. begin
  1700. Message(parser_e_generic_methods_only_in_methods);
  1701. again:=false;
  1702. p1:=cerrornode.create;
  1703. end;
  1704. postfixoperators(p1,again);
  1705. end;
  1706. _INTCONST :
  1707. begin
  1708. { try cardinal first }
  1709. val(pattern,card,code);
  1710. if code<>0 then
  1711. begin
  1712. { then longint }
  1713. valint(pattern,l,code);
  1714. if code <> 0 then
  1715. begin
  1716. { then int64 }
  1717. val(pattern,ic,code);
  1718. if code<>0 then
  1719. begin
  1720. {finally float }
  1721. val(pattern,d,code);
  1722. if code<>0 then
  1723. begin
  1724. Message(cg_e_invalid_integer);
  1725. consume(_INTCONST);
  1726. l:=1;
  1727. p1:=cordconstnode.create(l,s32bittype,true);
  1728. end
  1729. else
  1730. begin
  1731. consume(_INTCONST);
  1732. p1:=crealconstnode.create(d,pbestrealtype^);
  1733. end;
  1734. end
  1735. else
  1736. begin
  1737. consume(_INTCONST);
  1738. p1:=cordconstnode.create(ic,cs64bittype,true);
  1739. end
  1740. end
  1741. else
  1742. begin
  1743. consume(_INTCONST);
  1744. p1:=cordconstnode.create(l,s32bittype,true)
  1745. end
  1746. end
  1747. else
  1748. begin
  1749. consume(_INTCONST);
  1750. { check whether the value isn't in the longint range as well }
  1751. { (longint is easier to perform calculations with) (JM) }
  1752. if card <= $7fffffff then
  1753. { no sign extension necessary, so not longint typecast (JM) }
  1754. p1:=cordconstnode.create(card,s32bittype,true)
  1755. else
  1756. p1:=cordconstnode.create(card,u32bittype,true)
  1757. end;
  1758. end;
  1759. _REALNUMBER :
  1760. begin
  1761. val(pattern,d,code);
  1762. if code<>0 then
  1763. begin
  1764. Message(parser_e_error_in_real);
  1765. d:=1.0;
  1766. end;
  1767. consume(_REALNUMBER);
  1768. p1:=crealconstnode.create(d,pbestrealtype^);
  1769. end;
  1770. _STRING :
  1771. begin
  1772. string_dec(htype);
  1773. { STRING can be also a type cast }
  1774. if token=_LKLAMMER then
  1775. begin
  1776. consume(_LKLAMMER);
  1777. p1:=comp_expr(true);
  1778. consume(_RKLAMMER);
  1779. p1:=ctypeconvnode.create_explicit(p1,htype);
  1780. { handle postfix operators here e.g. string(a)[10] }
  1781. again:=true;
  1782. postfixoperators(p1,again);
  1783. end
  1784. else
  1785. p1:=ctypenode.create(htype);
  1786. end;
  1787. _FILE :
  1788. begin
  1789. htype:=cfiletype;
  1790. consume(_FILE);
  1791. { FILE can be also a type cast }
  1792. if token=_LKLAMMER then
  1793. begin
  1794. consume(_LKLAMMER);
  1795. p1:=comp_expr(true);
  1796. consume(_RKLAMMER);
  1797. p1:=ctypeconvnode.create_explicit(p1,htype);
  1798. { handle postfix operators here e.g. string(a)[10] }
  1799. again:=true;
  1800. postfixoperators(p1,again);
  1801. end
  1802. else
  1803. begin
  1804. p1:=ctypenode.create(htype);
  1805. end;
  1806. end;
  1807. _CSTRING :
  1808. begin
  1809. p1:=cstringconstnode.createstr(pattern,st_default);
  1810. consume(_CSTRING);
  1811. end;
  1812. _CCHAR :
  1813. begin
  1814. p1:=cordconstnode.create(ord(pattern[1]),cchartype,true);
  1815. consume(_CCHAR);
  1816. end;
  1817. _CWSTRING:
  1818. begin
  1819. p1:=cstringconstnode.createwstr(patternw);
  1820. consume(_CWSTRING);
  1821. end;
  1822. _CWCHAR:
  1823. begin
  1824. p1:=cordconstnode.create(ord(getcharwidestring(patternw,0)),cwidechartype,true);
  1825. consume(_CWCHAR);
  1826. end;
  1827. _KLAMMERAFFE :
  1828. begin
  1829. consume(_KLAMMERAFFE);
  1830. got_addrn:=true;
  1831. { support both @<x> and @(<x>) }
  1832. if try_to_consume(_LKLAMMER) then
  1833. begin
  1834. p1:=factor(true);
  1835. if token in [_CARET,_POINT,_LECKKLAMMER] then
  1836. begin
  1837. again:=true;
  1838. postfixoperators(p1,again);
  1839. end;
  1840. consume(_RKLAMMER);
  1841. end
  1842. else
  1843. p1:=factor(true);
  1844. if token in [_CARET,_POINT,_LECKKLAMMER] then
  1845. begin
  1846. again:=true;
  1847. postfixoperators(p1,again);
  1848. end;
  1849. got_addrn:=false;
  1850. p1:=caddrnode.create(p1);
  1851. { Store the procvar that we are expecting, the
  1852. addrn will use the information to find the correct
  1853. procdef or it will return an error }
  1854. if assigned(getprocvardef) and
  1855. (taddrnode(p1).left.nodetype = loadn) then
  1856. taddrnode(p1).getprocvardef:=getprocvardef;
  1857. end;
  1858. _LKLAMMER :
  1859. begin
  1860. consume(_LKLAMMER);
  1861. p1:=comp_expr(true);
  1862. consume(_RKLAMMER);
  1863. { it's not a good solution }
  1864. { but (a+b)^ makes some problems }
  1865. if token in [_CARET,_POINT,_LECKKLAMMER] then
  1866. begin
  1867. again:=true;
  1868. postfixoperators(p1,again);
  1869. end;
  1870. end;
  1871. _LECKKLAMMER :
  1872. begin
  1873. consume(_LECKKLAMMER);
  1874. p1:=factor_read_set;
  1875. consume(_RECKKLAMMER);
  1876. end;
  1877. _PLUS :
  1878. begin
  1879. consume(_PLUS);
  1880. p1:=factor(false);
  1881. end;
  1882. _MINUS :
  1883. begin
  1884. consume(_MINUS);
  1885. p1:=sub_expr(oppower,false);
  1886. p1:=cunaryminusnode.create(p1);
  1887. end;
  1888. _OP_NOT :
  1889. begin
  1890. consume(_OP_NOT);
  1891. p1:=factor(false);
  1892. p1:=cnotnode.create(p1);
  1893. end;
  1894. _TRUE :
  1895. begin
  1896. consume(_TRUE);
  1897. p1:=cordconstnode.create(1,booltype,false);
  1898. end;
  1899. _FALSE :
  1900. begin
  1901. consume(_FALSE);
  1902. p1:=cordconstnode.create(0,booltype,false);
  1903. end;
  1904. _NIL :
  1905. begin
  1906. consume(_NIL);
  1907. p1:=cnilnode.create;
  1908. { It's really ugly code nil^, but delphi allows it }
  1909. if token in [_CARET] then
  1910. begin
  1911. again:=true;
  1912. postfixoperators(p1,again);
  1913. end;
  1914. end;
  1915. else
  1916. begin
  1917. p1:=cerrornode.create;
  1918. consume(token);
  1919. Message(cg_e_illegal_expression);
  1920. end;
  1921. end;
  1922. { generate error node if no node is created }
  1923. if not assigned(p1) then
  1924. begin
  1925. {$ifdef EXTDEBUG}
  1926. Comment(V_Warning,'factor: p1=nil');
  1927. {$endif}
  1928. p1:=cerrornode.create;
  1929. end;
  1930. { get the resulttype for the node }
  1931. if (not assigned(p1.resulttype.def)) then
  1932. do_resulttypepass(p1);
  1933. { tp7 procvar handling, but not if the next token
  1934. will be a := }
  1935. 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. check_tp_procvar(p1);
  2064. if token in [_ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  2065. afterassignment:=true;
  2066. oldp1:=p1;
  2067. case token of
  2068. _POINTPOINT :
  2069. begin
  2070. consume(_POINTPOINT);
  2071. p2:=sub_expr(opcompare,true);
  2072. p1:=crangenode.create(p1,p2);
  2073. end;
  2074. _ASSIGNMENT :
  2075. begin
  2076. consume(_ASSIGNMENT);
  2077. if (p1.resulttype.def.deftype=procvardef) then
  2078. getprocvardef:=tprocvardef(p1.resulttype.def);
  2079. p2:=sub_expr(opcompare,true);
  2080. if assigned(getprocvardef) then
  2081. handle_procvar(getprocvardef,p2);
  2082. getprocvardef:=nil;
  2083. p1:=cassignmentnode.create(p1,p2);
  2084. end;
  2085. _PLUSASN :
  2086. begin
  2087. consume(_PLUSASN);
  2088. p2:=sub_expr(opcompare,true);
  2089. p1:=cassignmentnode.create(p1,caddnode.create(addn,p1.getcopy,p2));
  2090. end;
  2091. _MINUSASN :
  2092. begin
  2093. consume(_MINUSASN);
  2094. p2:=sub_expr(opcompare,true);
  2095. p1:=cassignmentnode.create(p1,caddnode.create(subn,p1.getcopy,p2));
  2096. end;
  2097. _STARASN :
  2098. begin
  2099. consume(_STARASN );
  2100. p2:=sub_expr(opcompare,true);
  2101. p1:=cassignmentnode.create(p1,caddnode.create(muln,p1.getcopy,p2));
  2102. end;
  2103. _SLASHASN :
  2104. begin
  2105. consume(_SLASHASN );
  2106. p2:=sub_expr(opcompare,true);
  2107. p1:=cassignmentnode.create(p1,caddnode.create(slashn,p1.getcopy,p2));
  2108. end;
  2109. end;
  2110. { get the resulttype for this expression }
  2111. if not assigned(p1.resulttype.def) then
  2112. do_resulttypepass(p1);
  2113. afterassignment:=oldafterassignment;
  2114. if p1<>oldp1 then
  2115. p1.set_tree_filepos(filepos);
  2116. expr:=p1;
  2117. end;
  2118. {$ifdef int64funcresok}
  2119. function get_intconst:TConstExprInt;
  2120. {$else int64funcresok}
  2121. function get_intconst:longint;
  2122. {$endif int64funcresok}
  2123. {Reads an expression, tries to evalute it and check if it is an integer
  2124. constant. Then the constant is returned.}
  2125. var
  2126. p:tnode;
  2127. begin
  2128. p:=comp_expr(true);
  2129. if not codegenerror then
  2130. begin
  2131. if (p.nodetype<>ordconstn) or
  2132. not(is_integer(p.resulttype.def)) then
  2133. Message(cg_e_illegal_expression)
  2134. else
  2135. get_intconst:=tordconstnode(p).value;
  2136. end;
  2137. p.free;
  2138. end;
  2139. function get_stringconst:string;
  2140. {Reads an expression, tries to evaluate it and checks if it is a string
  2141. constant. Then the constant is returned.}
  2142. var
  2143. p:tnode;
  2144. begin
  2145. get_stringconst:='';
  2146. p:=comp_expr(true);
  2147. if p.nodetype<>stringconstn then
  2148. begin
  2149. if (p.nodetype=ordconstn) and is_char(p.resulttype.def) then
  2150. get_stringconst:=char(tordconstnode(p).value)
  2151. else
  2152. Message(cg_e_illegal_expression);
  2153. end
  2154. else
  2155. get_stringconst:=strpas(tstringconstnode(p).value_str);
  2156. p.free;
  2157. end;
  2158. end.
  2159. {
  2160. $Log$
  2161. Revision 1.113 2003-04-27 11:21:33 peter
  2162. * aktprocdef renamed to current_procdef
  2163. * procinfo renamed to current_procinfo
  2164. * procinfo will now be stored in current_module so it can be
  2165. cleaned up properly
  2166. * gen_main_procsym changed to create_main_proc and release_main_proc
  2167. to also generate a tprocinfo structure
  2168. * fixed unit implicit initfinal
  2169. Revision 1.112 2003/04/27 07:29:50 peter
  2170. * current_procdef cleanup, current_procdef is now always nil when parsing
  2171. a new procdef declaration
  2172. * aktprocsym removed
  2173. * lexlevel removed, use symtable.symtablelevel instead
  2174. * implicit init/final code uses the normal genentry/genexit
  2175. * funcret state checking updated for new funcret handling
  2176. Revision 1.111 2003/04/26 00:33:07 peter
  2177. * vo_is_result flag added for the special RESULT symbol
  2178. Revision 1.110 2003/04/25 20:59:33 peter
  2179. * removed funcretn,funcretsym, function result is now in varsym
  2180. and aliases for result and function name are added using absolutesym
  2181. * vs_hidden parameter for funcret passed in parameter
  2182. * vs_hidden fixes
  2183. * writenode changed to printnode and released from extdebug
  2184. * -vp option added to generate a tree.log with the nodetree
  2185. * nicer printnode for statements, callnode
  2186. Revision 1.109 2003/04/23 10:13:55 peter
  2187. * firstaddr will check procvardef
  2188. Revision 1.108 2003/04/22 23:50:23 peter
  2189. * firstpass uses expectloc
  2190. * checks if there are differences between the expectloc and
  2191. location.loc from secondpass in EXTDEBUG
  2192. Revision 1.107 2003/04/11 15:49:01 peter
  2193. * default property also increased the reference count for the
  2194. property symbol
  2195. Revision 1.106 2003/04/11 14:50:08 peter
  2196. * fix tw2454
  2197. Revision 1.105 2003/03/27 17:44:13 peter
  2198. * fixed small mem leaks
  2199. Revision 1.104 2003/03/17 18:55:30 peter
  2200. * allow more tokens instead of only semicolon after inherited
  2201. Revision 1.103 2003/03/17 16:54:41 peter
  2202. * support DefaultHandler and anonymous inheritance fixed
  2203. for message methods
  2204. Revision 1.102 2003/01/30 21:46:57 peter
  2205. * self fixes for static methods (merged)
  2206. Revision 1.101 2003/01/16 22:12:22 peter
  2207. * Find the correct procvar to load when using @ in fpc mode
  2208. Revision 1.100 2003/01/15 01:44:32 peter
  2209. * merged methodpointer fixes from 1.0.x
  2210. Revision 1.98 2003/01/12 17:51:42 peter
  2211. * tp procvar handling fix for tb0448
  2212. Revision 1.97 2003/01/05 22:44:14 peter
  2213. * remove a lot of code to support typen in loadn-procsym
  2214. Revision 1.96 2002/12/11 22:40:36 peter
  2215. * assigned(procvar) fix for delphi mode, fixes tb0430
  2216. Revision 1.95 2002/11/30 11:12:48 carl
  2217. + checking for symbols used with hint directives is done mostly in pexpr
  2218. only now
  2219. Revision 1.94 2002/11/27 15:33:47 peter
  2220. * the never ending story of tp procvar hacks
  2221. Revision 1.93 2002/11/26 22:58:24 peter
  2222. * fix for tw2178. When a ^ or . follows a procsym then the procsym
  2223. needs to be called
  2224. Revision 1.92 2002/11/25 17:43:22 peter
  2225. * splitted defbase in defutil,symutil,defcmp
  2226. * merged isconvertable and is_equal into compare_defs(_ext)
  2227. * made operator search faster by walking the list only once
  2228. Revision 1.91 2002/11/22 22:48:10 carl
  2229. * memory optimization with tconstsym (1.5%)
  2230. Revision 1.90 2002/11/20 22:49:55 pierre
  2231. * commented check code tht was invalid in 1.1
  2232. Revision 1.89 2002/11/18 18:34:41 peter
  2233. * fix crash with EXTDEBUG code
  2234. Revision 1.88 2002/11/18 17:48:21 peter
  2235. * fix tw2209 (merged)
  2236. Revision 1.87 2002/11/18 17:31:58 peter
  2237. * pass proccalloption to ret_in_xxx and push_xxx functions
  2238. Revision 1.86 2002/10/05 00:48:57 peter
  2239. * support inherited; support for overload as it is handled by
  2240. delphi. This is only for delphi mode as it is working is
  2241. undocumented and hard to predict what is done
  2242. Revision 1.85 2002/10/04 21:13:59 peter
  2243. * ignore vecn,subscriptn when checking for a procvar loadn
  2244. Revision 1.84 2002/10/02 20:51:22 peter
  2245. * don't check interfaces for class methods
  2246. Revision 1.83 2002/10/02 18:20:52 peter
  2247. * Copy() is now internal syssym that calls compilerprocs
  2248. Revision 1.82 2002/09/30 07:00:48 florian
  2249. * fixes to common code to get the alpha compiler compiled applied
  2250. Revision 1.81 2002/09/16 19:06:14 peter
  2251. * allow ^ after nil
  2252. Revision 1.80 2002/09/07 15:25:07 peter
  2253. * old logs removed and tabs fixed
  2254. Revision 1.79 2002/09/07 12:16:03 carl
  2255. * second part bug report 1996 fix, testrange in cordconstnode
  2256. only called if option is set (also make parsing a tiny faster)
  2257. Revision 1.78 2002/09/03 16:26:27 daniel
  2258. * Make Tprocdef.defs protected
  2259. Revision 1.77 2002/08/18 20:06:24 peter
  2260. * inlining is now also allowed in interface
  2261. * renamed write/load to ppuwrite/ppuload
  2262. * tnode storing in ppu
  2263. * nld,ncon,nbas are already updated for storing in ppu
  2264. Revision 1.76 2002/08/17 09:23:39 florian
  2265. * first part of procinfo rewrite
  2266. Revision 1.75 2002/08/01 16:37:47 jonas
  2267. - removed some superfluous "in_paras := true" statements
  2268. Revision 1.74 2002/07/26 21:15:41 florian
  2269. * rewrote the system handling
  2270. Revision 1.73 2002/07/23 09:51:23 daniel
  2271. * Tried to make Tprocsym.defs protected. I didn't succeed but the cleanups
  2272. are worth comitting.
  2273. Revision 1.72 2002/07/20 11:57:55 florian
  2274. * types.pas renamed to defbase.pas because D6 contains a types
  2275. unit so this would conflicts if D6 programms are compiled
  2276. + Willamette/SSE2 instructions to assembler added
  2277. Revision 1.71 2002/07/16 15:34:20 florian
  2278. * exit is now a syssym instead of a keyword
  2279. Revision 1.70 2002/07/06 20:18:02 carl
  2280. * longstring declaration now gives parser error since its not supported!
  2281. Revision 1.69 2002/06/12 15:46:14 jonas
  2282. * fixed web bug 1995
  2283. Revision 1.68 2002/05/18 13:34:12 peter
  2284. * readded missing revisions
  2285. Revision 1.67 2002/05/16 19:46:43 carl
  2286. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  2287. + try to fix temp allocation (still in ifdef)
  2288. + generic constructor calls
  2289. + start of tassembler / tmodulebase class cleanup
  2290. Revision 1.65 2002/05/12 16:53:09 peter
  2291. * moved entry and exitcode to ncgutil and cgobj
  2292. * foreach gets extra argument for passing local data to the
  2293. iterator function
  2294. * -CR checks also class typecasts at runtime by changing them
  2295. into as
  2296. * fixed compiler to cycle with the -CR option
  2297. * fixed stabs with elf writer, finally the global variables can
  2298. be watched
  2299. * removed a lot of routines from cga unit and replaced them by
  2300. calls to cgobj
  2301. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  2302. u32bit then the other is typecasted also to u32bit without giving
  2303. a rangecheck warning/error.
  2304. * fixed pascal calling method with reversing also the high tree in
  2305. the parast, detected by tcalcst3 test
  2306. Revision 1.64 2002/04/23 19:16:34 peter
  2307. * add pinline unit that inserts compiler supported functions using
  2308. one or more statements
  2309. * moved finalize and setlength from ninl to pinline
  2310. Revision 1.63 2002/04/21 19:02:05 peter
  2311. * removed newn and disposen nodes, the code is now directly
  2312. inlined from pexpr
  2313. * -an option that will write the secondpass nodes to the .s file, this
  2314. requires EXTDEBUG define to actually write the info
  2315. * fixed various internal errors and crashes due recent code changes
  2316. Revision 1.62 2002/04/16 16:11:17 peter
  2317. * using inherited; without a parent having the same function
  2318. will do nothing like delphi
  2319. Revision 1.61 2002/04/07 13:31:36 carl
  2320. + change unit use
  2321. Revision 1.60 2002/04/01 20:57:13 jonas
  2322. * fixed web bug 1907
  2323. * fixed some other procvar related bugs (all related to accepting procvar
  2324. constructs with either too many or too little parameters)
  2325. (both merged, includes second typo fix of pexpr.pas)
  2326. Revision 1.59 2002/03/31 20:26:35 jonas
  2327. + a_loadfpu_* and a_loadmm_* methods in tcg
  2328. * register allocation is now handled by a class and is mostly processor
  2329. independent (+rgobj.pas and i386/rgcpu.pas)
  2330. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  2331. * some small improvements and fixes to the optimizer
  2332. * some register allocation fixes
  2333. * some fpuvaroffset fixes in the unary minus node
  2334. * push/popusedregisters is now called rg.save/restoreusedregisters and
  2335. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  2336. also better optimizable)
  2337. * fixed and optimized register saving/restoring for new/dispose nodes
  2338. * LOC_FPU locations now also require their "register" field to be set to
  2339. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  2340. - list field removed of the tnode class because it's not used currently
  2341. and can cause hard-to-find bugs
  2342. }