pexpr.pas 81 KB

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