pexpr.pas 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915
  1. {
  2. $Id$
  3. Copyright (c) 1998 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. interface
  20. uses symtable,tree;
  21. { reads a whole expression }
  22. function expr : ptree;
  23. { reads an expression without assignements and .. }
  24. function comp_expr(accept_equal : boolean):Ptree;
  25. { reads a single factor }
  26. function factor(getaddr : boolean) : ptree;
  27. { the ID token has to be consumed before calling this function }
  28. procedure do_member_read(const sym : psym;var p1 : ptree;
  29. var pd : pdef;var again : boolean);
  30. function get_intconst:longint;
  31. function get_stringconst:string;
  32. implementation
  33. uses
  34. cobjects,globals,scanner,aasm,pass_1,systems,
  35. hcodegen,types,verbose
  36. { parser specific stuff }
  37. ,pbase,pdecl
  38. { processor specific stuff }
  39. {$ifdef i386}
  40. ,i386
  41. {$endif}
  42. {$ifdef m68k}
  43. ,m68k
  44. {$endif}
  45. ;
  46. function parse_paras(_colon,in_prop_paras : boolean) : ptree;
  47. var
  48. p1,p2 : ptree;
  49. end_of_paras : ttoken;
  50. begin
  51. if in_prop_paras then
  52. end_of_paras:=RECKKLAMMER
  53. else
  54. end_of_paras:=RKLAMMER;
  55. if token=end_of_paras then
  56. begin
  57. parse_paras:=nil;
  58. exit;
  59. end;
  60. p2:=nil;
  61. inc(parsing_para_level);
  62. while true do
  63. begin
  64. p1:=comp_expr(true);
  65. p2:=gencallparanode(p1,p2);
  66. { it's for the str(l:5,s); }
  67. if _colon and (token=COLON) then
  68. begin
  69. consume(COLON);
  70. p1:=comp_expr(true);
  71. p2:=gencallparanode(p1,p2);
  72. p2^.is_colon_para:=true;
  73. if token=COLON then
  74. begin
  75. consume(COLON);
  76. p1:=comp_expr(true);
  77. p2:=gencallparanode(p1,p2);
  78. p2^.is_colon_para:=true;
  79. end
  80. end;
  81. if token=COMMA then
  82. consume(COMMA)
  83. else
  84. break;
  85. end;
  86. dec(parsing_para_level);
  87. parse_paras:=p2;
  88. end;
  89. function statement_syssym(l : longint;var pd : pdef) : ptree;
  90. { const regnames:array[R_EAX..R_EDI] of string[3]=
  91. ('EAX','ECX','EDX','EBX','','','ESI','EDI'); }
  92. var
  93. p1,p2 : ptree;
  94. paras : ptree;
  95. prev_in_args : boolean;
  96. Store_valid : boolean;
  97. begin
  98. prev_in_args:=in_args;
  99. Store_valid:=Must_be_valid;
  100. case l of
  101. in_ord_x:
  102. begin
  103. consume(LKLAMMER);
  104. in_args:=true;
  105. Must_be_valid:=true;
  106. p1:=comp_expr(true);
  107. consume(RKLAMMER);
  108. do_firstpass(p1);
  109. p1:=geninlinenode(in_ord_x,p1);
  110. do_firstpass(p1);
  111. statement_syssym := p1;
  112. pd:=p1^.resulttype;
  113. end;
  114. in_break:
  115. begin
  116. statement_syssym:=genzeronode(breakn);
  117. pd:=voiddef;
  118. end;
  119. in_continue:
  120. begin
  121. statement_syssym:=genzeronode(continuen);
  122. pd:=voiddef;
  123. end;
  124. in_typeof_x : begin
  125. consume(LKLAMMER);
  126. in_args:=true;
  127. p1:=comp_expr(true);
  128. consume(RKLAMMER);
  129. pd:=voidpointerdef;
  130. if p1^.treetype=typen then
  131. begin
  132. if (p1^.resulttype=nil) then
  133. begin
  134. Message(sym_e_type_mismatch);
  135. statement_syssym:=genzeronode(errorn);
  136. end
  137. else
  138. if p1^.resulttype^.deftype=objectdef then
  139. statement_syssym:=geninlinenode(in_typeof_x,p1)
  140. else
  141. begin
  142. Message(sym_e_type_mismatch);
  143. statement_syssym:=genzeronode(errorn);
  144. end;
  145. end
  146. else
  147. begin
  148. Must_be_valid:=false;
  149. do_firstpass(p1);
  150. if (p1^.resulttype=nil) then
  151. begin
  152. Message(sym_e_type_mismatch);
  153. statement_syssym:=genzeronode(errorn)
  154. end
  155. else
  156. if p1^.resulttype^.deftype=objectdef then
  157. statement_syssym:=geninlinenode(in_typeof_x,p1)
  158. else
  159. begin
  160. Message(sym_e_type_mismatch);
  161. statement_syssym:=genzeronode(errorn)
  162. end;
  163. end;
  164. end;
  165. in_sizeof_x : begin
  166. consume(LKLAMMER);
  167. in_args:=true;
  168. p1:=comp_expr(true);
  169. consume(RKLAMMER);
  170. pd:=s32bitdef;
  171. if p1^.treetype=typen then
  172. begin
  173. statement_syssym:=genordinalconstnode(
  174. p1^.resulttype^.size,pd);
  175. { p1 not needed !}
  176. disposetree(p1);
  177. end
  178. else
  179. begin
  180. Must_be_valid:=false;
  181. do_firstpass(p1);
  182. if p1^.resulttype^.deftype<>objectdef then
  183. begin
  184. statement_syssym:=genordinalconstnode(
  185. p1^.resulttype^.size,pd);
  186. { p1 not needed !}
  187. disposetree(p1);
  188. end
  189. else
  190. begin
  191. statement_syssym:=geninlinenode(in_sizeof_x,p1);
  192. end;
  193. end;
  194. end;
  195. in_assigned_x : begin
  196. consume(LKLAMMER);
  197. in_args:=true;
  198. p1:=comp_expr(true);
  199. Must_be_valid:=true;
  200. do_firstpass(p1);
  201. case p1^.resulttype^.deftype of
  202. pointerdef,procvardef,
  203. classrefdef:
  204. ;
  205. objectdef:
  206. if not(pobjectdef(p1^.resulttype)^.isclass) then
  207. Message(parser_e_illegal_parameter_list);
  208. else Message(parser_e_illegal_parameter_list);
  209. end;
  210. p2:=gencallparanode(p1,nil);
  211. p2:=geninlinenode(in_assigned_x,p2);
  212. consume(RKLAMMER);
  213. pd:=booldef;
  214. statement_syssym:=p2;
  215. end;
  216. in_ofs_x : begin
  217. consume(LKLAMMER);
  218. in_args:=true;
  219. p1:=comp_expr(true);
  220. p1:=gensinglenode(addrn,p1);
  221. Must_be_valid:=false;
  222. do_firstpass(p1);
  223. { Ofs() returns a longint, not a pointer }
  224. p1^.resulttype:=u32bitdef;
  225. pd:=p1^.resulttype;
  226. consume(RKLAMMER);
  227. statement_syssym:=p1;
  228. end;
  229. in_seg_x : begin
  230. consume(LKLAMMER);
  231. in_args:=true;
  232. p1:=comp_expr(true);
  233. do_firstpass(p1);
  234. if p1^.location.loc<>LOC_REFERENCE then
  235. Message(cg_e_illegal_expression);
  236. p1:=genordinalconstnode(0,s32bitdef);
  237. Must_be_valid:=false;
  238. pd:=s32bitdef;
  239. consume(RKLAMMER);
  240. statement_syssym:=p1;
  241. end;
  242. in_high_x,
  243. in_low_x : begin
  244. consume(LKLAMMER);
  245. in_args:=true;
  246. p1:=comp_expr(true);
  247. do_firstpass(p1);
  248. Must_be_valid:=false;
  249. p2:=geninlinenode(l,p1);
  250. consume(RKLAMMER);
  251. pd:=s32bitdef;
  252. statement_syssym:=p2;
  253. end;
  254. in_succ_x,
  255. in_pred_x : begin
  256. consume(LKLAMMER);
  257. in_args:=true;
  258. p1:=comp_expr(true);
  259. do_firstpass(p1);
  260. Must_be_valid:=false;
  261. p2:=geninlinenode(l,p1);
  262. consume(RKLAMMER);
  263. pd:=p1^.resulttype;
  264. statement_syssym:=p2;
  265. end;
  266. in_inc_x,
  267. in_dec_x : begin
  268. consume(LKLAMMER);
  269. in_args:=true;
  270. p1:=comp_expr(true);
  271. p2:=gencallparanode(p1,nil);
  272. Must_be_valid:=false;
  273. if token=COMMA then
  274. begin
  275. consume(COMMA);
  276. p1:=comp_expr(true);
  277. p2:=gencallparanode(p1,p2);
  278. end;
  279. statement_syssym:=geninlinenode(l,p2);
  280. consume(RKLAMMER);
  281. pd:=voiddef;
  282. end;
  283. in_concat_x : begin
  284. consume(LKLAMMER);
  285. in_args:=true;
  286. p2:=nil;
  287. while true do
  288. begin
  289. p1:=comp_expr(true);
  290. Must_be_valid:=true;
  291. do_firstpass(p1);
  292. if not((p1^.resulttype^.deftype=stringdef) or
  293. ((p1^.resulttype^.deftype=orddef) and
  294. (porddef(p1^.resulttype)^.typ=uchar)
  295. )
  296. ) then Message(parser_e_illegal_parameter_list);
  297. if p2<>nil then
  298. p2:=gennode(addn,p2,p1)
  299. else p2:=p1;
  300. if token=COMMA then
  301. consume(COMMA)
  302. else break;
  303. end;
  304. consume(RKLAMMER);
  305. pd:=cstringdef;
  306. statement_syssym:=p2;
  307. end;
  308. in_read_x,
  309. in_readln_x : begin
  310. if token=LKLAMMER then
  311. begin
  312. consume(LKLAMMER);
  313. in_args:=true;
  314. Must_be_valid:=false;
  315. paras:=parse_paras(false,false);
  316. consume(RKLAMMER);
  317. end
  318. else
  319. paras:=nil;
  320. pd:=voiddef;
  321. p1:=geninlinenode(l,paras);
  322. do_firstpass(p1);
  323. statement_syssym := p1;
  324. end;
  325. in_write_x,
  326. in_writeln_x : begin
  327. if token=LKLAMMER then
  328. begin
  329. consume(LKLAMMER);
  330. in_args:=true;
  331. Must_be_valid:=true;
  332. paras:=parse_paras(true,false);
  333. consume(RKLAMMER);
  334. end
  335. else
  336. paras:=nil;
  337. pd:=voiddef;
  338. p1 := geninlinenode(l,paras);
  339. do_firstpass(p1);
  340. statement_syssym := p1;
  341. end;
  342. in_str_x_string : begin
  343. consume(LKLAMMER);
  344. in_args:=true;
  345. paras:=parse_paras(true,false);
  346. consume(RKLAMMER);
  347. p1 := geninlinenode(l,paras);
  348. do_firstpass(p1);
  349. statement_syssym := p1;
  350. pd:=voiddef;
  351. end;
  352. in_include_x_y,
  353. in_exclude_x_y:
  354. begin
  355. consume(LKLAMMER);
  356. in_args:=true;
  357. p1:=comp_expr(true);
  358. Must_be_valid:=false;
  359. consume(COMMA);
  360. p2:=comp_expr(true);
  361. { just a bit lisp feeling }
  362. statement_syssym:=geninlinenode(l,
  363. gencallparanode(p1,gencallparanode(p2,nil)));
  364. consume(RKLAMMER);
  365. pd:=voiddef;
  366. end;
  367. {in_val_x : begin
  368. consume(LKLAMMER);
  369. paras:=parse_paras(false);
  370. consume(RKLAMMER);
  371. p1 := geninlinenode(l,paras);
  372. do_firstpass(p1);
  373. statement_syssym := p1;
  374. pd:=voiddef;
  375. end; }
  376. else internalerror(15);
  377. end;
  378. in_args:=prev_in_args;
  379. Must_be_valid:=Store_valid;
  380. end;
  381. { reads the parameter for a subroutine call }
  382. procedure do_proc_call(getaddr : boolean;var again : boolean;var p1:Ptree;var pd:Pdef);
  383. var
  384. prev_in_args : boolean;
  385. prevafterassn : boolean;
  386. begin
  387. prev_in_args:=in_args;
  388. prevafterassn:=afterassignment;
  389. afterassignment:=false;
  390. { want we only determine the address of }
  391. { a subroutine }
  392. if not(getaddr) then
  393. begin
  394. if token=LKLAMMER then
  395. begin
  396. consume(LKLAMMER);
  397. in_args:=true;
  398. p1^.left:=parse_paras(false,false);
  399. consume(RKLAMMER);
  400. end
  401. else p1^.left:=nil;
  402. { do firstpass because we need the }
  403. { result type }
  404. do_firstpass(p1);
  405. end
  406. else
  407. begin
  408. { address operator @: }
  409. p1^.left:=nil;
  410. { forget pd }
  411. pd:=nil;
  412. { no postfix operators }
  413. again:=false;
  414. end;
  415. pd:=p1^.resulttype;
  416. in_args:=prev_in_args;
  417. afterassignment:=prevafterassn;
  418. end;
  419. { the following procedure handles the access to a property symbol }
  420. procedure handle_propertysym(sym : psym;var p1 : ptree;
  421. var pd : pdef);
  422. var
  423. paras : ptree;
  424. p2 : ptree;
  425. begin
  426. paras:=nil;
  427. { property parameters? }
  428. if token=LECKKLAMMER then
  429. begin
  430. consume(LECKKLAMMER);
  431. paras:=parse_paras(false,true);
  432. consume(RECKKLAMMER);
  433. end;
  434. { indexed property }
  435. if (ppropertysym(sym)^.options and ppo_indexed)<>0 then
  436. begin
  437. p2:=genordinalconstnode(ppropertysym(sym)^.index,s32bitdef);
  438. paras:=gencallparanode(p2,paras);
  439. end;
  440. if not(afterassignment) and not(in_args) then
  441. begin
  442. { write property: }
  443. { no result }
  444. pd:=voiddef;
  445. if assigned(ppropertysym(sym)^.writeaccesssym) then
  446. begin
  447. if ppropertysym(sym)^.writeaccesssym^.typ=procsym then
  448. begin
  449. { generate the method call }
  450. p1:=genmethodcallnode(pprocsym(
  451. ppropertysym(sym)^.writeaccesssym),
  452. ppropertysym(sym)^.writeaccesssym^.owner,p1);
  453. { we know the procedure to call, so
  454. force the usage of that procedure }
  455. p1^.procdefinition:=pprocdef(ppropertysym(sym)^.writeaccessdef);
  456. p1^.left:=paras;
  457. consume(ASSIGNMENT);
  458. { read the expression }
  459. p2:=comp_expr(true);
  460. p1^.left:=gencallparanode(p2,p1^.left);
  461. end
  462. else if ppropertysym(sym)^.writeaccesssym^.typ=varsym then
  463. begin
  464. if assigned(paras) then
  465. message(parser_e_no_paras_allowed);
  466. { subscribed access? }
  467. if p1=nil then
  468. begin
  469. p1:=genloadnode(pvarsym(ppropertysym(sym)^.readaccesssym),
  470. ppropertysym(sym)^.readaccesssym^.owner);
  471. end
  472. else
  473. p1:=gensubscriptnode(pvarsym(
  474. ppropertysym(sym)^.readaccesssym),p1);
  475. consume(ASSIGNMENT);
  476. { read the expression }
  477. p2:=comp_expr(true);
  478. p1:=gennode(assignn,p1,p2);
  479. end
  480. else
  481. begin
  482. p1:=genzeronode(errorn);
  483. Message(parser_e_no_procedure_to_access_property);
  484. end;
  485. end
  486. else
  487. begin
  488. p1:=genzeronode(errorn);
  489. Message(parser_e_no_procedure_to_access_property);
  490. end;
  491. end
  492. else
  493. begin
  494. { read property: }
  495. pd:=ppropertysym(sym)^.proptype;
  496. if assigned(ppropertysym(sym)^.readaccesssym) then
  497. begin
  498. if ppropertysym(sym)^.readaccesssym^.typ=varsym then
  499. begin
  500. if assigned(paras) then
  501. message(parser_e_no_paras_allowed);
  502. { subscribed access? }
  503. if p1=nil then
  504. begin
  505. p1:=genloadnode(pvarsym(ppropertysym(sym)^.readaccesssym),
  506. ppropertysym(sym)^.readaccesssym^.owner);
  507. end
  508. else
  509. p1:=gensubscriptnode(pvarsym(
  510. ppropertysym(sym)^.readaccesssym),p1);
  511. end
  512. else if ppropertysym(sym)^.readaccesssym^.typ=procsym then
  513. begin
  514. { generate the method call }
  515. p1:=genmethodcallnode(pprocsym(
  516. ppropertysym(sym)^.readaccesssym),
  517. ppropertysym(sym)^.readaccesssym^.owner,p1);
  518. { we know the procedure to call, so
  519. force the usage of that procedure }
  520. p1^.procdefinition:=pprocdef(ppropertysym(sym)^.readaccessdef);
  521. { insert paras }
  522. p1^.left:=paras;
  523. { if we should be delphi compatible }
  524. { then force type conversion }
  525. { }
  526. { isn't neccessary, the result types }
  527. { have to match excatly }
  528. {if cs_delphi2_compatible in aktswitches then
  529. p1:=gentypeconvnode(p1,pd);
  530. }
  531. end
  532. else
  533. begin
  534. p1:=genzeronode(errorn);
  535. Message(sym_e_type_mismatch);
  536. end;
  537. end
  538. else
  539. begin
  540. { error, no function to read property }
  541. p1:=genzeronode(errorn);
  542. Message(parser_e_no_procedure_to_access_property);
  543. end;
  544. end;
  545. end;
  546. { the ID token has to be consumed before calling this function }
  547. procedure do_member_read(const sym : psym;var p1 : ptree;
  548. var pd : pdef;var again : boolean);
  549. var
  550. static_name : string;
  551. isclassref : boolean;
  552. begin
  553. if sym=nil then
  554. begin
  555. { pattern is still valid unless
  556. there is another ID just after the ID of sym }
  557. Message1(sym_e_id_no_member,pattern);
  558. disposetree(p1);
  559. p1:=genzeronode(errorn);
  560. { try to clean up }
  561. pd:=generrordef;
  562. again:=false;
  563. end
  564. else
  565. begin
  566. isclassref:=pd^.deftype=classrefdef;
  567. { check protected and private members }
  568. if ((sym^.properties and sp_private)<>0) and
  569. (pobjectdef(sym^.owner^.defowner)^.owner^.symtabletype=unitsymtable) then
  570. Message(parser_e_cant_access_private_member);
  571. { this is wrong protected should not be overwritten but
  572. can be called !! PM
  573. if ((sym^.properties and sp_protected)<>0) and
  574. (pobjectdef(pd)^.owner^.symtabletype=unitsymtable) then
  575. Message(parser_e_cant_access_protected_member); }
  576. { we assume, that only procsyms and varsyms are in an object }
  577. { symbol table, for classes, properties are allowed }
  578. case sym^.typ of
  579. procsym:
  580. begin
  581. p1:=genmethodcallnode(pprocsym(sym),srsymtable,p1);
  582. do_proc_call(false,again,p1,pd);
  583. { now we know the real method e.g. we can check for }
  584. { a class method }
  585. if isclassref and ((p1^.procdefinition^.options and (poclassmethod or poconstructor))=0) then
  586. Message(parser_e_only_class_methods_via_class_ref);
  587. end;
  588. varsym:
  589. begin
  590. if ((sym^.properties and sp_protected)<>0) and
  591. (pobjectdef(pd)^.owner^.symtabletype=unitsymtable) and
  592. not(afterassignment) and
  593. not(in_args) then
  594. Message(parser_e_cant_access_protected_member);
  595. if isclassref then
  596. Message(parser_e_only_class_methods_via_class_ref);
  597. if (sym^.properties and sp_static)<>0 then
  598. begin
  599. static_name:=lower(srsymtable^.name^)+'_'+sym^.name;
  600. getsym(static_name,true);
  601. disposetree(p1);
  602. p1:=genloadnode(pvarsym(srsym),srsymtable);
  603. end
  604. else
  605. p1:=gensubscriptnode(pvarsym(sym),p1);
  606. pd:=pvarsym(sym)^.definition;
  607. end;
  608. propertysym:
  609. begin
  610. if isclassref then
  611. Message(parser_e_only_class_methods_via_class_ref);
  612. handle_propertysym(sym,p1,pd);
  613. end;
  614. else internalerror(16);
  615. end;
  616. end;
  617. end;
  618. function factor(getaddr : boolean) : ptree;
  619. var
  620. l : longint;
  621. p1,p2,p3 : ptree;
  622. code : word;
  623. pd,pd2 : pdef;
  624. unit_specific, again : boolean;
  625. static_name : string;
  626. sym : pvarsym;
  627. classh : pobjectdef;
  628. d : bestreal;
  629. constset : pconstset;
  630. propsym : ppropertysym;
  631. oldp1 : ptree;
  632. filepos : tfileposinfo;
  633. procedure check_tokenpos;
  634. begin
  635. if (p1<>oldp1) then
  636. begin
  637. if assigned(p1) then
  638. set_tree_filepos(p1,filepos);
  639. oldp1:=p1;
  640. filepos:=tokenpos;
  641. end;
  642. end;
  643. { p1 and p2 must contain valid values }
  644. procedure postfixoperators;
  645. begin
  646. check_tokenpos;
  647. while again do
  648. begin
  649. case token of
  650. CARET:
  651. begin
  652. consume(CARET);
  653. if pd^.deftype<>pointerdef then
  654. begin
  655. { ^ as binary operator is a problem!!!! (FK) }
  656. again:=false;
  657. Message(cg_e_invalid_qualifier);
  658. disposetree(p1);
  659. p1:=genzeronode(errorn);
  660. end
  661. else
  662. begin
  663. p1:=gensinglenode(derefn,p1);
  664. pd:=ppointerdef(pd)^.definition;
  665. end;
  666. end;
  667. LECKKLAMMER:
  668. begin
  669. if (pd^.deftype=objectdef) and
  670. pobjectdef(pd)^.isclass then
  671. begin
  672. { default property }
  673. propsym:=search_default_property(pobjectdef(pd));
  674. if not(assigned(propsym)) then
  675. begin
  676. disposetree(p1);
  677. p1:=genzeronode(errorn);
  678. again:=false;
  679. message(parser_e_no_default_property_available);
  680. end
  681. else
  682. begin
  683. p1:=nil;
  684. handle_propertysym(propsym,p1,pd);
  685. end;
  686. end
  687. else
  688. begin
  689. consume(LECKKLAMMER);
  690. repeat
  691. if (pd^.deftype<>arraydef) and
  692. (pd^.deftype<>stringdef) and
  693. (pd^.deftype<>pointerdef) then
  694. begin
  695. Message(cg_e_invalid_qualifier);
  696. disposetree(p1);
  697. p1:=genzeronode(errorn);
  698. again:=false;
  699. end
  700. else if (pd^.deftype=pointerdef) then
  701. begin
  702. p2:=comp_expr(true);
  703. p1:=gennode(vecn,p1,p2);
  704. pd:=ppointerdef(pd)^.definition;
  705. end
  706. else
  707. begin
  708. p2:=comp_expr(true);
  709. {$ifdef i386}
  710. { support SEG:OFS for go32v2 Mem[] }
  711. if (target_info.target=target_GO32V2) and
  712. (p1^.treetype=loadn) and
  713. assigned(p1^.symtableentry) and
  714. assigned(p1^.symtableentry^.owner^.name) and
  715. (p1^.symtableentry^.owner^.name^='SYSTEM') and
  716. ((p1^.symtableentry^.name='MEM') or
  717. (p1^.symtableentry^.name='MEMW') or
  718. (p1^.symtableentry^.name='MEML')) then
  719. begin
  720. if (token=COLON) then
  721. begin
  722. consume(COLON);
  723. p3:=gennode(muln,genordinalconstnode($10,s32bitdef),p2);
  724. p2:=comp_expr(true);
  725. p2:=gennode(addn,p2,p3);
  726. p1:=gennode(vecn,p1,p2);
  727. p1^.memseg:=true;
  728. p1^.memindex:=true;
  729. end
  730. else
  731. begin
  732. p1:=gennode(vecn,p1,p2);
  733. p1^.memindex:=true;
  734. end;
  735. end
  736. else
  737. {$endif}
  738. p1:=gennode(vecn,p1,p2);
  739. if pd^.deftype=stringdef then
  740. pd:=cchardef
  741. else
  742. pd:=parraydef(pd)^.definition;
  743. end;
  744. if token=COMMA then consume(COMMA)
  745. else break;
  746. until false;
  747. consume(RECKKLAMMER);
  748. end;
  749. end;
  750. POINT:
  751. begin
  752. consume(POINT);
  753. case pd^.deftype of
  754. recorddef:
  755. begin
  756. sym:=pvarsym(precdef(pd)^.symtable^.search(pattern));
  757. consume(ID);
  758. if sym=nil then
  759. begin
  760. Message(sym_e_illegal_field);
  761. disposetree(p1);
  762. p1:=genzeronode(errorn);
  763. end
  764. else
  765. begin
  766. p1:=gensubscriptnode(sym,p1);
  767. pd:=sym^.definition;
  768. end;
  769. end;
  770. classrefdef:
  771. begin
  772. classh:=pobjectdef(pclassrefdef(pd)^.definition);
  773. sym:=nil;
  774. while assigned(classh) do
  775. begin
  776. sym:=pvarsym(classh^.publicsyms^.search(pattern));
  777. srsymtable:=classh^.publicsyms;
  778. if assigned(sym) then
  779. break;
  780. classh:=classh^.childof;
  781. end;
  782. consume(ID);
  783. do_member_read(sym,p1,pd,again);
  784. end;
  785. objectdef:
  786. begin
  787. classh:=pobjectdef(pd);
  788. sym:=nil;
  789. while assigned(classh) do
  790. begin
  791. sym:=pvarsym(classh^.publicsyms^.search(pattern));
  792. srsymtable:=classh^.publicsyms;
  793. if assigned(sym) then
  794. break;
  795. classh:=classh^.childof;
  796. end;
  797. consume(ID);
  798. do_member_read(sym,p1,pd,again);
  799. end;
  800. pointerdef:
  801. begin
  802. if ppointerdef(pd)^.definition^.deftype
  803. in [recorddef,objectdef,classrefdef] then
  804. begin
  805. Message(cg_e_invalid_qualifier);
  806. { exterror:=strpnew(' may be pointer deref ^ is missing');
  807. error(invalid_qualifizier); }
  808. Comment(V_hint,' may be pointer deref ^ is missing');
  809. end
  810. else
  811. Message(cg_e_invalid_qualifier);
  812. end
  813. else
  814. begin
  815. Message(cg_e_invalid_qualifier);
  816. disposetree(p1);
  817. p1:=genzeronode(errorn);
  818. end;
  819. end;
  820. end;
  821. else
  822. begin
  823. { is this a procedure variable ? }
  824. if assigned(pd) then
  825. begin
  826. if (pd^.deftype=procvardef) then
  827. begin
  828. if getprocvar then
  829. again:=false
  830. else
  831. if (token=LKLAMMER) or
  832. ((pprocvardef(pd)^.para1=nil) and
  833. (token<>ASSIGNMENT) and (not in_args)) then
  834. begin
  835. { do this in a strange way }
  836. { it's not a clean solution }
  837. p2:=p1;
  838. p1:=gencallnode(nil,
  839. nil);
  840. p1^.right:=p2;
  841. p1^.unit_specific:=unit_specific;
  842. if token=LKLAMMER then
  843. begin
  844. consume(LKLAMMER);
  845. p1^.left:=parse_paras(false,false);
  846. consume(RKLAMMER);
  847. end;
  848. pd:=pprocvardef(pd)^.retdef;
  849. p1^.resulttype:=pd;
  850. end
  851. else again:=false;
  852. p1^.resulttype:=pd;
  853. end
  854. else again:=false;
  855. end
  856. else again:=false;
  857. end;
  858. end;
  859. check_tokenpos;
  860. end;
  861. end;
  862. procedure do_set(p : pconstset;pos : longint);
  863. var
  864. l : longint;
  865. begin
  866. if (pos>255) or
  867. (pos<0) then
  868. Message(parser_e_illegal_set_expr);
  869. l:=pos div 8;
  870. { do we allow the same twice }
  871. if (p^[l] and (1 shl (pos mod 8)))<>0 then
  872. Message(parser_e_illegal_set_expr);
  873. p^[l]:=p^[l] or (1 shl (pos mod 8));
  874. end;
  875. {$ifdef TEST_FUNCRET}
  876. function is_func_ret(sym : psym) : boolean;
  877. var
  878. p : pprocinfo;
  879. begin
  880. p:=@procinfo;
  881. is_func_ret:=false;
  882. while assigned(p) do
  883. begin
  884. { is this an access to a function result ? }
  885. if assigned(aktprocsym) and
  886. ((sym^.name=aktprocsym^.name){ or
  887. ((pvarsym(srsym)=opsym) and
  888. ((p^.flags and pi_operator)<>0))}) and
  889. (p^.retdef<>pdef(voiddef)) and
  890. (token<>LKLAMMER) and
  891. (not ((cs_tp_compatible in aktswitches) and
  892. (afterassignment or in_args))) then
  893. begin
  894. p1:=genzeronode(funcretn);
  895. pd:=p^.retdef;
  896. p1^.funcretprocinfo:=p;
  897. p1^.retdef:=pd;
  898. is_func_ret:=true;
  899. exit;
  900. end;
  901. p:=p^.parent;
  902. end;
  903. end;
  904. {$endif TEST_FUNCRET}
  905. var
  906. possible_error : boolean;
  907. begin
  908. oldp1:=nil;
  909. filepos:=tokenpos;
  910. case token of
  911. ID:
  912. begin
  913. { allow post fix operators }
  914. again:=true;
  915. if (cs_delphi2_compatible in aktswitches) and
  916. (pattern='RESULT') and
  917. assigned(aktprocsym) and
  918. (procinfo.retdef<>pdef(voiddef)) then
  919. begin
  920. consume(ID);
  921. p1:=genzeronode(funcretn);
  922. pd:=procinfo.retdef;
  923. {$ifdef TEST_FUNCRET}
  924. p1^.funcretprocinfo:=pointer(@procinfo);
  925. p1^.retdef:=pd;
  926. {$endif TEST_FUNCRET}
  927. end
  928. else
  929. begin
  930. if lastsymknown then
  931. begin
  932. srsym:=lastsrsym;
  933. srsymtable:=lastsrsymtable;
  934. lastsymknown:=false;
  935. end
  936. else
  937. getsym(pattern,true);
  938. consume(ID);
  939. {$ifndef TEST_FUNCRET}
  940. { is this an access to a function result ? }
  941. if assigned(aktprocsym) and
  942. ((srsym^.name=aktprocsym^.name){ or
  943. ((pvarsym(srsym)=opsym) and
  944. ((procinfo.flags and pi_operator)<>0))}) and
  945. (procinfo.retdef<>pdef(voiddef)) and
  946. (token<>LKLAMMER) and
  947. (not ((cs_tp_compatible in aktswitches) and
  948. (afterassignment or in_args))) then
  949. begin
  950. p1:=genzeronode(funcretn);
  951. pd:=procinfo.retdef;
  952. end
  953. else
  954. {$else TEST_FUNCRET}
  955. if not is_func_ret(srsym) then
  956. {$endif TEST_FUNCRET}
  957. { else it's a normal symbol }
  958. begin
  959. if srsym^.typ=unitsym then
  960. begin
  961. consume(POINT);
  962. getsymonlyin(punitsym(srsym)^.unitsymtable,pattern);
  963. unit_specific:=true;
  964. consume(ID);
  965. end
  966. else
  967. unit_specific:=false;
  968. if not assigned(srsym) then
  969. Begin
  970. p1:=genzeronode(errorn);
  971. { try to clean up }
  972. pd:=generrordef;
  973. end
  974. else
  975. Begin
  976. { check semantics of private }
  977. if (srsym^.typ in [propertysym,procsym,varsym]) and
  978. (srsymtable^.symtabletype=objectsymtable) then
  979. begin
  980. if ((srsym^.properties and sp_private)<>0) and
  981. (pobjectdef(srsym^.owner^.defowner)^.
  982. owner^.symtabletype=unitsymtable) then
  983. Message(parser_e_cant_access_private_member);
  984. end;
  985. case srsym^.typ of
  986. absolutesym:
  987. begin
  988. p1:=genloadnode(pvarsym(srsym),srsymtable);
  989. pd:=pabsolutesym(srsym)^.definition;
  990. end;
  991. varsym:
  992. begin
  993. { are we in a class method ? }
  994. if (srsymtable^.symtabletype=objectsymtable) and
  995. assigned(aktprocsym) and
  996. ((aktprocsym^.definition^.options and poclassmethod)<>0) then
  997. Message(parser_e_only_class_methods);
  998. if (srsym^.properties and sp_static)<>0 then
  999. begin
  1000. static_name:=lower(srsymtable^.name^)+'_'+srsym^.name;
  1001. getsym(static_name,true);
  1002. end;
  1003. p1:=genloadnode(pvarsym(srsym),srsymtable);
  1004. if pvarsym(srsym)^.is_valid=0 then
  1005. begin
  1006. p1^.is_first := true;
  1007. { set special between first loaded
  1008. until checked in firstpass }
  1009. pvarsym(srsym)^.is_valid:=2;
  1010. end;
  1011. pd:=pvarsym(srsym)^.definition;
  1012. end;
  1013. typedconstsym:
  1014. begin
  1015. p1:=gentypedconstloadnode(ptypedconstsym(srsym),srsymtable);
  1016. pd:=ptypedconstsym(srsym)^.definition;
  1017. end;
  1018. syssym:
  1019. p1:=statement_syssym(psyssym(srsym)^.number,pd);
  1020. typesym:
  1021. begin
  1022. pd:=ptypesym(srsym)^.definition;
  1023. { if we read a type declaration }
  1024. { we have to return the type and }
  1025. { nothing else }
  1026. if block_type=bt_type then
  1027. begin
  1028. p1:=genzeronode(typen);
  1029. p1^.resulttype:=pd;
  1030. pd:=voiddef;
  1031. end
  1032. else
  1033. begin
  1034. if token=LKLAMMER then
  1035. begin
  1036. consume(LKLAMMER);
  1037. p1:=comp_expr(true);
  1038. consume(RKLAMMER);
  1039. p1:=gentypeconvnode(p1,pd);
  1040. p1^.explizit:=true;
  1041. end
  1042. else if (token=POINT) and
  1043. (pd^.deftype=objectdef) and
  1044. ((pobjectdef(pd)^.options and oois_class)=0) then
  1045. begin
  1046. consume(POINT);
  1047. if assigned(procinfo._class) then
  1048. begin
  1049. if procinfo._class^.isrelated(pobjectdef(pd)) then
  1050. begin
  1051. p1:=genzeronode(typen);
  1052. p1^.resulttype:=pd;
  1053. srsymtable:=pobjectdef(pd)^.publicsyms;
  1054. sym:=pvarsym(srsymtable^.search(pattern));
  1055. { search also in inherited methods }
  1056. while sym=nil do
  1057. begin
  1058. pd:=pobjectdef(pd)^.childof;
  1059. srsymtable:=pobjectdef(pd)^.publicsyms;
  1060. sym:=pvarsym(srsymtable^.search(pattern));
  1061. end;
  1062. consume(ID);
  1063. do_member_read(sym,p1,pd,again);
  1064. end
  1065. else
  1066. begin
  1067. Message(parser_e_no_super_class);
  1068. pd:=generrordef;
  1069. again:=false;
  1070. end;
  1071. end
  1072. else
  1073. begin
  1074. { allows @TObject.Load }
  1075. { also allows static methods and variables }
  1076. p1:=genzeronode(typen);
  1077. p1^.resulttype:=pd;
  1078. srsymtable:=pobjectdef(pd)^.publicsyms;
  1079. sym:=pvarsym(srsymtable^.search(pattern));
  1080. if not(getaddr) and
  1081. ((sym^.properties and sp_static)=0) then
  1082. Message(sym_e_only_static_in_static)
  1083. else
  1084. begin
  1085. consume(ID);
  1086. do_member_read(sym,p1,pd,again);
  1087. end;
  1088. end
  1089. end
  1090. else
  1091. begin
  1092. { class reference ? }
  1093. if (pd^.deftype=objectdef)
  1094. and ((pobjectdef(pd)^.options and oois_class)<>0) then
  1095. begin
  1096. p1:=genzeronode(typen);
  1097. p1^.resulttype:=pd;
  1098. pd:=new(pclassrefdef,init(pd));
  1099. p1:=gensinglenode(loadvmtn,p1);
  1100. p1^.resulttype:=pd;
  1101. end
  1102. else
  1103. begin
  1104. { generate a type node }
  1105. { (for typeof etc) }
  1106. p1:=genzeronode(typen);
  1107. p1^.resulttype:=pd;
  1108. pd:=voiddef;
  1109. end;
  1110. end;
  1111. end;
  1112. end;
  1113. enumsym:
  1114. begin
  1115. p1:=genenumnode(penumsym(srsym));
  1116. pd:=p1^.resulttype;
  1117. end;
  1118. constsym:
  1119. begin
  1120. case pconstsym(srsym)^.consttype of
  1121. constint:
  1122. p1:=genordinalconstnode(pconstsym(srsym)^.value,s32bitdef);
  1123. conststring:
  1124. p1:=genstringconstnode(pstring(pconstsym(srsym)^.value)^);
  1125. constchar:
  1126. p1:=genordinalconstnode(pconstsym(srsym)^.value,cchardef);
  1127. constreal:
  1128. p1:=genrealconstnode(pdouble(pconstsym(srsym)^.value)^);
  1129. constbool:
  1130. p1:=genordinalconstnode(pconstsym(srsym)^.value,booldef);
  1131. constseta:
  1132. p1:=gensetconstruktnode(pconstset(pconstsym(srsym)^.value),
  1133. psetdef(pconstsym(srsym)^.definition));
  1134. constord:
  1135. p1:=genordinalconstnode(pconstsym(srsym)^.value,
  1136. pconstsym(srsym)^.definition);
  1137. end;
  1138. pd:=p1^.resulttype;
  1139. end;
  1140. procsym:
  1141. begin
  1142. { are we in a class method ? }
  1143. possible_error:=(srsymtable^.symtabletype=objectsymtable) and
  1144. assigned(aktprocsym) and
  1145. ((aktprocsym^.definition^.options and poclassmethod)<>0);
  1146. p1:=gencallnode(pprocsym(srsym),srsymtable);
  1147. p1^.unit_specific:=unit_specific;
  1148. do_proc_call(getaddr,again,p1,pd);
  1149. if possible_error and
  1150. ((p1^.procdefinition^.options and poclassmethod)=0) then
  1151. Message(parser_e_only_class_methods);
  1152. end;
  1153. propertysym:
  1154. begin
  1155. { access to property in a method }
  1156. { are we in a class method ? }
  1157. if (srsymtable^.symtabletype=objectsymtable) and
  1158. assigned(aktprocsym) and
  1159. ((aktprocsym^.definition^.options and poclassmethod)<>0) then
  1160. Message(parser_e_only_class_methods);
  1161. { no method pointer }
  1162. p1:=nil;
  1163. handle_propertysym(srsym,p1,pd);
  1164. end;
  1165. errorsym:
  1166. begin
  1167. p1:=genzeronode(errorn);
  1168. pd:=generrordef;
  1169. if token=LKLAMMER then
  1170. begin
  1171. consume(LKLAMMER);
  1172. parse_paras(false,false);
  1173. consume(RKLAMMER);
  1174. end;
  1175. end;
  1176. else
  1177. begin
  1178. p1:=genzeronode(errorn);
  1179. pd:=generrordef;
  1180. Message(cg_e_illegal_expression);
  1181. end;
  1182. end; { end case }
  1183. end;
  1184. end;
  1185. end;
  1186. { handle post fix operators }
  1187. postfixoperators;
  1188. end;
  1189. _NEW : begin
  1190. consume(_NEW);
  1191. consume(LKLAMMER);
  1192. p1:=factor(false);
  1193. if p1^.treetype<>typen then
  1194. Message(sym_e_type_id_expected);
  1195. pd:=p1^.resulttype;
  1196. pd2:=pd;
  1197. if (pd^.deftype<>pointerdef) or
  1198. (ppointerdef(pd)^.definition^.deftype<>objectdef) then
  1199. begin
  1200. Message(parser_e_pointer_to_class_expected);
  1201. { if an error occurs, read til the end of the new }
  1202. { statement }
  1203. p1:=genzeronode(errorn);
  1204. l:=1;
  1205. while true do
  1206. begin
  1207. case token of
  1208. LKLAMMER : inc(l);
  1209. RKLAMMER : dec(l);
  1210. end;
  1211. consume(token);
  1212. if l=0 then
  1213. break;
  1214. end;
  1215. end
  1216. else
  1217. begin
  1218. disposetree(p1);
  1219. p1:=genzeronode(hnewn);
  1220. p1^.resulttype:=ppointerdef(pd)^.definition;
  1221. consume(COMMA);
  1222. afterassignment:=false;
  1223. { determines the current object defintion }
  1224. classh:=pobjectdef(ppointerdef(pd)^.definition);
  1225. { check for an abstract class }
  1226. if (classh^.options and oois_abstract)<>0 then
  1227. Message(sym_e_no_instance_of_abstract_object);
  1228. { search the constructor also in the symbol tables of }
  1229. { the parents }
  1230. { no constructor found }
  1231. sym:=nil;
  1232. while assigned(classh) do
  1233. begin
  1234. sym:=pvarsym(classh^.publicsyms^.search(pattern));
  1235. srsymtable:=classh^.publicsyms;
  1236. if assigned(sym) then
  1237. break;
  1238. classh:=classh^.childof;
  1239. end;
  1240. consume(ID);
  1241. do_member_read(sym,p1,pd,again);
  1242. if (p1^.treetype<>calln) or
  1243. (assigned(p1^.procdefinition) and
  1244. ((p1^.procdefinition^.options and poconstructor)=0)) then
  1245. Message(parser_e_expr_have_to_be_constructor_call);
  1246. p1:=gensinglenode(newn,p1);
  1247. { set the resulttype }
  1248. p1^.resulttype:=pd2;
  1249. consume(RKLAMMER);
  1250. end;
  1251. end;
  1252. _SELF:
  1253. begin
  1254. again:=true;
  1255. consume(_SELF);
  1256. if not assigned(procinfo._class) then
  1257. begin
  1258. p1:=genzeronode(errorn);
  1259. pd:=generrordef;
  1260. again:=false;
  1261. Message(parser_e_self_not_in_method);
  1262. end
  1263. else
  1264. begin
  1265. if (aktprocsym^.definition^.options and poclassmethod)<>0 then
  1266. begin
  1267. { self in class methods is a class reference type }
  1268. pd:=new(pclassrefdef,init(procinfo._class));
  1269. p1:=genselfnode(pd);
  1270. p1^.resulttype:=pd;
  1271. end
  1272. else
  1273. begin
  1274. p1:=genselfnode(procinfo._class);
  1275. p1^.resulttype:=procinfo._class;
  1276. end;
  1277. pd:=p1^.resulttype;
  1278. postfixoperators;
  1279. end;
  1280. end;
  1281. _INHERITED : begin
  1282. again:=true;
  1283. consume(_INHERITED);
  1284. if assigned(procinfo._class) then
  1285. begin
  1286. classh:=procinfo._class^.childof;
  1287. while assigned(classh) do
  1288. begin
  1289. srsymtable:=pobjectdef(classh)^.publicsyms;
  1290. sym:=pvarsym(srsymtable^.search(pattern));
  1291. if assigned(sym) then
  1292. begin
  1293. p1:=genzeronode(typen);
  1294. p1^.resulttype:=classh;
  1295. pd:=p1^.resulttype;
  1296. consume(ID);
  1297. do_member_read(sym,p1,pd,again);
  1298. break;
  1299. end;
  1300. classh:=classh^.childof;
  1301. end;
  1302. if classh=nil then
  1303. begin
  1304. Message1(sym_e_id_no_member,pattern);
  1305. again:=false;
  1306. pd:=generrordef;
  1307. p1:=genzeronode(errorn);
  1308. end;
  1309. end
  1310. else
  1311. Message(parser_e_generic_methods_only_in_methods);
  1312. postfixoperators;
  1313. end;
  1314. INTCONST : begin
  1315. valint(pattern,l,code);
  1316. if code<>0 then
  1317. begin
  1318. val(pattern,d,code);
  1319. if code<>0 then
  1320. begin
  1321. Message(cg_e_invalid_integer);
  1322. l:=1;
  1323. consume(INTCONST);
  1324. p1:=genordinalconstnode(l,s32bitdef);
  1325. end
  1326. else
  1327. begin
  1328. consume(INTCONST);
  1329. p1:=genrealconstnode(d);
  1330. end;
  1331. end
  1332. else
  1333. begin
  1334. consume(INTCONST);
  1335. p1:=genordinalconstnode(l,s32bitdef);
  1336. end;
  1337. end;
  1338. REALNUMBER : begin
  1339. val(pattern,d,code);
  1340. if code<>0 then
  1341. begin
  1342. Message(parser_e_error_in_real);
  1343. d:=1.0;
  1344. end;
  1345. consume(REALNUMBER);
  1346. p1:=genrealconstnode(d);
  1347. end;
  1348. { FILE and STRING can be also a type cast }
  1349. _STRING:
  1350. begin
  1351. pd:=stringtype;
  1352. consume(LKLAMMER);
  1353. p1:=comp_expr(true);
  1354. consume(RKLAMMER);
  1355. p1:=gentypeconvnode(p1,pd);
  1356. p1^.explizit:=true;
  1357. { handle postfix operators here e.g. string(a)[10] }
  1358. again:=true;
  1359. postfixoperators;
  1360. end;
  1361. _FILE:
  1362. begin
  1363. pd:=cfiledef;
  1364. consume(_FILE);
  1365. if token=LKLAMMER then
  1366. begin
  1367. consume(LKLAMMER);
  1368. p1:=comp_expr(true);
  1369. consume(RKLAMMER);
  1370. p1:=gentypeconvnode(p1,pd);
  1371. end
  1372. else
  1373. begin
  1374. p1:=genzeronode(typen);
  1375. p1^.resulttype:=pd;
  1376. end;
  1377. p1^.explizit:=true;
  1378. { handle postfix operators here e.g. string(a)[10] }
  1379. again:=true;
  1380. postfixoperators;
  1381. end;
  1382. CSTRING:
  1383. begin
  1384. p1:=genstringconstnode(pattern);
  1385. consume(CSTRING);
  1386. end;
  1387. CCHAR:
  1388. begin
  1389. p1:=genordinalconstnode(ord(pattern[1]),cchardef);
  1390. consume(CCHAR);
  1391. end;
  1392. KLAMMERAFFE : begin
  1393. consume(KLAMMERAFFE);
  1394. p1:=factor(true);
  1395. p1:=gensinglenode(addrn,p1);
  1396. end;
  1397. LKLAMMER : begin
  1398. consume(LKLAMMER);
  1399. p1:=comp_expr(true);
  1400. consume(RKLAMMER);
  1401. { it's not a good solution }
  1402. { but (a+b)^ makes some problems }
  1403. case token of
  1404. CARET,POINT,LECKKLAMMER:
  1405. begin
  1406. { we need the resulttype }
  1407. { of the expression in pd }
  1408. do_firstpass(p1);
  1409. pd:=p1^.resulttype;
  1410. again:=true;
  1411. postfixoperators;
  1412. end;
  1413. end;
  1414. end;
  1415. LECKKLAMMER : begin
  1416. consume(LECKKLAMMER);
  1417. new(constset);
  1418. for l:=0 to 31 do
  1419. constset^[l]:=0;
  1420. p2:=nil;
  1421. pd:=nil;
  1422. if token<>RECKKLAMMER then
  1423. while true do
  1424. begin
  1425. p1:=comp_expr(true);
  1426. do_firstpass(p1);
  1427. case p1^.treetype of
  1428. ordconstn : begin
  1429. if porddef(p1^.resulttype)=s32bitdef then
  1430. p1^.resulttype:=u8bitdef;
  1431. if pd=nil then
  1432. pd:=p1^.resulttype;
  1433. if not(is_equal(pd,p1^.resulttype)) then
  1434. Message(parser_e_typeconflict_in_set)
  1435. else
  1436. if token=POINTPOINT then
  1437. begin
  1438. consume(POINTPOINT);
  1439. p3:=comp_expr(true);
  1440. do_firstpass(p3);
  1441. if porddef(p3^.resulttype)=s32bitdef then
  1442. p3^.resulttype:=u8bitdef;
  1443. if not(is_equal(pd,p3^.resulttype)) then
  1444. Message(parser_e_typeconflict_in_set)
  1445. else
  1446. for l:=p1^.value to p3^.value do
  1447. do_set(constset,l);
  1448. disposetree(p3);
  1449. end
  1450. else
  1451. do_set(constset,p1^.value);
  1452. disposetree(p1);
  1453. end;
  1454. stringconstn : begin
  1455. if pd=nil then
  1456. pd:=cchardef;
  1457. if not(is_equal(pd,cchardef)) then
  1458. Message(parser_e_typeconflict_in_set)
  1459. else
  1460. for l:=1 to length(pstring(p1^.values)^) do
  1461. do_set(constset,ord(pstring(p1^.values)^[l]));
  1462. disposetree(p1);
  1463. end;
  1464. else
  1465. begin
  1466. if porddef(p1^.resulttype)=s32bitdef then
  1467. p1^.resulttype:=u8bitdef;
  1468. if pd=nil then
  1469. pd:=p1^.resulttype;
  1470. if not(is_equal(pd,p1^.resulttype)) then
  1471. Message(parser_e_typeconflict_in_set);
  1472. p2:=gennode(setelen,p1,p2);
  1473. end;
  1474. end;
  1475. if token=COMMA then
  1476. consume(COMMA)
  1477. else break;
  1478. end;
  1479. consume(RECKKLAMMER);
  1480. p1:=gensinglenode(setconstrn,p2);
  1481. { use smallset if emudef with max < 32
  1482. but allow sets for enumdefs with more than
  1483. 256 elements like tasmop !! }
  1484. if assigned(pd) and (pd^.deftype=enumdef) and (penumdef(pd)^.max<=255) then
  1485. p1^.resulttype:=new(psetdef,init(pd,penumdef(pd)^.max))
  1486. else
  1487. p1^.resulttype:=new(psetdef,init(pd,255));
  1488. p1^.constset:=constset;
  1489. end;
  1490. PLUS : begin
  1491. consume(PLUS);
  1492. p1:=factor(false);
  1493. end;
  1494. MINUS : begin
  1495. consume(MINUS);
  1496. p1:=factor(false);
  1497. p1:=gensinglenode(umminusn,p1);
  1498. end;
  1499. _NOT : begin
  1500. consume(_NOT);
  1501. p1:=factor(false);
  1502. p1:=gensinglenode(notn,p1);
  1503. end;
  1504. _TRUE : begin
  1505. consume(_TRUE);
  1506. p1:=genordinalconstnode(1,booldef);
  1507. end;
  1508. _FALSE : begin
  1509. consume(_FALSE);
  1510. p1:=genordinalconstnode(0,booldef);
  1511. end;
  1512. _NIL : begin
  1513. consume(_NIL);
  1514. p1:=genzeronode(niln);
  1515. end;
  1516. else
  1517. begin
  1518. p1:=genzeronode(errorn);
  1519. consume(token);
  1520. Message(cg_e_illegal_expression);
  1521. end;
  1522. end;
  1523. factor:=p1;
  1524. check_tokenpos;
  1525. end;
  1526. type Toperator_precedence=(opcompare,opaddition,opmultiply);
  1527. const tok2node:array[PLUS.._XOR] of Ttreetyp=
  1528. (addn,subn,muln,slashn,equaln,gtn,ltn,gten,lten,
  1529. isn,asn,inn,
  1530. symdifn,starstarn,nothingn,caretn,unequaln,nothingn,
  1531. nothingn,nothingn,nothingn,nothingn,nothingn,
  1532. nothingn,nothingn,nothingn,nothingn,nothingn,
  1533. nothingn,nothingn,nothingn,nothingn,nothingn,
  1534. nothingn,andn,nothingn,nothingn,nothingn,
  1535. nothingn,nothingn,nothingn,nothingn,nothingn,
  1536. nothingn,nothingn,divn,nothingn,nothingn,
  1537. nothingn,nothingn,nothingn,nothingn,nothingn,
  1538. nothingn,nothingn,nothingn,nothingn,nothingn,
  1539. nothingn,nothingn,nothingn,nothingn,nothingn,
  1540. modn,nothingn,nothingn,nothingn,nothingn,
  1541. nothingn,nothingn,orn,
  1542. nothingn,nothingn,nothingn,nothingn,nothingn,
  1543. nothingn,nothingn,shln,shrn,
  1544. nothingn,nothingn,nothingn,nothingn,nothingn,
  1545. nothingn,nothingn,nothingn,nothingn,nothingn,
  1546. nothingn,xorn);
  1547. operator_levels:array[Toperator_precedence] of set of Ttoken=
  1548. ([LT,LTE,GT,GTE,EQUAL,UNEQUAL,_IN,_IS],
  1549. [PLUS,MINUS,_OR,_XOR],
  1550. [CARET,SYMDIF,STARSTAR,STAR,SLASH,_DIV,_MOD,_AND,_SHL,_SHR,_AS]);
  1551. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):Ptree;
  1552. {Reads a subexpression while the operators are of the current precedence
  1553. level, or any higher level. Replaces the old term, simpl_expr and
  1554. simpl2_expr.}
  1555. var p1,p2:Ptree;
  1556. oldt:Ttoken;
  1557. filepos : tfileposinfo;
  1558. begin
  1559. { if pred_level=high(Toperator_precedence) then }
  1560. if pred_level=opmultiply then
  1561. p1:=factor(getprocvar)
  1562. else
  1563. p1:=sub_expr(succ(pred_level),true);
  1564. repeat
  1565. { aweful hack to support const a : 1..2=1; }
  1566. { disadvantage of tables :) FK }
  1567. if (token in operator_levels[pred_level]) and
  1568. ((token<>EQUAL) or accept_equal) then
  1569. begin
  1570. oldt:=token;
  1571. filepos:=tokenpos;
  1572. consume(token);
  1573. { if pred_level=high(Toperator_precedence) then }
  1574. if pred_level=opmultiply then
  1575. p2:=factor(getprocvar)
  1576. else
  1577. p2:=sub_expr(succ(pred_level),true);
  1578. p1:=gennode(tok2node[oldt],p1,p2);
  1579. set_tree_filepos(p1,filepos);
  1580. end
  1581. else
  1582. break;
  1583. until false;
  1584. sub_expr:=p1;
  1585. end;
  1586. function comp_expr(accept_equal : boolean):Ptree;
  1587. var
  1588. oldafterassignment : boolean;
  1589. begin
  1590. oldafterassignment:=afterassignment;
  1591. afterassignment:=true;
  1592. comp_expr:=sub_expr(opcompare,accept_equal);
  1593. afterassignment:=oldafterassignment;
  1594. end;
  1595. function expr : ptree;
  1596. var
  1597. p1,p2 : ptree;
  1598. oldafterassignment : boolean;
  1599. oldp1 : ptree;
  1600. filepos : tfileposinfo;
  1601. begin
  1602. oldafterassignment:=afterassignment;
  1603. p1:=sub_expr(opcompare,true);
  1604. if token in [ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  1605. afterassignment:=true;
  1606. filepos:=tokenpos;
  1607. oldp1:=p1;
  1608. case token of
  1609. POINTPOINT : begin
  1610. consume(POINTPOINT);
  1611. p2:=sub_expr(opcompare,true);
  1612. p1:=gennode(rangen,p1,p2);
  1613. end;
  1614. ASSIGNMENT : begin
  1615. consume(ASSIGNMENT);
  1616. { avoid a firstpass of a procedure if
  1617. it must be assigned to a procvar }
  1618. { should be recursive for a:=b:=c !!! }
  1619. if (p1^.resulttype<>nil) and (p1^.resulttype^.deftype=procvardef) then
  1620. getprocvar:=true;
  1621. p2:=sub_expr(opcompare,true);
  1622. if getprocvar and (p2^.treetype=calln) then
  1623. begin
  1624. p2^.treetype:=loadn;
  1625. p2^.resulttype:=pprocsym(p2^.symtableprocentry)^.definition;
  1626. p2^.symtableentry:=p2^.symtableprocentry;
  1627. end;
  1628. getprocvar:=false;
  1629. p1:=gennode(assignn,p1,p2);
  1630. end;
  1631. { this is the code for C like assignements }
  1632. { from an improvement of Peter Schaefer }
  1633. _PLUSASN : begin
  1634. consume(_PLUSASN );
  1635. p2:=sub_expr(opcompare,true);
  1636. p1:=gennode(assignn,p1,gennode(addn,getcopy(p1),p2));
  1637. { was first
  1638. p1:=gennode(assignn,p1,gennode(addn,p1,p2));
  1639. but disposetree assumes that we have a real
  1640. *** tree *** }
  1641. end;
  1642. _MINUSASN : begin
  1643. consume(_MINUSASN );
  1644. p2:=sub_expr(opcompare,true);
  1645. p1:=gennode(assignn,p1,gennode(subn,getcopy(p1),p2));
  1646. end;
  1647. _STARASN : begin
  1648. consume(_STARASN );
  1649. p2:=sub_expr(opcompare,true);
  1650. p1:=gennode(assignn,p1,gennode(muln,getcopy(p1),p2));
  1651. end;
  1652. _SLASHASN : begin
  1653. consume(_SLASHASN );
  1654. p2:=sub_expr(opcompare,true);
  1655. p1:=gennode(assignn,p1,gennode(slashn,getcopy(p1),p2));
  1656. end;
  1657. end;
  1658. afterassignment:=oldafterassignment;
  1659. if p1<>oldp1 then
  1660. set_tree_filepos(p1,filepos);
  1661. expr:=p1;
  1662. end;
  1663. function get_intconst:longint;
  1664. {Reads an expression, tries to evalute it and check if it is an integer
  1665. constant. Then the constant is returned.}
  1666. var p:Ptree;
  1667. begin
  1668. p:=comp_expr(true);
  1669. do_firstpass(p);
  1670. if (p^.treetype<>ordconstn) and
  1671. (p^.resulttype^.deftype=orddef) and
  1672. not (Porddef(p^.resulttype)^.typ in
  1673. [uvoid,uchar,bool8bit]) then
  1674. Message(cg_e_illegal_expression)
  1675. else
  1676. get_intconst:=p^.value;
  1677. disposetree(p);
  1678. end;
  1679. function get_stringconst:string;
  1680. {Reads an expression, tries to evaluate it and checks if it is a string
  1681. constant. Then the constant is returned.}
  1682. var p:Ptree;
  1683. begin
  1684. get_stringconst:='';
  1685. p:=comp_expr(true);
  1686. do_firstpass(p);
  1687. if p^.treetype<>stringconstn then
  1688. if (p^.treetype=ordconstn) and
  1689. (p^.resulttype^.deftype=orddef) and
  1690. (Porddef(p^.resulttype)^.typ=uchar) then
  1691. get_stringconst:=char(p^.value)
  1692. else
  1693. Message(cg_e_illegal_expression)
  1694. else
  1695. get_stringconst:=p^.values^;
  1696. disposetree(p);
  1697. end;
  1698. end.
  1699. {
  1700. $Log$
  1701. Revision 1.26 1998-06-09 16:01:46 pierre
  1702. + added procedure directive parsing for procvars
  1703. (accepted are popstack cdecl and pascal)
  1704. + added C vars with the following syntax
  1705. var C calias 'true_c_name';(can be followed by external)
  1706. reason is that you must add the Cprefix
  1707. which is target dependent
  1708. Revision 1.25 1998/06/05 14:37:33 pierre
  1709. * fixes for inline for operators
  1710. * inline procedure more correctly restricted
  1711. Revision 1.24 1998/06/04 23:51:52 peter
  1712. * m68k compiles
  1713. + .def file creation moved to gendef.pas so it could also be used
  1714. for win32
  1715. Revision 1.23 1998/06/04 09:55:40 pierre
  1716. * demangled name of procsym reworked to become independant of the mangling scheme
  1717. Come test_funcret improvements (not yet working)S: ----------------------------------------------------------------------
  1718. Revision 1.22 1998/06/02 17:03:03 pierre
  1719. * with node corrected for objects
  1720. * small bugs for SUPPORT_MMX fixed
  1721. Revision 1.21 1998/05/27 19:45:05 peter
  1722. * symtable.pas splitted into includefiles
  1723. * symtable adapted for $ifdef NEWPPU
  1724. Revision 1.20 1998/05/26 07:53:59 pierre
  1725. * bug fix for empty sets (nil pd was dereferenced )
  1726. Revision 1.19 1998/05/25 17:11:43 pierre
  1727. * firstpasscount bug fixed
  1728. now all is already set correctly the first time
  1729. under EXTDEBUG try -gp to skip all other firstpasses
  1730. it works !!
  1731. * small bug fixes
  1732. - for smallsets with -dTESTSMALLSET
  1733. - some warnings removed (by correcting code !)
  1734. Revision 1.18 1998/05/23 01:21:20 peter
  1735. + aktasmmode, aktoptprocessor, aktoutputformat
  1736. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  1737. + $LIBNAME to set the library name where the unit will be put in
  1738. * splitted cgi386 a bit (codeseg to large for bp7)
  1739. * nasm, tasm works again. nasm moved to ag386nsm.pas
  1740. Revision 1.17 1998/05/22 12:37:03 carl
  1741. * crash bugfix (patched msanually to main branch)
  1742. Revision 1.16 1998/05/21 19:33:32 peter
  1743. + better procedure directive handling and only one table
  1744. Revision 1.15 1998/05/20 09:42:35 pierre
  1745. + UseTokenInfo now default
  1746. * unit in interface uses and implementation uses gives error now
  1747. * only one error for unknown symbol (uses lastsymknown boolean)
  1748. the problem came from the label code !
  1749. + first inlined procedures and function work
  1750. (warning there might be allowed cases were the result is still wrong !!)
  1751. * UseBrower updated gives a global list of all position of all used symbols
  1752. with switch -gb
  1753. Revision 1.14 1998/05/11 13:07:56 peter
  1754. + $ifdef NEWPPU for the new ppuformat
  1755. + $define GDB not longer required
  1756. * removed all warnings and stripped some log comments
  1757. * no findfirst/findnext anymore to remove smartlink *.o files
  1758. Revision 1.13 1998/05/06 08:38:45 pierre
  1759. * better position info with UseTokenInfo
  1760. UseTokenInfo greatly simplified
  1761. + added check for changed tree after first time firstpass
  1762. (if we could remove all the cases were it happen
  1763. we could skip all firstpass if firstpasscount > 1)
  1764. Only with ExtDebug
  1765. Revision 1.12 1998/05/05 12:05:42 florian
  1766. * problems with properties fixed
  1767. * crash fixed: i:=l when i and l are undefined, was a problem with
  1768. implementation of private/protected
  1769. Revision 1.11 1998/05/04 11:22:26 florian
  1770. * problem with DOM solved: it crashes when accessing a property in a method
  1771. Revision 1.10 1998/05/01 16:38:45 florian
  1772. * handling of private and protected fixed
  1773. + change_keywords_to_tp implemented to remove
  1774. keywords which aren't supported by tp
  1775. * break and continue are now symbols of the system unit
  1776. + widestring, longstring and ansistring type released
  1777. Revision 1.9 1998/04/29 10:33:58 pierre
  1778. + added some code for ansistring (not complete nor working yet)
  1779. * corrected operator overloading
  1780. * corrected nasm output
  1781. + started inline procedures
  1782. + added starstarn : use ** for exponentiation (^ gave problems)
  1783. + started UseTokenInfo cond to get accurate positions
  1784. Revision 1.8 1998/04/14 23:27:03 florian
  1785. + exclude/include with constant second parameter added
  1786. Revision 1.7 1998/04/09 23:02:15 florian
  1787. * small problems solved to get remake3 work
  1788. Revision 1.6 1998/04/09 22:16:35 florian
  1789. * problem with previous REGALLOC solved
  1790. * improved property support
  1791. Revision 1.5 1998/04/08 10:26:09 florian
  1792. * correct error handling of virtual constructors
  1793. * problem with new type declaration handling fixed
  1794. Revision 1.4 1998/04/07 22:45:05 florian
  1795. * bug0092, bug0115 and bug0121 fixed
  1796. + packed object/class/array
  1797. Revision 1.3 1998/04/07 13:19:46 pierre
  1798. * bugfixes for reset_gdb_info
  1799. in MEM parsing for go32v2
  1800. better external symbol creation
  1801. support for rhgdb.exe (lowercase file names)
  1802. }