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(aktprocdef.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. Is_func_ret
  932. ---------------------------------------------}
  933. function is_func_ret(var p1:tnode;var sym : tsym;var srsymtable:tsymtable) : boolean;
  934. var
  935. p : tprocinfo;
  936. storesymtablestack : tsymtable;
  937. begin
  938. is_func_ret:=false;
  939. if not assigned(procinfo) or
  940. ((sym.typ<>funcretsym) and ((procinfo.flags and pi_operator)=0)) then
  941. exit;
  942. p:=procinfo;
  943. while assigned(p) do
  944. begin
  945. { is this an access to a function result? Accessing _RESULT is
  946. always allowed and funcretn is generated }
  947. if assigned(p.procdef.funcretsym) and
  948. ((sym=tsym(p.procdef.resultfuncretsym)) or
  949. ((sym=tsym(p.procdef.funcretsym)) or
  950. ((sym=tsym(otsym)) and ((p.flags and pi_operator)<>0))) and
  951. (not is_void(p.procdef.rettype.def)) and
  952. (token<>_LKLAMMER) and
  953. (not (not(m_fpc in aktmodeswitches) and (afterassignment or in_args)))
  954. ) then
  955. begin
  956. if ((sym=tsym(otsym)) and
  957. ((p.flags and pi_operator)<>0)) then
  958. inc(otsym.refs);
  959. p1:=cfuncretnode.create(p.procdef.funcretsym);
  960. is_func_ret:=true;
  961. if tfuncretsym(p.procdef.funcretsym).funcretstate=vs_declared then
  962. begin
  963. tfuncretsym(p.procdef.funcretsym).funcretstate:=vs_declared_and_first_found;
  964. include(p1.flags,nf_is_first_funcret);
  965. end;
  966. exit;
  967. end;
  968. p:=p.parent;
  969. end;
  970. { we must use the function call, update the
  971. sym to be the procsym }
  972. if (sym.typ=funcretsym) then
  973. begin
  974. storesymtablestack:=symtablestack;
  975. symtablestack:=sym.owner.next;
  976. searchsym(sym.name,sym,srsymtable);
  977. check_hints(sym);
  978. if not assigned(sym) then
  979. sym:=generrorsym;
  980. if (sym.typ<>procsym) then
  981. Message(cg_e_illegal_expression);
  982. symtablestack:=storesymtablestack;
  983. end;
  984. end;
  985. {---------------------------------------------
  986. Factor_read_id
  987. ---------------------------------------------}
  988. procedure factor_read_id(var p1:tnode;var again:boolean);
  989. var
  990. pc : pchar;
  991. len : longint;
  992. srsym : tsym;
  993. possible_error : boolean;
  994. srsymtable : tsymtable;
  995. htype : ttype;
  996. static_name : string;
  997. begin
  998. { allow post fix operators }
  999. again:=true;
  1000. consume_sym(srsym,srsymtable);
  1001. if not is_func_ret(p1,srsym,srsymtable) then
  1002. begin
  1003. { check semantics of private }
  1004. if (srsym.typ in [propertysym,procsym,varsym]) and
  1005. (srsym.owner.symtabletype=objectsymtable) then
  1006. begin
  1007. if (sp_private in srsym.symoptions) and
  1008. (tobjectdef(srsym.owner.defowner).owner.symtabletype=globalsymtable) and
  1009. (tobjectdef(srsym.owner.defowner).owner.unitid<>0) then
  1010. Message(parser_e_cant_access_private_member);
  1011. end;
  1012. case srsym.typ of
  1013. absolutesym :
  1014. begin
  1015. p1:=cloadnode.create(srsym,srsymtable);
  1016. end;
  1017. varsym :
  1018. begin
  1019. { are we in a class method, we check here the
  1020. srsymtable, because a field in another object
  1021. also has objectsymtable. And withsymtable is
  1022. not possible for self in class methods (PFV) }
  1023. if (srsymtable.symtabletype=objectsymtable) and
  1024. assigned(aktprocsym) and
  1025. (po_classmethod in aktprocdef.procoptions) then
  1026. Message(parser_e_only_class_methods);
  1027. if (sp_static in srsym.symoptions) then
  1028. begin
  1029. static_name:=lower(srsym.owner.name^)+'_'+srsym.name;
  1030. searchsym(static_name,srsym,srsymtable);
  1031. check_hints(srsym);
  1032. end;
  1033. p1:=cloadnode.create(srsym,srsymtable);
  1034. if tvarsym(srsym).varstate=vs_declared then
  1035. begin
  1036. include(p1.flags,nf_first);
  1037. { set special between first loaded until checked in resulttypepass }
  1038. tvarsym(srsym).varstate:=vs_declared_and_first_found;
  1039. end;
  1040. end;
  1041. typedconstsym :
  1042. begin
  1043. p1:=cloadnode.create(srsym,srsymtable);
  1044. end;
  1045. syssym :
  1046. begin
  1047. p1:=statement_syssym(tsyssym(srsym).number);
  1048. end;
  1049. typesym :
  1050. begin
  1051. htype.setsym(srsym);
  1052. if not assigned(htype.def) then
  1053. begin
  1054. again:=false;
  1055. end
  1056. else
  1057. begin
  1058. if token=_LKLAMMER then
  1059. begin
  1060. consume(_LKLAMMER);
  1061. p1:=comp_expr(true);
  1062. consume(_RKLAMMER);
  1063. p1:=ctypeconvnode.create(p1,htype);
  1064. include(p1.flags,nf_explizit);
  1065. end
  1066. else { not LKLAMMER }
  1067. if (token=_POINT) and
  1068. is_object(htype.def) then
  1069. begin
  1070. consume(_POINT);
  1071. if assigned(procinfo) and
  1072. assigned(procinfo._class) and
  1073. not(getaddr) then
  1074. begin
  1075. if procinfo._class.is_related(tobjectdef(htype.def)) then
  1076. begin
  1077. p1:=ctypenode.create(htype);
  1078. { search also in inherited methods }
  1079. srsym:=searchsym_in_class(tobjectdef(htype.def),pattern);
  1080. check_hints(srsym);
  1081. consume(_ID);
  1082. do_member_read(false,srsym,p1,again);
  1083. end
  1084. else
  1085. begin
  1086. Message(parser_e_no_super_class);
  1087. again:=false;
  1088. end;
  1089. end
  1090. else
  1091. begin
  1092. { allows @TObject.Load }
  1093. { also allows static methods and variables }
  1094. p1:=ctypenode.create(htype);
  1095. { TP allows also @TMenu.Load if Load is only }
  1096. { defined in an anchestor class }
  1097. srsym:=search_class_member(tobjectdef(htype.def),pattern);
  1098. check_hints(srsym);
  1099. if not assigned(srsym) then
  1100. Message1(sym_e_id_no_member,pattern)
  1101. else if not(getaddr) and not(sp_static in srsym.symoptions) then
  1102. Message(sym_e_only_static_in_static)
  1103. else
  1104. begin
  1105. consume(_ID);
  1106. do_member_read(getaddr,srsym,p1,again);
  1107. end;
  1108. end;
  1109. end
  1110. else
  1111. begin
  1112. { class reference ? }
  1113. if is_class(htype.def) then
  1114. begin
  1115. if getaddr and (token=_POINT) then
  1116. begin
  1117. consume(_POINT);
  1118. { allows @Object.Method }
  1119. { also allows static methods and variables }
  1120. p1:=ctypenode.create(htype);
  1121. { TP allows also @TMenu.Load if Load is only }
  1122. { defined in an anchestor class }
  1123. srsym:=search_class_member(tobjectdef(htype.def),pattern);
  1124. check_hints(srsym);
  1125. if not assigned(srsym) then
  1126. Message1(sym_e_id_no_member,pattern)
  1127. else
  1128. begin
  1129. consume(_ID);
  1130. do_member_read(getaddr,srsym,p1,again);
  1131. end;
  1132. end
  1133. else
  1134. begin
  1135. p1:=ctypenode.create(htype);
  1136. { For a type block we simply return only
  1137. the type. For all other blocks we return
  1138. a loadvmt node }
  1139. if (block_type<>bt_type) then
  1140. p1:=cloadvmtnode.create(p1);
  1141. end;
  1142. end
  1143. else
  1144. p1:=ctypenode.create(htype);
  1145. end;
  1146. end;
  1147. end;
  1148. enumsym :
  1149. begin
  1150. p1:=genenumnode(tenumsym(srsym));
  1151. end;
  1152. constsym :
  1153. begin
  1154. case tconstsym(srsym).consttyp of
  1155. constint :
  1156. begin
  1157. { do a very dirty trick to bootstrap this code }
  1158. if (tconstsym(srsym).value.valueord>=-(int64(2147483647)+int64(1))) and
  1159. (tconstsym(srsym).value.valueord<=2147483647) then
  1160. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,s32bittype,true)
  1161. else if (tconstsym(srsym).value.valueord > maxlongint) and
  1162. (tconstsym(srsym).value.valueord <= int64(maxlongint)+int64(maxlongint)+1) then
  1163. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,u32bittype,true)
  1164. else
  1165. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,cs64bittype,true);
  1166. end;
  1167. conststring :
  1168. begin
  1169. len:=tconstsym(srsym).value.len;
  1170. if not(cs_ansistrings in aktlocalswitches) and (len>255) then
  1171. len:=255;
  1172. getmem(pc,len+1);
  1173. move(pchar(tconstsym(srsym).value.valueptr)^,pc^,len);
  1174. pc[len]:=#0;
  1175. p1:=cstringconstnode.createpchar(pc,len);
  1176. end;
  1177. constchar :
  1178. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,cchartype,true);
  1179. constreal :
  1180. p1:=crealconstnode.create(pbestreal(tconstsym(srsym).value.valueptr)^,pbestrealtype^);
  1181. constbool :
  1182. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,booltype,true);
  1183. constset :
  1184. p1:=csetconstnode.create(pconstset(tconstsym(srsym).value.valueptr),tconstsym(srsym).consttype);
  1185. constord :
  1186. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,tconstsym(srsym).consttype,true);
  1187. constpointer :
  1188. p1:=cpointerconstnode.create(tconstsym(srsym).value.valueordptr,tconstsym(srsym).consttype);
  1189. constnil :
  1190. p1:=cnilnode.create;
  1191. constresourcestring:
  1192. begin
  1193. p1:=cloadnode.create(srsym,srsymtable);
  1194. do_resulttypepass(p1);
  1195. p1.resulttype:=cansistringtype;
  1196. end;
  1197. constguid :
  1198. p1:=cguidconstnode.create(pguid(tconstsym(srsym).value.valueptr)^);
  1199. end;
  1200. end;
  1201. procsym :
  1202. begin
  1203. { are we in a class method ? }
  1204. possible_error:=(srsym.owner.symtabletype=objectsymtable) and
  1205. not(is_interface(tdef(srsym.owner.defowner))) and
  1206. assigned(aktprocsym) and
  1207. (po_classmethod in aktprocdef.procoptions);
  1208. do_proc_call(srsym,srsymtable,
  1209. (getaddr and not(token in [_CARET,_POINT])),
  1210. again,p1);
  1211. { we need to know which procedure is called }
  1212. if possible_error then
  1213. begin
  1214. do_resulttypepass(p1);
  1215. if not(po_classmethod in tcallnode(p1).procdefinition.procoptions) then
  1216. Message(parser_e_only_class_methods);
  1217. end;
  1218. end;
  1219. propertysym :
  1220. begin
  1221. { access to property in a method }
  1222. { are we in a class method ? }
  1223. if (srsym.owner.symtabletype=objectsymtable) and
  1224. assigned(aktprocsym) and
  1225. (po_classmethod in aktprocdef.procoptions) then
  1226. Message(parser_e_only_class_methods);
  1227. { no method pointer }
  1228. p1:=nil;
  1229. handle_propertysym(srsym,srsymtable,p1,getaddr);
  1230. end;
  1231. labelsym :
  1232. begin
  1233. consume(_COLON);
  1234. if tlabelsym(srsym).defined then
  1235. Message(sym_e_label_already_defined);
  1236. tlabelsym(srsym).defined:=true;
  1237. p1:=clabelnode.create(tlabelsym(srsym),nil);
  1238. end;
  1239. errorsym :
  1240. begin
  1241. p1:=cerrornode.create;
  1242. if token=_LKLAMMER then
  1243. begin
  1244. consume(_LKLAMMER);
  1245. parse_paras(false,false);
  1246. consume(_RKLAMMER);
  1247. end;
  1248. end;
  1249. else
  1250. begin
  1251. p1:=cerrornode.create;
  1252. Message(cg_e_illegal_expression);
  1253. end;
  1254. end; { end case }
  1255. end;
  1256. end;
  1257. {---------------------------------------------
  1258. Factor_Read_Set
  1259. ---------------------------------------------}
  1260. { Read a set between [] }
  1261. function factor_read_set:tnode;
  1262. var
  1263. p1,p2 : tnode;
  1264. lastp,
  1265. buildp : tarrayconstructornode;
  1266. begin
  1267. buildp:=nil;
  1268. { be sure that a least one arrayconstructn is used, also for an
  1269. empty [] }
  1270. if token=_RECKKLAMMER then
  1271. buildp:=carrayconstructornode.create(nil,buildp)
  1272. else
  1273. begin
  1274. while true do
  1275. begin
  1276. p1:=comp_expr(true);
  1277. if token=_POINTPOINT then
  1278. begin
  1279. consume(_POINTPOINT);
  1280. p2:=comp_expr(true);
  1281. p1:=carrayconstructorrangenode.create(p1,p2);
  1282. end;
  1283. { insert at the end of the tree, to get the correct order }
  1284. if not assigned(buildp) then
  1285. begin
  1286. buildp:=carrayconstructornode.create(p1,nil);
  1287. lastp:=buildp;
  1288. end
  1289. else
  1290. begin
  1291. lastp.right:=carrayconstructornode.create(p1,nil);
  1292. lastp:=tarrayconstructornode(lastp.right);
  1293. end;
  1294. { there could be more elements }
  1295. if token=_COMMA then
  1296. consume(_COMMA)
  1297. else
  1298. break;
  1299. end;
  1300. end;
  1301. factor_read_set:=buildp;
  1302. end;
  1303. {---------------------------------------------
  1304. PostFixOperators
  1305. ---------------------------------------------}
  1306. procedure postfixoperators(var p1:tnode;var again:boolean);
  1307. { tries to avoid syntax errors after invalid qualifiers }
  1308. procedure recoverconsume_postfixops;
  1309. begin
  1310. while true do
  1311. begin
  1312. case token of
  1313. _CARET:
  1314. consume(_CARET);
  1315. _POINT:
  1316. begin
  1317. consume(_POINT);
  1318. if token=_ID then
  1319. consume(_ID);
  1320. end;
  1321. _LECKKLAMMER:
  1322. begin
  1323. consume(_LECKKLAMMER);
  1324. repeat
  1325. comp_expr(true);
  1326. if token=_COMMA then
  1327. consume(_COMMA)
  1328. else
  1329. break;
  1330. until false;
  1331. consume(_RECKKLAMMER);
  1332. end
  1333. else
  1334. break;
  1335. end;
  1336. end;
  1337. end;
  1338. var
  1339. store_static : boolean;
  1340. protsym : tpropertysym;
  1341. p2,p3 : tnode;
  1342. hsym : tsym;
  1343. classh : tobjectdef;
  1344. begin
  1345. again:=true;
  1346. while again do
  1347. begin
  1348. { we need the resulttype }
  1349. do_resulttypepass(p1);
  1350. if codegenerror then
  1351. begin
  1352. recoverconsume_postfixops;
  1353. exit;
  1354. end;
  1355. { handle token }
  1356. case token of
  1357. _CARET:
  1358. begin
  1359. consume(_CARET);
  1360. if (p1.resulttype.def.deftype<>pointerdef) then
  1361. begin
  1362. { ^ as binary operator is a problem!!!! (FK) }
  1363. again:=false;
  1364. Message(cg_e_invalid_qualifier);
  1365. recoverconsume_postfixops;
  1366. p1.destroy;
  1367. p1:=cerrornode.create;
  1368. end
  1369. else
  1370. begin
  1371. p1:=cderefnode.create(p1);
  1372. end;
  1373. end;
  1374. _LECKKLAMMER:
  1375. begin
  1376. if is_class_or_interface(p1.resulttype.def) then
  1377. begin
  1378. { default property }
  1379. protsym:=search_default_property(tobjectdef(p1.resulttype.def));
  1380. if not(assigned(protsym)) then
  1381. begin
  1382. p1.destroy;
  1383. p1:=cerrornode.create;
  1384. again:=false;
  1385. message(parser_e_no_default_property_available);
  1386. end
  1387. else
  1388. begin
  1389. { The property symbol is referenced indirect }
  1390. inc(protsym.refs);
  1391. handle_propertysym(protsym,protsym.owner,p1,getaddr);
  1392. end;
  1393. end
  1394. else
  1395. begin
  1396. consume(_LECKKLAMMER);
  1397. repeat
  1398. case p1.resulttype.def.deftype of
  1399. pointerdef:
  1400. begin
  1401. { support delphi autoderef }
  1402. if (tpointerdef(p1.resulttype.def).pointertype.def.deftype=arraydef) and
  1403. (m_autoderef in aktmodeswitches) then
  1404. begin
  1405. p1:=cderefnode.create(p1);
  1406. end;
  1407. p2:=comp_expr(true);
  1408. p1:=cvecnode.create(p1,p2);
  1409. end;
  1410. stringdef :
  1411. begin
  1412. p2:=comp_expr(true);
  1413. p1:=cvecnode.create(p1,p2);
  1414. end;
  1415. arraydef :
  1416. begin
  1417. p2:=comp_expr(true);
  1418. { support SEG:OFS for go32v2 Mem[] }
  1419. if (target_info.system=system_i386_go32v2) and
  1420. (p1.nodetype=loadn) and
  1421. assigned(tloadnode(p1).symtableentry) and
  1422. assigned(tloadnode(p1).symtableentry.owner.name) and
  1423. (tloadnode(p1).symtableentry.owner.name^='SYSTEM') and
  1424. ((tloadnode(p1).symtableentry.name='MEM') or
  1425. (tloadnode(p1).symtableentry.name='MEMW') or
  1426. (tloadnode(p1).symtableentry.name='MEML')) then
  1427. begin
  1428. if (token=_COLON) then
  1429. begin
  1430. consume(_COLON);
  1431. p3:=caddnode.create(muln,cordconstnode.create($10,s32bittype,false),p2);
  1432. p2:=comp_expr(true);
  1433. p2:=caddnode.create(addn,p2,p3);
  1434. p1:=cvecnode.create(p1,p2);
  1435. include(tvecnode(p1).flags,nf_memseg);
  1436. include(tvecnode(p1).flags,nf_memindex);
  1437. end
  1438. else
  1439. begin
  1440. p1:=cvecnode.create(p1,p2);
  1441. include(tvecnode(p1).flags,nf_memindex);
  1442. end;
  1443. end
  1444. else
  1445. p1:=cvecnode.create(p1,p2);
  1446. end;
  1447. else
  1448. begin
  1449. Message(cg_e_invalid_qualifier);
  1450. p1.destroy;
  1451. p1:=cerrornode.create;
  1452. comp_expr(true);
  1453. again:=false;
  1454. end;
  1455. end;
  1456. do_resulttypepass(p1);
  1457. if token=_COMMA then
  1458. consume(_COMMA)
  1459. else
  1460. break;
  1461. until false;
  1462. consume(_RECKKLAMMER);
  1463. end;
  1464. end;
  1465. _POINT :
  1466. begin
  1467. consume(_POINT);
  1468. if (p1.resulttype.def.deftype=pointerdef) and
  1469. (m_autoderef in aktmodeswitches) then
  1470. begin
  1471. p1:=cderefnode.create(p1);
  1472. do_resulttypepass(p1);
  1473. end;
  1474. case p1.resulttype.def.deftype of
  1475. recorddef:
  1476. begin
  1477. hsym:=tsym(trecorddef(p1.resulttype.def).symtable.search(pattern));
  1478. check_hints(hsym);
  1479. if assigned(hsym) and
  1480. (hsym.typ=varsym) then
  1481. p1:=csubscriptnode.create(hsym,p1)
  1482. else
  1483. begin
  1484. Message1(sym_e_illegal_field,pattern);
  1485. p1.destroy;
  1486. p1:=cerrornode.create;
  1487. end;
  1488. consume(_ID);
  1489. end;
  1490. variantdef:
  1491. begin
  1492. end;
  1493. classrefdef:
  1494. begin
  1495. classh:=tobjectdef(tclassrefdef(p1.resulttype.def).pointertype.def);
  1496. hsym:=searchsym_in_class(classh,pattern);
  1497. check_hints(hsym);
  1498. if hsym=nil then
  1499. begin
  1500. Message1(sym_e_id_no_member,pattern);
  1501. p1.destroy;
  1502. p1:=cerrornode.create;
  1503. { try to clean up }
  1504. consume(_ID);
  1505. end
  1506. else
  1507. begin
  1508. consume(_ID);
  1509. do_member_read(getaddr,hsym,p1,again);
  1510. end;
  1511. end;
  1512. objectdef:
  1513. begin
  1514. store_static:=allow_only_static;
  1515. allow_only_static:=false;
  1516. classh:=tobjectdef(p1.resulttype.def);
  1517. hsym:=searchsym_in_class(classh,pattern);
  1518. check_hints(hsym);
  1519. allow_only_static:=store_static;
  1520. if hsym=nil then
  1521. begin
  1522. Message1(sym_e_id_no_member,pattern);
  1523. p1.destroy;
  1524. p1:=cerrornode.create;
  1525. { try to clean up }
  1526. consume(_ID);
  1527. end
  1528. else
  1529. begin
  1530. consume(_ID);
  1531. do_member_read(getaddr,hsym,p1,again);
  1532. end;
  1533. end;
  1534. pointerdef:
  1535. begin
  1536. Message(cg_e_invalid_qualifier);
  1537. if tpointerdef(p1.resulttype.def).pointertype.def.deftype in [recorddef,objectdef,classrefdef] then
  1538. Message(parser_h_maybe_deref_caret_missing);
  1539. end;
  1540. else
  1541. begin
  1542. Message(cg_e_invalid_qualifier);
  1543. p1.destroy;
  1544. p1:=cerrornode.create;
  1545. consume(_ID);
  1546. end;
  1547. end;
  1548. end;
  1549. else
  1550. begin
  1551. { is this a procedure variable ? }
  1552. if assigned(p1.resulttype.def) then
  1553. begin
  1554. if (p1.resulttype.def.deftype=procvardef) then
  1555. begin
  1556. if assigned(getprocvardef) and
  1557. equal_defs(p1.resulttype.def,getprocvardef) then
  1558. again:=false
  1559. else
  1560. if (token=_LKLAMMER) or
  1561. ((tprocvardef(p1.resulttype.def).para.empty) and
  1562. (not((token in [_ASSIGNMENT,_UNEQUAL,_EQUAL]))) and
  1563. (not afterassignment) and
  1564. (not in_args)) then
  1565. begin
  1566. { do this in a strange way }
  1567. { it's not a clean solution }
  1568. p2:=p1;
  1569. p1:=ccallnode.create(nil,nil,nil,nil);
  1570. tcallnode(p1).set_procvar(p2);
  1571. if try_to_consume(_LKLAMMER) then
  1572. begin
  1573. tcallnode(p1).left:=parse_paras(false,false);
  1574. consume(_RKLAMMER);
  1575. end;
  1576. { proc():= is never possible }
  1577. if token=_ASSIGNMENT then
  1578. begin
  1579. Message(cg_e_illegal_expression);
  1580. p1:=cerrornode.create;
  1581. again:=false;
  1582. end;
  1583. end
  1584. else
  1585. again:=false;
  1586. end
  1587. else
  1588. again:=false;
  1589. end
  1590. else
  1591. again:=false;
  1592. end;
  1593. end;
  1594. end; { while again }
  1595. end;
  1596. {---------------------------------------------
  1597. Factor (Main)
  1598. ---------------------------------------------}
  1599. var
  1600. l : longint;
  1601. card : cardinal;
  1602. ic : TConstExprInt;
  1603. oldp1,
  1604. p1 : tnode;
  1605. code : integer;
  1606. again : boolean;
  1607. sym : tsym;
  1608. classh : tobjectdef;
  1609. d : bestreal;
  1610. hs : string;
  1611. htype : ttype;
  1612. filepos : tfileposinfo;
  1613. {---------------------------------------------
  1614. Helpers
  1615. ---------------------------------------------}
  1616. procedure check_tokenpos;
  1617. begin
  1618. if (p1<>oldp1) then
  1619. begin
  1620. if assigned(p1) then
  1621. p1.set_tree_filepos(filepos);
  1622. oldp1:=p1;
  1623. filepos:=akttokenpos;
  1624. end;
  1625. end;
  1626. begin
  1627. oldp1:=nil;
  1628. p1:=nil;
  1629. filepos:=akttokenpos;
  1630. again:=false;
  1631. if token=_ID then
  1632. begin
  1633. factor_read_id(p1,again);
  1634. if again then
  1635. begin
  1636. check_tokenpos;
  1637. { handle post fix operators }
  1638. postfixoperators(p1,again);
  1639. end;
  1640. end
  1641. else
  1642. case token of
  1643. _SELF :
  1644. begin
  1645. again:=true;
  1646. consume(_SELF);
  1647. if not assigned(procinfo._class) then
  1648. begin
  1649. p1:=cerrornode.create;
  1650. again:=false;
  1651. Message(parser_e_self_not_in_method);
  1652. end
  1653. else
  1654. begin
  1655. if (po_classmethod in aktprocdef.procoptions) then
  1656. begin
  1657. { self in class methods is a class reference type }
  1658. htype.setdef(procinfo._class);
  1659. p1:=cselfnode.create(tclassrefdef.create(htype));
  1660. end
  1661. else
  1662. p1:=cselfnode.create(procinfo._class);
  1663. postfixoperators(p1,again);
  1664. end;
  1665. end;
  1666. _INHERITED :
  1667. begin
  1668. again:=true;
  1669. consume(_INHERITED);
  1670. if assigned(procinfo._class) then
  1671. begin
  1672. classh:=procinfo._class.childof;
  1673. { if inherited; only then we need the method with
  1674. the same name }
  1675. if token in endtokens then
  1676. begin
  1677. hs:=aktprocsym.name;
  1678. anon_inherited:=true;
  1679. { For message methods we need to search using the message
  1680. number or string }
  1681. if (po_msgint in aktprocsym.first_procdef.procoptions) then
  1682. sym:=searchsym_in_class_by_msgint(classh,aktprocsym.first_procdef.messageinf.i)
  1683. else
  1684. if (po_msgstr in aktprocsym.first_procdef.procoptions) then
  1685. sym:=searchsym_in_class_by_msgstr(classh,aktprocsym.first_procdef.messageinf.str)
  1686. else
  1687. sym:=searchsym_in_class(classh,hs);
  1688. end
  1689. else
  1690. begin
  1691. hs:=pattern;
  1692. consume(_ID);
  1693. anon_inherited:=false;
  1694. sym:=searchsym_in_class(classh,hs);
  1695. end;
  1696. if assigned(sym) then
  1697. begin
  1698. check_hints(sym);
  1699. if sym.typ=procsym then
  1700. begin
  1701. htype.setdef(classh);
  1702. p1:=ctypenode.create(htype);
  1703. end;
  1704. do_member_read(false,sym,p1,again);
  1705. { Add flag to indicate that inherited is used }
  1706. if p1.nodetype=calln then
  1707. include(p1.flags,nf_anon_inherited);
  1708. end
  1709. else
  1710. begin
  1711. if anon_inherited then
  1712. begin
  1713. { we didn't find a member in the parents call the
  1714. DefaultHandler }
  1715. sym:=searchsym_in_class(classh,'DEFAULTHANDLER');
  1716. if not assigned(sym) or
  1717. (sym.typ<>procsym) then
  1718. internalerror(200303171);
  1719. p1:=nil;
  1720. do_proc_call(sym,sym.owner,false,again,p1);
  1721. end
  1722. else
  1723. begin
  1724. Message1(sym_e_id_no_member,hs);
  1725. p1:=cerrornode.create;
  1726. end;
  1727. again:=false;
  1728. end;
  1729. { turn auto inheriting off }
  1730. anon_inherited:=false;
  1731. end
  1732. else
  1733. begin
  1734. Message(parser_e_generic_methods_only_in_methods);
  1735. again:=false;
  1736. p1:=cerrornode.create;
  1737. end;
  1738. postfixoperators(p1,again);
  1739. end;
  1740. _INTCONST :
  1741. begin
  1742. { try cardinal first }
  1743. val(pattern,card,code);
  1744. if code<>0 then
  1745. begin
  1746. { then longint }
  1747. valint(pattern,l,code);
  1748. if code <> 0 then
  1749. begin
  1750. { then int64 }
  1751. val(pattern,ic,code);
  1752. if code<>0 then
  1753. begin
  1754. {finally float }
  1755. val(pattern,d,code);
  1756. if code<>0 then
  1757. begin
  1758. Message(cg_e_invalid_integer);
  1759. consume(_INTCONST);
  1760. l:=1;
  1761. p1:=cordconstnode.create(l,s32bittype,true);
  1762. end
  1763. else
  1764. begin
  1765. consume(_INTCONST);
  1766. p1:=crealconstnode.create(d,pbestrealtype^);
  1767. end;
  1768. end
  1769. else
  1770. begin
  1771. consume(_INTCONST);
  1772. p1:=cordconstnode.create(ic,cs64bittype,true);
  1773. end
  1774. end
  1775. else
  1776. begin
  1777. consume(_INTCONST);
  1778. p1:=cordconstnode.create(l,s32bittype,true)
  1779. end
  1780. end
  1781. else
  1782. begin
  1783. consume(_INTCONST);
  1784. { check whether the value isn't in the longint range as well }
  1785. { (longint is easier to perform calculations with) (JM) }
  1786. if card <= $7fffffff then
  1787. { no sign extension necessary, so not longint typecast (JM) }
  1788. p1:=cordconstnode.create(card,s32bittype,true)
  1789. else
  1790. p1:=cordconstnode.create(card,u32bittype,true)
  1791. end;
  1792. end;
  1793. _REALNUMBER :
  1794. begin
  1795. val(pattern,d,code);
  1796. if code<>0 then
  1797. begin
  1798. Message(parser_e_error_in_real);
  1799. d:=1.0;
  1800. end;
  1801. consume(_REALNUMBER);
  1802. p1:=crealconstnode.create(d,pbestrealtype^);
  1803. end;
  1804. _STRING :
  1805. begin
  1806. string_dec(htype);
  1807. { STRING can be also a type cast }
  1808. if token=_LKLAMMER then
  1809. begin
  1810. consume(_LKLAMMER);
  1811. p1:=comp_expr(true);
  1812. consume(_RKLAMMER);
  1813. p1:=ctypeconvnode.create(p1,htype);
  1814. include(p1.flags,nf_explizit);
  1815. { handle postfix operators here e.g. string(a)[10] }
  1816. again:=true;
  1817. postfixoperators(p1,again);
  1818. end
  1819. else
  1820. p1:=ctypenode.create(htype);
  1821. end;
  1822. _FILE :
  1823. begin
  1824. htype:=cfiletype;
  1825. consume(_FILE);
  1826. { FILE can be also a type cast }
  1827. if token=_LKLAMMER then
  1828. begin
  1829. consume(_LKLAMMER);
  1830. p1:=comp_expr(true);
  1831. consume(_RKLAMMER);
  1832. p1:=ctypeconvnode.create(p1,htype);
  1833. include(p1.flags,nf_explizit);
  1834. { handle postfix operators here e.g. string(a)[10] }
  1835. again:=true;
  1836. postfixoperators(p1,again);
  1837. end
  1838. else
  1839. begin
  1840. p1:=ctypenode.create(htype);
  1841. end;
  1842. end;
  1843. _CSTRING :
  1844. begin
  1845. p1:=cstringconstnode.createstr(pattern,st_default);
  1846. consume(_CSTRING);
  1847. end;
  1848. _CCHAR :
  1849. begin
  1850. p1:=cordconstnode.create(ord(pattern[1]),cchartype,true);
  1851. consume(_CCHAR);
  1852. end;
  1853. _CWSTRING:
  1854. begin
  1855. p1:=cstringconstnode.createwstr(patternw);
  1856. consume(_CWSTRING);
  1857. end;
  1858. _CWCHAR:
  1859. begin
  1860. p1:=cordconstnode.create(ord(getcharwidestring(patternw,0)),cwidechartype,true);
  1861. consume(_CWCHAR);
  1862. end;
  1863. _KLAMMERAFFE :
  1864. begin
  1865. consume(_KLAMMERAFFE);
  1866. got_addrn:=true;
  1867. { support both @<x> and @(<x>) }
  1868. if try_to_consume(_LKLAMMER) then
  1869. begin
  1870. p1:=factor(true);
  1871. if token in [_CARET,_POINT,_LECKKLAMMER] then
  1872. begin
  1873. again:=true;
  1874. postfixoperators(p1,again);
  1875. end;
  1876. consume(_RKLAMMER);
  1877. end
  1878. else
  1879. p1:=factor(true);
  1880. if token in [_CARET,_POINT,_LECKKLAMMER] then
  1881. begin
  1882. again:=true;
  1883. postfixoperators(p1,again);
  1884. end;
  1885. got_addrn:=false;
  1886. p1:=caddrnode.create(p1);
  1887. if assigned(getprocvardef) and
  1888. (taddrnode(p1).left.nodetype = loadn) and
  1889. { make sure we found a valid procedure, otherwise the }
  1890. { "getprocvardef" will become the default in taddrnode }
  1891. { while there should be an error (JM) }
  1892. assigned(tloadnode(taddrnode(p1).left).procdef) then
  1893. taddrnode(p1).getprocvardef:=getprocvardef;
  1894. end;
  1895. _LKLAMMER :
  1896. begin
  1897. consume(_LKLAMMER);
  1898. p1:=comp_expr(true);
  1899. consume(_RKLAMMER);
  1900. { it's not a good solution }
  1901. { but (a+b)^ makes some problems }
  1902. if token in [_CARET,_POINT,_LECKKLAMMER] then
  1903. begin
  1904. again:=true;
  1905. postfixoperators(p1,again);
  1906. end;
  1907. end;
  1908. _LECKKLAMMER :
  1909. begin
  1910. consume(_LECKKLAMMER);
  1911. p1:=factor_read_set;
  1912. consume(_RECKKLAMMER);
  1913. end;
  1914. _PLUS :
  1915. begin
  1916. consume(_PLUS);
  1917. p1:=factor(false);
  1918. end;
  1919. _MINUS :
  1920. begin
  1921. consume(_MINUS);
  1922. p1:=sub_expr(oppower,false);
  1923. p1:=cunaryminusnode.create(p1);
  1924. end;
  1925. _OP_NOT :
  1926. begin
  1927. consume(_OP_NOT);
  1928. p1:=factor(false);
  1929. p1:=cnotnode.create(p1);
  1930. end;
  1931. _TRUE :
  1932. begin
  1933. consume(_TRUE);
  1934. p1:=cordconstnode.create(1,booltype,false);
  1935. end;
  1936. _FALSE :
  1937. begin
  1938. consume(_FALSE);
  1939. p1:=cordconstnode.create(0,booltype,false);
  1940. end;
  1941. _NIL :
  1942. begin
  1943. consume(_NIL);
  1944. p1:=cnilnode.create;
  1945. { It's really ugly code nil^, but delphi allows it }
  1946. if token in [_CARET] then
  1947. begin
  1948. again:=true;
  1949. postfixoperators(p1,again);
  1950. end;
  1951. end;
  1952. else
  1953. begin
  1954. p1:=cerrornode.create;
  1955. consume(token);
  1956. Message(cg_e_illegal_expression);
  1957. end;
  1958. end;
  1959. { generate error node if no node is created }
  1960. if not assigned(p1) then
  1961. begin
  1962. {$ifdef EXTDEBUG}
  1963. Comment(V_Warning,'factor: p1=nil');
  1964. {$endif}
  1965. p1:=cerrornode.create;
  1966. end;
  1967. { get the resulttype for the node }
  1968. if (not assigned(p1.resulttype.def)) then
  1969. do_resulttypepass(p1);
  1970. { tp7 procvar handling, but not if the next token
  1971. will be a := }
  1972. check_tp_procvar(p1);
  1973. factor:=p1;
  1974. check_tokenpos;
  1975. end;
  1976. {$ifdef fpc}
  1977. {$maxfpuregisters default}
  1978. {$endif fpc}
  1979. {****************************************************************************
  1980. Sub_Expr
  1981. ****************************************************************************}
  1982. const
  1983. { Warning these stay be ordered !! }
  1984. operator_levels:array[Toperator_precedence] of set of Ttoken=
  1985. ([_LT,_LTE,_GT,_GTE,_EQUAL,_UNEQUAL,_OP_IN,_OP_IS],
  1986. [_PLUS,_MINUS,_OP_OR,_OP_XOR],
  1987. [_CARET,_SYMDIF,_STARSTAR,_STAR,_SLASH,
  1988. _OP_AS,_OP_AND,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR],
  1989. [_STARSTAR] );
  1990. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):tnode;
  1991. {Reads a subexpression while the operators are of the current precedence
  1992. level, or any higher level. Replaces the old term, simpl_expr and
  1993. simpl2_expr.}
  1994. var
  1995. p1,p2 : tnode;
  1996. oldt : Ttoken;
  1997. filepos : tfileposinfo;
  1998. begin
  1999. if pred_level=highest_precedence then
  2000. p1:=factor(false)
  2001. else
  2002. p1:=sub_expr(succ(pred_level),true);
  2003. repeat
  2004. if (token in operator_levels[pred_level]) and
  2005. ((token<>_EQUAL) or accept_equal) then
  2006. begin
  2007. oldt:=token;
  2008. filepos:=akttokenpos;
  2009. consume(token);
  2010. if pred_level=highest_precedence then
  2011. p2:=factor(false)
  2012. else
  2013. p2:=sub_expr(succ(pred_level),true);
  2014. case oldt of
  2015. _PLUS :
  2016. p1:=caddnode.create(addn,p1,p2);
  2017. _MINUS :
  2018. p1:=caddnode.create(subn,p1,p2);
  2019. _STAR :
  2020. p1:=caddnode.create(muln,p1,p2);
  2021. _SLASH :
  2022. p1:=caddnode.create(slashn,p1,p2);
  2023. _EQUAL :
  2024. p1:=caddnode.create(equaln,p1,p2);
  2025. _GT :
  2026. p1:=caddnode.create(gtn,p1,p2);
  2027. _LT :
  2028. p1:=caddnode.create(ltn,p1,p2);
  2029. _GTE :
  2030. p1:=caddnode.create(gten,p1,p2);
  2031. _LTE :
  2032. p1:=caddnode.create(lten,p1,p2);
  2033. _SYMDIF :
  2034. p1:=caddnode.create(symdifn,p1,p2);
  2035. _STARSTAR :
  2036. p1:=caddnode.create(starstarn,p1,p2);
  2037. _OP_AS :
  2038. p1:=casnode.create(p1,p2);
  2039. _OP_IN :
  2040. p1:=cinnode.create(p1,p2);
  2041. _OP_IS :
  2042. p1:=cisnode.create(p1,p2);
  2043. _OP_OR :
  2044. p1:=caddnode.create(orn,p1,p2);
  2045. _OP_AND :
  2046. p1:=caddnode.create(andn,p1,p2);
  2047. _OP_DIV :
  2048. p1:=cmoddivnode.create(divn,p1,p2);
  2049. _OP_NOT :
  2050. p1:=cnotnode.create(p1);
  2051. _OP_MOD :
  2052. p1:=cmoddivnode.create(modn,p1,p2);
  2053. _OP_SHL :
  2054. p1:=cshlshrnode.create(shln,p1,p2);
  2055. _OP_SHR :
  2056. p1:=cshlshrnode.create(shrn,p1,p2);
  2057. _OP_XOR :
  2058. p1:=caddnode.create(xorn,p1,p2);
  2059. _ASSIGNMENT :
  2060. p1:=cassignmentnode.create(p1,p2);
  2061. _CARET :
  2062. p1:=caddnode.create(caretn,p1,p2);
  2063. _UNEQUAL :
  2064. p1:=caddnode.create(unequaln,p1,p2);
  2065. end;
  2066. p1.set_tree_filepos(filepos);
  2067. end
  2068. else
  2069. break;
  2070. until false;
  2071. sub_expr:=p1;
  2072. end;
  2073. function comp_expr(accept_equal : boolean):tnode;
  2074. var
  2075. oldafterassignment : boolean;
  2076. p1 : tnode;
  2077. begin
  2078. oldafterassignment:=afterassignment;
  2079. afterassignment:=true;
  2080. p1:=sub_expr(opcompare,accept_equal);
  2081. { get the resulttype for this expression }
  2082. if not assigned(p1.resulttype.def) then
  2083. do_resulttypepass(p1);
  2084. afterassignment:=oldafterassignment;
  2085. comp_expr:=p1;
  2086. end;
  2087. function expr : tnode;
  2088. var
  2089. p1,p2 : tnode;
  2090. oldafterassignment : boolean;
  2091. oldp1 : tnode;
  2092. filepos : tfileposinfo;
  2093. begin
  2094. oldafterassignment:=afterassignment;
  2095. p1:=sub_expr(opcompare,true);
  2096. { get the resulttype for this expression }
  2097. if not assigned(p1.resulttype.def) then
  2098. do_resulttypepass(p1);
  2099. filepos:=akttokenpos;
  2100. check_tp_procvar(p1);
  2101. if token in [_ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  2102. afterassignment:=true;
  2103. oldp1:=p1;
  2104. case token of
  2105. _POINTPOINT :
  2106. begin
  2107. consume(_POINTPOINT);
  2108. p2:=sub_expr(opcompare,true);
  2109. p1:=crangenode.create(p1,p2);
  2110. end;
  2111. _ASSIGNMENT :
  2112. begin
  2113. consume(_ASSIGNMENT);
  2114. if (p1.resulttype.def.deftype=procvardef) then
  2115. getprocvardef:=tprocvardef(p1.resulttype.def);
  2116. p2:=sub_expr(opcompare,true);
  2117. if assigned(getprocvardef) then
  2118. handle_procvar(getprocvardef,p2);
  2119. getprocvardef:=nil;
  2120. p1:=cassignmentnode.create(p1,p2);
  2121. end;
  2122. _PLUSASN :
  2123. begin
  2124. consume(_PLUSASN);
  2125. p2:=sub_expr(opcompare,true);
  2126. p1:=cassignmentnode.create(p1,caddnode.create(addn,p1.getcopy,p2));
  2127. end;
  2128. _MINUSASN :
  2129. begin
  2130. consume(_MINUSASN);
  2131. p2:=sub_expr(opcompare,true);
  2132. p1:=cassignmentnode.create(p1,caddnode.create(subn,p1.getcopy,p2));
  2133. end;
  2134. _STARASN :
  2135. begin
  2136. consume(_STARASN );
  2137. p2:=sub_expr(opcompare,true);
  2138. p1:=cassignmentnode.create(p1,caddnode.create(muln,p1.getcopy,p2));
  2139. end;
  2140. _SLASHASN :
  2141. begin
  2142. consume(_SLASHASN );
  2143. p2:=sub_expr(opcompare,true);
  2144. p1:=cassignmentnode.create(p1,caddnode.create(slashn,p1.getcopy,p2));
  2145. end;
  2146. end;
  2147. { get the resulttype for this expression }
  2148. if not assigned(p1.resulttype.def) then
  2149. do_resulttypepass(p1);
  2150. afterassignment:=oldafterassignment;
  2151. if p1<>oldp1 then
  2152. p1.set_tree_filepos(filepos);
  2153. expr:=p1;
  2154. end;
  2155. {$ifdef int64funcresok}
  2156. function get_intconst:TConstExprInt;
  2157. {$else int64funcresok}
  2158. function get_intconst:longint;
  2159. {$endif int64funcresok}
  2160. {Reads an expression, tries to evalute it and check if it is an integer
  2161. constant. Then the constant is returned.}
  2162. var
  2163. p:tnode;
  2164. begin
  2165. p:=comp_expr(true);
  2166. if not codegenerror then
  2167. begin
  2168. if (p.nodetype<>ordconstn) or
  2169. not(is_integer(p.resulttype.def)) then
  2170. Message(cg_e_illegal_expression)
  2171. else
  2172. get_intconst:=tordconstnode(p).value;
  2173. end;
  2174. p.free;
  2175. end;
  2176. function get_stringconst:string;
  2177. {Reads an expression, tries to evaluate it and checks if it is a string
  2178. constant. Then the constant is returned.}
  2179. var
  2180. p:tnode;
  2181. begin
  2182. get_stringconst:='';
  2183. p:=comp_expr(true);
  2184. if p.nodetype<>stringconstn then
  2185. begin
  2186. if (p.nodetype=ordconstn) and is_char(p.resulttype.def) then
  2187. get_stringconst:=char(tordconstnode(p).value)
  2188. else
  2189. Message(cg_e_illegal_expression);
  2190. end
  2191. else
  2192. get_stringconst:=strpas(tstringconstnode(p).value_str);
  2193. p.free;
  2194. end;
  2195. end.
  2196. {
  2197. $Log$
  2198. Revision 1.107 2003-04-11 15:49:01 peter
  2199. * default property also increased the reference count for the
  2200. property symbol
  2201. Revision 1.106 2003/04/11 14:50:08 peter
  2202. * fix tw2454
  2203. Revision 1.105 2003/03/27 17:44:13 peter
  2204. * fixed small mem leaks
  2205. Revision 1.104 2003/03/17 18:55:30 peter
  2206. * allow more tokens instead of only semicolon after inherited
  2207. Revision 1.103 2003/03/17 16:54:41 peter
  2208. * support DefaultHandler and anonymous inheritance fixed
  2209. for message methods
  2210. Revision 1.102 2003/01/30 21:46:57 peter
  2211. * self fixes for static methods (merged)
  2212. Revision 1.101 2003/01/16 22:12:22 peter
  2213. * Find the correct procvar to load when using @ in fpc mode
  2214. Revision 1.100 2003/01/15 01:44:32 peter
  2215. * merged methodpointer fixes from 1.0.x
  2216. Revision 1.98 2003/01/12 17:51:42 peter
  2217. * tp procvar handling fix for tb0448
  2218. Revision 1.97 2003/01/05 22:44:14 peter
  2219. * remove a lot of code to support typen in loadn-procsym
  2220. Revision 1.96 2002/12/11 22:40:36 peter
  2221. * assigned(procvar) fix for delphi mode, fixes tb0430
  2222. Revision 1.95 2002/11/30 11:12:48 carl
  2223. + checking for symbols used with hint directives is done mostly in pexpr
  2224. only now
  2225. Revision 1.94 2002/11/27 15:33:47 peter
  2226. * the never ending story of tp procvar hacks
  2227. Revision 1.93 2002/11/26 22:58:24 peter
  2228. * fix for tw2178. When a ^ or . follows a procsym then the procsym
  2229. needs to be called
  2230. Revision 1.92 2002/11/25 17:43:22 peter
  2231. * splitted defbase in defutil,symutil,defcmp
  2232. * merged isconvertable and is_equal into compare_defs(_ext)
  2233. * made operator search faster by walking the list only once
  2234. Revision 1.91 2002/11/22 22:48:10 carl
  2235. * memory optimization with tconstsym (1.5%)
  2236. Revision 1.90 2002/11/20 22:49:55 pierre
  2237. * commented check code tht was invalid in 1.1
  2238. Revision 1.89 2002/11/18 18:34:41 peter
  2239. * fix crash with EXTDEBUG code
  2240. Revision 1.88 2002/11/18 17:48:21 peter
  2241. * fix tw2209 (merged)
  2242. Revision 1.87 2002/11/18 17:31:58 peter
  2243. * pass proccalloption to ret_in_xxx and push_xxx functions
  2244. Revision 1.86 2002/10/05 00:48:57 peter
  2245. * support inherited; support for overload as it is handled by
  2246. delphi. This is only for delphi mode as it is working is
  2247. undocumented and hard to predict what is done
  2248. Revision 1.85 2002/10/04 21:13:59 peter
  2249. * ignore vecn,subscriptn when checking for a procvar loadn
  2250. Revision 1.84 2002/10/02 20:51:22 peter
  2251. * don't check interfaces for class methods
  2252. Revision 1.83 2002/10/02 18:20:52 peter
  2253. * Copy() is now internal syssym that calls compilerprocs
  2254. Revision 1.82 2002/09/30 07:00:48 florian
  2255. * fixes to common code to get the alpha compiler compiled applied
  2256. Revision 1.81 2002/09/16 19:06:14 peter
  2257. * allow ^ after nil
  2258. Revision 1.80 2002/09/07 15:25:07 peter
  2259. * old logs removed and tabs fixed
  2260. Revision 1.79 2002/09/07 12:16:03 carl
  2261. * second part bug report 1996 fix, testrange in cordconstnode
  2262. only called if option is set (also make parsing a tiny faster)
  2263. Revision 1.78 2002/09/03 16:26:27 daniel
  2264. * Make Tprocdef.defs protected
  2265. Revision 1.77 2002/08/18 20:06:24 peter
  2266. * inlining is now also allowed in interface
  2267. * renamed write/load to ppuwrite/ppuload
  2268. * tnode storing in ppu
  2269. * nld,ncon,nbas are already updated for storing in ppu
  2270. Revision 1.76 2002/08/17 09:23:39 florian
  2271. * first part of procinfo rewrite
  2272. Revision 1.75 2002/08/01 16:37:47 jonas
  2273. - removed some superfluous "in_paras := true" statements
  2274. Revision 1.74 2002/07/26 21:15:41 florian
  2275. * rewrote the system handling
  2276. Revision 1.73 2002/07/23 09:51:23 daniel
  2277. * Tried to make Tprocsym.defs protected. I didn't succeed but the cleanups
  2278. are worth comitting.
  2279. Revision 1.72 2002/07/20 11:57:55 florian
  2280. * types.pas renamed to defbase.pas because D6 contains a types
  2281. unit so this would conflicts if D6 programms are compiled
  2282. + Willamette/SSE2 instructions to assembler added
  2283. Revision 1.71 2002/07/16 15:34:20 florian
  2284. * exit is now a syssym instead of a keyword
  2285. Revision 1.70 2002/07/06 20:18:02 carl
  2286. * longstring declaration now gives parser error since its not supported!
  2287. Revision 1.69 2002/06/12 15:46:14 jonas
  2288. * fixed web bug 1995
  2289. Revision 1.68 2002/05/18 13:34:12 peter
  2290. * readded missing revisions
  2291. Revision 1.67 2002/05/16 19:46:43 carl
  2292. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  2293. + try to fix temp allocation (still in ifdef)
  2294. + generic constructor calls
  2295. + start of tassembler / tmodulebase class cleanup
  2296. Revision 1.65 2002/05/12 16:53:09 peter
  2297. * moved entry and exitcode to ncgutil and cgobj
  2298. * foreach gets extra argument for passing local data to the
  2299. iterator function
  2300. * -CR checks also class typecasts at runtime by changing them
  2301. into as
  2302. * fixed compiler to cycle with the -CR option
  2303. * fixed stabs with elf writer, finally the global variables can
  2304. be watched
  2305. * removed a lot of routines from cga unit and replaced them by
  2306. calls to cgobj
  2307. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  2308. u32bit then the other is typecasted also to u32bit without giving
  2309. a rangecheck warning/error.
  2310. * fixed pascal calling method with reversing also the high tree in
  2311. the parast, detected by tcalcst3 test
  2312. Revision 1.64 2002/04/23 19:16:34 peter
  2313. * add pinline unit that inserts compiler supported functions using
  2314. one or more statements
  2315. * moved finalize and setlength from ninl to pinline
  2316. Revision 1.63 2002/04/21 19:02:05 peter
  2317. * removed newn and disposen nodes, the code is now directly
  2318. inlined from pexpr
  2319. * -an option that will write the secondpass nodes to the .s file, this
  2320. requires EXTDEBUG define to actually write the info
  2321. * fixed various internal errors and crashes due recent code changes
  2322. Revision 1.62 2002/04/16 16:11:17 peter
  2323. * using inherited; without a parent having the same function
  2324. will do nothing like delphi
  2325. Revision 1.61 2002/04/07 13:31:36 carl
  2326. + change unit use
  2327. Revision 1.60 2002/04/01 20:57:13 jonas
  2328. * fixed web bug 1907
  2329. * fixed some other procvar related bugs (all related to accepting procvar
  2330. constructs with either too many or too little parameters)
  2331. (both merged, includes second typo fix of pexpr.pas)
  2332. Revision 1.59 2002/03/31 20:26:35 jonas
  2333. + a_loadfpu_* and a_loadmm_* methods in tcg
  2334. * register allocation is now handled by a class and is mostly processor
  2335. independent (+rgobj.pas and i386/rgcpu.pas)
  2336. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  2337. * some small improvements and fixes to the optimizer
  2338. * some register allocation fixes
  2339. * some fpuvaroffset fixes in the unary minus node
  2340. * push/popusedregisters is now called rg.save/restoreusedregisters and
  2341. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  2342. also better optimizable)
  2343. * fixed and optimized register saving/restoring for new/dispose nodes
  2344. * LOC_FPU locations now also require their "register" field to be set to
  2345. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  2346. - list field removed of the tnode class because it's not used currently
  2347. and can cause hard-to-find bugs
  2348. }