pexpr.pas 94 KB

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