pexpr.pas 109 KB

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