pexpr.pas 96 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658
  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,symdef,symbase,
  23. node,ncal,
  24. tokens,globtype,globals;
  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. procedure symlist_to_node(var p1:tnode;st:tsymtable;pl:tsymlist);
  33. function node_to_symlist(p1:tnode):tsymlist;
  34. function parse_paras(__colon : boolean;end_of_paras : ttoken) : tnode;
  35. { the ID token has to be consumed before calling this function }
  36. procedure do_member_read(classh:tobjectdef;getaddr : boolean;sym : tsym;var p1 : tnode;var again : boolean;callflags:tcallnodeflags);
  37. {$ifdef int64funcresok}
  38. function get_intconst:TConstExprInt;
  39. {$else int64funcresok}
  40. function get_intconst:longint;
  41. {$endif int64funcresok}
  42. function get_stringconst:string;
  43. implementation
  44. uses
  45. { common }
  46. cutils,
  47. { global }
  48. verbose,
  49. systems,widestr,
  50. { symtable }
  51. symconst,symtable,symsym,defutil,defcmp,
  52. { module }
  53. fmodule,ppu,
  54. { pass 1 }
  55. pass_1,htypechk,
  56. nmat,nadd,nmem,nset,ncnv,ninl,ncon,nld,nflw,nbas,nutils,
  57. { parser }
  58. scanner,
  59. pbase,pinline,
  60. { codegen }
  61. cgbase,procinfo,cpuinfo
  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. { true, if the inherited call is anonymous }
  72. anon_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 try_to_consume(_LECKKLAMMER) then
  83. begin
  84. p:=comp_expr(true);
  85. if not is_constintnode(p) then
  86. begin
  87. Message(parser_e_illegal_expression);
  88. { error recovery }
  89. consume(_RECKKLAMMER);
  90. end
  91. else
  92. begin
  93. if (tordconstnode(p).value<=0) then
  94. begin
  95. Message(parser_e_invalid_string_size);
  96. tordconstnode(p).value:=255;
  97. end;
  98. consume(_RECKKLAMMER);
  99. if tordconstnode(p).value>255 then
  100. begin
  101. { longstring is currently unsupported (CEC)! }
  102. { t.setdef(tstringdef.createlong(tordconstnode(p).value))}
  103. Message(parser_e_invalid_string_size);
  104. tordconstnode(p).value:=255;
  105. t.setdef(tstringdef.createshort(tordconstnode(p).value));
  106. end
  107. else
  108. if tordconstnode(p).value<>255 then
  109. t.setdef(tstringdef.createshort(tordconstnode(p).value));
  110. end;
  111. p.free;
  112. end
  113. else
  114. begin
  115. if cs_ansistrings in aktlocalswitches then
  116. {$ifdef ansistring_bits}
  117. case aktansistring_bits of
  118. sb_16:
  119. t:=cansistringtype16;
  120. sb_32:
  121. t:=cansistringtype32;
  122. sb_64:
  123. t:=cansistringtype64;
  124. end
  125. {$else}
  126. t:=cansistringtype
  127. {$endif}
  128. else
  129. t:=cshortstringtype;
  130. end;
  131. end;
  132. procedure symlist_to_node(var p1:tnode;st:tsymtable;pl:tsymlist);
  133. var
  134. plist : psymlistitem;
  135. begin
  136. plist:=pl.firstsym;
  137. while assigned(plist) do
  138. begin
  139. case plist^.sltype of
  140. sl_load :
  141. begin
  142. if not assigned(st) then
  143. st:=plist^.sym.owner;
  144. { p1 can already contain the loadnode of
  145. the class variable. When there is no tree yet we
  146. may need to load it for with or objects }
  147. if not assigned(p1) then
  148. begin
  149. case st.symtabletype of
  150. withsymtable :
  151. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  152. objectsymtable :
  153. p1:=load_self_node;
  154. end;
  155. end;
  156. if assigned(p1) then
  157. p1:=csubscriptnode.create(plist^.sym,p1)
  158. else
  159. p1:=cloadnode.create(plist^.sym,st);
  160. end;
  161. sl_subscript :
  162. p1:=csubscriptnode.create(plist^.sym,p1);
  163. sl_typeconv :
  164. p1:=ctypeconvnode.create_explicit(p1,plist^.tt);
  165. sl_absolutetype :
  166. begin
  167. p1:=ctypeconvnode.create(p1,plist^.tt);
  168. include(p1.flags,nf_absolute);
  169. end;
  170. sl_vec :
  171. p1:=cvecnode.create(p1,cordconstnode.create(plist^.value,s32inttype,true));
  172. else
  173. internalerror(200110205);
  174. end;
  175. plist:=plist^.next;
  176. end;
  177. end;
  178. function node_to_symlist(p1:tnode):tsymlist;
  179. var
  180. sl : tsymlist;
  181. procedure addnode(p:tnode);
  182. begin
  183. case p.nodetype of
  184. subscriptn :
  185. begin
  186. addnode(tsubscriptnode(p).left);
  187. sl.addsym(sl_subscript,tsubscriptnode(p).vs);
  188. end;
  189. typeconvn :
  190. begin
  191. addnode(ttypeconvnode(p).left);
  192. if nf_absolute in ttypeconvnode(p).flags then
  193. sl.addtype(sl_absolutetype,ttypeconvnode(p).totype)
  194. else
  195. sl.addtype(sl_typeconv,ttypeconvnode(p).totype);
  196. end;
  197. vecn :
  198. begin
  199. addnode(tvecnode(p).left);
  200. if tvecnode(p).right.nodetype=ordconstn then
  201. sl.addconst(sl_vec,tordconstnode(tvecnode(p).right).value)
  202. else
  203. begin
  204. Message(parser_e_illegal_expression);
  205. { recovery }
  206. sl.addconst(sl_vec,0);
  207. end;
  208. end;
  209. loadn :
  210. sl.addsym(sl_load,tloadnode(p).symtableentry);
  211. else
  212. internalerror(200310282);
  213. end;
  214. end;
  215. begin
  216. sl:=tsymlist.create;
  217. addnode(p1);
  218. result:=sl;
  219. end;
  220. function parse_paras(__colon : boolean;end_of_paras : ttoken) : tnode;
  221. var
  222. p1,p2 : tnode;
  223. prev_in_args : boolean;
  224. old_allow_array_constructor : boolean;
  225. begin
  226. if token=end_of_paras then
  227. begin
  228. parse_paras:=nil;
  229. exit;
  230. end;
  231. { save old values }
  232. prev_in_args:=in_args;
  233. old_allow_array_constructor:=allow_array_constructor;
  234. { set para parsing values }
  235. in_args:=true;
  236. inc(parsing_para_level);
  237. allow_array_constructor:=true;
  238. p2:=nil;
  239. repeat
  240. p1:=comp_expr(true);
  241. p2:=ccallparanode.create(p1,p2);
  242. { it's for the str(l:5,s); }
  243. if __colon and (token=_COLON) then
  244. begin
  245. consume(_COLON);
  246. p1:=comp_expr(true);
  247. p2:=ccallparanode.create(p1,p2);
  248. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  249. if try_to_consume(_COLON) then
  250. begin
  251. p1:=comp_expr(true);
  252. p2:=ccallparanode.create(p1,p2);
  253. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  254. end
  255. end;
  256. until not try_to_consume(_COMMA);
  257. allow_array_constructor:=old_allow_array_constructor;
  258. dec(parsing_para_level);
  259. in_args:=prev_in_args;
  260. parse_paras:=p2;
  261. end;
  262. function gen_c_style_operator(ntyp:tnodetype;p1,p2:tnode) : tnode;
  263. var
  264. hp : tnode;
  265. htype : ttype;
  266. temp : ttempcreatenode;
  267. newstatement : tstatementnode;
  268. begin
  269. { Properties are not allowed, because the write can
  270. be different from the read }
  271. if (nf_isproperty in p1.flags) then
  272. begin
  273. Message(type_e_variable_id_expected);
  274. { We can continue with the loading,
  275. it'll not create errors. Only the expected
  276. result can be wrong }
  277. end;
  278. hp:=p1;
  279. while assigned(hp) and
  280. (hp.nodetype in [derefn,subscriptn,vecn,typeconvn]) do
  281. hp:=tunarynode(hp).left;
  282. if not assigned(hp) then
  283. internalerror(200410121);
  284. if (hp.nodetype=calln) then
  285. begin
  286. resulttypepass(p1);
  287. result:=internalstatements(newstatement);
  288. htype.setdef(tpointerdef.create(p1.resulttype));
  289. temp:=ctempcreatenode.create(htype,sizeof(aint),tt_persistent,false);
  290. addstatement(newstatement,temp);
  291. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(temp),caddrnode.create_internal(p1)));
  292. addstatement(newstatement,cassignmentnode.create(
  293. cderefnode.create(ctemprefnode.create(temp)),
  294. caddnode.create(ntyp,
  295. cderefnode.create(ctemprefnode.create(temp)),
  296. p2)));
  297. addstatement(newstatement,ctempdeletenode.create(temp));
  298. end
  299. else
  300. result:=cassignmentnode.create(p1,caddnode.create(ntyp,p1.getcopy,p2));
  301. end;
  302. function statement_syssym(l : longint) : tnode;
  303. var
  304. p1,p2,paras : tnode;
  305. err,
  306. prev_in_args : boolean;
  307. begin
  308. prev_in_args:=in_args;
  309. case l of
  310. in_new_x :
  311. begin
  312. if afterassignment or in_args then
  313. statement_syssym:=new_function
  314. else
  315. statement_syssym:=new_dispose_statement(true);
  316. end;
  317. in_dispose_x :
  318. begin
  319. statement_syssym:=new_dispose_statement(false);
  320. end;
  321. in_ord_x :
  322. begin
  323. consume(_LKLAMMER);
  324. in_args:=true;
  325. p1:=comp_expr(true);
  326. consume(_RKLAMMER);
  327. p1:=geninlinenode(in_ord_x,false,p1);
  328. statement_syssym := p1;
  329. end;
  330. in_exit :
  331. begin
  332. if try_to_consume(_LKLAMMER) then
  333. begin
  334. if not (m_mac in aktmodeswitches) then
  335. begin
  336. p1:=comp_expr(true);
  337. consume(_RKLAMMER);
  338. if (block_type=bt_except) then
  339. begin
  340. Message(parser_e_exit_with_argument_not__possible);
  341. { recovery }
  342. p1.free;
  343. p1:=nil;
  344. end
  345. else if (not assigned(current_procinfo) or
  346. is_void(current_procinfo.procdef.rettype.def)) then
  347. begin
  348. Message(parser_e_void_function);
  349. { recovery }
  350. p1.free;
  351. p1:=nil;
  352. end;
  353. end
  354. else
  355. begin
  356. if not (current_procinfo.procdef.procsym.name = pattern) then
  357. Message(parser_e_macpas_exit_wrong_param);
  358. consume(_ID);
  359. consume(_RKLAMMER);
  360. p1:=nil;
  361. end
  362. end
  363. else
  364. p1:=nil;
  365. statement_syssym:=cexitnode.create(p1);
  366. end;
  367. in_break :
  368. begin
  369. if not (m_mac in aktmodeswitches) then
  370. statement_syssym:=cbreaknode.create
  371. else
  372. begin
  373. Message1(sym_e_id_not_found, orgpattern);
  374. statement_syssym:=cerrornode.create;
  375. end;
  376. end;
  377. in_continue :
  378. begin
  379. if not (m_mac in aktmodeswitches) then
  380. statement_syssym:=ccontinuenode.create
  381. else
  382. begin
  383. Message1(sym_e_id_not_found, orgpattern);
  384. statement_syssym:=cerrornode.create;
  385. end;
  386. end;
  387. in_leave :
  388. begin
  389. if m_mac in aktmodeswitches then
  390. statement_syssym:=cbreaknode.create
  391. else
  392. begin
  393. Message1(sym_e_id_not_found, orgpattern);
  394. statement_syssym:=cerrornode.create;
  395. end;
  396. end;
  397. in_cycle :
  398. begin
  399. if m_mac in aktmodeswitches then
  400. statement_syssym:=ccontinuenode.create
  401. else
  402. begin
  403. Message1(sym_e_id_not_found, orgpattern);
  404. statement_syssym:=cerrornode.create;
  405. end;
  406. end;
  407. in_typeof_x :
  408. begin
  409. consume(_LKLAMMER);
  410. in_args:=true;
  411. p1:=comp_expr(true);
  412. consume(_RKLAMMER);
  413. if p1.nodetype=typen then
  414. ttypenode(p1).allowed:=true;
  415. { Allow classrefdef, which is required for
  416. Typeof(self) in static class methods }
  417. if (p1.resulttype.def.deftype = objectdef) or
  418. (assigned(current_procinfo) and
  419. ((po_classmethod in current_procinfo.procdef.procoptions) or
  420. (po_staticmethod in current_procinfo.procdef.procoptions)) and
  421. (p1.resulttype.def.deftype=classrefdef)) then
  422. statement_syssym:=geninlinenode(in_typeof_x,false,p1)
  423. else
  424. begin
  425. Message(parser_e_class_id_expected);
  426. p1.destroy;
  427. statement_syssym:=cerrornode.create;
  428. end;
  429. end;
  430. in_sizeof_x :
  431. begin
  432. consume(_LKLAMMER);
  433. in_args:=true;
  434. p1:=comp_expr(true);
  435. consume(_RKLAMMER);
  436. if (p1.nodetype<>typen) and
  437. (
  438. (is_object(p1.resulttype.def) and
  439. (oo_has_constructor in tobjectdef(p1.resulttype.def).objectoptions)) or
  440. is_open_array(p1.resulttype.def) or
  441. is_open_string(p1.resulttype.def)
  442. ) then
  443. statement_syssym:=geninlinenode(in_sizeof_x,false,p1)
  444. else
  445. begin
  446. statement_syssym:=cordconstnode.create(p1.resulttype.def.size,sinttype,true);
  447. { p1 not needed !}
  448. p1.destroy;
  449. end;
  450. end;
  451. in_typeinfo_x :
  452. begin
  453. consume(_LKLAMMER);
  454. in_args:=true;
  455. p1:=comp_expr(true);
  456. { When reading a class type it is parsed as loadvmtaddrn,
  457. typeinfo only needs the type so we remove the loadvmtaddrn }
  458. if p1.nodetype=loadvmtaddrn then
  459. begin
  460. p2:=tloadvmtaddrnode(p1).left;
  461. tloadvmtaddrnode(p1).left:=nil;
  462. p1.free;
  463. p1:=p2;
  464. end;
  465. if p1.nodetype=typen then
  466. ttypenode(p1).allowed:=true
  467. else
  468. begin
  469. p1.destroy;
  470. p1:=cerrornode.create;
  471. Message(parser_e_illegal_parameter_list);
  472. end;
  473. consume(_RKLAMMER);
  474. p2:=geninlinenode(in_typeinfo_x,false,p1);
  475. statement_syssym:=p2;
  476. end;
  477. in_assigned_x :
  478. begin
  479. err:=false;
  480. consume(_LKLAMMER);
  481. in_args:=true;
  482. p1:=comp_expr(true);
  483. { When reading a class type it is parsed as loadvmtaddrn,
  484. typeinfo only needs the type so we remove the loadvmtaddrn }
  485. if p1.nodetype=loadvmtaddrn then
  486. begin
  487. p2:=tloadvmtaddrnode(p1).left;
  488. tloadvmtaddrnode(p1).left:=nil;
  489. p1.free;
  490. p1:=p2;
  491. end;
  492. if not codegenerror then
  493. begin
  494. case p1.resulttype.def.deftype of
  495. procdef, { procvar }
  496. pointerdef,
  497. procvardef,
  498. classrefdef : ;
  499. objectdef :
  500. if not is_class_or_interface(p1.resulttype.def) then
  501. begin
  502. Message(parser_e_illegal_parameter_list);
  503. err:=true;
  504. end;
  505. arraydef :
  506. if not is_dynamic_array(p1.resulttype.def) then
  507. begin
  508. Message(parser_e_illegal_parameter_list);
  509. err:=true;
  510. end;
  511. else
  512. begin
  513. Message(parser_e_illegal_parameter_list);
  514. err:=true;
  515. end;
  516. end;
  517. end
  518. else
  519. err:=true;
  520. if not err then
  521. begin
  522. p2:=ccallparanode.create(p1,nil);
  523. p2:=geninlinenode(in_assigned_x,false,p2);
  524. end
  525. else
  526. begin
  527. p1.free;
  528. p2:=cerrornode.create;
  529. end;
  530. consume(_RKLAMMER);
  531. statement_syssym:=p2;
  532. end;
  533. in_addr_x :
  534. begin
  535. consume(_LKLAMMER);
  536. in_args:=true;
  537. p1:=comp_expr(true);
  538. p1:=caddrnode.create(p1);
  539. if cs_typed_addresses in aktlocalswitches then
  540. include(p1.flags,nf_typedaddr);
  541. consume(_RKLAMMER);
  542. statement_syssym:=p1;
  543. end;
  544. in_ofs_x :
  545. begin
  546. consume(_LKLAMMER);
  547. in_args:=true;
  548. p1:=comp_expr(true);
  549. p1:=caddrnode.create(p1);
  550. do_resulttypepass(p1);
  551. { Ofs() returns a cardinal/qword, not a pointer }
  552. p1.resulttype:=uinttype;
  553. consume(_RKLAMMER);
  554. statement_syssym:=p1;
  555. end;
  556. in_seg_x :
  557. begin
  558. consume(_LKLAMMER);
  559. in_args:=true;
  560. p1:=comp_expr(true);
  561. p1:=geninlinenode(in_seg_x,false,p1);
  562. consume(_RKLAMMER);
  563. statement_syssym:=p1;
  564. end;
  565. in_high_x,
  566. in_low_x :
  567. begin
  568. consume(_LKLAMMER);
  569. in_args:=true;
  570. p1:=comp_expr(true);
  571. p2:=geninlinenode(l,false,p1);
  572. consume(_RKLAMMER);
  573. statement_syssym:=p2;
  574. end;
  575. in_succ_x,
  576. in_pred_x :
  577. begin
  578. consume(_LKLAMMER);
  579. in_args:=true;
  580. p1:=comp_expr(true);
  581. p2:=geninlinenode(l,false,p1);
  582. consume(_RKLAMMER);
  583. statement_syssym:=p2;
  584. end;
  585. in_inc_x,
  586. in_dec_x :
  587. begin
  588. consume(_LKLAMMER);
  589. in_args:=true;
  590. p1:=comp_expr(true);
  591. if try_to_consume(_COMMA) then
  592. p2:=ccallparanode.create(comp_expr(true),nil)
  593. else
  594. p2:=nil;
  595. p2:=ccallparanode.create(p1,p2);
  596. statement_syssym:=geninlinenode(l,false,p2);
  597. consume(_RKLAMMER);
  598. end;
  599. in_initialize_x:
  600. begin
  601. statement_syssym:=inline_initialize;
  602. end;
  603. in_finalize_x:
  604. begin
  605. statement_syssym:=inline_finalize;
  606. end;
  607. in_copy_x:
  608. begin
  609. statement_syssym:=inline_copy;
  610. end;
  611. in_concat_x :
  612. begin
  613. consume(_LKLAMMER);
  614. in_args:=true;
  615. p2:=nil;
  616. repeat
  617. p1:=comp_expr(true);
  618. set_varstate(p1,vs_used,[vsf_must_be_valid]);
  619. if not((p1.resulttype.def.deftype=stringdef) or
  620. ((p1.resulttype.def.deftype=orddef) and
  621. (torddef(p1.resulttype.def).typ=uchar))) then
  622. Message(parser_e_illegal_parameter_list);
  623. if p2<>nil then
  624. p2:=caddnode.create(addn,p2,p1)
  625. else
  626. p2:=p1;
  627. until not try_to_consume(_COMMA);
  628. consume(_RKLAMMER);
  629. statement_syssym:=p2;
  630. end;
  631. in_read_x,
  632. in_readln_x :
  633. begin
  634. if try_to_consume(_LKLAMMER) then
  635. begin
  636. paras:=parse_paras(false,_RKLAMMER);
  637. consume(_RKLAMMER);
  638. end
  639. else
  640. paras:=nil;
  641. p1:=geninlinenode(l,false,paras);
  642. statement_syssym := p1;
  643. end;
  644. in_setlength_x:
  645. begin
  646. statement_syssym := inline_setlength;
  647. end;
  648. in_length_x:
  649. begin
  650. consume(_LKLAMMER);
  651. in_args:=true;
  652. p1:=comp_expr(true);
  653. p2:=geninlinenode(l,false,p1);
  654. consume(_RKLAMMER);
  655. statement_syssym:=p2;
  656. end;
  657. in_write_x,
  658. in_writeln_x :
  659. begin
  660. if try_to_consume(_LKLAMMER) then
  661. begin
  662. paras:=parse_paras(true,_RKLAMMER);
  663. consume(_RKLAMMER);
  664. end
  665. else
  666. paras:=nil;
  667. p1 := geninlinenode(l,false,paras);
  668. statement_syssym := p1;
  669. end;
  670. in_str_x_string :
  671. begin
  672. consume(_LKLAMMER);
  673. paras:=parse_paras(true,_RKLAMMER);
  674. consume(_RKLAMMER);
  675. p1 := geninlinenode(l,false,paras);
  676. statement_syssym := p1;
  677. end;
  678. in_val_x:
  679. Begin
  680. consume(_LKLAMMER);
  681. in_args := true;
  682. p1:= ccallparanode.create(comp_expr(true), nil);
  683. consume(_COMMA);
  684. p2 := ccallparanode.create(comp_expr(true),p1);
  685. if try_to_consume(_COMMA) then
  686. p2 := ccallparanode.create(comp_expr(true),p2);
  687. consume(_RKLAMMER);
  688. p2 := geninlinenode(l,false,p2);
  689. statement_syssym := p2;
  690. End;
  691. in_include_x_y,
  692. in_exclude_x_y :
  693. begin
  694. consume(_LKLAMMER);
  695. in_args:=true;
  696. p1:=comp_expr(true);
  697. consume(_COMMA);
  698. p2:=comp_expr(true);
  699. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,nil)));
  700. consume(_RKLAMMER);
  701. end;
  702. in_assert_x_y :
  703. begin
  704. consume(_LKLAMMER);
  705. in_args:=true;
  706. p1:=comp_expr(true);
  707. if try_to_consume(_COMMA) then
  708. p2:=comp_expr(true)
  709. else
  710. begin
  711. { then insert an empty string }
  712. p2:=cstringconstnode.createstr('',st_default);
  713. end;
  714. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,nil)));
  715. consume(_RKLAMMER);
  716. end;
  717. else
  718. internalerror(15);
  719. end;
  720. in_args:=prev_in_args;
  721. end;
  722. function maybe_load_methodpointer(st:tsymtable;var p1:tnode):boolean;
  723. begin
  724. maybe_load_methodpointer:=false;
  725. if not assigned(p1) then
  726. begin
  727. case st.symtabletype of
  728. withsymtable :
  729. begin
  730. if (st.defowner.deftype=objectdef) then
  731. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  732. end;
  733. objectsymtable :
  734. begin
  735. p1:=load_self_node;
  736. { We are calling a member }
  737. maybe_load_methodpointer:=true;
  738. end;
  739. end;
  740. end;
  741. end;
  742. { reads the parameter for a subroutine call }
  743. procedure do_proc_call(sym:tsym;st:tsymtable;obj:tobjectdef;getaddr:boolean;var again : boolean;var p1:tnode;callflags:tcallnodeflags);
  744. var
  745. membercall,
  746. prevafterassn : boolean;
  747. i : integer;
  748. para,p2 : tnode;
  749. currpara : tparavarsym;
  750. aprocdef : tprocdef;
  751. begin
  752. prevafterassn:=afterassignment;
  753. afterassignment:=false;
  754. membercall:=false;
  755. aprocdef:=nil;
  756. { when it is a call to a member we need to load the
  757. methodpointer first }
  758. membercall:=maybe_load_methodpointer(st,p1);
  759. { When we are expecting a procvar we also need
  760. to get the address in some cases }
  761. if assigned(getprocvardef) then
  762. begin
  763. if (block_type=bt_const) or
  764. getaddr then
  765. begin
  766. aprocdef:=Tprocsym(sym).search_procdef_byprocvardef(getprocvardef);
  767. getaddr:=true;
  768. end
  769. else
  770. if (m_tp_procvar in aktmodeswitches) then
  771. begin
  772. aprocdef:=Tprocsym(sym).search_procdef_byprocvardef(getprocvardef);
  773. if assigned(aprocdef) then
  774. getaddr:=true;
  775. end;
  776. end;
  777. { only need to get the address of the procedure? }
  778. if getaddr then
  779. begin
  780. { Retrieve info which procvar to call. For tp_procvar the
  781. aprocdef is already loaded above so we can reuse it }
  782. if not assigned(aprocdef) and
  783. assigned(getprocvardef) then
  784. aprocdef:=Tprocsym(sym).search_procdef_byprocvardef(getprocvardef);
  785. { generate a methodcallnode or proccallnode }
  786. { we shouldn't convert things like @tcollection.load }
  787. p2:=cloadnode.create_procvar(sym,aprocdef,st);
  788. if assigned(p1) then
  789. begin
  790. if (p1.nodetype<>typen) then
  791. tloadnode(p2).set_mp(p1)
  792. else
  793. p1.free;
  794. end;
  795. p1:=p2;
  796. { no postfix operators }
  797. again:=false;
  798. end
  799. else
  800. begin
  801. para:=nil;
  802. if anon_inherited then
  803. begin
  804. if not assigned(current_procinfo) then
  805. internalerror(200305054);
  806. for i:=0 to current_procinfo.procdef.paras.count-1 do
  807. begin
  808. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  809. if not(vo_is_hidden_para in currpara.varoptions) then
  810. para:=ccallparanode.create(cloadnode.create(currpara,currpara.owner),para);
  811. end;
  812. end
  813. else
  814. begin
  815. if try_to_consume(_LKLAMMER) then
  816. begin
  817. para:=parse_paras(false,_RKLAMMER);
  818. consume(_RKLAMMER);
  819. end;
  820. end;
  821. { indicate if this call was generated by a member and
  822. no explicit self is used, this is needed to determine
  823. how to handle a destructor call (PFV) }
  824. if membercall then
  825. include(callflags,cnf_member_call);
  826. if assigned(obj) then
  827. begin
  828. if (st.symtabletype<>objectsymtable) then
  829. internalerror(200310031);
  830. p1:=ccallnode.create(para,tprocsym(sym),obj.symtable,p1,callflags);
  831. end
  832. else
  833. p1:=ccallnode.create(para,tprocsym(sym),st,p1,callflags);
  834. end;
  835. afterassignment:=prevafterassn;
  836. end;
  837. procedure handle_procvar(pv : tprocvardef;var p2 : tnode);
  838. var
  839. hp,hp2 : tnode;
  840. hpp : ^tnode;
  841. currprocdef : tprocdef;
  842. begin
  843. if not assigned(pv) then
  844. internalerror(200301121);
  845. if (m_tp_procvar in aktmodeswitches) then
  846. begin
  847. hp:=p2;
  848. hpp:=@p2;
  849. while assigned(hp) and
  850. (hp.nodetype=typeconvn) do
  851. begin
  852. hp:=ttypeconvnode(hp).left;
  853. { save orignal address of the old tree so we can replace the node }
  854. hpp:=@hp;
  855. end;
  856. if (hp.nodetype=calln) and
  857. { a procvar can't have parameters! }
  858. not assigned(tcallnode(hp).left) then
  859. begin
  860. currprocdef:=tcallnode(hp).symtableprocentry.search_procdef_byprocvardef(pv);
  861. if assigned(currprocdef) then
  862. begin
  863. hp2:=cloadnode.create_procvar(tprocsym(tcallnode(hp).symtableprocentry),currprocdef,tcallnode(hp).symtableproc);
  864. if (po_methodpointer in pv.procoptions) then
  865. tloadnode(hp2).set_mp(tcallnode(hp).get_load_methodpointer);
  866. hp.destroy;
  867. { replace the old callnode with the new loadnode }
  868. hpp^:=hp2;
  869. end;
  870. end;
  871. end;
  872. end;
  873. { the following procedure handles the access to a property symbol }
  874. procedure handle_propertysym(sym : tsym;st : tsymtable;var p1 : tnode);
  875. var
  876. paras : tnode;
  877. p2 : tnode;
  878. membercall : boolean;
  879. callflags : tcallnodeflags;
  880. begin
  881. paras:=nil;
  882. { property parameters? read them only if the property really }
  883. { has parameters }
  884. if (ppo_hasparameters in tpropertysym(sym).propoptions) then
  885. begin
  886. if try_to_consume(_LECKKLAMMER) then
  887. begin
  888. paras:=parse_paras(false,_RECKKLAMMER);
  889. consume(_RECKKLAMMER);
  890. end;
  891. end;
  892. { indexed property }
  893. if (ppo_indexed in tpropertysym(sym).propoptions) then
  894. begin
  895. p2:=cordconstnode.create(tpropertysym(sym).index,tpropertysym(sym).indextype,true);
  896. paras:=ccallparanode.create(p2,paras);
  897. end;
  898. { we need only a write property if a := follows }
  899. { if not(afterassignment) and not(in_args) then }
  900. if token=_ASSIGNMENT then
  901. begin
  902. { write property: }
  903. if not tpropertysym(sym).writeaccess.empty then
  904. begin
  905. case tpropertysym(sym).writeaccess.firstsym^.sym.typ of
  906. procsym :
  907. begin
  908. callflags:=[];
  909. { generate the method call }
  910. membercall:=maybe_load_methodpointer(st,p1);
  911. if membercall then
  912. include(callflags,cnf_member_call);
  913. p1:=ccallnode.create(paras,tprocsym(tpropertysym(sym).writeaccess.firstsym^.sym),st,p1,callflags);
  914. paras:=nil;
  915. consume(_ASSIGNMENT);
  916. { read the expression }
  917. if tpropertysym(sym).proptype.def.deftype=procvardef then
  918. getprocvardef:=tprocvardef(tpropertysym(sym).proptype.def);
  919. p2:=comp_expr(true);
  920. if assigned(getprocvardef) then
  921. handle_procvar(getprocvardef,p2);
  922. tcallnode(p1).left:=ccallparanode.create(p2,tcallnode(p1).left);
  923. { mark as property, both the tcallnode and the real call block }
  924. include(p1.flags,nf_isproperty);
  925. getprocvardef:=nil;
  926. end;
  927. fieldvarsym :
  928. begin
  929. { generate access code }
  930. symlist_to_node(p1,st,tpropertysym(sym).writeaccess);
  931. include(p1.flags,nf_isproperty);
  932. consume(_ASSIGNMENT);
  933. { read the expression }
  934. p2:=comp_expr(true);
  935. p1:=cassignmentnode.create(p1,p2);
  936. end
  937. else
  938. begin
  939. p1:=cerrornode.create;
  940. Message(parser_e_no_procedure_to_access_property);
  941. end;
  942. end;
  943. end
  944. else
  945. begin
  946. p1:=cerrornode.create;
  947. Message(parser_e_no_procedure_to_access_property);
  948. end;
  949. end
  950. else
  951. begin
  952. { read property: }
  953. if not tpropertysym(sym).readaccess.empty then
  954. begin
  955. case tpropertysym(sym).readaccess.firstsym^.sym.typ of
  956. fieldvarsym :
  957. begin
  958. { generate access code }
  959. symlist_to_node(p1,st,tpropertysym(sym).readaccess);
  960. include(p1.flags,nf_isproperty);
  961. end;
  962. procsym :
  963. begin
  964. callflags:=[];
  965. { generate the method call }
  966. membercall:=maybe_load_methodpointer(st,p1);
  967. if membercall then
  968. include(callflags,cnf_member_call);
  969. p1:=ccallnode.create(paras,tprocsym(tpropertysym(sym).readaccess.firstsym^.sym),st,p1,callflags);
  970. paras:=nil;
  971. include(p1.flags,nf_isproperty);
  972. end
  973. else
  974. begin
  975. p1:=cerrornode.create;
  976. Message(type_e_mismatch);
  977. end;
  978. end;
  979. end
  980. else
  981. begin
  982. { error, no function to read property }
  983. p1:=cerrornode.create;
  984. Message(parser_e_no_procedure_to_access_property);
  985. end;
  986. end;
  987. { release paras if not used }
  988. if assigned(paras) then
  989. paras.free;
  990. end;
  991. { the ID token has to be consumed before calling this function }
  992. procedure do_member_read(classh:tobjectdef;getaddr : boolean;sym : tsym;var p1 : tnode;var again : boolean;callflags:tcallnodeflags);
  993. var
  994. static_name : string;
  995. isclassref : boolean;
  996. srsymtable : tsymtable;
  997. begin
  998. if sym=nil then
  999. begin
  1000. { pattern is still valid unless
  1001. there is another ID just after the ID of sym }
  1002. Message1(sym_e_id_no_member,orgpattern);
  1003. p1.free;
  1004. p1:=cerrornode.create;
  1005. { try to clean up }
  1006. again:=false;
  1007. end
  1008. else
  1009. begin
  1010. if assigned(p1) then
  1011. begin
  1012. if not assigned(p1.resulttype.def) then
  1013. do_resulttypepass(p1);
  1014. isclassref:=(p1.resulttype.def.deftype=classrefdef);
  1015. end
  1016. else
  1017. isclassref:=false;
  1018. { we assume, that only procsyms and varsyms are in an object }
  1019. { symbol table, for classes, properties are allowed }
  1020. case sym.typ of
  1021. procsym:
  1022. begin
  1023. do_proc_call(sym,sym.owner,classh,
  1024. (getaddr and not(token in [_CARET,_POINT])),
  1025. again,p1,callflags);
  1026. { we need to know which procedure is called }
  1027. do_resulttypepass(p1);
  1028. { calling using classref? }
  1029. if isclassref and
  1030. (p1.nodetype=calln) and
  1031. assigned(tcallnode(p1).procdefinition) and
  1032. not(po_classmethod in tcallnode(p1).procdefinition.procoptions) and
  1033. not(tcallnode(p1).procdefinition.proctypeoption=potype_constructor) then
  1034. Message(parser_e_only_class_methods_via_class_ref);
  1035. end;
  1036. fieldvarsym:
  1037. begin
  1038. if (sp_static in sym.symoptions) then
  1039. begin
  1040. static_name:=lower(sym.owner.name^)+'_'+sym.name;
  1041. searchsym(static_name,sym,srsymtable);
  1042. check_hints(sym);
  1043. p1.free;
  1044. p1:=cloadnode.create(sym,srsymtable);
  1045. end
  1046. else
  1047. begin
  1048. if isclassref then
  1049. Message(parser_e_only_class_methods_via_class_ref);
  1050. p1:=csubscriptnode.create(sym,p1);
  1051. end;
  1052. end;
  1053. propertysym:
  1054. begin
  1055. if isclassref then
  1056. Message(parser_e_only_class_methods_via_class_ref);
  1057. handle_propertysym(sym,sym.owner,p1);
  1058. end;
  1059. else internalerror(16);
  1060. end;
  1061. end;
  1062. end;
  1063. {****************************************************************************
  1064. Factor
  1065. ****************************************************************************}
  1066. {$ifdef fpc}
  1067. {$maxfpuregisters 0}
  1068. {$endif fpc}
  1069. function factor(getaddr : boolean) : tnode;
  1070. {---------------------------------------------
  1071. Factor_read_id
  1072. ---------------------------------------------}
  1073. procedure factor_read_id(var p1:tnode;var again:boolean);
  1074. var
  1075. pc : pchar;
  1076. len : longint;
  1077. srsym : tsym;
  1078. possible_error : boolean;
  1079. srsymtable : tsymtable;
  1080. storesymtablestack : tsymtable;
  1081. htype : ttype;
  1082. static_name : string;
  1083. begin
  1084. { allow post fix operators }
  1085. again:=true;
  1086. consume_sym(srsym,srsymtable);
  1087. { Access to funcret or need to call the function? }
  1088. if (srsym.typ in [absolutevarsym,localvarsym,paravarsym]) and
  1089. (vo_is_funcret in tabstractvarsym(srsym).varoptions) and
  1090. (
  1091. (token=_LKLAMMER) or
  1092. (not(m_fpc in aktmodeswitches) and
  1093. (afterassignment or in_args) and
  1094. not(vo_is_result in tabstractvarsym(srsym).varoptions))
  1095. ) then
  1096. begin
  1097. storesymtablestack:=symtablestack;
  1098. symtablestack:=srsym.owner.next;
  1099. searchsym(srsym.name,srsym,srsymtable);
  1100. if not assigned(srsym) then
  1101. srsym:=generrorsym;
  1102. if (srsym.typ<>procsym) then
  1103. Message(parser_e_illegal_expression);
  1104. symtablestack:=storesymtablestack;
  1105. end;
  1106. begin
  1107. case srsym.typ of
  1108. absolutevarsym :
  1109. begin
  1110. if (tabsolutevarsym(srsym).abstyp=tovar) then
  1111. begin
  1112. p1:=nil;
  1113. symlist_to_node(p1,nil,tabsolutevarsym(srsym).ref);
  1114. p1:=ctypeconvnode.create(p1,tabsolutevarsym(srsym).vartype);
  1115. include(p1.flags,nf_absolute);
  1116. end
  1117. else
  1118. p1:=cloadnode.create(srsym,srsymtable);
  1119. end;
  1120. globalvarsym,
  1121. localvarsym,
  1122. paravarsym,
  1123. fieldvarsym :
  1124. begin
  1125. if (sp_static in srsym.symoptions) then
  1126. begin
  1127. static_name:=lower(srsym.owner.name^)+'_'+srsym.name;
  1128. searchsym(static_name,srsym,srsymtable);
  1129. check_hints(srsym);
  1130. end
  1131. else
  1132. begin
  1133. { are we in a class method, we check here the
  1134. srsymtable, because a field in another object
  1135. also has objectsymtable. And withsymtable is
  1136. not possible for self in class methods (PFV) }
  1137. if (srsymtable.symtabletype=objectsymtable) and
  1138. assigned(current_procinfo) and
  1139. (po_classmethod in current_procinfo.procdef.procoptions) then
  1140. Message(parser_e_only_class_methods);
  1141. end;
  1142. case srsymtable.symtabletype of
  1143. objectsymtable :
  1144. begin
  1145. p1:=csubscriptnode.create(srsym,load_self_node);
  1146. node_tree_set_filepos(p1,aktfilepos);
  1147. end;
  1148. withsymtable :
  1149. begin
  1150. p1:=csubscriptnode.create(srsym,tnode(twithsymtable(srsymtable).withrefnode).getcopy);
  1151. node_tree_set_filepos(p1,aktfilepos);
  1152. end;
  1153. else
  1154. p1:=cloadnode.create(srsym,srsymtable);
  1155. end;
  1156. end;
  1157. typedconstsym :
  1158. begin
  1159. p1:=cloadnode.create(srsym,srsymtable);
  1160. end;
  1161. syssym :
  1162. begin
  1163. p1:=statement_syssym(tsyssym(srsym).number);
  1164. end;
  1165. typesym :
  1166. begin
  1167. htype.setsym(srsym);
  1168. if not assigned(htype.def) then
  1169. begin
  1170. again:=false;
  1171. end
  1172. else
  1173. begin
  1174. { We need to know if this unit uses Variants }
  1175. if (htype.def=cvarianttype.def) and
  1176. not(cs_compilesystem in aktmoduleswitches) then
  1177. current_module.flags:=current_module.flags or uf_uses_variants;
  1178. if try_to_consume(_LKLAMMER) then
  1179. begin
  1180. p1:=comp_expr(true);
  1181. consume(_RKLAMMER);
  1182. p1:=ctypeconvnode.create_explicit(p1,htype);
  1183. end
  1184. else { not LKLAMMER }
  1185. if (token=_POINT) and
  1186. is_object(htype.def) then
  1187. begin
  1188. consume(_POINT);
  1189. if assigned(current_procinfo) and
  1190. assigned(current_procinfo.procdef._class) and
  1191. not(getaddr) then
  1192. begin
  1193. if current_procinfo.procdef._class.is_related(tobjectdef(htype.def)) then
  1194. begin
  1195. p1:=ctypenode.create(htype);
  1196. { search also in inherited methods }
  1197. srsym:=searchsym_in_class(tobjectdef(htype.def),pattern);
  1198. check_hints(srsym);
  1199. consume(_ID);
  1200. do_member_read(tobjectdef(htype.def),false,srsym,p1,again,[]);
  1201. end
  1202. else
  1203. begin
  1204. Message(parser_e_no_super_class);
  1205. again:=false;
  1206. end;
  1207. end
  1208. else
  1209. begin
  1210. { allows @TObject.Load }
  1211. { also allows static methods and variables }
  1212. p1:=ctypenode.create(htype);
  1213. { TP allows also @TMenu.Load if Load is only }
  1214. { defined in an anchestor class }
  1215. srsym:=search_class_member(tobjectdef(htype.def),pattern);
  1216. check_hints(srsym);
  1217. if not assigned(srsym) then
  1218. Message1(sym_e_id_no_member,orgpattern)
  1219. else if not(getaddr) and not(sp_static in srsym.symoptions) then
  1220. Message(sym_e_only_static_in_static)
  1221. else
  1222. begin
  1223. consume(_ID);
  1224. do_member_read(tobjectdef(htype.def),getaddr,srsym,p1,again,[]);
  1225. end;
  1226. end;
  1227. end
  1228. else
  1229. begin
  1230. { class reference ? }
  1231. if is_class(htype.def) then
  1232. begin
  1233. if getaddr and (token=_POINT) then
  1234. begin
  1235. consume(_POINT);
  1236. { allows @Object.Method }
  1237. { also allows static methods and variables }
  1238. p1:=ctypenode.create(htype);
  1239. { TP allows also @TMenu.Load if Load is only }
  1240. { defined in an anchestor class }
  1241. srsym:=search_class_member(tobjectdef(htype.def),pattern);
  1242. check_hints(srsym);
  1243. if not assigned(srsym) then
  1244. Message1(sym_e_id_no_member,orgpattern)
  1245. else
  1246. begin
  1247. consume(_ID);
  1248. do_member_read(tobjectdef(htype.def),getaddr,srsym,p1,again,[]);
  1249. end;
  1250. end
  1251. else
  1252. begin
  1253. p1:=ctypenode.create(htype);
  1254. { For a type block we simply return only
  1255. the type. For all other blocks we return
  1256. a loadvmt node }
  1257. if (block_type<>bt_type) then
  1258. p1:=cloadvmtaddrnode.create(p1);
  1259. end;
  1260. end
  1261. else
  1262. p1:=ctypenode.create(htype);
  1263. end;
  1264. end;
  1265. end;
  1266. enumsym :
  1267. begin
  1268. p1:=genenumnode(tenumsym(srsym));
  1269. end;
  1270. constsym :
  1271. begin
  1272. case tconstsym(srsym).consttyp of
  1273. constord :
  1274. begin
  1275. if tconstsym(srsym).consttype.def=nil then
  1276. internalerror(200403232);
  1277. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,tconstsym(srsym).consttype,true);
  1278. end;
  1279. conststring :
  1280. begin
  1281. len:=tconstsym(srsym).value.len;
  1282. if not(cs_ansistrings in aktlocalswitches) and (len>255) then
  1283. len:=255;
  1284. getmem(pc,len+1);
  1285. move(pchar(tconstsym(srsym).value.valueptr)^,pc^,len);
  1286. pc[len]:=#0;
  1287. p1:=cstringconstnode.createpchar(pc,len);
  1288. end;
  1289. constreal :
  1290. p1:=crealconstnode.create(pbestreal(tconstsym(srsym).value.valueptr)^,pbestrealtype^);
  1291. constset :
  1292. p1:=csetconstnode.create(pconstset(tconstsym(srsym).value.valueptr),tconstsym(srsym).consttype);
  1293. constpointer :
  1294. p1:=cpointerconstnode.create(tconstsym(srsym).value.valueordptr,tconstsym(srsym).consttype);
  1295. constnil :
  1296. p1:=cnilnode.create;
  1297. constresourcestring:
  1298. begin
  1299. p1:=cloadnode.create(srsym,srsymtable);
  1300. do_resulttypepass(p1);
  1301. {$ifdef ansistring_bits}
  1302. case aktansistring_bits of
  1303. sb_16:
  1304. p1.resulttype:=cansistringtype16;
  1305. sb_32:
  1306. p1.resulttype:=cansistringtype32;
  1307. sb_64:
  1308. p1.resulttype:=cansistringtype64;
  1309. end;
  1310. {$else}
  1311. p1.resulttype:=cansistringtype;
  1312. {$endif}
  1313. end;
  1314. constguid :
  1315. p1:=cguidconstnode.create(pguid(tconstsym(srsym).value.valueptr)^);
  1316. end;
  1317. end;
  1318. procsym :
  1319. begin
  1320. { are we in a class method ? }
  1321. possible_error:=(srsymtable.symtabletype<>withsymtable) and
  1322. (srsym.owner.symtabletype=objectsymtable) and
  1323. not(is_interface(tdef(srsym.owner.defowner))) and
  1324. assigned(current_procinfo) and
  1325. (po_classmethod in current_procinfo.procdef.procoptions);
  1326. do_proc_call(srsym,srsymtable,nil,
  1327. (getaddr and not(token in [_CARET,_POINT])),
  1328. again,p1,[]);
  1329. { we need to know which procedure is called }
  1330. if possible_error then
  1331. begin
  1332. do_resulttypepass(p1);
  1333. if (p1.nodetype=calln) and
  1334. assigned(tcallnode(p1).procdefinition) and
  1335. not(tcallnode(p1).procdefinition.proctypeoption=potype_constructor) and
  1336. not(po_classmethod in tcallnode(p1).procdefinition.procoptions) then
  1337. Message(parser_e_only_class_methods);
  1338. end;
  1339. end;
  1340. propertysym :
  1341. begin
  1342. { access to property in a method }
  1343. { are we in a class method ? }
  1344. if (srsymtable.symtabletype=objectsymtable) and
  1345. assigned(current_procinfo) and
  1346. (po_classmethod in current_procinfo.procdef.procoptions) then
  1347. Message(parser_e_only_class_methods);
  1348. { no method pointer }
  1349. p1:=nil;
  1350. handle_propertysym(srsym,srsymtable,p1);
  1351. end;
  1352. labelsym :
  1353. begin
  1354. { Support @label }
  1355. if getaddr then
  1356. p1:=cloadnode.create(srsym,srsym.owner)
  1357. else
  1358. begin
  1359. consume(_COLON);
  1360. if tlabelsym(srsym).defined then
  1361. Message(sym_e_label_already_defined);
  1362. tlabelsym(srsym).defined:=true;
  1363. p1:=clabelnode.create(tlabelsym(srsym),nil);
  1364. end;
  1365. end;
  1366. errorsym :
  1367. begin
  1368. p1:=cerrornode.create;
  1369. if try_to_consume(_LKLAMMER) then
  1370. begin
  1371. parse_paras(false,_RKLAMMER);
  1372. consume(_RKLAMMER);
  1373. end;
  1374. end;
  1375. else
  1376. begin
  1377. p1:=cerrornode.create;
  1378. Message(parser_e_illegal_expression);
  1379. end;
  1380. end; { end case }
  1381. end;
  1382. end;
  1383. {---------------------------------------------
  1384. Factor_Read_Set
  1385. ---------------------------------------------}
  1386. { Read a set between [] }
  1387. function factor_read_set:tnode;
  1388. var
  1389. p1,p2 : tnode;
  1390. lastp,
  1391. buildp : tarrayconstructornode;
  1392. begin
  1393. buildp:=nil;
  1394. { be sure that a least one arrayconstructn is used, also for an
  1395. empty [] }
  1396. if token=_RECKKLAMMER then
  1397. buildp:=carrayconstructornode.create(nil,buildp)
  1398. else
  1399. repeat
  1400. p1:=comp_expr(true);
  1401. if try_to_consume(_POINTPOINT) then
  1402. begin
  1403. p2:=comp_expr(true);
  1404. p1:=carrayconstructorrangenode.create(p1,p2);
  1405. end;
  1406. { insert at the end of the tree, to get the correct order }
  1407. if not assigned(buildp) then
  1408. begin
  1409. buildp:=carrayconstructornode.create(p1,nil);
  1410. lastp:=buildp;
  1411. end
  1412. else
  1413. begin
  1414. lastp.right:=carrayconstructornode.create(p1,nil);
  1415. lastp:=tarrayconstructornode(lastp.right);
  1416. end;
  1417. { there could be more elements }
  1418. until not try_to_consume(_COMMA);
  1419. factor_read_set:=buildp;
  1420. end;
  1421. {---------------------------------------------
  1422. PostFixOperators
  1423. ---------------------------------------------}
  1424. procedure postfixoperators(var p1:tnode;var again:boolean);
  1425. { tries to avoid syntax errors after invalid qualifiers }
  1426. procedure recoverconsume_postfixops;
  1427. begin
  1428. repeat
  1429. if not try_to_consume(_CARET) then
  1430. if try_to_consume(_POINT) then
  1431. try_to_consume(_ID)
  1432. else if try_to_consume(_LECKKLAMMER) then
  1433. begin
  1434. repeat
  1435. comp_expr(true);
  1436. until not try_to_consume(_COMMA);
  1437. consume(_RECKKLAMMER);
  1438. end
  1439. else
  1440. break;
  1441. until false;
  1442. end;
  1443. procedure handle_variantarray;
  1444. var
  1445. p4 : tnode;
  1446. newstatement : tstatementnode;
  1447. tempresultvariant,
  1448. temp : ttempcreatenode;
  1449. paras : tcallparanode;
  1450. newblock : tnode;
  1451. countindices : aint;
  1452. begin
  1453. { create statements with call initialize the arguments and
  1454. call fpc_dynarr_setlength }
  1455. newblock:=internalstatements(newstatement);
  1456. { get temp for array of indicies,
  1457. we set the real size later }
  1458. temp:=ctempcreatenode.create(sinttype,4,tt_persistent,false);
  1459. addstatement(newstatement,temp);
  1460. countindices:=0;
  1461. repeat
  1462. p4:=comp_expr(true);
  1463. addstatement(newstatement,cassignmentnode.create(
  1464. ctemprefnode.create_offset(temp,countindices*sinttype.def.size),p4));
  1465. inc(countindices);
  1466. until not try_to_consume(_COMMA);
  1467. { set real size }
  1468. temp.size:=countindices*sinttype.def.size;
  1469. consume(_RECKKLAMMER);
  1470. { we need only a write access if a := follows }
  1471. if token=_ASSIGNMENT then
  1472. begin
  1473. consume(_ASSIGNMENT);
  1474. p4:=comp_expr(true);
  1475. { create call to fpc_vararray_put }
  1476. paras:=ccallparanode.create(cordconstnode.create
  1477. (countindices,sinttype,true),
  1478. ccallparanode.create(caddrnode.create_internal
  1479. (ctemprefnode.create(temp)),
  1480. ccallparanode.create(ctypeconvnode.create_internal(p4,cvarianttype),
  1481. ccallparanode.create(p1
  1482. ,nil))));
  1483. addstatement(newstatement,ccallnode.createintern('fpc_vararray_put',paras));
  1484. addstatement(newstatement,ctempdeletenode.create(temp));
  1485. end
  1486. else
  1487. begin
  1488. { create temp for result }
  1489. tempresultvariant:=ctempcreatenode.create(cvarianttype,cvarianttype.def.size,tt_persistent,true);
  1490. addstatement(newstatement,tempresultvariant);
  1491. { create call to fpc_vararray_get }
  1492. paras:=ccallparanode.create(cordconstnode.create
  1493. (countindices,sinttype,true),
  1494. ccallparanode.create(caddrnode.create_internal
  1495. (ctemprefnode.create(temp)),
  1496. ccallparanode.create(p1,
  1497. ccallparanode.create(
  1498. ctemprefnode.create(tempresultvariant)
  1499. ,nil))));
  1500. addstatement(newstatement,ccallnode.createintern('fpc_vararray_get',paras));
  1501. addstatement(newstatement,ctempdeletenode.create(temp));
  1502. { the last statement should return the value as
  1503. location and type, this is done be referencing the
  1504. temp and converting it first from a persistent temp to
  1505. normal temp }
  1506. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempresultvariant));
  1507. addstatement(newstatement,ctemprefnode.create(tempresultvariant));
  1508. end;
  1509. p1:=newblock;
  1510. end;
  1511. var
  1512. store_static : boolean;
  1513. protsym : tpropertysym;
  1514. p2,p3 : tnode;
  1515. hsym : tsym;
  1516. classh : tobjectdef;
  1517. label
  1518. skipreckklammercheck;
  1519. begin
  1520. again:=true;
  1521. while again do
  1522. begin
  1523. { we need the resulttype }
  1524. do_resulttypepass(p1);
  1525. if codegenerror then
  1526. begin
  1527. recoverconsume_postfixops;
  1528. exit;
  1529. end;
  1530. { handle token }
  1531. case token of
  1532. _CARET:
  1533. begin
  1534. consume(_CARET);
  1535. if (p1.resulttype.def.deftype<>pointerdef) then
  1536. begin
  1537. { ^ as binary operator is a problem!!!! (FK) }
  1538. again:=false;
  1539. Message(parser_e_invalid_qualifier);
  1540. recoverconsume_postfixops;
  1541. p1.destroy;
  1542. p1:=cerrornode.create;
  1543. end
  1544. else
  1545. begin
  1546. p1:=cderefnode.create(p1);
  1547. end;
  1548. end;
  1549. _LECKKLAMMER:
  1550. begin
  1551. if is_class_or_interface(p1.resulttype.def) then
  1552. begin
  1553. { default property }
  1554. protsym:=search_default_property(tobjectdef(p1.resulttype.def));
  1555. if not(assigned(protsym)) then
  1556. begin
  1557. p1.destroy;
  1558. p1:=cerrornode.create;
  1559. again:=false;
  1560. message(parser_e_no_default_property_available);
  1561. end
  1562. else
  1563. begin
  1564. { The property symbol is referenced indirect }
  1565. inc(protsym.refs);
  1566. handle_propertysym(protsym,protsym.owner,p1);
  1567. end;
  1568. end
  1569. else
  1570. begin
  1571. consume(_LECKKLAMMER);
  1572. repeat
  1573. case p1.resulttype.def.deftype of
  1574. pointerdef:
  1575. begin
  1576. { support delphi autoderef }
  1577. if (tpointerdef(p1.resulttype.def).pointertype.def.deftype=arraydef) and
  1578. (m_autoderef in aktmodeswitches) then
  1579. begin
  1580. p1:=cderefnode.create(p1);
  1581. end;
  1582. p2:=comp_expr(true);
  1583. p1:=cvecnode.create(p1,p2);
  1584. end;
  1585. variantdef:
  1586. begin
  1587. handle_variantarray;
  1588. { the RECKKLAMMER is already read }
  1589. goto skipreckklammercheck;
  1590. end;
  1591. stringdef :
  1592. begin
  1593. p2:=comp_expr(true);
  1594. p1:=cvecnode.create(p1,p2);
  1595. end;
  1596. arraydef :
  1597. begin
  1598. p2:=comp_expr(true);
  1599. { support SEG:OFS for go32v2 Mem[] }
  1600. if (target_info.system in [system_i386_go32v2,system_i386_watcom]) and
  1601. (p1.nodetype=loadn) and
  1602. assigned(tloadnode(p1).symtableentry) and
  1603. assigned(tloadnode(p1).symtableentry.owner.name) and
  1604. (tloadnode(p1).symtableentry.owner.name^='SYSTEM') and
  1605. ((tloadnode(p1).symtableentry.name='MEM') or
  1606. (tloadnode(p1).symtableentry.name='MEMW') or
  1607. (tloadnode(p1).symtableentry.name='MEML')) then
  1608. begin
  1609. if try_to_consume(_COLON) then
  1610. begin
  1611. p3:=caddnode.create(muln,cordconstnode.create($10,s32inttype,false),p2);
  1612. p2:=comp_expr(true);
  1613. p2:=caddnode.create(addn,p2,p3);
  1614. p1:=cvecnode.create(p1,p2);
  1615. include(tvecnode(p1).flags,nf_memseg);
  1616. include(tvecnode(p1).flags,nf_memindex);
  1617. end
  1618. else
  1619. begin
  1620. p1:=cvecnode.create(p1,p2);
  1621. include(tvecnode(p1).flags,nf_memindex);
  1622. end;
  1623. end
  1624. else
  1625. p1:=cvecnode.create(p1,p2);
  1626. end;
  1627. else
  1628. begin
  1629. Message(parser_e_invalid_qualifier);
  1630. p1.destroy;
  1631. p1:=cerrornode.create;
  1632. comp_expr(true);
  1633. again:=false;
  1634. end;
  1635. end;
  1636. do_resulttypepass(p1);
  1637. until not try_to_consume(_COMMA);
  1638. consume(_RECKKLAMMER);
  1639. { handle_variantarray eats the RECKKLAMMER and jumps here }
  1640. skipreckklammercheck:
  1641. end;
  1642. end;
  1643. _POINT :
  1644. begin
  1645. consume(_POINT);
  1646. if (p1.resulttype.def.deftype=pointerdef) and
  1647. (m_autoderef in aktmodeswitches) then
  1648. begin
  1649. p1:=cderefnode.create(p1);
  1650. do_resulttypepass(p1);
  1651. end;
  1652. case p1.resulttype.def.deftype of
  1653. recorddef:
  1654. begin
  1655. if token=_ID then
  1656. begin
  1657. hsym:=tsym(trecorddef(p1.resulttype.def).symtable.search(pattern));
  1658. check_hints(hsym);
  1659. if assigned(hsym) and
  1660. (hsym.typ=fieldvarsym) then
  1661. p1:=csubscriptnode.create(hsym,p1)
  1662. else
  1663. begin
  1664. Message1(sym_e_illegal_field,pattern);
  1665. p1.destroy;
  1666. p1:=cerrornode.create;
  1667. end;
  1668. end;
  1669. consume(_ID);
  1670. end;
  1671. variantdef:
  1672. begin
  1673. end;
  1674. classrefdef:
  1675. begin
  1676. if token=_ID then
  1677. begin
  1678. classh:=tobjectdef(tclassrefdef(p1.resulttype.def).pointertype.def);
  1679. hsym:=searchsym_in_class(classh,pattern);
  1680. check_hints(hsym);
  1681. if hsym=nil then
  1682. begin
  1683. Message1(sym_e_id_no_member,orgpattern);
  1684. p1.destroy;
  1685. p1:=cerrornode.create;
  1686. { try to clean up }
  1687. consume(_ID);
  1688. end
  1689. else
  1690. begin
  1691. consume(_ID);
  1692. do_member_read(classh,getaddr,hsym,p1,again,[]);
  1693. end;
  1694. end
  1695. else { Error }
  1696. Consume(_ID);
  1697. end;
  1698. objectdef:
  1699. begin
  1700. if token=_ID then
  1701. begin
  1702. store_static:=allow_only_static;
  1703. allow_only_static:=false;
  1704. classh:=tobjectdef(p1.resulttype.def);
  1705. hsym:=searchsym_in_class(classh,pattern);
  1706. check_hints(hsym);
  1707. allow_only_static:=store_static;
  1708. if hsym=nil then
  1709. begin
  1710. Message1(sym_e_id_no_member,orgpattern);
  1711. p1.destroy;
  1712. p1:=cerrornode.create;
  1713. { try to clean up }
  1714. consume(_ID);
  1715. end
  1716. else
  1717. begin
  1718. consume(_ID);
  1719. do_member_read(classh,getaddr,hsym,p1,again,[]);
  1720. end;
  1721. end
  1722. else { Error }
  1723. Consume(_ID);
  1724. end;
  1725. pointerdef:
  1726. begin
  1727. Message(parser_e_invalid_qualifier);
  1728. if tpointerdef(p1.resulttype.def).pointertype.def.deftype in [recorddef,objectdef,classrefdef] then
  1729. Message(parser_h_maybe_deref_caret_missing);
  1730. end;
  1731. else
  1732. begin
  1733. Message(parser_e_invalid_qualifier);
  1734. p1.destroy;
  1735. p1:=cerrornode.create;
  1736. { Error }
  1737. consume(_ID);
  1738. end;
  1739. end;
  1740. end;
  1741. else
  1742. begin
  1743. { is this a procedure variable ? }
  1744. if assigned(p1.resulttype.def) and
  1745. (p1.resulttype.def.deftype=procvardef) then
  1746. begin
  1747. if assigned(getprocvardef) and
  1748. equal_defs(p1.resulttype.def,getprocvardef) then
  1749. again:=false
  1750. else
  1751. begin
  1752. if try_to_consume(_LKLAMMER) then
  1753. begin
  1754. p2:=parse_paras(false,_RKLAMMER);
  1755. consume(_RKLAMMER);
  1756. p1:=ccallnode.create_procvar(p2,p1);
  1757. { proc():= is never possible }
  1758. if token=_ASSIGNMENT then
  1759. begin
  1760. Message(parser_e_illegal_expression);
  1761. p1.free;
  1762. p1:=cerrornode.create;
  1763. again:=false;
  1764. end;
  1765. end
  1766. else
  1767. again:=false;
  1768. end;
  1769. end
  1770. else
  1771. again:=false;
  1772. end;
  1773. end;
  1774. end; { while again }
  1775. end;
  1776. {---------------------------------------------
  1777. Factor (Main)
  1778. ---------------------------------------------}
  1779. var
  1780. l : longint;
  1781. ic : int64;
  1782. qc : qword;
  1783. {$ifndef cpu64}
  1784. card : cardinal;
  1785. {$endif cpu64}
  1786. oldp1,
  1787. p1 : tnode;
  1788. code : integer;
  1789. again : boolean;
  1790. sym : tsym;
  1791. pd : tprocdef;
  1792. classh : tobjectdef;
  1793. d : bestreal;
  1794. hs,hsorg : string;
  1795. htype : ttype;
  1796. filepos : tfileposinfo;
  1797. {---------------------------------------------
  1798. Helpers
  1799. ---------------------------------------------}
  1800. procedure check_tokenpos;
  1801. begin
  1802. if (p1<>oldp1) then
  1803. begin
  1804. if assigned(p1) then
  1805. p1.fileinfo:=filepos;
  1806. oldp1:=p1;
  1807. filepos:=akttokenpos;
  1808. end;
  1809. end;
  1810. begin
  1811. oldp1:=nil;
  1812. p1:=nil;
  1813. filepos:=akttokenpos;
  1814. again:=false;
  1815. if token=_ID then
  1816. begin
  1817. again:=true;
  1818. { Handle references to self }
  1819. if (idtoken=_SELF) and
  1820. not(block_type in [bt_const,bt_type]) and
  1821. assigned(current_procinfo) and
  1822. assigned(current_procinfo.procdef._class) then
  1823. begin
  1824. p1:=load_self_node;
  1825. consume(_ID);
  1826. again:=true;
  1827. end
  1828. else
  1829. factor_read_id(p1,again);
  1830. if again then
  1831. begin
  1832. check_tokenpos;
  1833. { handle post fix operators }
  1834. postfixoperators(p1,again);
  1835. end;
  1836. end
  1837. else
  1838. case token of
  1839. _INHERITED :
  1840. begin
  1841. again:=true;
  1842. consume(_INHERITED);
  1843. if assigned(current_procinfo) and
  1844. assigned(current_procinfo.procdef._class) then
  1845. begin
  1846. classh:=current_procinfo.procdef._class.childof;
  1847. { if inherited; only then we need the method with
  1848. the same name }
  1849. if token in endtokens then
  1850. begin
  1851. hs:=current_procinfo.procdef.procsym.name;
  1852. hsorg:=current_procinfo.procdef.procsym.realname;
  1853. anon_inherited:=true;
  1854. { For message methods we need to search using the message
  1855. number or string }
  1856. pd:=tprocsym(current_procinfo.procdef.procsym).first_procdef;
  1857. if (po_msgint in pd.procoptions) then
  1858. sym:=searchsym_in_class_by_msgint(classh,pd.messageinf.i)
  1859. else
  1860. if (po_msgstr in pd.procoptions) then
  1861. sym:=searchsym_in_class_by_msgstr(classh,pd.messageinf.str)
  1862. else
  1863. sym:=searchsym_in_class(classh,hs);
  1864. end
  1865. else
  1866. begin
  1867. hs:=pattern;
  1868. hsorg:=orgpattern;
  1869. consume(_ID);
  1870. anon_inherited:=false;
  1871. sym:=searchsym_in_class(classh,hs);
  1872. end;
  1873. if assigned(sym) then
  1874. begin
  1875. check_hints(sym);
  1876. { load the procdef from the inherited class and
  1877. not from self }
  1878. if sym.typ in [procsym,propertysym] then
  1879. begin
  1880. if (sym.typ = procsym) then
  1881. begin
  1882. htype.setdef(classh);
  1883. if (po_classmethod in current_procinfo.procdef.procoptions) or
  1884. (po_staticmethod in current_procinfo.procdef.procoptions) then
  1885. htype.setdef(tclassrefdef.create(htype));
  1886. p1:=ctypenode.create(htype);
  1887. end;
  1888. end
  1889. else
  1890. begin
  1891. Message(parser_e_methode_id_expected);
  1892. p1:=cerrornode.create;
  1893. end;
  1894. do_member_read(classh,false,sym,p1,again,[cnf_inherited,cnf_anon_inherited]);
  1895. end
  1896. else
  1897. begin
  1898. if anon_inherited then
  1899. begin
  1900. { For message methods we need to call DefaultHandler }
  1901. if (po_msgint in pd.procoptions) or
  1902. (po_msgstr in pd.procoptions) then
  1903. begin
  1904. sym:=searchsym_in_class(classh,'DEFAULTHANDLER');
  1905. if not assigned(sym) or
  1906. (sym.typ<>procsym) then
  1907. internalerror(200303171);
  1908. p1:=nil;
  1909. do_proc_call(sym,sym.owner,classh,false,again,p1,[]);
  1910. end
  1911. else
  1912. begin
  1913. { we need to ignore the inherited; }
  1914. p1:=cnothingnode.create;
  1915. end;
  1916. end
  1917. else
  1918. begin
  1919. Message1(sym_e_id_no_member,hsorg);
  1920. p1:=cerrornode.create;
  1921. end;
  1922. again:=false;
  1923. end;
  1924. { turn auto inheriting off }
  1925. anon_inherited:=false;
  1926. end
  1927. else
  1928. begin
  1929. Message(parser_e_generic_methods_only_in_methods);
  1930. again:=false;
  1931. p1:=cerrornode.create;
  1932. end;
  1933. postfixoperators(p1,again);
  1934. end;
  1935. _INTCONST :
  1936. begin
  1937. {$ifdef cpu64}
  1938. { when already running under 64bit must read int64 constant, because reading
  1939. cardinal first will also succeed (code=0) for values > maxcardinal, because
  1940. range checking is off by default (PFV) }
  1941. val(pattern,ic,code);
  1942. if code=0 then
  1943. begin
  1944. consume(_INTCONST);
  1945. int_to_type(ic,htype);
  1946. p1:=cordconstnode.create(ic,htype,true);
  1947. end
  1948. else
  1949. begin
  1950. { try qword next }
  1951. val(pattern,qc,code);
  1952. if code=0 then
  1953. begin
  1954. consume(_INTCONST);
  1955. htype:=u64inttype;
  1956. p1:=cordconstnode.create(qc,htype,true);
  1957. end;
  1958. end;
  1959. {$else}
  1960. { try cardinal first }
  1961. val(pattern,card,code);
  1962. if code=0 then
  1963. begin
  1964. consume(_INTCONST);
  1965. int_to_type(card,htype);
  1966. p1:=cordconstnode.create(card,htype,true);
  1967. end
  1968. else
  1969. begin
  1970. { then longint }
  1971. val(pattern,l,code);
  1972. if code = 0 then
  1973. begin
  1974. consume(_INTCONST);
  1975. int_to_type(l,htype);
  1976. p1:=cordconstnode.create(l,htype,true);
  1977. end
  1978. else
  1979. begin
  1980. { then int64 }
  1981. val(pattern,ic,code);
  1982. if code=0 then
  1983. begin
  1984. consume(_INTCONST);
  1985. int_to_type(ic,htype);
  1986. p1:=cordconstnode.create(ic,htype,true);
  1987. end
  1988. else
  1989. begin
  1990. { try qword next }
  1991. val(pattern,qc,code);
  1992. if code=0 then
  1993. begin
  1994. consume(_INTCONST);
  1995. htype:=u64inttype;
  1996. p1:=cordconstnode.create(tconstexprint(qc),htype,true);
  1997. end;
  1998. end;
  1999. end;
  2000. end;
  2001. {$endif}
  2002. if code<>0 then
  2003. begin
  2004. { finally float }
  2005. val(pattern,d,code);
  2006. if code<>0 then
  2007. begin
  2008. Message(parser_e_invalid_integer);
  2009. consume(_INTCONST);
  2010. l:=1;
  2011. p1:=cordconstnode.create(l,sinttype,true);
  2012. end
  2013. else
  2014. begin
  2015. consume(_INTCONST);
  2016. p1:=crealconstnode.create(d,pbestrealtype^);
  2017. end;
  2018. end;
  2019. end;
  2020. _REALNUMBER :
  2021. begin
  2022. val(pattern,d,code);
  2023. if code<>0 then
  2024. begin
  2025. Message(parser_e_error_in_real);
  2026. d:=1.0;
  2027. end;
  2028. consume(_REALNUMBER);
  2029. p1:=crealconstnode.create(d,pbestrealtype^);
  2030. end;
  2031. _STRING :
  2032. begin
  2033. string_dec(htype);
  2034. { STRING can be also a type cast }
  2035. if try_to_consume(_LKLAMMER) then
  2036. begin
  2037. p1:=comp_expr(true);
  2038. consume(_RKLAMMER);
  2039. p1:=ctypeconvnode.create_explicit(p1,htype);
  2040. { handle postfix operators here e.g. string(a)[10] }
  2041. again:=true;
  2042. postfixoperators(p1,again);
  2043. end
  2044. else
  2045. p1:=ctypenode.create(htype);
  2046. end;
  2047. _FILE :
  2048. begin
  2049. htype:=cfiletype;
  2050. consume(_FILE);
  2051. { FILE can be also a type cast }
  2052. if try_to_consume(_LKLAMMER) then
  2053. begin
  2054. p1:=comp_expr(true);
  2055. consume(_RKLAMMER);
  2056. p1:=ctypeconvnode.create_explicit(p1,htype);
  2057. { handle postfix operators here e.g. string(a)[10] }
  2058. again:=true;
  2059. postfixoperators(p1,again);
  2060. end
  2061. else
  2062. begin
  2063. p1:=ctypenode.create(htype);
  2064. end;
  2065. end;
  2066. _CSTRING :
  2067. begin
  2068. p1:=cstringconstnode.createstr(pattern,st_default);
  2069. consume(_CSTRING);
  2070. end;
  2071. _CCHAR :
  2072. begin
  2073. p1:=cordconstnode.create(ord(pattern[1]),cchartype,true);
  2074. consume(_CCHAR);
  2075. end;
  2076. _CWSTRING:
  2077. begin
  2078. p1:=cstringconstnode.createwstr(patternw);
  2079. consume(_CWSTRING);
  2080. end;
  2081. _CWCHAR:
  2082. begin
  2083. p1:=cordconstnode.create(ord(getcharwidestring(patternw,0)),cwidechartype,true);
  2084. consume(_CWCHAR);
  2085. end;
  2086. _KLAMMERAFFE :
  2087. begin
  2088. consume(_KLAMMERAFFE);
  2089. got_addrn:=true;
  2090. { support both @<x> and @(<x>) }
  2091. if try_to_consume(_LKLAMMER) then
  2092. begin
  2093. p1:=factor(true);
  2094. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2095. begin
  2096. again:=true;
  2097. postfixoperators(p1,again);
  2098. end;
  2099. consume(_RKLAMMER);
  2100. end
  2101. else
  2102. p1:=factor(true);
  2103. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2104. begin
  2105. again:=true;
  2106. postfixoperators(p1,again);
  2107. end;
  2108. got_addrn:=false;
  2109. p1:=caddrnode.create(p1);
  2110. if cs_typed_addresses in aktlocalswitches then
  2111. include(p1.flags,nf_typedaddr);
  2112. { Store the procvar that we are expecting, the
  2113. addrn will use the information to find the correct
  2114. procdef or it will return an error }
  2115. if assigned(getprocvardef) and
  2116. (taddrnode(p1).left.nodetype = loadn) then
  2117. taddrnode(p1).getprocvardef:=getprocvardef;
  2118. end;
  2119. _LKLAMMER :
  2120. begin
  2121. consume(_LKLAMMER);
  2122. p1:=comp_expr(true);
  2123. consume(_RKLAMMER);
  2124. { it's not a good solution }
  2125. { but (a+b)^ makes some problems }
  2126. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2127. begin
  2128. again:=true;
  2129. postfixoperators(p1,again);
  2130. end;
  2131. end;
  2132. _LECKKLAMMER :
  2133. begin
  2134. consume(_LECKKLAMMER);
  2135. p1:=factor_read_set;
  2136. consume(_RECKKLAMMER);
  2137. end;
  2138. _PLUS :
  2139. begin
  2140. consume(_PLUS);
  2141. p1:=factor(false);
  2142. end;
  2143. _MINUS :
  2144. begin
  2145. consume(_MINUS);
  2146. if (token = _INTCONST) then
  2147. begin
  2148. { ugly hack, but necessary to be able to parse }
  2149. { -9223372036854775808 as int64 (JM) }
  2150. pattern := '-'+pattern;
  2151. p1:=sub_expr(oppower,false);
  2152. { -1 ** 4 should be - (1 ** 4) and not
  2153. (-1) ** 4
  2154. This was the reason of tw0869.pp test failure PM }
  2155. if p1.nodetype=starstarn then
  2156. begin
  2157. if tbinarynode(p1).left.nodetype=ordconstn then
  2158. begin
  2159. tordconstnode(tbinarynode(p1).left).value:=-tordconstnode(tbinarynode(p1).left).value;
  2160. p1:=cunaryminusnode.create(p1);
  2161. end
  2162. else if tbinarynode(p1).left.nodetype=realconstn then
  2163. begin
  2164. trealconstnode(tbinarynode(p1).left).value_real:=-trealconstnode(tbinarynode(p1).left).value_real;
  2165. p1:=cunaryminusnode.create(p1);
  2166. end
  2167. else
  2168. internalerror(20021029);
  2169. end;
  2170. end
  2171. else
  2172. begin
  2173. p1:=sub_expr(oppower,false);
  2174. p1:=cunaryminusnode.create(p1);
  2175. end;
  2176. end;
  2177. _OP_NOT :
  2178. begin
  2179. consume(_OP_NOT);
  2180. p1:=factor(false);
  2181. p1:=cnotnode.create(p1);
  2182. end;
  2183. _TRUE :
  2184. begin
  2185. consume(_TRUE);
  2186. p1:=cordconstnode.create(1,booltype,false);
  2187. end;
  2188. _FALSE :
  2189. begin
  2190. consume(_FALSE);
  2191. p1:=cordconstnode.create(0,booltype,false);
  2192. end;
  2193. _NIL :
  2194. begin
  2195. consume(_NIL);
  2196. p1:=cnilnode.create;
  2197. { It's really ugly code nil^, but delphi allows it }
  2198. if token in [_CARET] then
  2199. begin
  2200. again:=true;
  2201. postfixoperators(p1,again);
  2202. end;
  2203. end;
  2204. else
  2205. begin
  2206. p1:=cerrornode.create;
  2207. consume(token);
  2208. Message(parser_e_illegal_expression);
  2209. end;
  2210. end;
  2211. { generate error node if no node is created }
  2212. if not assigned(p1) then
  2213. begin
  2214. {$ifdef EXTDEBUG}
  2215. Comment(V_Warning,'factor: p1=nil');
  2216. {$endif}
  2217. p1:=cerrornode.create;
  2218. end;
  2219. { get the resulttype for the node }
  2220. if (not assigned(p1.resulttype.def)) then
  2221. do_resulttypepass(p1);
  2222. factor:=p1;
  2223. check_tokenpos;
  2224. end;
  2225. {$ifdef fpc}
  2226. {$maxfpuregisters default}
  2227. {$endif fpc}
  2228. {****************************************************************************
  2229. Sub_Expr
  2230. ****************************************************************************}
  2231. const
  2232. { Warning these stay be ordered !! }
  2233. operator_levels:array[Toperator_precedence] of set of Ttoken=
  2234. ([_LT,_LTE,_GT,_GTE,_EQUAL,_UNEQUAL,_OP_IN],
  2235. [_PLUS,_MINUS,_OP_OR,_PIPE,_OP_XOR],
  2236. [_CARET,_SYMDIF,_STARSTAR,_STAR,_SLASH,
  2237. _OP_AS,_OP_IS,_OP_AND,_AMPERSAND,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR],
  2238. [_STARSTAR] );
  2239. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):tnode;
  2240. {Reads a subexpression while the operators are of the current precedence
  2241. level, or any higher level. Replaces the old term, simpl_expr and
  2242. simpl2_expr.}
  2243. var
  2244. p1,p2 : tnode;
  2245. oldt : Ttoken;
  2246. filepos : tfileposinfo;
  2247. begin
  2248. if pred_level=highest_precedence then
  2249. p1:=factor(false)
  2250. else
  2251. p1:=sub_expr(succ(pred_level),true);
  2252. repeat
  2253. if (token in operator_levels[pred_level]) and
  2254. ((token<>_EQUAL) or accept_equal) then
  2255. begin
  2256. oldt:=token;
  2257. filepos:=akttokenpos;
  2258. consume(token);
  2259. if pred_level=highest_precedence then
  2260. p2:=factor(false)
  2261. else
  2262. p2:=sub_expr(succ(pred_level),true);
  2263. case oldt of
  2264. _PLUS :
  2265. p1:=caddnode.create(addn,p1,p2);
  2266. _MINUS :
  2267. p1:=caddnode.create(subn,p1,p2);
  2268. _STAR :
  2269. p1:=caddnode.create(muln,p1,p2);
  2270. _SLASH :
  2271. p1:=caddnode.create(slashn,p1,p2);
  2272. _EQUAL :
  2273. p1:=caddnode.create(equaln,p1,p2);
  2274. _GT :
  2275. p1:=caddnode.create(gtn,p1,p2);
  2276. _LT :
  2277. p1:=caddnode.create(ltn,p1,p2);
  2278. _GTE :
  2279. p1:=caddnode.create(gten,p1,p2);
  2280. _LTE :
  2281. p1:=caddnode.create(lten,p1,p2);
  2282. _SYMDIF :
  2283. p1:=caddnode.create(symdifn,p1,p2);
  2284. _STARSTAR :
  2285. p1:=caddnode.create(starstarn,p1,p2);
  2286. _OP_AS :
  2287. p1:=casnode.create(p1,p2);
  2288. _OP_IN :
  2289. p1:=cinnode.create(p1,p2);
  2290. _OP_IS :
  2291. p1:=cisnode.create(p1,p2);
  2292. _OP_OR,
  2293. _PIPE {macpas only} :
  2294. p1:=caddnode.create(orn,p1,p2);
  2295. _OP_AND,
  2296. _AMPERSAND {macpas only} :
  2297. p1:=caddnode.create(andn,p1,p2);
  2298. _OP_DIV :
  2299. p1:=cmoddivnode.create(divn,p1,p2);
  2300. _OP_NOT :
  2301. p1:=cnotnode.create(p1);
  2302. _OP_MOD :
  2303. p1:=cmoddivnode.create(modn,p1,p2);
  2304. _OP_SHL :
  2305. p1:=cshlshrnode.create(shln,p1,p2);
  2306. _OP_SHR :
  2307. p1:=cshlshrnode.create(shrn,p1,p2);
  2308. _OP_XOR :
  2309. p1:=caddnode.create(xorn,p1,p2);
  2310. _ASSIGNMENT :
  2311. p1:=cassignmentnode.create(p1,p2);
  2312. _CARET :
  2313. p1:=caddnode.create(caretn,p1,p2);
  2314. _UNEQUAL :
  2315. p1:=caddnode.create(unequaln,p1,p2);
  2316. end;
  2317. p1.fileinfo:=filepos;
  2318. end
  2319. else
  2320. break;
  2321. until false;
  2322. sub_expr:=p1;
  2323. end;
  2324. function comp_expr(accept_equal : boolean):tnode;
  2325. var
  2326. oldafterassignment : boolean;
  2327. p1 : tnode;
  2328. begin
  2329. oldafterassignment:=afterassignment;
  2330. afterassignment:=true;
  2331. p1:=sub_expr(opcompare,accept_equal);
  2332. { get the resulttype for this expression }
  2333. if not assigned(p1.resulttype.def) then
  2334. do_resulttypepass(p1);
  2335. afterassignment:=oldafterassignment;
  2336. comp_expr:=p1;
  2337. end;
  2338. function expr : tnode;
  2339. var
  2340. p1,p2 : tnode;
  2341. oldafterassignment : boolean;
  2342. oldp1 : tnode;
  2343. filepos : tfileposinfo;
  2344. begin
  2345. oldafterassignment:=afterassignment;
  2346. p1:=sub_expr(opcompare,true);
  2347. { get the resulttype for this expression }
  2348. if not assigned(p1.resulttype.def) then
  2349. do_resulttypepass(p1);
  2350. filepos:=akttokenpos;
  2351. if token in [_ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  2352. afterassignment:=true;
  2353. oldp1:=p1;
  2354. case token of
  2355. _POINTPOINT :
  2356. begin
  2357. consume(_POINTPOINT);
  2358. p2:=sub_expr(opcompare,true);
  2359. p1:=crangenode.create(p1,p2);
  2360. end;
  2361. _ASSIGNMENT :
  2362. begin
  2363. consume(_ASSIGNMENT);
  2364. if (p1.resulttype.def.deftype=procvardef) then
  2365. getprocvardef:=tprocvardef(p1.resulttype.def);
  2366. p2:=sub_expr(opcompare,true);
  2367. if assigned(getprocvardef) then
  2368. handle_procvar(getprocvardef,p2);
  2369. getprocvardef:=nil;
  2370. p1:=cassignmentnode.create(p1,p2);
  2371. end;
  2372. _PLUSASN :
  2373. begin
  2374. consume(_PLUSASN);
  2375. p2:=sub_expr(opcompare,true);
  2376. p1:=gen_c_style_operator(addn,p1,p2);
  2377. end;
  2378. _MINUSASN :
  2379. begin
  2380. consume(_MINUSASN);
  2381. p2:=sub_expr(opcompare,true);
  2382. p1:=gen_c_style_operator(subn,p1,p2);
  2383. end;
  2384. _STARASN :
  2385. begin
  2386. consume(_STARASN );
  2387. p2:=sub_expr(opcompare,true);
  2388. p1:=gen_c_style_operator(muln,p1,p2);
  2389. end;
  2390. _SLASHASN :
  2391. begin
  2392. consume(_SLASHASN );
  2393. p2:=sub_expr(opcompare,true);
  2394. p1:=gen_c_style_operator(slashn,p1,p2);
  2395. end;
  2396. end;
  2397. { get the resulttype for this expression }
  2398. if not assigned(p1.resulttype.def) then
  2399. do_resulttypepass(p1);
  2400. afterassignment:=oldafterassignment;
  2401. if p1<>oldp1 then
  2402. p1.fileinfo:=filepos;
  2403. expr:=p1;
  2404. end;
  2405. {$ifdef int64funcresok}
  2406. function get_intconst:TConstExprInt;
  2407. {$else int64funcresok}
  2408. function get_intconst:longint;
  2409. {$endif int64funcresok}
  2410. {Reads an expression, tries to evalute it and check if it is an integer
  2411. constant. Then the constant is returned.}
  2412. var
  2413. p:tnode;
  2414. begin
  2415. result:=0;
  2416. p:=comp_expr(true);
  2417. if not codegenerror then
  2418. begin
  2419. if (p.nodetype<>ordconstn) or
  2420. not(is_integer(p.resulttype.def)) then
  2421. Message(parser_e_illegal_expression)
  2422. else
  2423. result:=tordconstnode(p).value;
  2424. end;
  2425. p.free;
  2426. end;
  2427. function get_stringconst:string;
  2428. {Reads an expression, tries to evaluate it and checks if it is a string
  2429. constant. Then the constant is returned.}
  2430. var
  2431. p:tnode;
  2432. begin
  2433. get_stringconst:='';
  2434. p:=comp_expr(true);
  2435. if p.nodetype<>stringconstn then
  2436. begin
  2437. if (p.nodetype=ordconstn) and is_char(p.resulttype.def) then
  2438. get_stringconst:=char(tordconstnode(p).value)
  2439. else
  2440. Message(parser_e_illegal_expression);
  2441. end
  2442. else
  2443. get_stringconst:=strpas(tstringconstnode(p).value_str);
  2444. p.free;
  2445. end;
  2446. end.
  2447. {
  2448. $Log$
  2449. Revision 1.189 2005-04-06 11:49:37 michael
  2450. * Fix methodpointer copy from callnode to loadnode
  2451. Revision 1.188 2005/03/28 14:14:52 florian
  2452. * fpc_variant_get call fixed
  2453. Revision 1.187 2005/03/27 20:19:21 florian
  2454. + compiler support for reading/writing of vararrays
  2455. Revision 1.186 2005/03/25 22:20:19 peter
  2456. * add hint when passing an uninitialized variable to a var parameter
  2457. Revision 1.185 2005/03/05 16:37:42 florian
  2458. * fixed copy(dyn. array,...);
  2459. Revision 1.184 2005/03/04 09:56:35 jonas
  2460. * fixed Lazarus compilation
  2461. Revision 1.183 2005/03/04 07:46:36 jonas
  2462. * properties can also be inherited/overridden
  2463. Revision 1.182 2005/03/03 19:58:14 jonas
  2464. * fixed web bug 3738
  2465. Revision 1.181 2005/02/14 17:13:07 peter
  2466. * truncate log
  2467. Revision 1.180 2005/02/06 20:29:12 florian
  2468. * fixed precedence of is-operator
  2469. Revision 1.179 2005/01/20 17:05:53 peter
  2470. * use val() for decoding integers
  2471. Revision 1.178 2005/01/04 16:38:07 peter
  2472. * don't allow properties in C style operators
  2473. }