pexpr.pas 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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. {$i defines.inc}
  20. interface
  21. uses
  22. symtype,
  23. node;
  24. { reads a whole expression }
  25. function expr : tnode;
  26. { reads an expression without assignements and .. }
  27. function comp_expr(accept_equal : boolean):tnode;
  28. { reads a single factor }
  29. function factor(getaddr : boolean) : tnode;
  30. function string_dec : pdef;
  31. { the ID token has to be consumed before calling this function }
  32. procedure do_member_read(getaddr : boolean;const sym : psym;var p1 : tnode;
  33. var pd : pdef;var again : boolean);
  34. function get_intconst:longint;
  35. function get_stringconst:string;
  36. implementation
  37. uses
  38. { common }
  39. cutils,cobjects,
  40. { global }
  41. globtype,globals,tokens,verbose,
  42. systems,cpuinfo,
  43. { aasm }
  44. aasm,
  45. { symtable }
  46. symconst,symbase,symdef,symsym,symtable,types,
  47. {$ifdef GDB}
  48. gdb,
  49. {$endif}
  50. { pass 1 }
  51. pass_1,htypechk,
  52. nmat,nadd,ncal,nmem,nset,ncnv,ninl,ncon,nld,nflw,nbas,
  53. { parser }
  54. scanner,
  55. pbase,
  56. { codegen }
  57. {$ifdef newcg}
  58. cgbase
  59. {$else}
  60. hcodegen
  61. {$endif}
  62. ;
  63. { sub_expr(opmultiply) is need to get -1 ** 4 to be
  64. read as - (1**4) and not (-1)**4 PM }
  65. type
  66. Toperator_precedence=(opcompare,opaddition,opmultiply,oppower);
  67. const
  68. highest_precedence = oppower;
  69. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):tnode;forward;
  70. const
  71. allow_type : boolean = true;
  72. got_addrn : boolean = false;
  73. auto_inherited : boolean = false;
  74. function string_dec : pdef;
  75. { reads a string type with optional length }
  76. { and returns a pointer to the string }
  77. { definition }
  78. var
  79. p : tnode;
  80. d : pdef;
  81. begin
  82. consume(_STRING);
  83. if token=_LECKKLAMMER then
  84. begin
  85. consume(_LECKKLAMMER);
  86. p:=comp_expr(true);
  87. do_firstpass(p);
  88. if not is_constintnode(p) then
  89. begin
  90. Message(cg_e_illegal_expression);
  91. { error recovery }
  92. consume(_RECKKLAMMER);
  93. d:=cshortstringdef;
  94. end
  95. else
  96. begin
  97. if (tordconstnode(p).value<=0) then
  98. begin
  99. Message(parser_e_invalid_string_size);
  100. tordconstnode(p).value:=255;
  101. end;
  102. consume(_RECKKLAMMER);
  103. if tordconstnode(p).value>255 then
  104. d:=new(pstringdef,longinit(tordconstnode(p).value))
  105. else
  106. if tordconstnode(p).value<>255 then
  107. d:=new(pstringdef,shortinit(tordconstnode(p).value))
  108. else
  109. d:=cshortstringdef;
  110. end;
  111. p.free;
  112. end
  113. else
  114. begin
  115. if cs_ansistrings in aktlocalswitches then
  116. d:=cansistringdef
  117. else
  118. d:=cshortstringdef;
  119. end;
  120. string_dec:=d;
  121. end;
  122. function parse_paras(__colon,in_prop_paras : boolean) : tnode;
  123. var
  124. p1,p2 : tnode;
  125. end_of_paras : ttoken;
  126. begin
  127. if in_prop_paras then
  128. end_of_paras:=_RECKKLAMMER
  129. else
  130. end_of_paras:=_RKLAMMER;
  131. if token=end_of_paras then
  132. begin
  133. parse_paras:=nil;
  134. exit;
  135. end;
  136. p2:=nil;
  137. inc(parsing_para_level);
  138. while true do
  139. begin
  140. p1:=comp_expr(true);
  141. p2:=gencallparanode(p1,p2);
  142. { it's for the str(l:5,s); }
  143. if __colon and (token=_COLON) then
  144. begin
  145. consume(_COLON);
  146. p1:=comp_expr(true);
  147. p2:=gencallparanode(p1,p2);
  148. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  149. if token=_COLON then
  150. begin
  151. consume(_COLON);
  152. p1:=comp_expr(true);
  153. p2:=gencallparanode(p1,p2);
  154. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  155. end
  156. end;
  157. if token=_COMMA then
  158. consume(_COMMA)
  159. else
  160. break;
  161. end;
  162. dec(parsing_para_level);
  163. parse_paras:=p2;
  164. end;
  165. procedure check_tp_procvar(var p : tnode);
  166. var
  167. p1 : tnode;
  168. begin
  169. if (m_tp_procvar in aktmodeswitches) and
  170. (not got_addrn) and
  171. (not in_args) and
  172. (p.nodetype=loadn) then
  173. begin
  174. { support if procvar then for tp7 and many other expression like this }
  175. do_firstpass(p);
  176. set_varstate(p,false);
  177. { reset varstateset to maybe set used state later web bug769 PM }
  178. unset_varstate(p);
  179. if not(getprocvar) and (p.resulttype^.deftype=procvardef) then
  180. begin
  181. p1:=gencallnode(nil,nil);
  182. tcallnode(p1).right:=p;
  183. p1.resulttype:=pprocvardef(p.resulttype)^.rettype.def;
  184. firstpass(p1);
  185. p:=p1;
  186. end;
  187. end;
  188. end;
  189. function statement_syssym(l : longint;var pd : pdef) : tnode;
  190. var
  191. p1,p2,paras : tnode;
  192. prev_in_args : boolean;
  193. begin
  194. prev_in_args:=in_args;
  195. case l of
  196. in_ord_x :
  197. begin
  198. consume(_LKLAMMER);
  199. in_args:=true;
  200. p1:=comp_expr(true);
  201. consume(_RKLAMMER);
  202. do_firstpass(p1);
  203. p1:=geninlinenode(in_ord_x,false,p1);
  204. do_firstpass(p1);
  205. statement_syssym := p1;
  206. pd:=p1.resulttype;
  207. end;
  208. in_break :
  209. begin
  210. statement_syssym:=cbreaknode.create;
  211. pd:=voiddef;
  212. end;
  213. in_continue :
  214. begin
  215. statement_syssym:=ccontinuenode.create;
  216. pd:=voiddef;
  217. end;
  218. in_typeof_x :
  219. begin
  220. consume(_LKLAMMER);
  221. in_args:=true;
  222. {allow_type:=true;}
  223. p1:=comp_expr(true);
  224. {allow_type:=false;}
  225. consume(_RKLAMMER);
  226. pd:=voidpointerdef;
  227. if p1.nodetype=typen then
  228. begin
  229. if (ttypenode(p1).typenodetype=nil) then
  230. begin
  231. Message(type_e_mismatch);
  232. statement_syssym:=cerrornode.create;
  233. end
  234. else
  235. if ttypenode(p1).typenodetype^.deftype=objectdef then
  236. begin
  237. { we can use resulttype in pass_2 (PM) }
  238. p1.resulttype:=ttypenode(p1).typenodetype;
  239. statement_syssym:=geninlinenode(in_typeof_x,false,p1);
  240. end
  241. else
  242. begin
  243. Message(type_e_mismatch);
  244. p1.destroy;
  245. statement_syssym:=cerrornode.create;
  246. end;
  247. end
  248. else { not a type node }
  249. begin
  250. do_firstpass(p1);
  251. set_varstate(p1,false);
  252. if (p1.resulttype=nil) then
  253. begin
  254. Message(type_e_mismatch);
  255. p1.destroy;
  256. statement_syssym:=cerrornode.create;
  257. end
  258. else
  259. if p1.resulttype^.deftype=objectdef then
  260. statement_syssym:=geninlinenode(in_typeof_x,false,p1)
  261. else
  262. begin
  263. Message(type_e_mismatch);
  264. statement_syssym:=cerrornode.create;
  265. p1.destroy;
  266. end;
  267. end;
  268. end;
  269. in_sizeof_x :
  270. begin
  271. consume(_LKLAMMER);
  272. in_args:=true;
  273. {allow_type:=true;}
  274. p1:=comp_expr(true);
  275. {allow_type:=false; }
  276. consume(_RKLAMMER);
  277. pd:=s32bitdef;
  278. if p1.nodetype=typen then
  279. begin
  280. statement_syssym:=genordinalconstnode(ttypenode(p1).typenodetype^.size,pd);
  281. { p1 not needed !}
  282. p1.destroy;
  283. end
  284. else
  285. begin
  286. do_firstpass(p1);
  287. if ((p1.resulttype^.deftype=objectdef) and
  288. (oo_has_constructor in pobjectdef(p1.resulttype)^.objectoptions)) or
  289. is_open_array(p1.resulttype) or
  290. is_open_string(p1.resulttype) then
  291. statement_syssym:=geninlinenode(in_sizeof_x,false,p1)
  292. else
  293. begin
  294. statement_syssym:=genordinalconstnode(p1.resulttype^.size,pd);
  295. { p1 not needed !}
  296. p1.destroy;
  297. end;
  298. end;
  299. end;
  300. in_typeinfo_x :
  301. begin
  302. consume(_LKLAMMER);
  303. in_args:=true;
  304. {allow_type:=true;}
  305. p1:=comp_expr(true);
  306. do_firstpass(p1);
  307. {allow_type:=false; }
  308. if p1.nodetype<>typen then
  309. begin
  310. p1.destroy;
  311. p1:=cerrornode.create;
  312. Message(parser_e_illegal_parameter_list);
  313. end;
  314. consume(_RKLAMMER);
  315. p2:=gencallparanode(p1,nil);
  316. p2:=geninlinenode(in_typeinfo_x,false,p2);
  317. pd:=voidpointerdef;
  318. statement_syssym:=p2;
  319. end;
  320. in_assigned_x :
  321. begin
  322. consume(_LKLAMMER);
  323. in_args:=true;
  324. p1:=comp_expr(true);
  325. do_firstpass(p1);
  326. if not codegenerror then
  327. begin
  328. case p1.resulttype^.deftype of
  329. pointerdef,
  330. procvardef,
  331. classrefdef : ;
  332. objectdef :
  333. if not is_class_or_interface(p1.resulttype) then
  334. Message(parser_e_illegal_parameter_list);
  335. else
  336. Message(parser_e_illegal_parameter_list);
  337. end;
  338. end;
  339. p2:=gencallparanode(p1,nil);
  340. p2:=geninlinenode(in_assigned_x,false,p2);
  341. consume(_RKLAMMER);
  342. pd:=booldef;
  343. statement_syssym:=p2;
  344. end;
  345. in_addr_x :
  346. begin
  347. consume(_LKLAMMER);
  348. in_args:=true;
  349. p1:=comp_expr(true);
  350. p1:=caddrnode.create(p1);
  351. do_firstpass(p1);
  352. pd:=p1.resulttype;
  353. consume(_RKLAMMER);
  354. statement_syssym:=p1;
  355. end;
  356. in_ofs_x :
  357. begin
  358. consume(_LKLAMMER);
  359. in_args:=true;
  360. p1:=comp_expr(true);
  361. p1:=caddrnode.create(p1);
  362. do_firstpass(p1);
  363. { Ofs() returns a longint, not a pointer }
  364. p1.resulttype:=u32bitdef;
  365. pd:=p1.resulttype;
  366. consume(_RKLAMMER);
  367. statement_syssym:=p1;
  368. end;
  369. in_seg_x :
  370. begin
  371. consume(_LKLAMMER);
  372. in_args:=true;
  373. { the expression must be allowed to be assigned to }
  374. p1:=comp_expr(true);
  375. p1:=geninlinenode(in_assigned_x,false,p1);
  376. do_firstpass(p1);
  377. consume(_RKLAMMER);
  378. statement_syssym:=p1;
  379. end;
  380. in_high_x,
  381. in_low_x :
  382. begin
  383. consume(_LKLAMMER);
  384. in_args:=true;
  385. {allow_type:=true;}
  386. p1:=comp_expr(true);
  387. {allow_type:=false;}
  388. do_firstpass(p1);
  389. if p1.nodetype=typen then
  390. p1.resulttype:=ttypenode(p1).typenodetype;
  391. p2:=geninlinenode(l,false,p1);
  392. consume(_RKLAMMER);
  393. pd:=s32bitdef;
  394. statement_syssym:=p2;
  395. end;
  396. in_succ_x,
  397. in_pred_x :
  398. begin
  399. consume(_LKLAMMER);
  400. in_args:=true;
  401. p1:=comp_expr(true);
  402. do_firstpass(p1);
  403. p2:=geninlinenode(l,false,p1);
  404. consume(_RKLAMMER);
  405. pd:=p1.resulttype;
  406. statement_syssym:=p2;
  407. end;
  408. in_inc_x,
  409. in_dec_x :
  410. begin
  411. consume(_LKLAMMER);
  412. in_args:=true;
  413. p1:=comp_expr(true);
  414. if token=_COMMA then
  415. begin
  416. consume(_COMMA);
  417. p2:=gencallparanode(comp_expr(true),nil);
  418. end
  419. else
  420. p2:=nil;
  421. p2:=gencallparanode(p1,p2);
  422. statement_syssym:=geninlinenode(l,false,p2);
  423. consume(_RKLAMMER);
  424. pd:=voiddef;
  425. end;
  426. in_concat_x :
  427. begin
  428. consume(_LKLAMMER);
  429. in_args:=true;
  430. p2:=nil;
  431. while true do
  432. begin
  433. p1:=comp_expr(true);
  434. do_firstpass(p1);
  435. set_varstate(p1,true);
  436. if not((p1.resulttype^.deftype=stringdef) or
  437. ((p1.resulttype^.deftype=orddef) and
  438. (porddef(p1.resulttype)^.typ=uchar))) then
  439. Message(parser_e_illegal_parameter_list);
  440. if p2<>nil then
  441. p2:=caddnode.create(addn,p2,p1)
  442. else
  443. p2:=p1;
  444. if token=_COMMA then
  445. consume(_COMMA)
  446. else
  447. break;
  448. end;
  449. consume(_RKLAMMER);
  450. pd:=cshortstringdef;
  451. statement_syssym:=p2;
  452. end;
  453. in_read_x,
  454. in_readln_x :
  455. begin
  456. if token=_LKLAMMER then
  457. begin
  458. consume(_LKLAMMER);
  459. in_args:=true;
  460. paras:=parse_paras(false,false);
  461. consume(_RKLAMMER);
  462. end
  463. else
  464. paras:=nil;
  465. pd:=voiddef;
  466. p1:=geninlinenode(l,false,paras);
  467. do_firstpass(p1);
  468. statement_syssym := p1;
  469. end;
  470. in_setlength_x:
  471. begin
  472. if token=_LKLAMMER then
  473. begin
  474. consume(_LKLAMMER);
  475. in_args:=true;
  476. paras:=parse_paras(false,false);
  477. consume(_RKLAMMER);
  478. end
  479. else
  480. paras:=nil;
  481. pd:=voiddef;
  482. p1:=geninlinenode(l,false,paras);
  483. do_firstpass(p1);
  484. statement_syssym := p1;
  485. end;
  486. in_write_x,
  487. in_writeln_x :
  488. begin
  489. if token=_LKLAMMER then
  490. begin
  491. consume(_LKLAMMER);
  492. in_args:=true;
  493. paras:=parse_paras(true,false);
  494. consume(_RKLAMMER);
  495. end
  496. else
  497. paras:=nil;
  498. pd:=voiddef;
  499. p1 := geninlinenode(l,false,paras);
  500. do_firstpass(p1);
  501. statement_syssym := p1;
  502. end;
  503. in_str_x_string :
  504. begin
  505. consume(_LKLAMMER);
  506. in_args:=true;
  507. paras:=parse_paras(true,false);
  508. consume(_RKLAMMER);
  509. p1 := geninlinenode(l,false,paras);
  510. do_firstpass(p1);
  511. statement_syssym := p1;
  512. pd:=voiddef;
  513. end;
  514. in_val_x:
  515. Begin
  516. consume(_LKLAMMER);
  517. in_args := true;
  518. p1:= gencallparanode(comp_expr(true), nil);
  519. consume(_COMMA);
  520. p2 := gencallparanode(comp_expr(true),p1);
  521. if (token = _COMMA) then
  522. Begin
  523. consume(_COMMA);
  524. p2 := gencallparanode(comp_expr(true),p2)
  525. End;
  526. consume(_RKLAMMER);
  527. p2 := geninlinenode(l,false,p2);
  528. do_firstpass(p2);
  529. statement_syssym := p2;
  530. pd := voiddef;
  531. End;
  532. in_include_x_y,
  533. in_exclude_x_y :
  534. begin
  535. consume(_LKLAMMER);
  536. in_args:=true;
  537. p1:=comp_expr(true);
  538. consume(_COMMA);
  539. p2:=comp_expr(true);
  540. statement_syssym:=geninlinenode(l,false,gencallparanode(p1,gencallparanode(p2,nil)));
  541. consume(_RKLAMMER);
  542. pd:=voiddef;
  543. end;
  544. in_assert_x_y :
  545. begin
  546. consume(_LKLAMMER);
  547. in_args:=true;
  548. p1:=comp_expr(true);
  549. if token=_COMMA then
  550. begin
  551. consume(_COMMA);
  552. p2:=comp_expr(true);
  553. end
  554. else
  555. begin
  556. { then insert an empty string }
  557. p2:=genstringconstnode('',st_default);
  558. end;
  559. statement_syssym:=geninlinenode(l,false,gencallparanode(p1,gencallparanode(p2,nil)));
  560. consume(_RKLAMMER);
  561. pd:=voiddef;
  562. end;
  563. else
  564. internalerror(15);
  565. end;
  566. in_args:=prev_in_args;
  567. end;
  568. { reads the parameter for a subroutine call }
  569. procedure do_proc_call(getaddr : boolean;var again : boolean;var p1:tcallnode;var pd:Pdef);
  570. var
  571. prev_in_args : boolean;
  572. prevafterassn : boolean;
  573. hs,hs1 : pvarsym;
  574. st : psymtable;
  575. p2 : tnode;
  576. begin
  577. prev_in_args:=in_args;
  578. prevafterassn:=afterassignment;
  579. afterassignment:=false;
  580. { want we only determine the address of }
  581. { a subroutine ? }
  582. if not(getaddr) then
  583. begin
  584. if auto_inherited then
  585. begin
  586. st:=symtablestack;
  587. while assigned(st) and (st^.symtabletype<>parasymtable) do
  588. st:=st^.next;
  589. p2:=nil;
  590. if assigned(st) then
  591. begin
  592. hs:=pvarsym(st^.symindex^.first);
  593. while assigned(hs) do
  594. begin
  595. if hs^.typ<>varsym then
  596. internalerror(54382953);
  597. { if there is a localcopy then use that }
  598. if assigned(hs^.localvarsym) then
  599. hs1:=hs^.localvarsym
  600. else
  601. hs1:=hs;
  602. p2:=gencallparanode(genloadnode(hs1,hs1^.owner),p2);
  603. hs:=pvarsym(hs^.indexnext);
  604. end;
  605. end
  606. else
  607. internalerror(54382954);
  608. p1.left:=p2;
  609. end
  610. else
  611. begin
  612. if token=_LKLAMMER then
  613. begin
  614. consume(_LKLAMMER);
  615. in_args:=true;
  616. p1.left:=parse_paras(false,false);
  617. consume(_RKLAMMER);
  618. end
  619. else
  620. p1.left:=nil;
  621. end;
  622. { do firstpass because we need the }
  623. { result type }
  624. do_firstpass(p1);
  625. {set_var_state is handled inside firstcalln }
  626. end
  627. else
  628. begin
  629. { address operator @: }
  630. p1.left:=nil;
  631. { forget pd }
  632. pd:=nil;
  633. if (p1.symtableproc^.symtabletype=withsymtable) and
  634. (p1.symtableproc^.defowner^.deftype=objectdef) then
  635. begin
  636. p1.methodpointer:=tnode(pwithsymtable(p1.symtableproc)^.withrefnode).getcopy;
  637. end
  638. else if not(assigned(p1.methodpointer)) then
  639. begin
  640. { we must provide a method pointer, if it isn't given, }
  641. { it is self }
  642. if assigned(procinfo) then
  643. begin
  644. p1.methodpointer:=genselfnode(procinfo^._class);
  645. p1.methodpointer.resulttype:=procinfo^._class;
  646. end
  647. else
  648. begin
  649. p1.methodpointer:=genselfnode(nil);
  650. p1.methodpointer.resulttype:=nil;
  651. end;
  652. end;
  653. { no postfix operators }
  654. again:=false;
  655. end;
  656. pd:=p1.resulttype;
  657. in_args:=prev_in_args;
  658. afterassignment:=prevafterassn;
  659. end;
  660. procedure handle_procvar(pv : pprocvardef;var p2 : tnode);
  661. procedure doconv(procvar : pprocvardef;var t : tnode);
  662. var
  663. hp : tnode;
  664. begin
  665. hp:=nil;
  666. if (proc_to_procvar_equal(pprocsym(tcallnode(t).symtableprocentry)^.definition,procvar)) then
  667. begin
  668. if (po_methodpointer in procvar^.procoptions) then
  669. hp:=genloadmethodcallnode(pprocsym(tcallnode(t).symtableprocentry),tcallnode(t).symtableproc,tnode(tcallnode(t).methodpointer).getcopy)
  670. else
  671. hp:=genloadcallnode(pprocsym(tcallnode(t).symtableprocentry),tcallnode(t).symtableproc);
  672. end;
  673. if assigned(hp) then
  674. begin
  675. t.destroy;
  676. t:=hp;
  677. end;
  678. end;
  679. begin
  680. if (p2.nodetype=calln) then
  681. doconv(pv,tcallnode(p2))
  682. else
  683. if (p2.nodetype=typeconvn) and
  684. (ttypeconvnode(p2).left.nodetype=calln) then
  685. doconv(pv,tcallnode(ttypeconvnode(p2).left));
  686. end;
  687. { the following procedure handles the access to a property symbol }
  688. procedure handle_propertysym(sym : psym;st : psymtable;var p1 : tnode;
  689. var pd : pdef);
  690. var
  691. paras : tnode;
  692. p2 : tnode;
  693. plist : psymlistitem;
  694. begin
  695. paras:=nil;
  696. { property parameters? read them only if the property really }
  697. { has parameters }
  698. if ppo_hasparameters in ppropertysym(sym)^.propoptions then
  699. begin
  700. if token=_LECKKLAMMER then
  701. begin
  702. consume(_LECKKLAMMER);
  703. paras:=parse_paras(false,true);
  704. consume(_RECKKLAMMER);
  705. end;
  706. { indexed property }
  707. if (ppo_indexed in ppropertysym(sym)^.propoptions) then
  708. begin
  709. p2:=genordinalconstnode(ppropertysym(sym)^.index,ppropertysym(sym)^.indextype.def);
  710. paras:=gencallparanode(p2,paras);
  711. end;
  712. end;
  713. { we need only a write property if a := follows }
  714. { if not(afterassignment) and not(in_args) then }
  715. if token=_ASSIGNMENT then
  716. begin
  717. { write property: }
  718. { no result }
  719. pd:=voiddef;
  720. if not ppropertysym(sym)^.writeaccess^.empty then
  721. begin
  722. case ppropertysym(sym)^.writeaccess^.firstsym^.sym^.typ of
  723. procsym :
  724. begin
  725. { generate the method call }
  726. p1:=ccallnode.create(pprocsym(ppropertysym(sym)^.writeaccess^.firstsym^.sym),st,p1);
  727. { we know the procedure to call, so
  728. force the usage of that procedure }
  729. tcallnode(p1).procdefinition:=pprocdef(ppropertysym(sym)^.writeaccess^.def);
  730. tcallnode(p1).left:=paras;
  731. consume(_ASSIGNMENT);
  732. { read the expression }
  733. getprocvar:=ppropertysym(sym)^.proptype.def^.deftype=procvardef;
  734. p2:=comp_expr(true);
  735. if getprocvar then
  736. handle_procvar(pprocvardef(ppropertysym(sym)^.proptype.def),p2);
  737. tcallnode(p1).left:=gencallparanode(p2,tcallnode(p1).left);
  738. include(tcallnode(p1).flags,nf_isproperty);
  739. getprocvar:=false;
  740. end;
  741. varsym :
  742. begin
  743. if assigned(paras) then
  744. message(parser_e_no_paras_allowed);
  745. { subscribed access? }
  746. plist:=ppropertysym(sym)^.writeaccess^.firstsym;
  747. while assigned(plist) do
  748. begin
  749. if p1=nil then
  750. p1:=genloadnode(pvarsym(plist^.sym),st)
  751. else
  752. p1:=gensubscriptnode(pvarsym(plist^.sym),p1);
  753. plist:=plist^.next;
  754. end;
  755. include(tcallnode(p1).flags,nf_isproperty);
  756. consume(_ASSIGNMENT);
  757. { read the expression }
  758. p2:=comp_expr(true);
  759. p1:=cassignmentnode.create(p1,p2);
  760. end
  761. else
  762. begin
  763. p1:=cerrornode.create;
  764. Message(parser_e_no_procedure_to_access_property);
  765. end;
  766. end;
  767. end
  768. else
  769. begin
  770. p1:=cerrornode.create;
  771. Message(parser_e_no_procedure_to_access_property);
  772. end;
  773. end
  774. else
  775. begin
  776. { read property: }
  777. pd:=ppropertysym(sym)^.proptype.def;
  778. if not ppropertysym(sym)^.readaccess^.empty then
  779. begin
  780. case ppropertysym(sym)^.readaccess^.firstsym^.sym^.typ of
  781. varsym :
  782. begin
  783. if assigned(paras) then
  784. message(parser_e_no_paras_allowed);
  785. { subscribed access? }
  786. plist:=ppropertysym(sym)^.readaccess^.firstsym;
  787. while assigned(plist) do
  788. begin
  789. if p1=nil then
  790. p1:=genloadnode(pvarsym(plist^.sym),st)
  791. else
  792. p1:=gensubscriptnode(pvarsym(plist^.sym),p1);
  793. plist:=plist^.next;
  794. end;
  795. include(p1.flags,nf_isproperty);
  796. end;
  797. procsym :
  798. begin
  799. { generate the method call }
  800. p1:=ccallnode.create(pprocsym(ppropertysym(sym)^.readaccess^.firstsym^.sym),st,p1);
  801. { we know the procedure to call, so
  802. force the usage of that procedure }
  803. tcallnode(p1).procdefinition:=pprocdef(ppropertysym(sym)^.readaccess^.def);
  804. { insert paras }
  805. tcallnode(p1).left:=paras;
  806. include(p1.flags,nf_isproperty);
  807. end
  808. else
  809. begin
  810. p1:=cerrornode.create;
  811. Message(type_e_mismatch);
  812. end;
  813. end;
  814. end
  815. else
  816. begin
  817. { error, no function to read property }
  818. p1:=cerrornode.create;
  819. Message(parser_e_no_procedure_to_access_property);
  820. end;
  821. end;
  822. end;
  823. { the ID token has to be consumed before calling this function }
  824. procedure do_member_read(getaddr : boolean;const sym : psym;var p1 : tnode;
  825. var pd : pdef;var again : boolean);
  826. var
  827. static_name : string;
  828. isclassref : boolean;
  829. objdef : pobjectdef;
  830. begin
  831. if sym=nil then
  832. begin
  833. { pattern is still valid unless
  834. there is another ID just after the ID of sym }
  835. Message1(sym_e_id_no_member,pattern);
  836. p1.destroy;
  837. p1:=cerrornode.create;
  838. { try to clean up }
  839. pd:=generrordef;
  840. again:=false;
  841. end
  842. else
  843. begin
  844. objdef:=pobjectdef(sym^.owner^.defowner);
  845. isclassref:=(pd^.deftype=classrefdef);
  846. { check protected and private members }
  847. { please leave this code as it is, }
  848. { it has now the same behaviaor as TP/Delphi }
  849. if (sp_private in sym^.symoptions) and
  850. (objdef^.owner^.symtabletype=unitsymtable) then
  851. Message(parser_e_cant_access_private_member);
  852. if (sp_protected in sym^.symoptions) and
  853. (objdef^.owner^.symtabletype=unitsymtable) then
  854. begin
  855. if assigned(aktprocsym^.definition^._class) then
  856. begin
  857. if not aktprocsym^.definition^._class^.is_related(objdef) then
  858. Message(parser_e_cant_access_protected_member);
  859. end
  860. else
  861. Message(parser_e_cant_access_protected_member);
  862. end;
  863. { we assume, that only procsyms and varsyms are in an object }
  864. { symbol table, for classes, properties are allowed }
  865. case sym^.typ of
  866. procsym:
  867. begin
  868. p1:=ccallnode.create(pprocsym(sym),sym^.owner,p1);
  869. do_proc_call(getaddr or
  870. (getprocvar and
  871. ((block_type=bt_const) or
  872. ((m_tp_procvar in aktmodeswitches) and
  873. proc_to_procvar_equal(pprocsym(sym)^.definition,getprocvardef)
  874. )
  875. )
  876. ),again,tcallnode(p1),pd);
  877. if (block_type=bt_const) and
  878. getprocvar then
  879. handle_procvar(getprocvardef,p1);
  880. { now we know the real method e.g. we can check for a class method }
  881. if isclassref and
  882. assigned(tcallnode(p1).procdefinition) and
  883. not(po_classmethod in tcallnode(p1).procdefinition^.procoptions) and
  884. not(tcallnode(p1).procdefinition^.proctypeoption=potype_constructor) then
  885. Message(parser_e_only_class_methods_via_class_ref);
  886. end;
  887. varsym:
  888. begin
  889. if isclassref then
  890. Message(parser_e_only_class_methods_via_class_ref);
  891. if (sp_static in sym^.symoptions) then
  892. begin
  893. { static_name:=lower(srsymtable^.name^)+'_'+sym^.name;
  894. this is wrong for static field in with symtable (PM) }
  895. static_name:=lower(srsym^.owner^.name^)+'_'+sym^.name;
  896. getsym(static_name,true);
  897. p1.destroy;
  898. p1:=genloadnode(pvarsym(srsym),srsymtable);
  899. end
  900. else
  901. p1:=gensubscriptnode(pvarsym(sym),p1);
  902. pd:=pvarsym(sym)^.vartype.def;
  903. end;
  904. propertysym:
  905. begin
  906. if isclassref then
  907. Message(parser_e_only_class_methods_via_class_ref);
  908. handle_propertysym(sym,srsymtable,p1,pd);
  909. end;
  910. else internalerror(16);
  911. end;
  912. end;
  913. end;
  914. {****************************************************************************
  915. Factor
  916. ****************************************************************************}
  917. {$ifdef fpc}
  918. {$maxfpuregisters 0}
  919. {$endif fpc}
  920. function factor(getaddr : boolean) : tnode;
  921. var
  922. l : longint;
  923. ic : TConstExprInt;
  924. oldp1,
  925. p1,p2,p3 : tnode;
  926. code : integer;
  927. pd,pd2 : pdef;
  928. possible_error,
  929. {$ifdef TEST_PROCSYMS}
  930. unit_specific,
  931. {$endif TEST_PROCSYMS}
  932. again : boolean;
  933. sym : psym;
  934. classh : pobjectdef;
  935. d : bestreal;
  936. hs,
  937. static_name : string;
  938. propsym : ppropertysym;
  939. filepos : tfileposinfo;
  940. {---------------------------------------------
  941. Is_func_ret
  942. ---------------------------------------------}
  943. function is_func_ret(sym : psym) : boolean;
  944. var
  945. p : pprocinfo;
  946. storesymtablestack : psymtable;
  947. begin
  948. is_func_ret:=false;
  949. if not assigned(procinfo) or
  950. ((sym^.typ<>funcretsym) and ((procinfo^.flags and pi_operator)=0)) then
  951. exit;
  952. p:=procinfo;
  953. while assigned(p) do
  954. begin
  955. { is this an access to a function result? Accessing _RESULT is
  956. always allowed and funcretn is generated }
  957. if assigned(p^.funcretsym) and
  958. ((pfuncretsym(sym)=p^.resultfuncretsym) or
  959. ((pfuncretsym(sym)=p^.funcretsym) or
  960. ((pvarsym(sym)=opsym) and ((p^.flags and pi_operator)<>0))) and
  961. (p^.returntype.def<>pdef(voiddef)) and
  962. (token<>_LKLAMMER) and
  963. (not ((m_tp in aktmodeswitches) and (afterassignment or in_args)))
  964. ) then
  965. begin
  966. if ((pvarsym(sym)=opsym) and
  967. ((p^.flags and pi_operator)<>0)) then
  968. inc(opsym^.refs);
  969. p1:=cfuncretnode.create;
  970. pd:=p^.returntype.def;
  971. tfuncretnode(p1).funcretprocinfo:=p;
  972. tfuncretnode(p1).rettype.def:=pd;
  973. is_func_ret:=true;
  974. if p^.funcret_state=vs_declared then
  975. begin
  976. p^.funcret_state:=vs_declared_and_first_found;
  977. include(p1.flags,nf_is_first_funcret);
  978. end;
  979. exit;
  980. end;
  981. p:=p^.parent;
  982. end;
  983. { we must use the function call }
  984. if (sym^.typ=funcretsym) then
  985. begin
  986. storesymtablestack:=symtablestack;
  987. symtablestack:=srsymtable^.next;
  988. getsym(sym^.name,true);
  989. if srsym^.typ<>procsym then
  990. Message(cg_e_illegal_expression);
  991. symtablestack:=storesymtablestack;
  992. end;
  993. end;
  994. {---------------------------------------------
  995. Factor_read_id
  996. ---------------------------------------------}
  997. procedure factor_read_id;
  998. var
  999. pc : pchar;
  1000. len : longint;
  1001. begin
  1002. { allow post fix operators }
  1003. again:=true;
  1004. begin
  1005. if lastsymknown then
  1006. begin
  1007. srsym:=lastsrsym;
  1008. srsymtable:=lastsrsymtable;
  1009. lastsymknown:=false;
  1010. end
  1011. else
  1012. getsym(pattern,true);
  1013. consume(_ID);
  1014. if not is_func_ret(srsym) then
  1015. { else it's a normal symbol }
  1016. begin
  1017. { is it defined like UNIT.SYMBOL ? }
  1018. if srsym^.typ=unitsym then
  1019. begin
  1020. consume(_POINT);
  1021. getsymonlyin(punitsym(srsym)^.unitsymtable,pattern);
  1022. {$ifdef TEST_PROCSYMS}
  1023. unit_specific:=true;
  1024. {$endif TEST_PROCSYMS}
  1025. consume(_ID);
  1026. {$ifdef TEST_PROCSYMS}
  1027. end
  1028. else
  1029. unit_specific:=false;
  1030. {$else TEST_PROCSYMS}
  1031. end;
  1032. {$endif TEST_PROCSYMS}
  1033. if not assigned(srsym) then
  1034. Begin
  1035. p1:=cerrornode.create;
  1036. { try to clean up }
  1037. pd:=generrordef;
  1038. end
  1039. else
  1040. Begin
  1041. { check semantics of private }
  1042. if (srsym^.typ in [propertysym,procsym,varsym]) and
  1043. (srsymtable^.symtabletype=objectsymtable) then
  1044. begin
  1045. if (sp_private in srsym^.symoptions) and
  1046. (pobjectdef(srsym^.owner^.defowner)^.owner^.symtabletype=unitsymtable) then
  1047. Message(parser_e_cant_access_private_member);
  1048. end;
  1049. case srsym^.typ of
  1050. absolutesym : begin
  1051. p1:=genloadnode(pvarsym(srsym),srsymtable);
  1052. pd:=pabsolutesym(srsym)^.vartype.def;
  1053. end;
  1054. varsym : begin
  1055. { are we in a class method ? }
  1056. if (srsymtable^.symtabletype=objectsymtable) and
  1057. assigned(aktprocsym) and
  1058. (po_classmethod in aktprocsym^.definition^.procoptions) then
  1059. Message(parser_e_only_class_methods);
  1060. if (sp_static in srsym^.symoptions) then
  1061. begin
  1062. static_name:=lower(srsym^.owner^.name^)+'_'+srsym^.name;
  1063. getsym(static_name,true);
  1064. end;
  1065. p1:=genloadnode(pvarsym(srsym),srsymtable);
  1066. if pvarsym(srsym)^.varstate=vs_declared then
  1067. begin
  1068. include(p1.flags,nf_first);
  1069. { set special between first loaded until checked in firstpass }
  1070. pvarsym(srsym)^.varstate:=vs_declared_and_first_found;
  1071. end;
  1072. pd:=pvarsym(srsym)^.vartype.def;
  1073. end;
  1074. typedconstsym : begin
  1075. p1:=gentypedconstloadnode(ptypedconstsym(srsym),srsymtable);
  1076. pd:=ptypedconstsym(srsym)^.typedconsttype.def;
  1077. end;
  1078. syssym : p1:=statement_syssym(psyssym(srsym)^.number,pd);
  1079. typesym : begin
  1080. pd:=ptypesym(srsym)^.restype.def;
  1081. if not assigned(pd) then
  1082. begin
  1083. pd:=generrordef;
  1084. again:=false;
  1085. end
  1086. else
  1087. begin
  1088. { if we read a type declaration }
  1089. { we have to return the type and }
  1090. { nothing else }
  1091. if block_type=bt_type then
  1092. begin
  1093. { we don't need sym reference when it's in the
  1094. current unit or system unit, because those
  1095. units are always loaded (PFV) }
  1096. if not(assigned(pd^.owner)) or
  1097. (pd^.owner^.unitid=0) or
  1098. (pd^.owner^.unitid=1) then
  1099. p1:=gentypenode(pd,nil)
  1100. else
  1101. p1:=gentypenode(pd,ptypesym(srsym));
  1102. { here we can also set resulttype !! }
  1103. p1.resulttype:=pd;
  1104. pd:=voiddef;
  1105. end
  1106. else { not type block }
  1107. begin
  1108. if token=_LKLAMMER then
  1109. begin
  1110. consume(_LKLAMMER);
  1111. p1:=comp_expr(true);
  1112. consume(_RKLAMMER);
  1113. p1:=gentypeconvnode(p1,pd);
  1114. include(p1.flags,nf_explizit);
  1115. end
  1116. else { not LKLAMMER }
  1117. if (token=_POINT) and
  1118. is_object(pd) then
  1119. begin
  1120. consume(_POINT);
  1121. if assigned(procinfo) and
  1122. assigned(procinfo^._class) and
  1123. not(getaddr) then
  1124. begin
  1125. if procinfo^._class^.is_related(pobjectdef(pd)) then
  1126. begin
  1127. p1:=gentypenode(pd,ptypesym(srsym));
  1128. p1.resulttype:=pd;
  1129. { search also in inherited methods }
  1130. repeat
  1131. srsymtable:=pobjectdef(pd)^.symtable;
  1132. sym:=pvarsym(srsymtable^.search(pattern));
  1133. if assigned(sym) then
  1134. break;
  1135. pd:=pobjectdef(pd)^.childof;
  1136. until not assigned(pd);
  1137. consume(_ID);
  1138. do_member_read(false,sym,p1,pd,again);
  1139. end
  1140. else
  1141. begin
  1142. Message(parser_e_no_super_class);
  1143. pd:=generrordef;
  1144. again:=false;
  1145. end;
  1146. end
  1147. else
  1148. begin
  1149. { allows @TObject.Load }
  1150. { also allows static methods and variables }
  1151. p1:=ctypenode.create(nil,nil);
  1152. p1.resulttype:=pd;
  1153. { TP allows also @TMenu.Load if Load is only }
  1154. { defined in an anchestor class }
  1155. sym:=pvarsym(search_class_member(pobjectdef(pd),pattern));
  1156. if not assigned(sym) then
  1157. Message1(sym_e_id_no_member,pattern)
  1158. else if not(getaddr) and not(sp_static in sym^.symoptions) then
  1159. Message(sym_e_only_static_in_static)
  1160. else
  1161. begin
  1162. consume(_ID);
  1163. do_member_read(getaddr,sym,p1,pd,again);
  1164. end;
  1165. end;
  1166. end
  1167. else
  1168. begin
  1169. { class reference ? }
  1170. if is_object(pd) then
  1171. begin
  1172. if getaddr and (token=_POINT) then
  1173. begin
  1174. consume(_POINT);
  1175. { allows @Object.Method }
  1176. { also allows static methods and variables }
  1177. p1:=gentypenode(nil,nil);
  1178. p1.resulttype:=pd;
  1179. { TP allows also @TMenu.Load if Load is only }
  1180. { defined in an anchestor class }
  1181. sym:=pvarsym(search_class_member(pobjectdef(pd),pattern));
  1182. if not assigned(sym) then
  1183. Message1(sym_e_id_no_member,pattern)
  1184. else
  1185. begin
  1186. consume(_ID);
  1187. do_member_read(getaddr,sym,p1,pd,again);
  1188. end;
  1189. end
  1190. else
  1191. begin
  1192. p1:=gentypenode(pd,nil);
  1193. p1.resulttype:=pd;
  1194. pd:=new(pclassrefdef,init(pd));
  1195. p1:=cloadvmtnode.create(p1);
  1196. p1.resulttype:=pd;
  1197. end;
  1198. end
  1199. else
  1200. begin
  1201. { generate a type node }
  1202. { (for typeof etc) }
  1203. if allow_type then
  1204. begin
  1205. p1:=gentypenode(pd,nil);
  1206. { here we must use typenodetype explicitly !! PM
  1207. p1.resulttype:=pd; }
  1208. pd:=voiddef;
  1209. end
  1210. else
  1211. Message(parser_e_no_type_not_allowed_here);
  1212. end;
  1213. end;
  1214. end;
  1215. end;
  1216. end;
  1217. enumsym : begin
  1218. p1:=genenumnode(penumsym(srsym));
  1219. pd:=p1.resulttype;
  1220. end;
  1221. constsym : begin
  1222. case pconstsym(srsym)^.consttyp of
  1223. constint :
  1224. { do a very dirty trick to bootstrap this code }
  1225. if (pconstsym(srsym)^.value>=-(int64(2147483647)+int64(1))) and (pconstsym(srsym)^.value<=2147483647) then
  1226. p1:=genordinalconstnode(pconstsym(srsym)^.value,s32bitdef)
  1227. else
  1228. p1:=genordinalconstnode(pconstsym(srsym)^.value,cs64bitdef);
  1229. conststring :
  1230. begin
  1231. len:=pconstsym(srsym)^.len;
  1232. if not(cs_ansistrings in aktlocalswitches) and (len>255) then
  1233. len:=255;
  1234. getmem(pc,len+1);
  1235. move(pchar(tpointerord(pconstsym(srsym)^.value))^,pc^,len);
  1236. pc[len]:=#0;
  1237. p1:=genpcharconstnode(pc,len);
  1238. end;
  1239. constchar :
  1240. p1:=genordinalconstnode(pconstsym(srsym)^.value,cchardef);
  1241. constreal :
  1242. p1:=genrealconstnode(pbestreal(tpointerord(pconstsym(srsym)^.value))^,bestrealdef^);
  1243. constbool :
  1244. p1:=genordinalconstnode(pconstsym(srsym)^.value,booldef);
  1245. constset :
  1246. p1:=gensetconstnode(pconstset(tpointerord(pconstsym(srsym)^.value)),
  1247. psetdef(pconstsym(srsym)^.consttype.def));
  1248. constord :
  1249. p1:=genordinalconstnode(pconstsym(srsym)^.value,
  1250. pconstsym(srsym)^.consttype.def);
  1251. constpointer :
  1252. p1:=genpointerconstnode(pconstsym(srsym)^.value,
  1253. pconstsym(srsym)^.consttype.def);
  1254. constnil :
  1255. p1:=cnilnode.create;
  1256. constresourcestring:
  1257. begin
  1258. p1:=genloadnode(pvarsym(srsym),srsymtable);
  1259. p1.resulttype:=cansistringdef;
  1260. end;
  1261. end;
  1262. pd:=p1.resulttype;
  1263. end;
  1264. procsym : begin
  1265. { are we in a class method ? }
  1266. possible_error:=(srsymtable^.symtabletype=objectsymtable) and
  1267. assigned(aktprocsym) and
  1268. (po_classmethod in aktprocsym^.definition^.procoptions);
  1269. p1:=gencallnode(pprocsym(srsym),srsymtable);
  1270. {$ifdef TEST_PROCSYMS}
  1271. p1.unit_specific:=unit_specific;
  1272. {$endif TEST_PROCSYMS}
  1273. do_proc_call(getaddr or
  1274. (getprocvar and
  1275. ((block_type=bt_const) or
  1276. ((m_tp_procvar in aktmodeswitches) and
  1277. proc_to_procvar_equal(pprocsym(srsym)^.definition,getprocvardef)
  1278. )
  1279. )
  1280. ),again,tcallnode(p1),pd);
  1281. if (block_type=bt_const) and
  1282. getprocvar then
  1283. handle_procvar(getprocvardef,p1);
  1284. if possible_error and
  1285. not(po_classmethod in tcallnode(p1).procdefinition^.procoptions) then
  1286. Message(parser_e_only_class_methods);
  1287. end;
  1288. propertysym : begin
  1289. { access to property in a method }
  1290. { are we in a class method ? }
  1291. if (srsymtable^.symtabletype=objectsymtable) and
  1292. assigned(aktprocsym) and
  1293. (po_classmethod in aktprocsym^.definition^.procoptions) then
  1294. Message(parser_e_only_class_methods);
  1295. { no method pointer }
  1296. p1:=nil;
  1297. handle_propertysym(srsym,srsymtable,p1,pd);
  1298. end;
  1299. errorsym : begin
  1300. p1:=cerrornode.create;
  1301. p1.resulttype:=generrordef;
  1302. pd:=generrordef;
  1303. if token=_LKLAMMER then
  1304. begin
  1305. consume(_LKLAMMER);
  1306. parse_paras(false,false);
  1307. consume(_RKLAMMER);
  1308. end;
  1309. end;
  1310. else
  1311. begin
  1312. p1:=cerrornode.create;
  1313. pd:=generrordef;
  1314. Message(cg_e_illegal_expression);
  1315. end;
  1316. end; { end case }
  1317. end;
  1318. end;
  1319. end;
  1320. end;
  1321. {---------------------------------------------
  1322. Factor_Read_Set
  1323. ---------------------------------------------}
  1324. { Read a set between [] }
  1325. function factor_read_set:tnode;
  1326. var
  1327. p1 : tnode;
  1328. lastp,
  1329. buildp : tarrayconstructornode;
  1330. begin
  1331. buildp:=nil;
  1332. { be sure that a least one arrayconstructn is used, also for an
  1333. empty [] }
  1334. if token=_RECKKLAMMER then
  1335. buildp:=carrayconstructornode.create(nil,buildp)
  1336. else
  1337. begin
  1338. while true do
  1339. begin
  1340. p1:=comp_expr(true);
  1341. if token=_POINTPOINT then
  1342. begin
  1343. consume(_POINTPOINT);
  1344. p2:=comp_expr(true);
  1345. p1:=carrayconstructorrangenode.create(p1,p2);
  1346. end;
  1347. { insert at the end of the tree, to get the correct order }
  1348. if not assigned(buildp) then
  1349. begin
  1350. buildp:=carrayconstructornode.create(p1,nil);
  1351. lastp:=buildp;
  1352. end
  1353. else
  1354. begin
  1355. lastp.right:=carrayconstructornode.create(p1,nil);
  1356. lastp:=tarrayconstructornode(lastp.right);
  1357. end;
  1358. { there could be more elements }
  1359. if token=_COMMA then
  1360. consume(_COMMA)
  1361. else
  1362. break;
  1363. end;
  1364. end;
  1365. factor_read_set:=buildp;
  1366. end;
  1367. {---------------------------------------------
  1368. Helpers
  1369. ---------------------------------------------}
  1370. procedure check_tokenpos;
  1371. begin
  1372. if (p1<>oldp1) then
  1373. begin
  1374. if assigned(p1) then
  1375. p1.set_tree_filepos(filepos);
  1376. oldp1:=p1;
  1377. filepos:=akttokenpos;
  1378. end;
  1379. end;
  1380. {---------------------------------------------
  1381. PostFixOperators
  1382. ---------------------------------------------}
  1383. procedure postfixoperators;
  1384. { tries to avoid syntax errors after invalid qualifiers }
  1385. procedure recoverconsume_postfixops;
  1386. begin
  1387. while true do
  1388. begin
  1389. case token of
  1390. _CARET:
  1391. consume(_CARET);
  1392. _POINT:
  1393. begin
  1394. consume(_POINT);
  1395. if token=_ID then
  1396. consume(_ID);
  1397. end;
  1398. _LECKKLAMMER:
  1399. begin
  1400. consume(_LECKKLAMMER);
  1401. repeat
  1402. comp_expr(true);
  1403. if token=_COMMA then
  1404. consume(_COMMA)
  1405. else
  1406. break;
  1407. until false;
  1408. consume(_RECKKLAMMER);
  1409. end
  1410. else
  1411. break;
  1412. end;
  1413. end;
  1414. end;
  1415. var
  1416. store_static : boolean;
  1417. { p1 and p2 must contain valid value_str }
  1418. begin
  1419. check_tokenpos;
  1420. while again do
  1421. begin
  1422. { prevent crashes with unknown types }
  1423. if not assigned(pd) then
  1424. begin
  1425. recoverconsume_postfixops;
  1426. exit;
  1427. end;
  1428. { handle token }
  1429. case token of
  1430. _CARET:
  1431. begin
  1432. consume(_CARET);
  1433. if (pd^.deftype<>pointerdef) then
  1434. begin
  1435. { ^ as binary operator is a problem!!!! (FK) }
  1436. again:=false;
  1437. Message(cg_e_invalid_qualifier);
  1438. recoverconsume_postfixops;
  1439. p1.destroy;
  1440. p1:=cerrornode.create;
  1441. end
  1442. else
  1443. begin
  1444. p1:=cderefnode.create(p1);
  1445. pd:=ppointerdef(pd)^.pointertype.def;
  1446. end;
  1447. end;
  1448. _LECKKLAMMER:
  1449. begin
  1450. if is_class_or_interface(pd) then
  1451. begin
  1452. { default property }
  1453. propsym:=search_default_property(pobjectdef(pd));
  1454. if not(assigned(propsym)) then
  1455. begin
  1456. p1.destroy;
  1457. p1:=cerrornode.create;
  1458. again:=false;
  1459. message(parser_e_no_default_property_available);
  1460. end
  1461. else
  1462. handle_propertysym(propsym,propsym^.owner,p1,pd);
  1463. end
  1464. else
  1465. begin
  1466. consume(_LECKKLAMMER);
  1467. repeat
  1468. case pd^.deftype of
  1469. pointerdef:
  1470. begin
  1471. pd:=ppointerdef(pd)^.pointertype.def;
  1472. { support delphi autoderef }
  1473. if (pd^.deftype=arraydef) and
  1474. (m_autoderef in aktmodeswitches) then
  1475. begin
  1476. p1:=cderefnode.create(p1);
  1477. pd:=parraydef(pd)^.elementtype.def;
  1478. end;
  1479. p2:=comp_expr(true);
  1480. p1:=cvecnode.create(p1,p2);
  1481. end;
  1482. stringdef : begin
  1483. p2:=comp_expr(true);
  1484. p1:=cvecnode.create(p1,p2);
  1485. pd:=cchardef
  1486. end;
  1487. arraydef : begin
  1488. p2:=comp_expr(true);
  1489. { support SEG:OFS for go32v2 Mem[] }
  1490. if (target_info.target=target_i386_go32v2) and
  1491. (p1.nodetype=loadn) and
  1492. assigned(tloadnode(p1).symtableentry) and
  1493. assigned(tloadnode(p1).symtableentry^.owner^.name) and
  1494. (tloadnode(p1).symtableentry^.owner^.name^='SYSTEM') and
  1495. ((tloadnode(p1).symtableentry^.name='MEM') or
  1496. (tloadnode(p1).symtableentry^.name='MEMW') or
  1497. (tloadnode(p1).symtableentry^.name='MEML')) then
  1498. begin
  1499. if (token=_COLON) then
  1500. begin
  1501. consume(_COLON);
  1502. p3:=caddnode.create(muln,genordinalconstnode($10,s32bitdef),p2);
  1503. p2:=comp_expr(true);
  1504. p2:=caddnode.create(addn,p2,p3);
  1505. p1:=cvecnode.create(p1,p2);
  1506. include(tvecnode(p1).flags,nf_memseg);
  1507. include(tvecnode(p1).flags,nf_memindex);
  1508. end
  1509. else
  1510. begin
  1511. p1:=cvecnode.create(p1,p2);
  1512. include(tvecnode(p1).flags,nf_memindex);
  1513. end;
  1514. end
  1515. else
  1516. p1:=cvecnode.create(p1,p2);
  1517. pd:=parraydef(pd)^.elementtype.def;
  1518. end;
  1519. else
  1520. begin
  1521. Message(cg_e_invalid_qualifier);
  1522. p1.destroy;
  1523. p1:=cerrornode.create;
  1524. comp_expr(true);
  1525. again:=false;
  1526. end;
  1527. end;
  1528. if token=_COMMA then
  1529. consume(_COMMA)
  1530. else
  1531. break;
  1532. until false;
  1533. consume(_RECKKLAMMER);
  1534. end;
  1535. end;
  1536. _POINT : begin
  1537. consume(_POINT);
  1538. if (pd^.deftype=pointerdef) and
  1539. (m_autoderef in aktmodeswitches) then
  1540. begin
  1541. p1:=cderefnode.create(p1);
  1542. pd:=ppointerdef(pd)^.pointertype.def;
  1543. end;
  1544. case pd^.deftype of
  1545. recorddef:
  1546. begin
  1547. sym:=psym(precorddef(pd)^.symtable^.search(pattern));
  1548. if assigned(sym) and
  1549. (sym^.typ=varsym) then
  1550. begin
  1551. p1:=gensubscriptnode(pvarsym(sym),p1);
  1552. pd:=pvarsym(sym)^.vartype.def;
  1553. end
  1554. else
  1555. begin
  1556. Message1(sym_e_illegal_field,pattern);
  1557. p1.destroy;
  1558. p1:=cerrornode.create;
  1559. end;
  1560. consume(_ID);
  1561. end;
  1562. classrefdef:
  1563. begin
  1564. classh:=pobjectdef(pclassrefdef(pd)^.pointertype.def);
  1565. sym:=nil;
  1566. while assigned(classh) do
  1567. begin
  1568. sym:=psym(classh^.symtable^.search(pattern));
  1569. srsymtable:=classh^.symtable;
  1570. if assigned(sym) then
  1571. break;
  1572. classh:=classh^.childof;
  1573. end;
  1574. if sym=nil then
  1575. begin
  1576. Message1(sym_e_id_no_member,pattern);
  1577. p1.destroy;
  1578. p1:=cerrornode.create;
  1579. { try to clean up }
  1580. pd:=generrordef;
  1581. consume(_ID);
  1582. end
  1583. else
  1584. begin
  1585. consume(_ID);
  1586. do_member_read(getaddr,sym,p1,pd,again);
  1587. end;
  1588. end;
  1589. objectdef:
  1590. begin
  1591. classh:=pobjectdef(pd);
  1592. sym:=nil;
  1593. store_static:=allow_only_static;
  1594. allow_only_static:=false;
  1595. while assigned(classh) do
  1596. begin
  1597. sym:=psym(classh^.symtable^.search(pattern));
  1598. srsymtable:=classh^.symtable;
  1599. if assigned(sym) then
  1600. break;
  1601. classh:=classh^.childof;
  1602. end;
  1603. allow_only_static:=store_static;
  1604. if sym=nil then
  1605. begin
  1606. Message1(sym_e_id_no_member,pattern);
  1607. p1.destroy;
  1608. p1:=cerrornode.create;
  1609. { try to clean up }
  1610. pd:=generrordef;
  1611. consume(_ID);
  1612. end
  1613. else
  1614. begin
  1615. consume(_ID);
  1616. do_member_read(getaddr,sym,p1,pd,again);
  1617. end;
  1618. end;
  1619. pointerdef:
  1620. begin
  1621. Message(cg_e_invalid_qualifier);
  1622. if ppointerdef(pd)^.pointertype.def^.deftype in [recorddef,objectdef,classrefdef] then
  1623. Message(parser_h_maybe_deref_caret_missing);
  1624. end;
  1625. else
  1626. begin
  1627. Message(cg_e_invalid_qualifier);
  1628. p1.destroy;
  1629. p1:=cerrornode.create;
  1630. consume(_ID);
  1631. end;
  1632. end;
  1633. end;
  1634. else
  1635. begin
  1636. { is this a procedure variable ? }
  1637. if assigned(pd) then
  1638. begin
  1639. if (pd^.deftype=procvardef) then
  1640. begin
  1641. if getprocvar and is_equal(pd,getprocvardef) then
  1642. again:=false
  1643. else
  1644. if (token=_LKLAMMER) or
  1645. ((pprocvardef(pd)^.para^.empty) and
  1646. (not((token in [_ASSIGNMENT,_UNEQUAL,_EQUAL]))) and
  1647. (not afterassignment) and
  1648. (not in_args)) then
  1649. begin
  1650. { do this in a strange way }
  1651. { it's not a clean solution }
  1652. p2:=p1;
  1653. p1:=gencallnode(nil,nil);
  1654. tcallnode(p1).right:=p2;
  1655. {$ifdef TEST_PROCSYMS}
  1656. p1.unit_specific:=unit_specific;
  1657. {$endif TEST_PROCSYMS}
  1658. tcallnode(p1).symtableprocentry:=pprocsym(sym);
  1659. if token=_LKLAMMER then
  1660. begin
  1661. consume(_LKLAMMER);
  1662. tcallnode(p1).left:=parse_paras(false,false);
  1663. consume(_RKLAMMER);
  1664. end;
  1665. pd:=pprocvardef(pd)^.rettype.def;
  1666. { proc():= is never possible }
  1667. if token=_ASSIGNMENT then
  1668. begin
  1669. Message(cg_e_illegal_expression);
  1670. p1:=cerrornode.create;
  1671. again:=false;
  1672. end;
  1673. p1.resulttype:=pd;
  1674. end
  1675. else
  1676. again:=false;
  1677. p1.resulttype:=pd;
  1678. end
  1679. else
  1680. again:=false;
  1681. end
  1682. else
  1683. again:=false;
  1684. end;
  1685. end;
  1686. check_tokenpos;
  1687. end; { while again }
  1688. end;
  1689. {---------------------------------------------
  1690. Factor (Main)
  1691. ---------------------------------------------}
  1692. begin
  1693. oldp1:=nil;
  1694. p1:=nil;
  1695. filepos:=akttokenpos;
  1696. if token=_ID then
  1697. begin
  1698. factor_read_id;
  1699. { handle post fix operators }
  1700. postfixoperators;
  1701. end
  1702. else
  1703. case token of
  1704. _NEW : begin
  1705. consume(_NEW);
  1706. consume(_LKLAMMER);
  1707. {allow_type:=true;}
  1708. p1:=factor(false);
  1709. {allow_type:=false;}
  1710. if p1.nodetype<>typen then
  1711. begin
  1712. Message(type_e_type_id_expected);
  1713. p1.destroy;
  1714. pd:=generrordef;
  1715. end
  1716. else
  1717. pd:=ttypenode(p1).typenodetype;
  1718. pd2:=pd;
  1719. if (pd^.deftype<>pointerdef) then
  1720. Message1(type_e_pointer_type_expected,pd^.typename)
  1721. else
  1722. if token=_RKLAMMER then
  1723. begin
  1724. if (ppointerdef(pd)^.pointertype.def^.deftype=objectdef) and
  1725. (oo_has_vmt in pobjectdef(ppointerdef(pd)^.pointertype.def)^.objectoptions) then
  1726. Message(parser_w_use_extended_syntax_for_objects);
  1727. p1:=cnewnode.create(nil);
  1728. p1.resulttype:=pd2;
  1729. consume(_RKLAMMER);
  1730. end
  1731. else
  1732. begin
  1733. p1.destroy;
  1734. p1:=chnewnode.create;
  1735. p1.resulttype:=ppointerdef(pd)^.pointertype.def;
  1736. consume(_COMMA);
  1737. afterassignment:=false;
  1738. { determines the current object defintion }
  1739. classh:=pobjectdef(ppointerdef(pd)^.pointertype.def);
  1740. if classh^.deftype<>objectdef then
  1741. Message(parser_e_pointer_to_class_expected)
  1742. else
  1743. begin
  1744. { check for an abstract class }
  1745. if (oo_has_abstract in classh^.objectoptions) then
  1746. Message(sym_e_no_instance_of_abstract_object);
  1747. { search the constructor also in the symbol tables of
  1748. the parents }
  1749. sym:=nil;
  1750. while assigned(classh) do
  1751. begin
  1752. sym:=psym(classh^.symtable^.search(pattern));
  1753. srsymtable:=classh^.symtable;
  1754. if assigned(sym) then
  1755. break;
  1756. classh:=classh^.childof;
  1757. end;
  1758. consume(_ID);
  1759. do_member_read(false,sym,p1,pd,again);
  1760. if (p1.nodetype<>calln) or
  1761. (assigned(tcallnode(p1).procdefinition) and
  1762. (tcallnode(p1).procdefinition^.proctypeoption<>potype_constructor)) then
  1763. Message(parser_e_expr_have_to_be_constructor_call);
  1764. end;
  1765. p1:=cnewnode.create(p1);
  1766. { set the resulttype }
  1767. p1.resulttype:=pd2;
  1768. consume(_RKLAMMER);
  1769. end;
  1770. postfixoperators;
  1771. end;
  1772. _SELF : begin
  1773. again:=true;
  1774. consume(_SELF);
  1775. if not assigned(procinfo^._class) then
  1776. begin
  1777. p1:=cerrornode.create;
  1778. pd:=generrordef;
  1779. again:=false;
  1780. Message(parser_e_self_not_in_method);
  1781. end
  1782. else
  1783. begin
  1784. if (po_classmethod in aktprocsym^.definition^.procoptions) then
  1785. begin
  1786. { self in class methods is a class reference type }
  1787. pd:=new(pclassrefdef,init(procinfo^._class));
  1788. p1:=genselfnode(pd);
  1789. p1.resulttype:=pd;
  1790. end
  1791. else
  1792. begin
  1793. p1:=genselfnode(procinfo^._class);
  1794. p1.resulttype:=procinfo^._class;
  1795. end;
  1796. pd:=p1.resulttype;
  1797. postfixoperators;
  1798. end;
  1799. end;
  1800. _INHERITED : begin
  1801. again:=true;
  1802. consume(_INHERITED);
  1803. if assigned(procinfo^._class) then
  1804. begin
  1805. { if inherited; only then we need the method with
  1806. the same name }
  1807. if token=_SEMICOLON then
  1808. begin
  1809. hs:=aktprocsym^.name;
  1810. auto_inherited:=true
  1811. end
  1812. else
  1813. begin
  1814. hs:=pattern;
  1815. consume(_ID);
  1816. auto_inherited:=false;
  1817. end;
  1818. classh:=procinfo^._class^.childof;
  1819. while assigned(classh) do
  1820. begin
  1821. srsymtable:=pobjectdef(classh)^.symtable;
  1822. sym:=psym(srsymtable^.search(hs));
  1823. if assigned(sym) then
  1824. begin
  1825. { only for procsyms we need to set the type (PFV) }
  1826. case sym^.typ of
  1827. procsym :
  1828. begin
  1829. p1:=ctypenode.create(nil,nil);
  1830. p1.resulttype:=classh;
  1831. pd:=p1.resulttype;
  1832. end;
  1833. varsym :
  1834. begin
  1835. p1:=nil;
  1836. pd:=pvarsym(sym)^.vartype.def;
  1837. end;
  1838. propertysym :
  1839. begin
  1840. p1:=nil;
  1841. pd:=ppropertysym(sym)^.proptype.def;
  1842. end;
  1843. else
  1844. internalerror(83251763);
  1845. end;
  1846. do_member_read(false,sym,p1,pd,again);
  1847. break;
  1848. end;
  1849. classh:=classh^.childof;
  1850. end;
  1851. if classh=nil then
  1852. begin
  1853. Message1(sym_e_id_no_member,hs);
  1854. again:=false;
  1855. pd:=generrordef;
  1856. p1:=cerrornode.create;
  1857. end;
  1858. { turn auto inheriting off }
  1859. auto_inherited:=false;
  1860. end
  1861. else
  1862. begin
  1863. Message(parser_e_generic_methods_only_in_methods);
  1864. again:=false;
  1865. pd:=generrordef;
  1866. p1:=cerrornode.create;
  1867. end;
  1868. postfixoperators;
  1869. end;
  1870. _INTCONST : begin
  1871. valint(pattern,l,code);
  1872. if code<>0 then
  1873. begin
  1874. { try int64 if available }
  1875. { if no int64 available longint is tried a second }
  1876. { time which doesn't hurt }
  1877. val(pattern,ic,code);
  1878. if code<>0 then
  1879. begin
  1880. val(pattern,d,code);
  1881. if code<>0 then
  1882. begin
  1883. Message(cg_e_invalid_integer);
  1884. consume(_INTCONST);
  1885. l:=1;
  1886. p1:=genordinalconstnode(l,s32bitdef);
  1887. end
  1888. else
  1889. begin
  1890. consume(_INTCONST);
  1891. p1:=genrealconstnode(d,bestrealdef^);
  1892. end;
  1893. end
  1894. else
  1895. begin
  1896. consume(_INTCONST);
  1897. p1:=genordinalconstnode(ic,cs64bitdef);
  1898. end;
  1899. end
  1900. else
  1901. begin
  1902. consume(_INTCONST);
  1903. p1:=genordinalconstnode(l,s32bitdef)
  1904. end;
  1905. end;
  1906. _REALNUMBER : begin
  1907. val(pattern,d,code);
  1908. if code<>0 then
  1909. begin
  1910. Message(parser_e_error_in_real);
  1911. d:=1.0;
  1912. end;
  1913. consume(_REALNUMBER);
  1914. p1:=genrealconstnode(d,bestrealdef^);
  1915. end;
  1916. _STRING : begin
  1917. pd:=string_dec;
  1918. { STRING can be also a type cast }
  1919. if token=_LKLAMMER then
  1920. begin
  1921. consume(_LKLAMMER);
  1922. p1:=comp_expr(true);
  1923. consume(_RKLAMMER);
  1924. p1:=gentypeconvnode(p1,pd);
  1925. include(p1.flags,nf_explizit);
  1926. { handle postfix operators here e.g. string(a)[10] }
  1927. again:=true;
  1928. postfixoperators;
  1929. end
  1930. else
  1931. p1:=gentypenode(pd,nil);
  1932. end;
  1933. _FILE : begin
  1934. pd:=cfiledef;
  1935. consume(_FILE);
  1936. { FILE can be also a type cast }
  1937. if token=_LKLAMMER then
  1938. begin
  1939. consume(_LKLAMMER);
  1940. p1:=comp_expr(true);
  1941. consume(_RKLAMMER);
  1942. p1:=gentypeconvnode(p1,pd);
  1943. include(p1.flags,nf_explizit);
  1944. { handle postfix operators here e.g. string(a)[10] }
  1945. again:=true;
  1946. postfixoperators;
  1947. end
  1948. else
  1949. p1:=gentypenode(pd,nil);
  1950. end;
  1951. _CSTRING : begin
  1952. p1:=genstringconstnode(pattern,st_default);
  1953. consume(_CSTRING);
  1954. end;
  1955. _CCHAR : begin
  1956. p1:=genordinalconstnode(ord(pattern[1]),cchardef);
  1957. consume(_CCHAR);
  1958. end;
  1959. _KLAMMERAFFE : begin
  1960. consume(_KLAMMERAFFE);
  1961. got_addrn:=true;
  1962. { support both @<x> and @(<x>) }
  1963. if token=_LKLAMMER then
  1964. begin
  1965. consume(_LKLAMMER);
  1966. p1:=factor(true);
  1967. consume(_RKLAMMER);
  1968. if token in [_CARET,_POINT,_LECKKLAMMER] then
  1969. begin
  1970. { we need the resulttype }
  1971. { of the expression in pd }
  1972. do_firstpass(p1);
  1973. pd:=p1.resulttype;
  1974. again:=true;
  1975. postfixoperators;
  1976. end;
  1977. end
  1978. else
  1979. p1:=factor(true);
  1980. got_addrn:=false;
  1981. p1:=caddrnode.create(p1);
  1982. end;
  1983. _LKLAMMER : begin
  1984. consume(_LKLAMMER);
  1985. p1:=comp_expr(true);
  1986. consume(_RKLAMMER);
  1987. { it's not a good solution }
  1988. { but (a+b)^ makes some problems }
  1989. if token in [_CARET,_POINT,_LECKKLAMMER] then
  1990. begin
  1991. { we need the resulttype }
  1992. { of the expression in pd }
  1993. do_firstpass(p1);
  1994. pd:=p1.resulttype;
  1995. again:=true;
  1996. postfixoperators;
  1997. end;
  1998. end;
  1999. _LECKKLAMMER : begin
  2000. consume(_LECKKLAMMER);
  2001. p1:=factor_read_set;
  2002. consume(_RECKKLAMMER);
  2003. end;
  2004. _PLUS : begin
  2005. consume(_PLUS);
  2006. p1:=factor(false);
  2007. end;
  2008. _MINUS : begin
  2009. consume(_MINUS);
  2010. p1:=sub_expr(oppower,false);
  2011. p1:=cunaryminusnode.create(p1);
  2012. end;
  2013. _OP_NOT : begin
  2014. consume(_OP_NOT);
  2015. p1:=factor(false);
  2016. p1:=cnotnode.create(p1);
  2017. end;
  2018. _TRUE : begin
  2019. consume(_TRUE);
  2020. p1:=genordinalconstnode(1,booldef);
  2021. end;
  2022. _FALSE : begin
  2023. consume(_FALSE);
  2024. p1:=genordinalconstnode(0,booldef);
  2025. end;
  2026. _NIL : begin
  2027. consume(_NIL);
  2028. p1:=cnilnode.create;
  2029. end;
  2030. else
  2031. begin
  2032. p1:=cerrornode.create;
  2033. consume(token);
  2034. Message(cg_e_illegal_expression);
  2035. end;
  2036. end;
  2037. { generate error node if no node is created }
  2038. if not assigned(p1) then
  2039. p1:=cerrornode.create;
  2040. { tp7 procvar handling, but not if the next token
  2041. will be a := }
  2042. if (m_tp_procvar in aktmodeswitches) and
  2043. (token<>_ASSIGNMENT) then
  2044. check_tp_procvar(p1);
  2045. factor:=p1;
  2046. check_tokenpos;
  2047. end;
  2048. {$ifdef fpc}
  2049. {$maxfpuregisters default}
  2050. {$endif fpc}
  2051. {****************************************************************************
  2052. Sub_Expr
  2053. ****************************************************************************}
  2054. const
  2055. { Warning these stay be ordered !! }
  2056. operator_levels:array[Toperator_precedence] of set of Ttoken=
  2057. ([_LT,_LTE,_GT,_GTE,_EQUAL,_UNEQUAL,_OP_IN,_OP_IS],
  2058. [_PLUS,_MINUS,_OP_OR,_OP_XOR],
  2059. [_CARET,_SYMDIF,_STARSTAR,_STAR,_SLASH,
  2060. _OP_AS,_OP_AND,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR],
  2061. [_STARSTAR] );
  2062. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):tnode;
  2063. {Reads a subexpression while the operators are of the current precedence
  2064. level, or any higher level. Replaces the old term, simpl_expr and
  2065. simpl2_expr.}
  2066. var
  2067. p1,p2 : tnode;
  2068. oldt : Ttoken;
  2069. filepos : tfileposinfo;
  2070. begin
  2071. if pred_level=highest_precedence then
  2072. p1:=factor(false)
  2073. else
  2074. p1:=sub_expr(succ(pred_level),true);
  2075. repeat
  2076. if (token in operator_levels[pred_level]) and
  2077. ((token<>_EQUAL) or accept_equal) then
  2078. begin
  2079. oldt:=token;
  2080. filepos:=akttokenpos;
  2081. consume(token);
  2082. if pred_level=highest_precedence then
  2083. p2:=factor(false)
  2084. else
  2085. p2:=sub_expr(succ(pred_level),true);
  2086. case oldt of
  2087. _PLUS :
  2088. p1:=caddnode.create(addn,p1,p2);
  2089. _MINUS :
  2090. p1:=caddnode.create(subn,p1,p2);
  2091. _STAR :
  2092. p1:=caddnode.create(muln,p1,p2);
  2093. _SLASH :
  2094. p1:=caddnode.create(slashn,p1,p2);
  2095. _EQUAL :
  2096. p1:=caddnode.create(equaln,p1,p2);
  2097. _GT :
  2098. p1:=caddnode.create(gtn,p1,p2);
  2099. _LT :
  2100. p1:=caddnode.create(ltn,p1,p2);
  2101. _GTE :
  2102. p1:=caddnode.create(gten,p1,p2);
  2103. _LTE :
  2104. p1:=caddnode.create(lten,p1,p2);
  2105. _SYMDIF :
  2106. p1:=caddnode.create(symdifn,p1,p2);
  2107. _STARSTAR :
  2108. p1:=caddnode.create(starstarn,p1,p2);
  2109. _OP_AS :
  2110. p1:=casnode.create(p1,p2);
  2111. _OP_IN :
  2112. p1:=cinnode.create(p1,p2);
  2113. _OP_IS :
  2114. p1:=cisnode.create(p1,p2);
  2115. _OP_OR :
  2116. p1:=caddnode.create(orn,p1,p2);
  2117. _OP_AND :
  2118. p1:=caddnode.create(andn,p1,p2);
  2119. _OP_DIV :
  2120. p1:=cmoddivnode.create(divn,p1,p2);
  2121. _OP_NOT :
  2122. p1:=cnotnode.create(p1);
  2123. _OP_MOD :
  2124. p1:=cmoddivnode.create(modn,p1,p2);
  2125. _OP_SHL :
  2126. p1:=cshlshrnode.create(shln,p1,p2);
  2127. _OP_SHR :
  2128. p1:=cshlshrnode.create(shrn,p1,p2);
  2129. _OP_XOR :
  2130. p1:=caddnode.create(xorn,p1,p2);
  2131. _ASSIGNMENT :
  2132. p1:=cassignmentnode.create(p1,p2);
  2133. _CARET :
  2134. p1:=caddnode.create(caretn,p1,p2);
  2135. _UNEQUAL :
  2136. p1:=caddnode.create(unequaln,p1,p2);
  2137. end;
  2138. p1.set_tree_filepos(filepos);
  2139. end
  2140. else
  2141. break;
  2142. until false;
  2143. sub_expr:=p1;
  2144. end;
  2145. function comp_expr(accept_equal : boolean):tnode;
  2146. var
  2147. oldafterassignment : boolean;
  2148. p1 : tnode;
  2149. begin
  2150. oldafterassignment:=afterassignment;
  2151. afterassignment:=true;
  2152. p1:=sub_expr(opcompare,accept_equal);
  2153. afterassignment:=oldafterassignment;
  2154. comp_expr:=p1;
  2155. end;
  2156. function expr : tnode;
  2157. var
  2158. p1,p2 : tnode;
  2159. oldafterassignment : boolean;
  2160. oldp1 : tnode;
  2161. filepos : tfileposinfo;
  2162. begin
  2163. oldafterassignment:=afterassignment;
  2164. p1:=sub_expr(opcompare,true);
  2165. filepos:=akttokenpos;
  2166. if (m_tp_procvar in aktmodeswitches) and
  2167. (token<>_ASSIGNMENT) then
  2168. check_tp_procvar(p1);
  2169. if token in [_ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  2170. afterassignment:=true;
  2171. oldp1:=p1;
  2172. case token of
  2173. _POINTPOINT :
  2174. begin
  2175. consume(_POINTPOINT);
  2176. p2:=sub_expr(opcompare,true);
  2177. p1:=crangenode.create(p1,p2);
  2178. end;
  2179. _ASSIGNMENT :
  2180. begin
  2181. consume(_ASSIGNMENT);
  2182. if (p1.resulttype<>nil) and (p1.resulttype^.deftype=procvardef) then
  2183. begin
  2184. getprocvar:=true;
  2185. getprocvardef:=pprocvardef(p1.resulttype);
  2186. end;
  2187. p2:=sub_expr(opcompare,true);
  2188. if getprocvar then
  2189. handle_procvar(getprocvardef,p2);
  2190. getprocvar:=false;
  2191. p1:=cassignmentnode.create(p1,p2);
  2192. end;
  2193. _PLUSASN :
  2194. begin
  2195. consume(_PLUSASN);
  2196. p2:=sub_expr(opcompare,true);
  2197. p1:=cassignmentnode.create(p1,caddnode.create(addn,p1.getcopy,p2));
  2198. end;
  2199. _MINUSASN :
  2200. begin
  2201. consume(_MINUSASN);
  2202. p2:=sub_expr(opcompare,true);
  2203. p1:=cassignmentnode.create(p1,caddnode.create(subn,p1.getcopy,p2));
  2204. end;
  2205. _STARASN :
  2206. begin
  2207. consume(_STARASN );
  2208. p2:=sub_expr(opcompare,true);
  2209. p1:=cassignmentnode.create(p1,caddnode.create(muln,p1.getcopy,p2));
  2210. end;
  2211. _SLASHASN :
  2212. begin
  2213. consume(_SLASHASN );
  2214. p2:=sub_expr(opcompare,true);
  2215. p1:=cassignmentnode.create(p1,caddnode.create(slashn,p1.getcopy,p2));
  2216. end;
  2217. end;
  2218. afterassignment:=oldafterassignment;
  2219. if p1<>oldp1 then
  2220. p1.set_tree_filepos(filepos);
  2221. expr:=p1;
  2222. end;
  2223. function get_intconst:longint;
  2224. {Reads an expression, tries to evalute it and check if it is an integer
  2225. constant. Then the constant is returned.}
  2226. var
  2227. p:tnode;
  2228. begin
  2229. p:=comp_expr(true);
  2230. do_firstpass(p);
  2231. if not codegenerror then
  2232. begin
  2233. if (p.nodetype<>ordconstn) and
  2234. (p.resulttype^.deftype=orddef) and
  2235. not(Porddef(p.resulttype)^.typ in [uvoid,uchar,bool8bit,bool16bit,bool32bit]) then
  2236. Message(cg_e_illegal_expression)
  2237. else
  2238. get_intconst:=tordconstnode(p).value;
  2239. end;
  2240. p.free;
  2241. end;
  2242. function get_stringconst:string;
  2243. {Reads an expression, tries to evaluate it and checks if it is a string
  2244. constant. Then the constant is returned.}
  2245. var
  2246. p:tnode;
  2247. begin
  2248. get_stringconst:='';
  2249. p:=comp_expr(true);
  2250. do_firstpass(p);
  2251. if p.nodetype<>stringconstn then
  2252. begin
  2253. if (p.nodetype=ordconstn) and is_char(p.resulttype) then
  2254. get_stringconst:=char(tordconstnode(p).value)
  2255. else
  2256. Message(cg_e_illegal_expression);
  2257. end
  2258. else
  2259. get_stringconst:=strpas(tstringconstnode(p).value_str);
  2260. p.free;
  2261. end;
  2262. end.
  2263. {
  2264. $Log$
  2265. Revision 1.15 2000-11-04 14:25:20 florian
  2266. + merged Attila's changes for interfaces, not tested yet
  2267. Revision 1.14 2000/10/31 22:02:49 peter
  2268. * symtable splitted, no real code changes
  2269. Revision 1.13 2000/10/26 23:40:54 peter
  2270. * fixed crash with call from type decl which is not allowed (merged)
  2271. Revision 1.12 2000/10/21 18:16:12 florian
  2272. * a lot of changes:
  2273. - basic dyn. array support
  2274. - basic C++ support
  2275. - some work for interfaces done
  2276. ....
  2277. Revision 1.11 2000/10/14 10:14:51 peter
  2278. * moehrendorf oct 2000 rewrite
  2279. Revision 1.10 2000/10/01 19:48:25 peter
  2280. * lot of compile updates for cg11
  2281. Revision 1.9 2000/09/24 21:19:50 peter
  2282. * delphi compile fixes
  2283. Revision 1.8 2000/09/24 15:06:22 peter
  2284. * use defines.inc
  2285. Revision 1.7 2000/08/27 16:11:51 peter
  2286. * moved some util functions from globals,cobjects to cutils
  2287. * splitted files into finput,fmodule
  2288. Revision 1.6 2000/08/20 15:12:49 peter
  2289. * auto derefence mode for array pointer (merged)
  2290. Revision 1.5 2000/08/16 18:33:53 peter
  2291. * splitted namedobjectitem.next into indexnext and listnext so it
  2292. can be used in both lists
  2293. * don't allow "word = word" type definitions (merged)
  2294. Revision 1.4 2000/08/16 13:06:06 florian
  2295. + support of 64 bit integer constants
  2296. Revision 1.3 2000/08/04 22:00:52 peter
  2297. * merges from fixes
  2298. Revision 1.2 2000/07/13 11:32:44 michael
  2299. + removed logs
  2300. }