pexpr.pas 83 KB

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