pexpr.pas 88 KB

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