pexpr.pas 90 KB

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