pexpr.pas 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831
  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:=expr;
  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:=expr;
  71. p2:=gencallparanode(p1,p2);
  72. p2^.is_colon_para:=true;
  73. if token=COLON then
  74. begin
  75. consume(COLON);
  76. p1:=expr;
  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:=expr;
  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:=expr;
  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:=expr;
  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:=expr;
  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:=expr;
  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:=expr;
  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:=expr;
  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:=expr;
  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:=expr;
  271. p2:=gencallparanode(p1,nil);
  272. Must_be_valid:=false;
  273. if token=COMMA then
  274. begin
  275. consume(COMMA);
  276. p1:=expr;
  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:=expr;
  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:=expr;
  358. Must_be_valid:=false;
  359. consume(COMMA);
  360. p2:=expr;
  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. oldafterassignment : boolean;
  425. p2 : ptree;
  426. begin
  427. paras:=nil;
  428. { property parameters? }
  429. if token=LECKKLAMMER then
  430. begin
  431. consume(LECKKLAMMER);
  432. paras:=parse_paras(false,true);
  433. consume(RECKKLAMMER);
  434. end;
  435. { indexed property }
  436. if (ppropertysym(sym)^.options and ppo_indexed)<>0 then
  437. begin
  438. p2:=genordinalconstnode(ppropertysym(sym)^.index,s32bitdef);
  439. paras:=gencallparanode(p2,paras);
  440. end;
  441. if not(afterassignment) and not(in_args) then
  442. begin
  443. { write property: }
  444. { no result }
  445. pd:=voiddef;
  446. if assigned(ppropertysym(sym)^.writeaccesssym) then
  447. begin
  448. if ppropertysym(sym)^.writeaccesssym^.typ=procsym then
  449. begin
  450. { generate the method call }
  451. p1:=genmethodcallnode(pprocsym(
  452. ppropertysym(sym)^.writeaccesssym),
  453. ppropertysym(sym)^.writeaccesssym^.owner,p1);
  454. { we know the procedure to call, so
  455. force the usage of that procedure }
  456. p1^.procdefinition:=pprocdef(ppropertysym(sym)^.writeaccessdef);
  457. p1^.left:=paras;
  458. { to be on the save side }
  459. oldafterassignment:=afterassignment;
  460. consume(ASSIGNMENT);
  461. { read the expression }
  462. afterassignment:=true;
  463. p2:=expr;
  464. p1^.left:=gencallparanode(p2,p1^.left);
  465. afterassignment:=oldafterassignment;
  466. end
  467. else if ppropertysym(sym)^.writeaccesssym^.typ=varsym then
  468. begin
  469. if assigned(paras) then
  470. message(parser_e_no_paras_allowed);
  471. p1:=gensubscriptnode(pvarsym(
  472. ppropertysym(sym)^.readaccesssym),p1);
  473. { to be on the save side }
  474. oldafterassignment:=afterassignment;
  475. consume(ASSIGNMENT);
  476. { read the expression }
  477. afterassignment:=true;
  478. p2:=expr;
  479. p1:=gennode(assignn,p1,p2);
  480. afterassignment:=oldafterassignment;
  481. end
  482. else
  483. begin
  484. p1:=genzeronode(errorn);
  485. Message(parser_e_no_procedure_to_access_property);
  486. end;
  487. end
  488. else
  489. begin
  490. p1:=genzeronode(errorn);
  491. Message(parser_e_no_procedure_to_access_property);
  492. end;
  493. end
  494. else
  495. begin
  496. { read property: }
  497. pd:=ppropertysym(sym)^.proptype;
  498. if assigned(ppropertysym(sym)^.readaccesssym) then
  499. begin
  500. if ppropertysym(sym)^.readaccesssym^.typ=varsym then
  501. begin
  502. if assigned(paras) then
  503. message(parser_e_no_paras_allowed);
  504. p1:=gensubscriptnode(pvarsym(
  505. ppropertysym(sym)^.readaccesssym),p1);
  506. end
  507. else if ppropertysym(sym)^.readaccesssym^.typ=procsym then
  508. begin
  509. { generate the method call }
  510. p1:=genmethodcallnode(pprocsym(
  511. ppropertysym(sym)^.readaccesssym),
  512. ppropertysym(sym)^.readaccesssym^.owner,p1);
  513. { we know the procedure to call, so
  514. force the usage of that procedure }
  515. p1^.procdefinition:=pprocdef(ppropertysym(sym)^.readaccessdef);
  516. { insert paras }
  517. p1^.left:=paras;
  518. { if we should be delphi compatible }
  519. { then force type conversion }
  520. { }
  521. { isn't neccessary, the result types }
  522. { have to match excatly }
  523. {if cs_delphi2_compatible in aktswitches then
  524. p1:=gentypeconvnode(p1,pd);
  525. }
  526. end
  527. else
  528. begin
  529. p1:=genzeronode(errorn);
  530. Message(sym_e_type_mismatch);
  531. end;
  532. end
  533. else
  534. begin
  535. { error, no function to read property }
  536. p1:=genzeronode(errorn);
  537. Message(parser_e_no_procedure_to_access_property);
  538. end;
  539. end;
  540. end;
  541. { the ID token has to be consumed before calling this function }
  542. procedure do_member_read(const sym : psym;var p1 : ptree;
  543. var pd : pdef;var again : boolean);
  544. var
  545. static_name : string;
  546. isclassref : boolean;
  547. begin
  548. if sym=nil then
  549. begin
  550. Message(sym_e_id_no_member);
  551. disposetree(p1);
  552. p1:=genzeronode(errorn);
  553. { try to clean up }
  554. pd:=generrordef;
  555. again:=false;
  556. end
  557. else
  558. begin
  559. isclassref:=pd^.deftype=classrefdef;
  560. { check protected and private members }
  561. if ((sym^.properties and sp_private)<>0) and
  562. (pobjectdef(sym^.owner^.defowner)^.owner^.symtabletype=unitsymtable) then
  563. Message(parser_e_cant_access_private_member);
  564. if ((sym^.properties and sp_protected)<>0) and
  565. (pobjectdef(pd)^.owner^.symtabletype=unitsymtable) then
  566. Message(parser_e_cant_access_protected_member);
  567. { we assume, that only procsyms and varsyms are in an object }
  568. { symbol table, for classes, properties are allowed }
  569. case sym^.typ of
  570. procsym:
  571. begin
  572. p1:=genmethodcallnode(pprocsym(sym),srsymtable,p1);
  573. do_proc_call(false,again,p1,pd);
  574. { now we know the real method e.g. we can check for }
  575. { a class method }
  576. if isclassref and ((p1^.procdefinition^.options and (poclassmethod or poconstructor))=0) then
  577. Message(parser_e_only_class_methods_via_class_ref);
  578. end;
  579. varsym:
  580. begin
  581. if isclassref then
  582. Message(parser_e_only_class_methods_via_class_ref);
  583. if (sym^.properties and sp_static)<>0 then
  584. begin
  585. static_name:=lowercase(srsymtable^.name^)+'_'+sym^.name;
  586. getsym(static_name,true);
  587. disposetree(p1);
  588. p1:=genloadnode(pvarsym(srsym),srsymtable);
  589. end
  590. else
  591. p1:=gensubscriptnode(pvarsym(sym),p1);
  592. pd:=pvarsym(sym)^.definition;
  593. end;
  594. propertysym:
  595. begin
  596. if isclassref then
  597. Message(parser_e_only_class_methods_via_class_ref);
  598. handle_propertysym(sym,p1,pd);
  599. end;
  600. else internalerror(16);
  601. end;
  602. end;
  603. end;
  604. function factor(getaddr : boolean) : ptree;
  605. var
  606. l : longint;
  607. p1,p2,p3 : ptree;
  608. code : word;
  609. pd,pd2 : pdef;
  610. unit_specific, again : boolean;
  611. static_name : string;
  612. sym : pvarsym;
  613. classh : pobjectdef;
  614. d : bestreal;
  615. constset : pconstset;
  616. propsym : ppropertysym;
  617. { p1 and p2 must contain valid values }
  618. procedure postfixoperators;
  619. begin
  620. while again do
  621. begin
  622. case token of
  623. CARET:
  624. begin
  625. consume(CARET);
  626. if pd^.deftype<>pointerdef then
  627. begin
  628. { ^ as binary operator is a problem!!!! (FK) }
  629. again:=false;
  630. Message(cg_e_invalid_qualifier);
  631. disposetree(p1);
  632. p1:=genzeronode(errorn);
  633. end
  634. else
  635. begin
  636. p1:=gensinglenode(derefn,p1);
  637. pd:=ppointerdef(pd)^.definition;
  638. end;
  639. end;
  640. LECKKLAMMER:
  641. begin
  642. if (pd^.deftype=objectdef) and
  643. pobjectdef(pd)^.isclass then
  644. begin
  645. { default property }
  646. propsym:=search_default_property(pobjectdef(pd));
  647. if not(assigned(propsym)) then
  648. begin
  649. disposetree(p1);
  650. p1:=genzeronode(errorn);
  651. again:=false;
  652. message(parser_e_no_default_property_available);
  653. end
  654. else
  655. begin
  656. p1:=nil;
  657. handle_propertysym(propsym,p1,pd);
  658. end;
  659. end
  660. else
  661. begin
  662. consume(LECKKLAMMER);
  663. repeat
  664. if (pd^.deftype<>arraydef) and
  665. (pd^.deftype<>stringdef) and
  666. (pd^.deftype<>pointerdef) then
  667. begin
  668. Message(cg_e_invalid_qualifier);
  669. disposetree(p1);
  670. p1:=genzeronode(errorn);
  671. again:=false;
  672. end
  673. else if (pd^.deftype=pointerdef) then
  674. begin
  675. p2:=expr;
  676. p1:=gennode(vecn,p1,p2);
  677. pd:=ppointerdef(pd)^.definition;
  678. end
  679. else
  680. begin
  681. p2:=expr;
  682. { support SEG:OFS for go32v2 Mem[] }
  683. if (target_info.target=target_GO32V2) and
  684. (p1^.treetype=loadn) and
  685. assigned(p1^.symtableentry) and
  686. assigned(p1^.symtableentry^.owner^.name) and
  687. (p1^.symtableentry^.owner^.name^='SYSTEM') and
  688. ((p1^.symtableentry^.name='MEM') or
  689. (p1^.symtableentry^.name='MEMW') or
  690. (p1^.symtableentry^.name='MEML')) then
  691. begin
  692. if (token=COLON) then
  693. begin
  694. consume(COLON);
  695. p3:=gennode(muln,genordinalconstnode($10,s32bitdef),p2);
  696. p2:=expr;
  697. p2:=gennode(addn,p2,p3);
  698. p1:=gennode(vecn,p1,p2);
  699. p1^.memseg:=true;
  700. p1^.memindex:=true;
  701. end
  702. else
  703. begin
  704. p1:=gennode(vecn,p1,p2);
  705. p1^.memindex:=true;
  706. end;
  707. end
  708. { else
  709. if (target_info.target=target_GO32V2) and
  710. assigned(p1^.symtableentry) and
  711. assigned(p1^.symtableentry^.owner^.name) and
  712. (p1^.symtableentry^.owner^.name^='SYSTEM') and
  713. ((p1^.symtableentry^.name='PORT') or
  714. (p1^.symtableentry^.name='PORTW') or
  715. (p1^.symtableentry^.name='PORTL')) then
  716. begin
  717. p1:=gennode(vecn,p1,p2);
  718. p1^.portindex:=true;
  719. p
  720. end;
  721. end }
  722. else
  723. p1:=gennode(vecn,p1,p2);
  724. if pd^.deftype=stringdef then
  725. pd:=cchardef
  726. else
  727. pd:=parraydef(pd)^.definition;
  728. end;
  729. if token=COMMA then consume(COMMA)
  730. else break;
  731. until false;
  732. consume(RECKKLAMMER);
  733. end;
  734. end;
  735. POINT:
  736. begin
  737. consume(POINT);
  738. case pd^.deftype of
  739. recorddef:
  740. begin
  741. sym:=pvarsym(precdef(pd)^.symtable^.search(pattern));
  742. consume(ID);
  743. if sym=nil then
  744. begin
  745. Message(sym_e_illegal_field);
  746. disposetree(p1);
  747. p1:=genzeronode(errorn);
  748. end
  749. else
  750. begin
  751. p1:=gensubscriptnode(sym,p1);
  752. pd:=sym^.definition;
  753. end;
  754. end;
  755. classrefdef:
  756. begin
  757. classh:=pobjectdef(pclassrefdef(pd)^.definition);
  758. sym:=nil;
  759. while assigned(classh) do
  760. begin
  761. sym:=pvarsym(classh^.publicsyms^.search(pattern));
  762. srsymtable:=classh^.publicsyms;
  763. if assigned(sym) then
  764. break;
  765. classh:=classh^.childof;
  766. end;
  767. consume(ID);
  768. do_member_read(sym,p1,pd,again);
  769. end;
  770. objectdef:
  771. begin
  772. classh:=pobjectdef(pd);
  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. pointerdef:
  786. begin
  787. if ppointerdef(pd)^.definition^.deftype
  788. in [recorddef,objectdef,classrefdef] then
  789. begin
  790. Message(cg_e_invalid_qualifier);
  791. { exterror:=strpnew(' may be pointer deref ^ is missing');
  792. error(invalid_qualifizier); }
  793. Comment(V_hint,' may be pointer deref ^ is missing');
  794. end
  795. else
  796. Message(cg_e_invalid_qualifier);
  797. end
  798. else
  799. begin
  800. Message(cg_e_invalid_qualifier);
  801. disposetree(p1);
  802. p1:=genzeronode(errorn);
  803. end;
  804. end;
  805. end;
  806. else
  807. begin
  808. { is this a procedure variable ? }
  809. if assigned(pd) then
  810. begin
  811. if (pd^.deftype=procvardef) then
  812. begin
  813. if getprocvar then
  814. again:=false
  815. else
  816. if (token=LKLAMMER) or
  817. ((pprocvardef(pd)^.para1=nil) and
  818. (token<>ASSIGNMENT) and (not in_args)) then
  819. begin
  820. { do this in a strange way }
  821. { it's not a clean solution }
  822. p2:=p1;
  823. p1:=gencallnode(nil,
  824. nil);
  825. p1^.right:=p2;
  826. p1^.unit_specific:=unit_specific;
  827. if token=LKLAMMER then
  828. begin
  829. consume(LKLAMMER);
  830. p1^.left:=parse_paras(false,false);
  831. consume(RKLAMMER);
  832. end;
  833. pd:=pprocvardef(pd)^.retdef;
  834. p1^.resulttype:=pd;
  835. end
  836. else again:=false;
  837. p1^.resulttype:=pd;
  838. end
  839. else again:=false;
  840. end
  841. else again:=false;
  842. end;
  843. end;
  844. end;
  845. end;
  846. procedure do_set(p : pconstset;pos : longint);
  847. var
  848. l : longint;
  849. begin
  850. if (pos>255) or
  851. (pos<0) then
  852. Message(parser_e_illegal_set_expr);
  853. l:=pos div 8;
  854. { do we allow the same twice }
  855. if (p^[l] and (1 shl (pos mod 8)))<>0 then
  856. Message(parser_e_illegal_set_expr);
  857. p^[l]:=p^[l] or (1 shl (pos mod 8));
  858. end;
  859. var
  860. possible_error : boolean;
  861. storesymtablestack : psymtable;
  862. actprocsym : pprocsym;
  863. begin
  864. case token of
  865. ID:
  866. begin
  867. { allow post fix operators }
  868. again:=true;
  869. if (cs_delphi2_compatible in aktswitches) and
  870. (pattern='RESULT') and
  871. assigned(aktprocsym) and
  872. (procinfo.retdef<>pdef(voiddef)) then
  873. begin
  874. consume(ID);
  875. p1:=genzeronode(funcretn);
  876. pd:=procinfo.retdef;
  877. {$ifdef TEST_FUNCRET}
  878. p1^.funcretprocinfo:=pointer(@procinfo);
  879. p1^.retdef:=pd;
  880. {$endif TEST_FUNCRET}
  881. end
  882. else
  883. begin
  884. getsym(pattern,true);
  885. consume(ID);
  886. { is this an access to a function result ? }
  887. if assigned(aktprocsym) and
  888. ((srsym^.name=aktprocsym^.name) or
  889. ((pvarsym(srsym)=opsym) and
  890. ((procinfo.flags and pi_operator)<>0))) and
  891. (procinfo.retdef<>pdef(voiddef)) and
  892. (token<>LKLAMMER) and
  893. (not ((cs_tp_compatible in aktswitches) and
  894. (afterassignment or in_args))) then
  895. begin
  896. p1:=genzeronode(funcretn);
  897. pd:=procinfo.retdef;
  898. {$ifdef TEST_FUNCRET}
  899. p1^.funcretprocinfo:=pointer(@procinfo);
  900. p1^.retdef:=pd;
  901. {$endif TEST_FUNCRET}
  902. end
  903. else
  904. { else it's a normal symbol }
  905. begin
  906. if srsym^.typ=unitsym then
  907. begin
  908. consume(POINT);
  909. getsymonlyin(punitsym(srsym)^.unitsymtable,pattern);
  910. unit_specific:=true;
  911. consume(ID);
  912. end
  913. else
  914. unit_specific:=false;
  915. { check semantics of private }
  916. if srsymtable^.symtabletype=objectsymtable then
  917. begin
  918. if ((srsym^.properties and sp_private)<>0) and
  919. (pobjectdef(srsym^.owner^.defowner)^.
  920. owner^.symtabletype=unitsymtable) then
  921. Message(parser_e_cant_access_private_member);
  922. end;
  923. case srsym^.typ of
  924. absolutesym:
  925. begin
  926. p1:=genloadnode(pvarsym(srsym),srsymtable);
  927. pd:=pabsolutesym(srsym)^.definition;
  928. end;
  929. varsym:
  930. begin
  931. { are we in a class method ? }
  932. if (srsymtable^.symtabletype=objectsymtable) and
  933. assigned(aktprocsym) and
  934. ((aktprocsym^.definition^.options and poclassmethod)<>0) then
  935. Message(parser_e_only_class_methods);
  936. if (srsym^.properties and sp_static)<>0 then
  937. begin
  938. static_name:=lowercase(srsymtable^.name^)+'_'+srsym^.name;
  939. getsym(static_name,true);
  940. end;
  941. p1:=genloadnode(pvarsym(srsym),srsymtable);
  942. if pvarsym(srsym)^.is_valid=0 then
  943. begin
  944. p1^.is_first := true;
  945. { set special between first loaded
  946. until checked in firstpass }
  947. pvarsym(srsym)^.is_valid:=2;
  948. end;
  949. pd:=pvarsym(srsym)^.definition;
  950. end;
  951. typedconstsym:
  952. begin
  953. p1:=gentypedconstloadnode(ptypedconstsym(srsym),srsymtable);
  954. pd:=ptypedconstsym(srsym)^.definition;
  955. end;
  956. syssym:
  957. p1:=statement_syssym(psyssym(srsym)^.number,pd);
  958. typesym:
  959. begin
  960. pd:=ptypesym(srsym)^.definition;
  961. { if we read a type declaration }
  962. { we have to return the type and }
  963. { nothing else }
  964. if block_type=bt_type then
  965. begin
  966. p1:=genzeronode(typen);
  967. p1^.resulttype:=pd;
  968. pd:=voiddef;
  969. end
  970. else
  971. begin
  972. if token=LKLAMMER then
  973. begin
  974. consume(LKLAMMER);
  975. p1:=expr;
  976. consume(RKLAMMER);
  977. p1:=gentypeconvnode(p1,pd);
  978. p1^.explizit:=true;
  979. end
  980. else if (token=POINT) and
  981. (pd^.deftype=objectdef) and
  982. ((pobjectdef(pd)^.options and oois_class)=0) then
  983. begin
  984. consume(POINT);
  985. if assigned(procinfo._class) then
  986. begin
  987. if procinfo._class^.isrelated(pobjectdef(pd)) then
  988. begin
  989. p1:=genzeronode(typen);
  990. p1^.resulttype:=pd;
  991. srsymtable:=pobjectdef(pd)^.publicsyms;
  992. sym:=pvarsym(srsymtable^.search(pattern));
  993. consume(ID);
  994. do_member_read(sym,p1,pd,again);
  995. end
  996. else
  997. begin
  998. Message(parser_e_no_super_class);
  999. pd:=generrordef;
  1000. again:=false;
  1001. end;
  1002. end
  1003. else
  1004. begin
  1005. { allows @TObject.Load }
  1006. { also allows static methods and variables }
  1007. p1:=genzeronode(typen);
  1008. p1^.resulttype:=pd;
  1009. srsymtable:=pobjectdef(pd)^.publicsyms;
  1010. sym:=pvarsym(srsymtable^.search(pattern));
  1011. if not(getaddr) and
  1012. ((sym^.properties and sp_static)=0) then
  1013. Message(sym_e_only_static_in_static)
  1014. else
  1015. begin
  1016. consume(ID);
  1017. do_member_read(sym,p1,pd,again);
  1018. end;
  1019. end
  1020. end
  1021. else
  1022. begin
  1023. { class reference ? }
  1024. if (pd^.deftype=objectdef)
  1025. and ((pobjectdef(pd)^.options and oois_class)<>0) then
  1026. begin
  1027. p1:=genzeronode(typen);
  1028. p1^.resulttype:=pd;
  1029. pd:=new(pclassrefdef,init(pd));
  1030. p1:=gensinglenode(loadvmtn,p1);
  1031. p1^.resulttype:=pd;
  1032. end
  1033. else
  1034. begin
  1035. { generate a type node }
  1036. { (for typeof etc) }
  1037. p1:=genzeronode(typen);
  1038. p1^.resulttype:=pd;
  1039. pd:=voiddef;
  1040. end;
  1041. end;
  1042. end;
  1043. end;
  1044. enumsym:
  1045. begin
  1046. p1:=genenumnode(penumsym(srsym));
  1047. pd:=p1^.resulttype;
  1048. end;
  1049. constsym:
  1050. begin
  1051. case pconstsym(srsym)^.consttype of
  1052. constint:
  1053. p1:=genordinalconstnode(pconstsym(srsym)^.value,s32bitdef);
  1054. conststring:
  1055. p1:=genstringconstnode(pstring(pconstsym(srsym)^.value)^);
  1056. constchar:
  1057. p1:=genordinalconstnode(pconstsym(srsym)^.value,cchardef);
  1058. constreal:
  1059. p1:=genrealconstnode(pdouble(pconstsym(srsym)^.value)^);
  1060. constbool:
  1061. p1:=genordinalconstnode(pconstsym(srsym)^.value,booldef);
  1062. constseta:
  1063. p1:=gensetconstruktnode(pconstset(pconstsym(srsym)^.value),
  1064. psetdef(pconstsym(srsym)^.definition));
  1065. constord:
  1066. p1:=genordinalconstnode(pconstsym(srsym)^.value,
  1067. pconstsym(srsym)^.definition);
  1068. end;
  1069. pd:=p1^.resulttype;
  1070. end;
  1071. procsym:
  1072. begin
  1073. { are we in a class method ? }
  1074. possible_error:=(srsymtable^.symtabletype=objectsymtable) and
  1075. assigned(aktprocsym) and
  1076. ((aktprocsym^.definition^.options and poclassmethod)<>0);
  1077. p1:=gencallnode(pprocsym(srsym),srsymtable);
  1078. p1^.unit_specific:=unit_specific;
  1079. do_proc_call(getaddr,again,p1,pd);
  1080. if possible_error and
  1081. ((p1^.procdefinition^.options and poclassmethod)=0) then
  1082. Message(parser_e_only_class_methods);
  1083. end;
  1084. propertysym:
  1085. begin
  1086. { access to property in a method }
  1087. { are we in a class method ? }
  1088. if (srsymtable^.symtabletype=objectsymtable) and
  1089. assigned(aktprocsym) and
  1090. ((aktprocsym^.definition^.options and poclassmethod)<>0) then
  1091. Message(parser_e_only_class_methods);
  1092. { no method pointer }
  1093. p1:=nil;
  1094. handle_propertysym(srsym,p1,pd);
  1095. end;
  1096. errorsym:
  1097. begin
  1098. p1:=genzeronode(errorn);
  1099. pd:=generrordef;
  1100. if token=LKLAMMER then
  1101. begin
  1102. consume(LKLAMMER);
  1103. parse_paras(false,false);
  1104. consume(RKLAMMER);
  1105. end;
  1106. end;
  1107. else
  1108. begin
  1109. p1:=genzeronode(errorn);
  1110. pd:=generrordef;
  1111. Message(cg_e_illegal_expression);
  1112. end;
  1113. end;
  1114. end;
  1115. end;
  1116. { handle post fix operators }
  1117. postfixoperators;
  1118. end;
  1119. _NEW : begin
  1120. consume(_NEW);
  1121. consume(LKLAMMER);
  1122. p1:=factor(false);
  1123. if p1^.treetype<>typen then
  1124. Message(sym_e_type_id_expected);
  1125. pd:=p1^.resulttype;
  1126. pd2:=pd;
  1127. if (pd^.deftype<>pointerdef) or
  1128. (ppointerdef(pd)^.definition^.deftype<>objectdef) then
  1129. begin
  1130. Message(parser_e_pointer_to_class_expected);
  1131. { if an error occurs, read til the end of the new }
  1132. { statement }
  1133. p1:=genzeronode(errorn);
  1134. l:=1;
  1135. while true do
  1136. begin
  1137. case token of
  1138. LKLAMMER : inc(l);
  1139. RKLAMMER : dec(l);
  1140. end;
  1141. consume(token);
  1142. if l=0 then
  1143. break;
  1144. end;
  1145. end
  1146. else
  1147. begin
  1148. disposetree(p1);
  1149. p1:=genzeronode(hnewn);
  1150. p1^.resulttype:=ppointerdef(pd)^.definition;
  1151. consume(COMMA);
  1152. afterassignment:=false;
  1153. { determines the current object defintion }
  1154. classh:=pobjectdef(ppointerdef(pd)^.definition);
  1155. { check for an abstract class }
  1156. if (classh^.options and oois_abstract)<>0 then
  1157. Message(sym_e_no_instance_of_abstract_object);
  1158. { search the constructor also in the symbol tables of }
  1159. { the parents }
  1160. { no constructor found }
  1161. sym:=nil;
  1162. while assigned(classh) do
  1163. begin
  1164. sym:=pvarsym(classh^.publicsyms^.search(pattern));
  1165. srsymtable:=classh^.publicsyms;
  1166. if assigned(sym) then
  1167. break;
  1168. classh:=classh^.childof;
  1169. end;
  1170. consume(ID);
  1171. do_member_read(sym,p1,pd,again);
  1172. if (p1^.treetype<>calln) or
  1173. (assigned(p1^.procdefinition) and
  1174. ((p1^.procdefinition^.options and poconstructor)=0)) then
  1175. Message(parser_e_expr_have_to_be_constructor_call);
  1176. p1:=gensinglenode(newn,p1);
  1177. { set the resulttype }
  1178. p1^.resulttype:=pd2;
  1179. consume(RKLAMMER);
  1180. end;
  1181. end;
  1182. _SELF:
  1183. begin
  1184. again:=true;
  1185. consume(_SELF);
  1186. if not assigned(procinfo._class) then
  1187. begin
  1188. p1:=genzeronode(errorn);
  1189. pd:=generrordef;
  1190. again:=false;
  1191. Message(parser_e_self_not_in_method);
  1192. end
  1193. else
  1194. begin
  1195. if (aktprocsym^.definition^.options and poclassmethod)<>0 then
  1196. begin
  1197. { self in class methods is a class reference type }
  1198. pd:=new(pclassrefdef,init(procinfo._class));
  1199. p1:=genselfnode(pd);
  1200. p1^.resulttype:=pd;
  1201. end
  1202. else
  1203. begin
  1204. p1:=genselfnode(procinfo._class);
  1205. p1^.resulttype:=procinfo._class;
  1206. end;
  1207. pd:=p1^.resulttype;
  1208. postfixoperators;
  1209. end;
  1210. end;
  1211. _INHERITED : begin
  1212. again:=true;
  1213. consume(_INHERITED);
  1214. if assigned(procinfo._class) then
  1215. begin
  1216. classh:=procinfo._class^.childof;
  1217. while assigned(classh) do
  1218. begin
  1219. srsymtable:=pobjectdef(classh)^.publicsyms;
  1220. sym:=pvarsym(srsymtable^.search(pattern));
  1221. if assigned(sym) then
  1222. begin
  1223. p1:=genzeronode(typen);
  1224. p1^.resulttype:=classh;
  1225. pd:=p1^.resulttype;
  1226. consume(ID);
  1227. do_member_read(sym,p1,pd,again);
  1228. break;
  1229. end;
  1230. classh:=classh^.childof;
  1231. end;
  1232. if classh=nil then
  1233. begin
  1234. Message1(sym_e_id_no_member,pattern);
  1235. again:=false;
  1236. pd:=generrordef;
  1237. p1:=genzeronode(errorn);
  1238. end;
  1239. end
  1240. else
  1241. Message(parser_e_generic_methods_only_in_methods);
  1242. postfixoperators;
  1243. end;
  1244. INTCONST : begin
  1245. valint(pattern,l,code);
  1246. if code<>0 then
  1247. begin
  1248. val(pattern,d,code);
  1249. if code<>0 then
  1250. begin
  1251. Message(cg_e_invalid_integer);
  1252. l:=1;
  1253. consume(INTCONST);
  1254. p1:=genordinalconstnode(l,s32bitdef);
  1255. end
  1256. else
  1257. begin
  1258. consume(INTCONST);
  1259. p1:=genrealconstnode(d);
  1260. end;
  1261. end
  1262. else
  1263. begin
  1264. consume(INTCONST);
  1265. p1:=genordinalconstnode(l,s32bitdef);
  1266. end;
  1267. end;
  1268. REALNUMBER : begin
  1269. val(pattern,d,code);
  1270. if code<>0 then
  1271. begin
  1272. Message(parser_e_error_in_real);
  1273. d:=1.0;
  1274. end;
  1275. consume(REALNUMBER);
  1276. p1:=genrealconstnode(d);
  1277. end;
  1278. { FILE and STRING can be also a type cast }
  1279. _STRING:
  1280. begin
  1281. pd:=stringtype;
  1282. consume(LKLAMMER);
  1283. p1:=expr;
  1284. consume(RKLAMMER);
  1285. p1:=gentypeconvnode(p1,pd);
  1286. p1^.explizit:=true;
  1287. { handle postfix operators here e.g. string(a)[10] }
  1288. again:=true;
  1289. postfixoperators;
  1290. end;
  1291. _FILE:
  1292. begin
  1293. pd:=cfiledef;
  1294. consume(_FILE);
  1295. consume(LKLAMMER);
  1296. p1:=expr;
  1297. consume(RKLAMMER);
  1298. p1:=gentypeconvnode(p1,pd);
  1299. p1^.explizit:=true;
  1300. { handle postfix operators here e.g. string(a)[10] }
  1301. again:=true;
  1302. postfixoperators;
  1303. end;
  1304. CSTRING:
  1305. begin
  1306. p1:=genstringconstnode(pattern);
  1307. consume(CSTRING);
  1308. end;
  1309. CCHAR:
  1310. begin
  1311. p1:=genordinalconstnode(ord(pattern[1]),cchardef);
  1312. consume(CCHAR);
  1313. end;
  1314. KLAMMERAFFE : begin
  1315. consume(KLAMMERAFFE);
  1316. p1:=factor(true);
  1317. p1:=gensinglenode(addrn,p1);
  1318. end;
  1319. LKLAMMER : begin
  1320. consume(LKLAMMER);
  1321. p1:=expr;
  1322. consume(RKLAMMER);
  1323. { it's not a good solution }
  1324. { but (a+b)^ makes some problems }
  1325. case token of
  1326. CARET,POINT,LECKKLAMMER:
  1327. begin
  1328. { we need the resulttype }
  1329. { of the expression in pd }
  1330. do_firstpass(p1);
  1331. pd:=p1^.resulttype;
  1332. again:=true;
  1333. postfixoperators;
  1334. end;
  1335. end;
  1336. end;
  1337. LECKKLAMMER : begin
  1338. consume(LECKKLAMMER);
  1339. new(constset);
  1340. for l:=0 to 31 do
  1341. constset^[l]:=0;
  1342. p2:=nil;
  1343. pd:=nil;
  1344. if token<>RECKKLAMMER then
  1345. while true do
  1346. begin
  1347. p1:=expr;
  1348. do_firstpass(p1);
  1349. case p1^.treetype of
  1350. ordconstn : begin
  1351. if pd=nil then
  1352. pd:=p1^.resulttype;
  1353. if not(is_equal(pd,p1^.resulttype)) then
  1354. Message(parser_e_typeconflict_in_set)
  1355. else
  1356. do_set(constset,p1^.value);
  1357. disposetree(p1);
  1358. end;
  1359. rangen : begin
  1360. if pd=nil then
  1361. pd:=p1^.left^.resulttype;
  1362. if not(is_equal(pd,p1^.left^.resulttype)) then
  1363. Message(parser_e_typeconflict_in_set)
  1364. else
  1365. for l:=p1^.left^.value to p1^.right^.value do
  1366. do_set(constset,l);
  1367. disposetree(p1);
  1368. end;
  1369. stringconstn : begin
  1370. if pd=nil then
  1371. pd:=cchardef;
  1372. if not(is_equal(pd,cchardef)) then
  1373. Message(parser_e_typeconflict_in_set)
  1374. else
  1375. for l:=1 to length(pstring(p1^.values)^) do
  1376. do_set(constset,ord(pstring(p1^.values)^[l]));
  1377. disposetree(p1);
  1378. end;
  1379. else
  1380. begin
  1381. if pd=nil then
  1382. pd:=p1^.resulttype;
  1383. if not(is_equal(pd,p1^.resulttype)) then
  1384. Message(parser_e_typeconflict_in_set);
  1385. p2:=gennode(setelen,p1,p2);
  1386. end;
  1387. end;
  1388. if token=COMMA then
  1389. consume(COMMA)
  1390. else break;
  1391. end;
  1392. consume(RECKKLAMMER);
  1393. p1:=gensinglenode(setconstrn,p2);
  1394. p1^.resulttype:=new(psetdef,init(pd,255));
  1395. p1^.constset:=constset;
  1396. end;
  1397. PLUS : begin
  1398. consume(PLUS);
  1399. p1:=factor(false);
  1400. end;
  1401. MINUS : begin
  1402. consume(MINUS);
  1403. p1:=factor(false);
  1404. p1:=gensinglenode(umminusn,p1);
  1405. end;
  1406. _NOT : begin
  1407. consume(_NOT);
  1408. p1:=factor(false);
  1409. p1:=gensinglenode(notn,p1);
  1410. end;
  1411. _TRUE : begin
  1412. consume(_TRUE);
  1413. p1:=genordinalconstnode(1,booldef);
  1414. end;
  1415. _FALSE : begin
  1416. consume(_FALSE);
  1417. p1:=genordinalconstnode(0,booldef);
  1418. end;
  1419. _NIL : begin
  1420. consume(_NIL);
  1421. p1:=genzeronode(niln);
  1422. end;
  1423. else
  1424. begin
  1425. p1:=genzeronode(errorn);
  1426. consume(token);
  1427. Message(cg_e_illegal_expression);
  1428. end;
  1429. end;
  1430. factor:=p1;
  1431. end;
  1432. type Toperator_precedence=(opcompare,opaddition,opmultiply);
  1433. const tok2node:array[PLUS.._XOR] of Ttreetyp=
  1434. (addn,subn,muln,slashn,equaln,gtn,ltn,gten,lten,
  1435. isn,asn,inn,
  1436. symdifn,starstarn,nothingn,caretn,unequaln,nothingn,
  1437. nothingn,nothingn,nothingn,nothingn,nothingn,
  1438. nothingn,nothingn,nothingn,nothingn,nothingn,
  1439. nothingn,nothingn,nothingn,nothingn,nothingn,
  1440. nothingn,andn,nothingn,nothingn,nothingn,
  1441. nothingn,nothingn,nothingn,nothingn,nothingn,
  1442. nothingn,nothingn,divn,nothingn,nothingn,
  1443. nothingn,nothingn,nothingn,nothingn,nothingn,
  1444. nothingn,nothingn,nothingn,nothingn,nothingn,
  1445. nothingn,nothingn,nothingn,nothingn,nothingn,
  1446. modn,nothingn,nothingn,nothingn,nothingn,
  1447. nothingn,nothingn,orn,
  1448. nothingn,nothingn,nothingn,nothingn,nothingn,
  1449. nothingn,nothingn,shln,shrn,
  1450. nothingn,nothingn,nothingn,nothingn,nothingn,
  1451. nothingn,nothingn,nothingn,nothingn,nothingn,
  1452. nothingn,xorn);
  1453. operator_levels:array[Toperator_precedence] of set of Ttoken=
  1454. ([LT,LTE,GT,GTE,EQUAL,UNEQUAL,_IN,_IS],
  1455. [PLUS,MINUS,_OR,_XOR],
  1456. [CARET,SYMDIF,STARSTAR,STAR,SLASH,_DIV,_MOD,_AND,_SHL,_SHR,_AS]);
  1457. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):Ptree;
  1458. {Reads a subexpression while the operators are of the current precedence
  1459. level, or any higher level. Replaces the old term, simpl_expr and
  1460. simpl2_expr.}
  1461. var p1,p2:Ptree;
  1462. oldt:Ttoken;
  1463. begin
  1464. { if pred_level=high(Toperator_precedence) then }
  1465. if pred_level=opmultiply then
  1466. p1:=factor(getprocvar)
  1467. else
  1468. p1:=sub_expr(succ(pred_level),true);
  1469. repeat
  1470. { aweful hack to support const a : 1..2=1; }
  1471. { disadvantage of tables :) FK }
  1472. if (token in operator_levels[pred_level]) and
  1473. ((token<>EQUAL) or accept_equal) then
  1474. begin
  1475. oldt:=token;
  1476. consume(token);
  1477. { if pred_level=high(Toperator_precedence) then }
  1478. if pred_level=opmultiply then
  1479. p2:=factor(getprocvar)
  1480. else
  1481. p2:=sub_expr(succ(pred_level),true);
  1482. p1:=gennode(tok2node[oldt],p1,p2);
  1483. end
  1484. else
  1485. break;
  1486. until false;
  1487. sub_expr:=p1;
  1488. end;
  1489. function comp_expr(accept_equal : boolean):Ptree;
  1490. begin
  1491. comp_expr:=sub_expr(opcompare,accept_equal);
  1492. end;
  1493. function expr : ptree;
  1494. var
  1495. p1,p2 : ptree;
  1496. oldafterassignment : boolean;
  1497. begin
  1498. oldafterassignment:=afterassignment;
  1499. p1:=sub_expr(opcompare,true);
  1500. if token in [ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  1501. afterassignment:=true;
  1502. case token of
  1503. POINTPOINT : begin
  1504. consume(POINTPOINT);
  1505. p2:=sub_expr(opcompare,true);
  1506. p1:=gennode(rangen,p1,p2);
  1507. end;
  1508. ASSIGNMENT : begin
  1509. consume(ASSIGNMENT);
  1510. { avoid a firstpass of a procedure if
  1511. it must be assigned to a procvar }
  1512. { should be recursive for a:=b:=c !!! }
  1513. if (p1^.resulttype<>nil) and (p1^.resulttype^.deftype=procvardef) then
  1514. getprocvar:=true;
  1515. p2:=sub_expr(opcompare,true);
  1516. if getprocvar and (p2^.treetype=calln) then
  1517. begin
  1518. p2^.treetype:=loadn;
  1519. p2^.resulttype:=pprocsym(p2^.symtableprocentry)^.definition;
  1520. p2^.symtableentry:=p2^.symtableprocentry;
  1521. end;
  1522. getprocvar:=false;
  1523. p1:=gennode(assignn,p1,p2);
  1524. end;
  1525. { this is the code for C like assignements }
  1526. { from an improvement of Peter Schaefer }
  1527. _PLUSASN : begin
  1528. consume(_PLUSASN );
  1529. p2:=sub_expr(opcompare,true);
  1530. p1:=gennode(assignn,p1,gennode(addn,getcopy(p1),p2));
  1531. { was first
  1532. p1:=gennode(assignn,p1,gennode(addn,p1,p2));
  1533. but disposetree assumes that we have a real
  1534. *** tree *** }
  1535. end;
  1536. _MINUSASN : begin
  1537. consume(_MINUSASN );
  1538. p2:=sub_expr(opcompare,true);
  1539. p1:=gennode(assignn,p1,gennode(subn,getcopy(p1),p2));
  1540. end;
  1541. _STARASN : begin
  1542. consume(_STARASN );
  1543. p2:=sub_expr(opcompare,true);
  1544. p1:=gennode(assignn,p1,gennode(muln,getcopy(p1),p2));
  1545. end;
  1546. _SLASHASN : begin
  1547. consume(_SLASHASN );
  1548. p2:=sub_expr(opcompare,true);
  1549. p1:=gennode(assignn,p1,gennode(slashn,getcopy(p1),p2));
  1550. end;
  1551. end;
  1552. afterassignment:=oldafterassignment;
  1553. expr:=p1;
  1554. end;
  1555. function get_intconst:longint;
  1556. {Reads an expression, tries to evalute it and check if it is an integer
  1557. constant. Then the constant is returned.}
  1558. var p:Ptree;
  1559. begin
  1560. p:=expr;
  1561. do_firstpass(p);
  1562. if (p^.treetype<>ordconstn) and
  1563. (p^.resulttype^.deftype=orddef) and
  1564. not (Porddef(p^.resulttype)^.typ in
  1565. [uvoid,uchar,bool8bit]) then
  1566. Message(cg_e_illegal_expression)
  1567. else
  1568. get_intconst:=p^.value;
  1569. disposetree(p);
  1570. end;
  1571. function get_stringconst:string;
  1572. {Reads an expression, tries to evaluate it and checks if it is a string
  1573. constant. Then the constant is returned.}
  1574. var p:Ptree;
  1575. begin
  1576. get_stringconst:='';
  1577. p:=expr;
  1578. do_firstpass(p);
  1579. if p^.treetype<>stringconstn then
  1580. if (p^.treetype=ordconstn) and
  1581. (p^.resulttype^.deftype=orddef) and
  1582. (Porddef(p^.resulttype)^.typ=uchar) then
  1583. get_stringconst:=char(p^.value)
  1584. else
  1585. Message(cg_e_illegal_expression)
  1586. else
  1587. get_stringconst:=p^.values^;
  1588. disposetree(p);
  1589. end;
  1590. end.
  1591. {
  1592. $Log$
  1593. Revision 1.10 1998-05-01 16:38:45 florian
  1594. * handling of private and protected fixed
  1595. + change_keywords_to_tp implemented to remove
  1596. keywords which aren't supported by tp
  1597. * break and continue are now symbols of the system unit
  1598. + widestring, longstring and ansistring type released
  1599. Revision 1.9 1998/04/29 10:33:58 pierre
  1600. + added some code for ansistring (not complete nor working yet)
  1601. * corrected operator overloading
  1602. * corrected nasm output
  1603. + started inline procedures
  1604. + added starstarn : use ** for exponentiation (^ gave problems)
  1605. + started UseTokenInfo cond to get accurate positions
  1606. Revision 1.8 1998/04/14 23:27:03 florian
  1607. + exclude/include with constant second parameter added
  1608. Revision 1.7 1998/04/09 23:02:15 florian
  1609. * small problems solved to get remake3 work
  1610. Revision 1.6 1998/04/09 22:16:35 florian
  1611. * problem with previous REGALLOC solved
  1612. * improved property support
  1613. Revision 1.5 1998/04/08 10:26:09 florian
  1614. * correct error handling of virtual constructors
  1615. * problem with new type declaration handling fixed
  1616. Revision 1.4 1998/04/07 22:45:05 florian
  1617. * bug0092, bug0115 and bug0121 fixed
  1618. + packed object/class/array
  1619. Revision 1.3 1998/04/07 13:19:46 pierre
  1620. * bugfixes for reset_gdb_info
  1621. in MEM parsing for go32v2
  1622. better external symbol creation
  1623. support for rhgdb.exe (lowercase file names)
  1624. Revision 1.2 1998/03/26 11:18:31 florian
  1625. - switch -Sa removed
  1626. - support of a:=b:=0 removed
  1627. Revision 1.1.1.1 1998/03/25 11:18:14 root
  1628. * Restored version
  1629. Revision 1.26 1998/03/24 21:48:33 florian
  1630. * just a couple of fixes applied:
  1631. - problem with fixed16 solved
  1632. - internalerror 10005 problem fixed
  1633. - patch for assembler reading
  1634. - small optimizer fix
  1635. - mem is now supported
  1636. Revision 1.25 1998/03/21 23:59:39 florian
  1637. * indexed properties fixed
  1638. * ppu i/o of properties fixed
  1639. * field can be also used for write access
  1640. * overriding of properties
  1641. Revision 1.24 1998/03/16 22:42:21 florian
  1642. * some fixes of Peter applied:
  1643. ofs problem, profiler support
  1644. Revision 1.23 1998/03/11 11:23:57 florian
  1645. * bug0081 and bug0109 fixed
  1646. Revision 1.22 1998/03/10 16:27:42 pierre
  1647. * better line info in stabs debug
  1648. * symtabletype and lexlevel separated into two fields of tsymtable
  1649. + ifdef MAKELIB for direct library output, not complete
  1650. + ifdef CHAINPROCSYMS for overloaded seach across units, not fully
  1651. working
  1652. + ifdef TESTFUNCRET for setting func result in underfunction, not
  1653. working
  1654. Revision 1.21 1998/03/10 01:17:24 peter
  1655. * all files have the same header
  1656. * messages are fully implemented, EXTDEBUG uses Comment()
  1657. + AG... files for the Assembler generation
  1658. Revision 1.20 1998/03/06 00:52:44 peter
  1659. * replaced all old messages from errore.msg, only ExtDebug and some
  1660. Comment() calls are left
  1661. * fixed options.pas
  1662. Revision 1.19 1998/03/02 01:49:02 peter
  1663. * renamed target_DOS to target_GO32V1
  1664. + new verbose system, merged old errors and verbose units into one new
  1665. verbose.pas, so errors.pas is obsolete
  1666. Revision 1.18 1998/03/01 22:46:18 florian
  1667. + some win95 linking stuff
  1668. * a couple of bugs fixed:
  1669. bug0055,bug0058,bug0059,bug0064,bug0072,bug0093,bug0095,bug0098
  1670. Revision 1.17 1998/02/27 21:24:06 florian
  1671. * dll support changed (dll name can be also a string contants)
  1672. Revision 1.16 1998/02/24 00:19:17 peter
  1673. * makefile works again (btw. linux does like any char after a \ )
  1674. * removed circular unit with assemble and files
  1675. * fixed a sigsegv in pexpr
  1676. * pmodule init unit/program is the almost the same, merged them
  1677. Revision 1.15 1998/02/13 10:35:24 daniel
  1678. * Made Motorola version compilable.
  1679. * Fixed optimizer
  1680. Revision 1.14 1998/02/12 17:19:20 florian
  1681. * fixed to get remake3 work, but needs additional fixes (output, I don't like
  1682. also that aktswitches isn't a pointer)
  1683. Revision 1.13 1998/02/12 11:50:26 daniel
  1684. Yes! Finally! After three retries, my patch!
  1685. Changes:
  1686. Complete rewrite of psub.pas.
  1687. Added support for DLL's.
  1688. Compiler requires less memory.
  1689. Platform units for each platform.
  1690. Revision 1.12 1998/02/11 21:56:37 florian
  1691. * bugfixes: bug0093, bug0053, bug0088, bug0087, bug0089
  1692. Revision 1.11 1998/02/01 22:41:11 florian
  1693. * clean up
  1694. + system.assigned([class])
  1695. + system.assigned([class of xxxx])
  1696. * first fixes of as and is-operator
  1697. Revision 1.10 1998/02/01 15:04:15 florian
  1698. * better error recovering
  1699. * some clean up
  1700. Revision 1.9 1998/01/30 21:27:05 carl
  1701. * partial bugfix #88, #89 and typeof and other inline functions
  1702. (these bugs have a deeper nesting level, and therefore i only fixed
  1703. the parser crashes - there is also a tree crash).
  1704. Revision 1.8 1998/01/26 17:31:01 florian
  1705. * stupid bug with self in class methods fixed
  1706. Revision 1.7 1998/01/25 22:29:02 florian
  1707. * a lot bug fixes on the DOM
  1708. Revision 1.6 1998/01/23 10:46:41 florian
  1709. * small problems with FCL object model fixed, objpas?.inc is compilable
  1710. Revision 1.5 1998/01/16 22:34:42 michael
  1711. * Changed 'conversation' to 'conversion'. Waayyy too much chatting going on
  1712. in this compiler :)
  1713. Revision 1.4 1998/01/16 18:03:15 florian
  1714. * small bug fixes, some stuff of delphi styled constructores added
  1715. Revision 1.3 1998/01/13 23:11:14 florian
  1716. + class methods
  1717. Revision 1.2 1998/01/09 09:09:59 michael
  1718. + Initial implementation, second try
  1719. }