pexpr.pas 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900
  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. Message(sym_e_id_no_member);
  556. disposetree(p1);
  557. p1:=genzeronode(errorn);
  558. { try to clean up }
  559. pd:=generrordef;
  560. again:=false;
  561. end
  562. else
  563. begin
  564. isclassref:=pd^.deftype=classrefdef;
  565. { check protected and private members }
  566. if ((sym^.properties and sp_private)<>0) and
  567. (pobjectdef(sym^.owner^.defowner)^.owner^.symtabletype=unitsymtable) then
  568. Message(parser_e_cant_access_private_member);
  569. { this is wrong protected should not be overwritten but
  570. can be called !! PM
  571. if ((sym^.properties and sp_protected)<>0) and
  572. (pobjectdef(pd)^.owner^.symtabletype=unitsymtable) then
  573. Message(parser_e_cant_access_protected_member); }
  574. { we assume, that only procsyms and varsyms are in an object }
  575. { symbol table, for classes, properties are allowed }
  576. case sym^.typ of
  577. procsym:
  578. begin
  579. p1:=genmethodcallnode(pprocsym(sym),srsymtable,p1);
  580. do_proc_call(false,again,p1,pd);
  581. { now we know the real method e.g. we can check for }
  582. { a class method }
  583. if isclassref and ((p1^.procdefinition^.options and (poclassmethod or poconstructor))=0) then
  584. Message(parser_e_only_class_methods_via_class_ref);
  585. end;
  586. varsym:
  587. begin
  588. if ((sym^.properties and sp_protected)<>0) and
  589. (pobjectdef(pd)^.owner^.symtabletype=unitsymtable) and
  590. not(afterassignment) and
  591. not(in_args) then
  592. Message(parser_e_cant_access_protected_member);
  593. if isclassref then
  594. Message(parser_e_only_class_methods_via_class_ref);
  595. if (sym^.properties and sp_static)<>0 then
  596. begin
  597. static_name:=lower(srsymtable^.name^)+'_'+sym^.name;
  598. getsym(static_name,true);
  599. disposetree(p1);
  600. p1:=genloadnode(pvarsym(srsym),srsymtable);
  601. end
  602. else
  603. p1:=gensubscriptnode(pvarsym(sym),p1);
  604. pd:=pvarsym(sym)^.definition;
  605. end;
  606. propertysym:
  607. begin
  608. if isclassref then
  609. Message(parser_e_only_class_methods_via_class_ref);
  610. handle_propertysym(sym,p1,pd);
  611. end;
  612. else internalerror(16);
  613. end;
  614. end;
  615. end;
  616. function factor(getaddr : boolean) : ptree;
  617. var
  618. l : longint;
  619. p1,p2,p3 : ptree;
  620. code : word;
  621. pd,pd2 : pdef;
  622. unit_specific, again : boolean;
  623. static_name : string;
  624. sym : pvarsym;
  625. classh : pobjectdef;
  626. d : bestreal;
  627. constset : pconstset;
  628. propsym : ppropertysym;
  629. oldp1 : ptree;
  630. filepos : tfileposinfo;
  631. procedure check_tokenpos;
  632. begin
  633. if (p1<>oldp1) then
  634. begin
  635. if assigned(p1) then
  636. set_tree_filepos(p1,filepos);
  637. oldp1:=p1;
  638. filepos:=tokenpos;
  639. end;
  640. end;
  641. { p1 and p2 must contain valid values }
  642. procedure postfixoperators;
  643. begin
  644. check_tokenpos;
  645. while again do
  646. begin
  647. case token of
  648. CARET:
  649. begin
  650. consume(CARET);
  651. if pd^.deftype<>pointerdef then
  652. begin
  653. { ^ as binary operator is a problem!!!! (FK) }
  654. again:=false;
  655. Message(cg_e_invalid_qualifier);
  656. disposetree(p1);
  657. p1:=genzeronode(errorn);
  658. end
  659. else
  660. begin
  661. p1:=gensinglenode(derefn,p1);
  662. pd:=ppointerdef(pd)^.definition;
  663. end;
  664. end;
  665. LECKKLAMMER:
  666. begin
  667. if (pd^.deftype=objectdef) and
  668. pobjectdef(pd)^.isclass then
  669. begin
  670. { default property }
  671. propsym:=search_default_property(pobjectdef(pd));
  672. if not(assigned(propsym)) then
  673. begin
  674. disposetree(p1);
  675. p1:=genzeronode(errorn);
  676. again:=false;
  677. message(parser_e_no_default_property_available);
  678. end
  679. else
  680. begin
  681. p1:=nil;
  682. handle_propertysym(propsym,p1,pd);
  683. end;
  684. end
  685. else
  686. begin
  687. consume(LECKKLAMMER);
  688. repeat
  689. if (pd^.deftype<>arraydef) and
  690. (pd^.deftype<>stringdef) and
  691. (pd^.deftype<>pointerdef) then
  692. begin
  693. Message(cg_e_invalid_qualifier);
  694. disposetree(p1);
  695. p1:=genzeronode(errorn);
  696. again:=false;
  697. end
  698. else if (pd^.deftype=pointerdef) then
  699. begin
  700. p2:=comp_expr(true);
  701. p1:=gennode(vecn,p1,p2);
  702. pd:=ppointerdef(pd)^.definition;
  703. end
  704. else
  705. begin
  706. p2:=comp_expr(true);
  707. {$ifdef i386}
  708. { support SEG:OFS for go32v2 Mem[] }
  709. if (target_info.target=target_GO32V2) and
  710. (p1^.treetype=loadn) and
  711. assigned(p1^.symtableentry) and
  712. assigned(p1^.symtableentry^.owner^.name) and
  713. (p1^.symtableentry^.owner^.name^='SYSTEM') and
  714. ((p1^.symtableentry^.name='MEM') or
  715. (p1^.symtableentry^.name='MEMW') or
  716. (p1^.symtableentry^.name='MEML')) then
  717. begin
  718. if (token=COLON) then
  719. begin
  720. consume(COLON);
  721. p3:=gennode(muln,genordinalconstnode($10,s32bitdef),p2);
  722. p2:=comp_expr(true);
  723. p2:=gennode(addn,p2,p3);
  724. p1:=gennode(vecn,p1,p2);
  725. p1^.memseg:=true;
  726. p1^.memindex:=true;
  727. end
  728. else
  729. begin
  730. p1:=gennode(vecn,p1,p2);
  731. p1^.memindex:=true;
  732. end;
  733. end
  734. else
  735. {$endif}
  736. p1:=gennode(vecn,p1,p2);
  737. if pd^.deftype=stringdef then
  738. pd:=cchardef
  739. else
  740. pd:=parraydef(pd)^.definition;
  741. end;
  742. if token=COMMA then consume(COMMA)
  743. else break;
  744. until false;
  745. consume(RECKKLAMMER);
  746. end;
  747. end;
  748. POINT:
  749. begin
  750. consume(POINT);
  751. case pd^.deftype of
  752. recorddef:
  753. begin
  754. sym:=pvarsym(precdef(pd)^.symtable^.search(pattern));
  755. consume(ID);
  756. if sym=nil then
  757. begin
  758. Message(sym_e_illegal_field);
  759. disposetree(p1);
  760. p1:=genzeronode(errorn);
  761. end
  762. else
  763. begin
  764. p1:=gensubscriptnode(sym,p1);
  765. pd:=sym^.definition;
  766. end;
  767. end;
  768. classrefdef:
  769. begin
  770. classh:=pobjectdef(pclassrefdef(pd)^.definition);
  771. sym:=nil;
  772. while assigned(classh) do
  773. begin
  774. sym:=pvarsym(classh^.publicsyms^.search(pattern));
  775. srsymtable:=classh^.publicsyms;
  776. if assigned(sym) then
  777. break;
  778. classh:=classh^.childof;
  779. end;
  780. consume(ID);
  781. do_member_read(sym,p1,pd,again);
  782. end;
  783. objectdef:
  784. begin
  785. classh:=pobjectdef(pd);
  786. sym:=nil;
  787. while assigned(classh) do
  788. begin
  789. sym:=pvarsym(classh^.publicsyms^.search(pattern));
  790. srsymtable:=classh^.publicsyms;
  791. if assigned(sym) then
  792. break;
  793. classh:=classh^.childof;
  794. end;
  795. consume(ID);
  796. do_member_read(sym,p1,pd,again);
  797. end;
  798. pointerdef:
  799. begin
  800. if ppointerdef(pd)^.definition^.deftype
  801. in [recorddef,objectdef,classrefdef] then
  802. begin
  803. Message(cg_e_invalid_qualifier);
  804. { exterror:=strpnew(' may be pointer deref ^ is missing');
  805. error(invalid_qualifizier); }
  806. Comment(V_hint,' may be pointer deref ^ is missing');
  807. end
  808. else
  809. Message(cg_e_invalid_qualifier);
  810. end
  811. else
  812. begin
  813. Message(cg_e_invalid_qualifier);
  814. disposetree(p1);
  815. p1:=genzeronode(errorn);
  816. end;
  817. end;
  818. end;
  819. else
  820. begin
  821. { is this a procedure variable ? }
  822. if assigned(pd) then
  823. begin
  824. if (pd^.deftype=procvardef) then
  825. begin
  826. if getprocvar then
  827. again:=false
  828. else
  829. if (token=LKLAMMER) or
  830. ((pprocvardef(pd)^.para1=nil) and
  831. (token<>ASSIGNMENT) and (not in_args)) then
  832. begin
  833. { do this in a strange way }
  834. { it's not a clean solution }
  835. p2:=p1;
  836. p1:=gencallnode(nil,
  837. nil);
  838. p1^.right:=p2;
  839. p1^.unit_specific:=unit_specific;
  840. if token=LKLAMMER then
  841. begin
  842. consume(LKLAMMER);
  843. p1^.left:=parse_paras(false,false);
  844. consume(RKLAMMER);
  845. end;
  846. pd:=pprocvardef(pd)^.retdef;
  847. p1^.resulttype:=pd;
  848. end
  849. else again:=false;
  850. p1^.resulttype:=pd;
  851. end
  852. else again:=false;
  853. end
  854. else again:=false;
  855. end;
  856. end;
  857. check_tokenpos;
  858. end;
  859. end;
  860. procedure do_set(p : pconstset;pos : longint);
  861. var
  862. l : longint;
  863. begin
  864. if (pos>255) or
  865. (pos<0) then
  866. Message(parser_e_illegal_set_expr);
  867. l:=pos div 8;
  868. { do we allow the same twice }
  869. if (p^[l] and (1 shl (pos mod 8)))<>0 then
  870. Message(parser_e_illegal_set_expr);
  871. p^[l]:=p^[l] or (1 shl (pos mod 8));
  872. end;
  873. {$ifdef TEST_FUNCRET}
  874. function is_func_ret(sym : psym) : boolean;
  875. var
  876. p : pprocinfo;
  877. begin
  878. p:=@procinfo;
  879. is_func_ret:=false;
  880. while assigned(p) do
  881. begin
  882. { is this an access to a function result ? }
  883. if assigned(aktprocsym) and
  884. ((sym^.name=aktprocsym^.name) or
  885. ((pvarsym(srsym)=opsym) and
  886. ((p^.flags and pi_operator)<>0))) and
  887. (p^.retdef<>pdef(voiddef)) and
  888. (token<>LKLAMMER) and
  889. (not ((cs_tp_compatible in aktswitches) and
  890. (afterassignment or in_args))) then
  891. begin
  892. p1:=genzeronode(funcretn);
  893. pd:=p^.retdef;
  894. p1^.funcretprocinfo:=p;
  895. p1^.retdef:=pd;
  896. is_func_ret:=true;
  897. exit;
  898. end;
  899. p:=p^.parent;
  900. end;
  901. end;
  902. {$endif TEST_FUNCRET}
  903. var
  904. possible_error : boolean;
  905. begin
  906. oldp1:=nil;
  907. filepos:=tokenpos;
  908. case token of
  909. ID:
  910. begin
  911. { allow post fix operators }
  912. again:=true;
  913. if (cs_delphi2_compatible in aktswitches) and
  914. (pattern='RESULT') and
  915. assigned(aktprocsym) and
  916. (procinfo.retdef<>pdef(voiddef)) then
  917. begin
  918. consume(ID);
  919. p1:=genzeronode(funcretn);
  920. pd:=procinfo.retdef;
  921. {$ifdef TEST_FUNCRET}
  922. p1^.funcretprocinfo:=pointer(@procinfo);
  923. p1^.retdef:=pd;
  924. {$endif TEST_FUNCRET}
  925. end
  926. else
  927. begin
  928. if lastsymknown then
  929. begin
  930. srsym:=lastsrsym;
  931. srsymtable:=lastsrsymtable;
  932. lastsymknown:=false;
  933. end
  934. else
  935. getsym(pattern,true);
  936. consume(ID);
  937. {$ifndef TEST_FUNCRET}
  938. { is this an access to a function result ? }
  939. if assigned(aktprocsym) and
  940. ((srsym^.name=aktprocsym^.name) or
  941. ((pvarsym(srsym)=opsym) and
  942. ((procinfo.flags and pi_operator)<>0))) and
  943. (procinfo.retdef<>pdef(voiddef)) and
  944. (token<>LKLAMMER) and
  945. (not ((cs_tp_compatible in aktswitches) and
  946. (afterassignment or in_args))) then
  947. begin
  948. p1:=genzeronode(funcretn);
  949. pd:=procinfo.retdef;
  950. end
  951. else
  952. {$else TEST_FUNCRET}
  953. if not is_func_ret(srsym) then
  954. {$endif TEST_FUNCRET}
  955. { else it's a normal symbol }
  956. begin
  957. if srsym^.typ=unitsym then
  958. begin
  959. consume(POINT);
  960. getsymonlyin(punitsym(srsym)^.unitsymtable,pattern);
  961. unit_specific:=true;
  962. consume(ID);
  963. end
  964. else
  965. unit_specific:=false;
  966. if not assigned(srsym) then
  967. Begin
  968. p1:=genzeronode(errorn);
  969. { try to clean up }
  970. pd:=generrordef;
  971. end
  972. else
  973. Begin
  974. { check semantics of private }
  975. if (srsym^.typ in [propertysym,procsym,varsym]) and
  976. (srsymtable^.symtabletype=objectsymtable) then
  977. begin
  978. if ((srsym^.properties and sp_private)<>0) and
  979. (pobjectdef(srsym^.owner^.defowner)^.
  980. owner^.symtabletype=unitsymtable) then
  981. Message(parser_e_cant_access_private_member);
  982. end;
  983. case srsym^.typ of
  984. absolutesym:
  985. begin
  986. p1:=genloadnode(pvarsym(srsym),srsymtable);
  987. pd:=pabsolutesym(srsym)^.definition;
  988. end;
  989. varsym:
  990. begin
  991. { are we in a class method ? }
  992. if (srsymtable^.symtabletype=objectsymtable) and
  993. assigned(aktprocsym) and
  994. ((aktprocsym^.definition^.options and poclassmethod)<>0) then
  995. Message(parser_e_only_class_methods);
  996. if (srsym^.properties and sp_static)<>0 then
  997. begin
  998. static_name:=lower(srsymtable^.name^)+'_'+srsym^.name;
  999. getsym(static_name,true);
  1000. end;
  1001. p1:=genloadnode(pvarsym(srsym),srsymtable);
  1002. if pvarsym(srsym)^.is_valid=0 then
  1003. begin
  1004. p1^.is_first := true;
  1005. { set special between first loaded
  1006. until checked in firstpass }
  1007. pvarsym(srsym)^.is_valid:=2;
  1008. end;
  1009. pd:=pvarsym(srsym)^.definition;
  1010. end;
  1011. typedconstsym:
  1012. begin
  1013. p1:=gentypedconstloadnode(ptypedconstsym(srsym),srsymtable);
  1014. pd:=ptypedconstsym(srsym)^.definition;
  1015. end;
  1016. syssym:
  1017. p1:=statement_syssym(psyssym(srsym)^.number,pd);
  1018. typesym:
  1019. begin
  1020. pd:=ptypesym(srsym)^.definition;
  1021. { if we read a type declaration }
  1022. { we have to return the type and }
  1023. { nothing else }
  1024. if block_type=bt_type then
  1025. begin
  1026. p1:=genzeronode(typen);
  1027. p1^.resulttype:=pd;
  1028. pd:=voiddef;
  1029. end
  1030. else
  1031. begin
  1032. if token=LKLAMMER then
  1033. begin
  1034. consume(LKLAMMER);
  1035. p1:=comp_expr(true);
  1036. consume(RKLAMMER);
  1037. p1:=gentypeconvnode(p1,pd);
  1038. p1^.explizit:=true;
  1039. end
  1040. else if (token=POINT) and
  1041. (pd^.deftype=objectdef) and
  1042. ((pobjectdef(pd)^.options and oois_class)=0) then
  1043. begin
  1044. consume(POINT);
  1045. if assigned(procinfo._class) then
  1046. begin
  1047. if procinfo._class^.isrelated(pobjectdef(pd)) then
  1048. begin
  1049. p1:=genzeronode(typen);
  1050. p1^.resulttype:=pd;
  1051. srsymtable:=pobjectdef(pd)^.publicsyms;
  1052. sym:=pvarsym(srsymtable^.search(pattern));
  1053. { search also in inherited methods }
  1054. while sym=nil do
  1055. begin
  1056. pd:=pobjectdef(pd)^.childof;
  1057. srsymtable:=pobjectdef(pd)^.publicsyms;
  1058. sym:=pvarsym(srsymtable^.search(pattern));
  1059. end;
  1060. consume(ID);
  1061. do_member_read(sym,p1,pd,again);
  1062. end
  1063. else
  1064. begin
  1065. Message(parser_e_no_super_class);
  1066. pd:=generrordef;
  1067. again:=false;
  1068. end;
  1069. end
  1070. else
  1071. begin
  1072. { allows @TObject.Load }
  1073. { also allows static methods and variables }
  1074. p1:=genzeronode(typen);
  1075. p1^.resulttype:=pd;
  1076. srsymtable:=pobjectdef(pd)^.publicsyms;
  1077. sym:=pvarsym(srsymtable^.search(pattern));
  1078. if not(getaddr) and
  1079. ((sym^.properties and sp_static)=0) then
  1080. Message(sym_e_only_static_in_static)
  1081. else
  1082. begin
  1083. consume(ID);
  1084. do_member_read(sym,p1,pd,again);
  1085. end;
  1086. end
  1087. end
  1088. else
  1089. begin
  1090. { class reference ? }
  1091. if (pd^.deftype=objectdef)
  1092. and ((pobjectdef(pd)^.options and oois_class)<>0) then
  1093. begin
  1094. p1:=genzeronode(typen);
  1095. p1^.resulttype:=pd;
  1096. pd:=new(pclassrefdef,init(pd));
  1097. p1:=gensinglenode(loadvmtn,p1);
  1098. p1^.resulttype:=pd;
  1099. end
  1100. else
  1101. begin
  1102. { generate a type node }
  1103. { (for typeof etc) }
  1104. p1:=genzeronode(typen);
  1105. p1^.resulttype:=pd;
  1106. pd:=voiddef;
  1107. end;
  1108. end;
  1109. end;
  1110. end;
  1111. enumsym:
  1112. begin
  1113. p1:=genenumnode(penumsym(srsym));
  1114. pd:=p1^.resulttype;
  1115. end;
  1116. constsym:
  1117. begin
  1118. case pconstsym(srsym)^.consttype of
  1119. constint:
  1120. p1:=genordinalconstnode(pconstsym(srsym)^.value,s32bitdef);
  1121. conststring:
  1122. p1:=genstringconstnode(pstring(pconstsym(srsym)^.value)^);
  1123. constchar:
  1124. p1:=genordinalconstnode(pconstsym(srsym)^.value,cchardef);
  1125. constreal:
  1126. p1:=genrealconstnode(pdouble(pconstsym(srsym)^.value)^);
  1127. constbool:
  1128. p1:=genordinalconstnode(pconstsym(srsym)^.value,booldef);
  1129. constseta:
  1130. p1:=gensetconstruktnode(pconstset(pconstsym(srsym)^.value),
  1131. psetdef(pconstsym(srsym)^.definition));
  1132. constord:
  1133. p1:=genordinalconstnode(pconstsym(srsym)^.value,
  1134. pconstsym(srsym)^.definition);
  1135. end;
  1136. pd:=p1^.resulttype;
  1137. end;
  1138. procsym:
  1139. begin
  1140. { are we in a class method ? }
  1141. possible_error:=(srsymtable^.symtabletype=objectsymtable) and
  1142. assigned(aktprocsym) and
  1143. ((aktprocsym^.definition^.options and poclassmethod)<>0);
  1144. p1:=gencallnode(pprocsym(srsym),srsymtable);
  1145. p1^.unit_specific:=unit_specific;
  1146. do_proc_call(getaddr,again,p1,pd);
  1147. if possible_error and
  1148. ((p1^.procdefinition^.options and poclassmethod)=0) then
  1149. Message(parser_e_only_class_methods);
  1150. end;
  1151. propertysym:
  1152. begin
  1153. { access to property in a method }
  1154. { are we in a class method ? }
  1155. if (srsymtable^.symtabletype=objectsymtable) and
  1156. assigned(aktprocsym) and
  1157. ((aktprocsym^.definition^.options and poclassmethod)<>0) then
  1158. Message(parser_e_only_class_methods);
  1159. { no method pointer }
  1160. p1:=nil;
  1161. handle_propertysym(srsym,p1,pd);
  1162. end;
  1163. errorsym:
  1164. begin
  1165. p1:=genzeronode(errorn);
  1166. pd:=generrordef;
  1167. if token=LKLAMMER then
  1168. begin
  1169. consume(LKLAMMER);
  1170. parse_paras(false,false);
  1171. consume(RKLAMMER);
  1172. end;
  1173. end;
  1174. else
  1175. begin
  1176. p1:=genzeronode(errorn);
  1177. pd:=generrordef;
  1178. Message(cg_e_illegal_expression);
  1179. end;
  1180. end; { end case }
  1181. end;
  1182. end;
  1183. end;
  1184. { handle post fix operators }
  1185. postfixoperators;
  1186. end;
  1187. _NEW : begin
  1188. consume(_NEW);
  1189. consume(LKLAMMER);
  1190. p1:=factor(false);
  1191. if p1^.treetype<>typen then
  1192. Message(sym_e_type_id_expected);
  1193. pd:=p1^.resulttype;
  1194. pd2:=pd;
  1195. if (pd^.deftype<>pointerdef) or
  1196. (ppointerdef(pd)^.definition^.deftype<>objectdef) then
  1197. begin
  1198. Message(parser_e_pointer_to_class_expected);
  1199. { if an error occurs, read til the end of the new }
  1200. { statement }
  1201. p1:=genzeronode(errorn);
  1202. l:=1;
  1203. while true do
  1204. begin
  1205. case token of
  1206. LKLAMMER : inc(l);
  1207. RKLAMMER : dec(l);
  1208. end;
  1209. consume(token);
  1210. if l=0 then
  1211. break;
  1212. end;
  1213. end
  1214. else
  1215. begin
  1216. disposetree(p1);
  1217. p1:=genzeronode(hnewn);
  1218. p1^.resulttype:=ppointerdef(pd)^.definition;
  1219. consume(COMMA);
  1220. afterassignment:=false;
  1221. { determines the current object defintion }
  1222. classh:=pobjectdef(ppointerdef(pd)^.definition);
  1223. { check for an abstract class }
  1224. if (classh^.options and oois_abstract)<>0 then
  1225. Message(sym_e_no_instance_of_abstract_object);
  1226. { search the constructor also in the symbol tables of }
  1227. { the parents }
  1228. { no constructor found }
  1229. sym:=nil;
  1230. while assigned(classh) do
  1231. begin
  1232. sym:=pvarsym(classh^.publicsyms^.search(pattern));
  1233. srsymtable:=classh^.publicsyms;
  1234. if assigned(sym) then
  1235. break;
  1236. classh:=classh^.childof;
  1237. end;
  1238. consume(ID);
  1239. do_member_read(sym,p1,pd,again);
  1240. if (p1^.treetype<>calln) or
  1241. (assigned(p1^.procdefinition) and
  1242. ((p1^.procdefinition^.options and poconstructor)=0)) then
  1243. Message(parser_e_expr_have_to_be_constructor_call);
  1244. p1:=gensinglenode(newn,p1);
  1245. { set the resulttype }
  1246. p1^.resulttype:=pd2;
  1247. consume(RKLAMMER);
  1248. end;
  1249. end;
  1250. _SELF:
  1251. begin
  1252. again:=true;
  1253. consume(_SELF);
  1254. if not assigned(procinfo._class) then
  1255. begin
  1256. p1:=genzeronode(errorn);
  1257. pd:=generrordef;
  1258. again:=false;
  1259. Message(parser_e_self_not_in_method);
  1260. end
  1261. else
  1262. begin
  1263. if (aktprocsym^.definition^.options and poclassmethod)<>0 then
  1264. begin
  1265. { self in class methods is a class reference type }
  1266. pd:=new(pclassrefdef,init(procinfo._class));
  1267. p1:=genselfnode(pd);
  1268. p1^.resulttype:=pd;
  1269. end
  1270. else
  1271. begin
  1272. p1:=genselfnode(procinfo._class);
  1273. p1^.resulttype:=procinfo._class;
  1274. end;
  1275. pd:=p1^.resulttype;
  1276. postfixoperators;
  1277. end;
  1278. end;
  1279. _INHERITED : begin
  1280. again:=true;
  1281. consume(_INHERITED);
  1282. if assigned(procinfo._class) then
  1283. begin
  1284. classh:=procinfo._class^.childof;
  1285. while assigned(classh) do
  1286. begin
  1287. srsymtable:=pobjectdef(classh)^.publicsyms;
  1288. sym:=pvarsym(srsymtable^.search(pattern));
  1289. if assigned(sym) then
  1290. begin
  1291. p1:=genzeronode(typen);
  1292. p1^.resulttype:=classh;
  1293. pd:=p1^.resulttype;
  1294. consume(ID);
  1295. do_member_read(sym,p1,pd,again);
  1296. break;
  1297. end;
  1298. classh:=classh^.childof;
  1299. end;
  1300. if classh=nil then
  1301. begin
  1302. Message1(sym_e_id_no_member,pattern);
  1303. again:=false;
  1304. pd:=generrordef;
  1305. p1:=genzeronode(errorn);
  1306. end;
  1307. end
  1308. else
  1309. Message(parser_e_generic_methods_only_in_methods);
  1310. postfixoperators;
  1311. end;
  1312. INTCONST : begin
  1313. valint(pattern,l,code);
  1314. if code<>0 then
  1315. begin
  1316. val(pattern,d,code);
  1317. if code<>0 then
  1318. begin
  1319. Message(cg_e_invalid_integer);
  1320. l:=1;
  1321. consume(INTCONST);
  1322. p1:=genordinalconstnode(l,s32bitdef);
  1323. end
  1324. else
  1325. begin
  1326. consume(INTCONST);
  1327. p1:=genrealconstnode(d);
  1328. end;
  1329. end
  1330. else
  1331. begin
  1332. consume(INTCONST);
  1333. p1:=genordinalconstnode(l,s32bitdef);
  1334. end;
  1335. end;
  1336. REALNUMBER : begin
  1337. val(pattern,d,code);
  1338. if code<>0 then
  1339. begin
  1340. Message(parser_e_error_in_real);
  1341. d:=1.0;
  1342. end;
  1343. consume(REALNUMBER);
  1344. p1:=genrealconstnode(d);
  1345. end;
  1346. { FILE and STRING can be also a type cast }
  1347. _STRING:
  1348. begin
  1349. pd:=stringtype;
  1350. consume(LKLAMMER);
  1351. p1:=comp_expr(true);
  1352. consume(RKLAMMER);
  1353. p1:=gentypeconvnode(p1,pd);
  1354. p1^.explizit:=true;
  1355. { handle postfix operators here e.g. string(a)[10] }
  1356. again:=true;
  1357. postfixoperators;
  1358. end;
  1359. _FILE:
  1360. begin
  1361. pd:=cfiledef;
  1362. consume(_FILE);
  1363. if token=LKLAMMER then
  1364. begin
  1365. consume(LKLAMMER);
  1366. p1:=comp_expr(true);
  1367. consume(RKLAMMER);
  1368. p1:=gentypeconvnode(p1,pd);
  1369. end
  1370. else
  1371. begin
  1372. p1:=genzeronode(typen);
  1373. p1^.resulttype:=pd;
  1374. end;
  1375. p1^.explizit:=true;
  1376. { handle postfix operators here e.g. string(a)[10] }
  1377. again:=true;
  1378. postfixoperators;
  1379. end;
  1380. CSTRING:
  1381. begin
  1382. p1:=genstringconstnode(pattern);
  1383. consume(CSTRING);
  1384. end;
  1385. CCHAR:
  1386. begin
  1387. p1:=genordinalconstnode(ord(pattern[1]),cchardef);
  1388. consume(CCHAR);
  1389. end;
  1390. KLAMMERAFFE : begin
  1391. consume(KLAMMERAFFE);
  1392. p1:=factor(true);
  1393. p1:=gensinglenode(addrn,p1);
  1394. end;
  1395. LKLAMMER : begin
  1396. consume(LKLAMMER);
  1397. p1:=comp_expr(true);
  1398. consume(RKLAMMER);
  1399. { it's not a good solution }
  1400. { but (a+b)^ makes some problems }
  1401. case token of
  1402. CARET,POINT,LECKKLAMMER:
  1403. begin
  1404. { we need the resulttype }
  1405. { of the expression in pd }
  1406. do_firstpass(p1);
  1407. pd:=p1^.resulttype;
  1408. again:=true;
  1409. postfixoperators;
  1410. end;
  1411. end;
  1412. end;
  1413. LECKKLAMMER : begin
  1414. consume(LECKKLAMMER);
  1415. new(constset);
  1416. for l:=0 to 31 do
  1417. constset^[l]:=0;
  1418. p2:=nil;
  1419. pd:=nil;
  1420. if token<>RECKKLAMMER then
  1421. while true do
  1422. begin
  1423. p1:=comp_expr(true);
  1424. do_firstpass(p1);
  1425. case p1^.treetype of
  1426. ordconstn : begin
  1427. if porddef(p1^.resulttype)=s32bitdef then
  1428. p1^.resulttype:=u8bitdef;
  1429. if pd=nil then
  1430. pd:=p1^.resulttype;
  1431. if not(is_equal(pd,p1^.resulttype)) then
  1432. Message(parser_e_typeconflict_in_set)
  1433. else
  1434. if token=POINTPOINT then
  1435. begin
  1436. consume(POINTPOINT);
  1437. p3:=comp_expr(true);
  1438. do_firstpass(p3);
  1439. if porddef(p3^.resulttype)=s32bitdef then
  1440. p3^.resulttype:=u8bitdef;
  1441. if not(is_equal(pd,p3^.resulttype)) then
  1442. Message(parser_e_typeconflict_in_set)
  1443. else
  1444. for l:=p1^.value to p3^.value do
  1445. do_set(constset,l);
  1446. disposetree(p3);
  1447. end
  1448. else
  1449. do_set(constset,p1^.value);
  1450. disposetree(p1);
  1451. end;
  1452. stringconstn : begin
  1453. if pd=nil then
  1454. pd:=cchardef;
  1455. if not(is_equal(pd,cchardef)) then
  1456. Message(parser_e_typeconflict_in_set)
  1457. else
  1458. for l:=1 to length(pstring(p1^.values)^) do
  1459. do_set(constset,ord(pstring(p1^.values)^[l]));
  1460. disposetree(p1);
  1461. end;
  1462. else
  1463. begin
  1464. if porddef(p1^.resulttype)=s32bitdef then
  1465. p1^.resulttype:=u8bitdef;
  1466. if pd=nil then
  1467. pd:=p1^.resulttype;
  1468. if not(is_equal(pd,p1^.resulttype)) then
  1469. Message(parser_e_typeconflict_in_set);
  1470. p2:=gennode(setelen,p1,p2);
  1471. end;
  1472. end;
  1473. if token=COMMA then
  1474. consume(COMMA)
  1475. else break;
  1476. end;
  1477. consume(RECKKLAMMER);
  1478. p1:=gensinglenode(setconstrn,p2);
  1479. { use smallset if emudef with max < 32
  1480. but allow sets for enumdefs with more than
  1481. 256 elements like tasmop !! }
  1482. if assigned(pd) and (pd^.deftype=enumdef) and (penumdef(pd)^.max<=255) then
  1483. p1^.resulttype:=new(psetdef,init(pd,penumdef(pd)^.max))
  1484. else
  1485. p1^.resulttype:=new(psetdef,init(pd,255));
  1486. p1^.constset:=constset;
  1487. end;
  1488. PLUS : begin
  1489. consume(PLUS);
  1490. p1:=factor(false);
  1491. end;
  1492. MINUS : begin
  1493. consume(MINUS);
  1494. p1:=factor(false);
  1495. p1:=gensinglenode(umminusn,p1);
  1496. end;
  1497. _NOT : begin
  1498. consume(_NOT);
  1499. p1:=factor(false);
  1500. p1:=gensinglenode(notn,p1);
  1501. end;
  1502. _TRUE : begin
  1503. consume(_TRUE);
  1504. p1:=genordinalconstnode(1,booldef);
  1505. end;
  1506. _FALSE : begin
  1507. consume(_FALSE);
  1508. p1:=genordinalconstnode(0,booldef);
  1509. end;
  1510. _NIL : begin
  1511. consume(_NIL);
  1512. p1:=genzeronode(niln);
  1513. end;
  1514. else
  1515. begin
  1516. p1:=genzeronode(errorn);
  1517. consume(token);
  1518. Message(cg_e_illegal_expression);
  1519. end;
  1520. end;
  1521. factor:=p1;
  1522. check_tokenpos;
  1523. end;
  1524. type Toperator_precedence=(opcompare,opaddition,opmultiply);
  1525. const tok2node:array[PLUS.._XOR] of Ttreetyp=
  1526. (addn,subn,muln,slashn,equaln,gtn,ltn,gten,lten,
  1527. isn,asn,inn,
  1528. symdifn,starstarn,nothingn,caretn,unequaln,nothingn,
  1529. nothingn,nothingn,nothingn,nothingn,nothingn,
  1530. nothingn,nothingn,nothingn,nothingn,nothingn,
  1531. nothingn,nothingn,nothingn,nothingn,nothingn,
  1532. nothingn,andn,nothingn,nothingn,nothingn,
  1533. nothingn,nothingn,nothingn,nothingn,nothingn,
  1534. nothingn,nothingn,divn,nothingn,nothingn,
  1535. nothingn,nothingn,nothingn,nothingn,nothingn,
  1536. nothingn,nothingn,nothingn,nothingn,nothingn,
  1537. nothingn,nothingn,nothingn,nothingn,nothingn,
  1538. modn,nothingn,nothingn,nothingn,nothingn,
  1539. nothingn,nothingn,orn,
  1540. nothingn,nothingn,nothingn,nothingn,nothingn,
  1541. nothingn,nothingn,shln,shrn,
  1542. nothingn,nothingn,nothingn,nothingn,nothingn,
  1543. nothingn,nothingn,nothingn,nothingn,nothingn,
  1544. nothingn,xorn);
  1545. operator_levels:array[Toperator_precedence] of set of Ttoken=
  1546. ([LT,LTE,GT,GTE,EQUAL,UNEQUAL,_IN,_IS],
  1547. [PLUS,MINUS,_OR,_XOR],
  1548. [CARET,SYMDIF,STARSTAR,STAR,SLASH,_DIV,_MOD,_AND,_SHL,_SHR,_AS]);
  1549. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):Ptree;
  1550. {Reads a subexpression while the operators are of the current precedence
  1551. level, or any higher level. Replaces the old term, simpl_expr and
  1552. simpl2_expr.}
  1553. var p1,p2:Ptree;
  1554. oldt:Ttoken;
  1555. filepos : tfileposinfo;
  1556. begin
  1557. { if pred_level=high(Toperator_precedence) then }
  1558. if pred_level=opmultiply then
  1559. p1:=factor(getprocvar)
  1560. else
  1561. p1:=sub_expr(succ(pred_level),true);
  1562. repeat
  1563. { aweful hack to support const a : 1..2=1; }
  1564. { disadvantage of tables :) FK }
  1565. if (token in operator_levels[pred_level]) and
  1566. ((token<>EQUAL) or accept_equal) then
  1567. begin
  1568. oldt:=token;
  1569. filepos:=tokenpos;
  1570. consume(token);
  1571. { if pred_level=high(Toperator_precedence) then }
  1572. if pred_level=opmultiply then
  1573. p2:=factor(getprocvar)
  1574. else
  1575. p2:=sub_expr(succ(pred_level),true);
  1576. p1:=gennode(tok2node[oldt],p1,p2);
  1577. set_tree_filepos(p1,filepos);
  1578. end
  1579. else
  1580. break;
  1581. until false;
  1582. sub_expr:=p1;
  1583. end;
  1584. function comp_expr(accept_equal : boolean):Ptree;
  1585. var
  1586. oldafterassignment : boolean;
  1587. begin
  1588. oldafterassignment:=afterassignment;
  1589. afterassignment:=true;
  1590. comp_expr:=sub_expr(opcompare,accept_equal);
  1591. afterassignment:=oldafterassignment;
  1592. end;
  1593. function expr : ptree;
  1594. var
  1595. p1,p2 : ptree;
  1596. oldafterassignment : boolean;
  1597. oldp1 : ptree;
  1598. filepos : tfileposinfo;
  1599. begin
  1600. oldafterassignment:=afterassignment;
  1601. p1:=sub_expr(opcompare,true);
  1602. if token in [ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  1603. afterassignment:=true;
  1604. filepos:=tokenpos;
  1605. oldp1:=p1;
  1606. case token of
  1607. POINTPOINT : begin
  1608. consume(POINTPOINT);
  1609. p2:=sub_expr(opcompare,true);
  1610. p1:=gennode(rangen,p1,p2);
  1611. end;
  1612. ASSIGNMENT : begin
  1613. consume(ASSIGNMENT);
  1614. { avoid a firstpass of a procedure if
  1615. it must be assigned to a procvar }
  1616. { should be recursive for a:=b:=c !!! }
  1617. if (p1^.resulttype<>nil) and (p1^.resulttype^.deftype=procvardef) then
  1618. getprocvar:=true;
  1619. p2:=sub_expr(opcompare,true);
  1620. if getprocvar and (p2^.treetype=calln) then
  1621. begin
  1622. p2^.treetype:=loadn;
  1623. p2^.resulttype:=pprocsym(p2^.symtableprocentry)^.definition;
  1624. p2^.symtableentry:=p2^.symtableprocentry;
  1625. end;
  1626. getprocvar:=false;
  1627. p1:=gennode(assignn,p1,p2);
  1628. end;
  1629. { this is the code for C like assignements }
  1630. { from an improvement of Peter Schaefer }
  1631. _PLUSASN : begin
  1632. consume(_PLUSASN );
  1633. p2:=sub_expr(opcompare,true);
  1634. p1:=gennode(assignn,p1,gennode(addn,getcopy(p1),p2));
  1635. { was first
  1636. p1:=gennode(assignn,p1,gennode(addn,p1,p2));
  1637. but disposetree assumes that we have a real
  1638. *** tree *** }
  1639. end;
  1640. _MINUSASN : begin
  1641. consume(_MINUSASN );
  1642. p2:=sub_expr(opcompare,true);
  1643. p1:=gennode(assignn,p1,gennode(subn,getcopy(p1),p2));
  1644. end;
  1645. _STARASN : begin
  1646. consume(_STARASN );
  1647. p2:=sub_expr(opcompare,true);
  1648. p1:=gennode(assignn,p1,gennode(muln,getcopy(p1),p2));
  1649. end;
  1650. _SLASHASN : begin
  1651. consume(_SLASHASN );
  1652. p2:=sub_expr(opcompare,true);
  1653. p1:=gennode(assignn,p1,gennode(slashn,getcopy(p1),p2));
  1654. end;
  1655. end;
  1656. afterassignment:=oldafterassignment;
  1657. if p1<>oldp1 then
  1658. set_tree_filepos(p1,filepos);
  1659. expr:=p1;
  1660. end;
  1661. function get_intconst:longint;
  1662. {Reads an expression, tries to evalute it and check if it is an integer
  1663. constant. Then the constant is returned.}
  1664. var p:Ptree;
  1665. begin
  1666. p:=comp_expr(true);
  1667. do_firstpass(p);
  1668. if (p^.treetype<>ordconstn) and
  1669. (p^.resulttype^.deftype=orddef) and
  1670. not (Porddef(p^.resulttype)^.typ in
  1671. [uvoid,uchar,bool8bit]) then
  1672. Message(cg_e_illegal_expression)
  1673. else
  1674. get_intconst:=p^.value;
  1675. disposetree(p);
  1676. end;
  1677. function get_stringconst:string;
  1678. {Reads an expression, tries to evaluate it and checks if it is a string
  1679. constant. Then the constant is returned.}
  1680. var p:Ptree;
  1681. begin
  1682. get_stringconst:='';
  1683. p:=comp_expr(true);
  1684. do_firstpass(p);
  1685. if p^.treetype<>stringconstn then
  1686. if (p^.treetype=ordconstn) and
  1687. (p^.resulttype^.deftype=orddef) and
  1688. (Porddef(p^.resulttype)^.typ=uchar) then
  1689. get_stringconst:=char(p^.value)
  1690. else
  1691. Message(cg_e_illegal_expression)
  1692. else
  1693. get_stringconst:=p^.values^;
  1694. disposetree(p);
  1695. end;
  1696. end.
  1697. {
  1698. $Log$
  1699. Revision 1.24 1998-06-04 23:51:52 peter
  1700. * m68k compiles
  1701. + .def file creation moved to gendef.pas so it could also be used
  1702. for win32
  1703. Revision 1.23 1998/06/04 09:55:40 pierre
  1704. * demangled name of procsym reworked to become independant of the mangling scheme
  1705. Come test_funcret improvements (not yet working)S: ----------------------------------------------------------------------
  1706. Revision 1.22 1998/06/02 17:03:03 pierre
  1707. * with node corrected for objects
  1708. * small bugs for SUPPORT_MMX fixed
  1709. Revision 1.21 1998/05/27 19:45:05 peter
  1710. * symtable.pas splitted into includefiles
  1711. * symtable adapted for $ifdef NEWPPU
  1712. Revision 1.20 1998/05/26 07:53:59 pierre
  1713. * bug fix for empty sets (nil pd was dereferenced )
  1714. Revision 1.19 1998/05/25 17:11:43 pierre
  1715. * firstpasscount bug fixed
  1716. now all is already set correctly the first time
  1717. under EXTDEBUG try -gp to skip all other firstpasses
  1718. it works !!
  1719. * small bug fixes
  1720. - for smallsets with -dTESTSMALLSET
  1721. - some warnings removed (by correcting code !)
  1722. Revision 1.18 1998/05/23 01:21:20 peter
  1723. + aktasmmode, aktoptprocessor, aktoutputformat
  1724. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  1725. + $LIBNAME to set the library name where the unit will be put in
  1726. * splitted cgi386 a bit (codeseg to large for bp7)
  1727. * nasm, tasm works again. nasm moved to ag386nsm.pas
  1728. Revision 1.17 1998/05/22 12:37:03 carl
  1729. * crash bugfix (patched msanually to main branch)
  1730. Revision 1.16 1998/05/21 19:33:32 peter
  1731. + better procedure directive handling and only one table
  1732. Revision 1.15 1998/05/20 09:42:35 pierre
  1733. + UseTokenInfo now default
  1734. * unit in interface uses and implementation uses gives error now
  1735. * only one error for unknown symbol (uses lastsymknown boolean)
  1736. the problem came from the label code !
  1737. + first inlined procedures and function work
  1738. (warning there might be allowed cases were the result is still wrong !!)
  1739. * UseBrower updated gives a global list of all position of all used symbols
  1740. with switch -gb
  1741. Revision 1.14 1998/05/11 13:07:56 peter
  1742. + $ifdef NEWPPU for the new ppuformat
  1743. + $define GDB not longer required
  1744. * removed all warnings and stripped some log comments
  1745. * no findfirst/findnext anymore to remove smartlink *.o files
  1746. Revision 1.13 1998/05/06 08:38:45 pierre
  1747. * better position info with UseTokenInfo
  1748. UseTokenInfo greatly simplified
  1749. + added check for changed tree after first time firstpass
  1750. (if we could remove all the cases were it happen
  1751. we could skip all firstpass if firstpasscount > 1)
  1752. Only with ExtDebug
  1753. Revision 1.12 1998/05/05 12:05:42 florian
  1754. * problems with properties fixed
  1755. * crash fixed: i:=l when i and l are undefined, was a problem with
  1756. implementation of private/protected
  1757. Revision 1.11 1998/05/04 11:22:26 florian
  1758. * problem with DOM solved: it crashes when accessing a property in a method
  1759. Revision 1.10 1998/05/01 16:38:45 florian
  1760. * handling of private and protected fixed
  1761. + change_keywords_to_tp implemented to remove
  1762. keywords which aren't supported by tp
  1763. * break and continue are now symbols of the system unit
  1764. + widestring, longstring and ansistring type released
  1765. Revision 1.9 1998/04/29 10:33:58 pierre
  1766. + added some code for ansistring (not complete nor working yet)
  1767. * corrected operator overloading
  1768. * corrected nasm output
  1769. + started inline procedures
  1770. + added starstarn : use ** for exponentiation (^ gave problems)
  1771. + started UseTokenInfo cond to get accurate positions
  1772. Revision 1.8 1998/04/14 23:27:03 florian
  1773. + exclude/include with constant second parameter added
  1774. Revision 1.7 1998/04/09 23:02:15 florian
  1775. * small problems solved to get remake3 work
  1776. Revision 1.6 1998/04/09 22:16:35 florian
  1777. * problem with previous REGALLOC solved
  1778. * improved property support
  1779. Revision 1.5 1998/04/08 10:26:09 florian
  1780. * correct error handling of virtual constructors
  1781. * problem with new type declaration handling fixed
  1782. Revision 1.4 1998/04/07 22:45:05 florian
  1783. * bug0092, bug0115 and bug0121 fixed
  1784. + packed object/class/array
  1785. Revision 1.3 1998/04/07 13:19:46 pierre
  1786. * bugfixes for reset_gdb_info
  1787. in MEM parsing for go32v2
  1788. better external symbol creation
  1789. support for rhgdb.exe (lowercase file names)
  1790. }