pexpr.pas 89 KB

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