pexpr.pas 109 KB

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