pexpr.pas 84 KB

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