pdecsub.pas 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl, Daniel Mantione
  3. Does the parsing of the procedures/functions
  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 pdecsub;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. tokens,symconst,symtype,symdef,symsym;
  22. type
  23. tpdflag=(
  24. pd_body, { directive needs a body }
  25. pd_implemen, { directive can be used implementation section }
  26. pd_interface, { directive can be used interface section }
  27. pd_object, { directive can be used object declaration }
  28. pd_procvar, { directive can be used procvar declaration }
  29. pd_notobject, { directive can not be used object declaration }
  30. pd_notobjintf, { directive can not be used interface declaration }
  31. pd_notprocvar, { directive can not be used procvar declaration }
  32. pd_dispinterface,{ directive can be used with dispinterface methods }
  33. pd_cppobject { directive can be used with cppclass }
  34. );
  35. tpdflags=set of tpdflag;
  36. function check_proc_directive(isprocvar:boolean):boolean;
  37. procedure insert_funcret_local(pd:tprocdef);
  38. function proc_add_definition(var currpd:tprocdef):boolean;
  39. function proc_get_importname(pd:tprocdef):string;
  40. procedure proc_set_mangledname(pd:tprocdef);
  41. procedure handle_calling_convention(pd:tabstractprocdef);
  42. procedure parse_parameter_dec(pd:tabstractprocdef);
  43. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  44. procedure parse_var_proc_directives(sym:tsym);
  45. procedure parse_object_proc_directives(pd:tabstractprocdef);
  46. function parse_proc_head(aclass:tobjectdef;potype:tproctypeoption;var pd:tprocdef):boolean;
  47. function parse_proc_dec(aclass:tobjectdef):tprocdef;
  48. implementation
  49. uses
  50. SysUtils,
  51. { common }
  52. cutils,cclasses,
  53. { global }
  54. globtype,globals,verbose,constexp,
  55. systems,
  56. cpuinfo,
  57. { symtable }
  58. symbase,symtable,defutil,defcmp,paramgr,cpupara,
  59. { pass 1 }
  60. fmodule,node,htypechk,
  61. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,
  62. { parser }
  63. scanner,
  64. pbase,pexpr,ptype,pdecl
  65. ;
  66. const
  67. { Please leave this here, this module should NOT use
  68. these variables.
  69. Declaring it as string here results in an error when compiling (PFV) }
  70. current_procinfo = 'error';
  71. procedure insert_funcret_para(pd:tabstractprocdef);
  72. var
  73. storepos : tfileposinfo;
  74. vs : tparavarsym;
  75. paranr : word;
  76. begin
  77. if not(pd.proctypeoption in [potype_constructor,potype_destructor]) and
  78. not is_void(pd.returndef) and
  79. paramanager.ret_in_param(pd.returndef,pd.proccalloption) then
  80. begin
  81. storepos:=current_tokenpos;
  82. if pd.typ=procdef then
  83. current_tokenpos:=tprocdef(pd).fileinfo;
  84. { For left to right add it at the end to be delphi compatible }
  85. if pd.proccalloption in (pushleftright_pocalls+[pocall_safecall]) then
  86. paranr:=paranr_result_leftright
  87. else
  88. paranr:=paranr_result;
  89. { Generate result variable accessing function result }
  90. vs:=tparavarsym.create('$result',paranr,vs_var,pd.returndef,[vo_is_funcret,vo_is_hidden_para]);
  91. pd.parast.insert(vs);
  92. { Store the this symbol as funcretsym for procedures }
  93. if pd.typ=procdef then
  94. tprocdef(pd).funcretsym:=vs;
  95. current_tokenpos:=storepos;
  96. end;
  97. end;
  98. procedure insert_parentfp_para(pd:tabstractprocdef);
  99. var
  100. storepos : tfileposinfo;
  101. vs : tparavarsym;
  102. begin
  103. if pd.parast.symtablelevel>normal_function_level then
  104. begin
  105. storepos:=current_tokenpos;
  106. if pd.typ=procdef then
  107. current_tokenpos:=tprocdef(pd).fileinfo;
  108. { Generate result variable accessing function result, it
  109. can't be put in a register since it must be accessable
  110. from the framepointer }
  111. vs:=tparavarsym.create('$parentfp',paranr_parentfp,vs_value
  112. ,voidpointertype,[vo_is_parentfp,vo_is_hidden_para]);
  113. vs.varregable:=vr_none;
  114. pd.parast.insert(vs);
  115. current_tokenpos:=storepos;
  116. end;
  117. end;
  118. procedure insert_self_and_vmt_para(pd:tabstractprocdef);
  119. var
  120. storepos : tfileposinfo;
  121. vs : tparavarsym;
  122. hdef : tdef;
  123. vsp : tvarspez;
  124. begin
  125. if (pd.typ=procvardef) and
  126. pd.is_methodpointer then
  127. begin
  128. { Generate self variable }
  129. vs:=tparavarsym.create('$self',paranr_self,vs_value,voidpointertype,[vo_is_self,vo_is_hidden_para]);
  130. pd.parast.insert(vs);
  131. end
  132. else
  133. begin
  134. if (pd.typ=procdef) and
  135. assigned(tprocdef(pd)._class) and
  136. (pd.parast.symtablelevel=normal_function_level) then
  137. begin
  138. { static class methods have no hidden self/vmt pointer }
  139. if (po_staticmethod in pd.procoptions) and
  140. (po_classmethod in pd.procoptions) then
  141. exit;
  142. storepos:=current_tokenpos;
  143. current_tokenpos:=tprocdef(pd).fileinfo;
  144. { Generate VMT variable for constructor/destructor }
  145. if (pd.proctypeoption in [potype_constructor,potype_destructor]) and not(is_cppclass(tprocdef(pd)._class)) then
  146. begin
  147. { can't use classrefdef as type because inheriting
  148. will then always file because of a type mismatch }
  149. vs:=tparavarsym.create('$vmt',paranr_vmt,vs_value,voidpointertype,[vo_is_vmt,vo_is_hidden_para]);
  150. pd.parast.insert(vs);
  151. end;
  152. { Generate self variable, for classes we need
  153. to use the generic voidpointer to be compatible with
  154. methodpointers }
  155. vsp:=vs_value;
  156. if (po_staticmethod in pd.procoptions) or
  157. (po_classmethod in pd.procoptions) then
  158. hdef:=tclassrefdef.create(tprocdef(pd)._class)
  159. else
  160. begin
  161. if is_object(tprocdef(pd)._class) then
  162. vsp:=vs_var;
  163. hdef:=tprocdef(pd)._class;
  164. end;
  165. vs:=tparavarsym.create('$self',paranr_self,vsp,hdef,[vo_is_self,vo_is_hidden_para]);
  166. pd.parast.insert(vs);
  167. current_tokenpos:=storepos;
  168. end;
  169. end;
  170. end;
  171. procedure insert_funcret_local(pd:tprocdef);
  172. var
  173. storepos : tfileposinfo;
  174. vs : tlocalvarsym;
  175. aliasvs : tabsolutevarsym;
  176. sl : tpropaccesslist;
  177. hs : string;
  178. begin
  179. { The result from constructors and destructors can't be accessed directly }
  180. if not(pd.proctypeoption in [potype_constructor,potype_destructor]) and
  181. not is_void(pd.returndef) then
  182. begin
  183. storepos:=current_tokenpos;
  184. current_tokenpos:=pd.fileinfo;
  185. { We need to insert a varsym for the result in the localst
  186. when it is returning in a register }
  187. if not paramanager.ret_in_param(pd.returndef,pd.proccalloption) then
  188. begin
  189. vs:=tlocalvarsym.create('$result',vs_value,pd.returndef,[vo_is_funcret]);
  190. pd.localst.insert(vs);
  191. pd.funcretsym:=vs;
  192. end;
  193. { insert the name of the procedure as alias for the function result,
  194. we can't use realname because that will not work for compilerprocs
  195. as the name is lowercase and unreachable from the code }
  196. if assigned(pd.resultname) then
  197. hs:=pd.resultname^
  198. else
  199. hs:=pd.procsym.name;
  200. sl:=tpropaccesslist.create;
  201. sl.addsym(sl_load,pd.funcretsym);
  202. aliasvs:=tabsolutevarsym.create_ref(hs,pd.returndef,sl);
  203. include(aliasvs.varoptions,vo_is_funcret);
  204. tlocalsymtable(pd.localst).insert(aliasvs);
  205. { insert result also if support is on }
  206. if (m_result in current_settings.modeswitches) then
  207. begin
  208. sl:=tpropaccesslist.create;
  209. sl.addsym(sl_load,pd.funcretsym);
  210. aliasvs:=tabsolutevarsym.create_ref('RESULT',pd.returndef,sl);
  211. include(aliasvs.varoptions,vo_is_funcret);
  212. include(aliasvs.varoptions,vo_is_result);
  213. tlocalsymtable(pd.localst).insert(aliasvs);
  214. end;
  215. current_tokenpos:=storepos;
  216. end;
  217. end;
  218. procedure insert_hidden_para(p:TObject;arg:pointer);
  219. var
  220. hvs : tparavarsym;
  221. pd : tabstractprocdef absolute arg;
  222. begin
  223. if (tsym(p).typ<>paravarsym) then
  224. exit;
  225. with tparavarsym(p) do
  226. begin
  227. { We need a local copy for a value parameter when only the
  228. address is pushed. Open arrays and Array of Const are
  229. an exception because they are allocated at runtime and the
  230. address that is pushed is patched }
  231. if (varspez=vs_value) and
  232. paramanager.push_addr_param(varspez,vardef,pd.proccalloption) and
  233. not(is_open_array(vardef) or
  234. is_array_of_const(vardef)) then
  235. include(varoptions,vo_has_local_copy);
  236. { needs high parameter ? }
  237. if paramanager.push_high_param(varspez,vardef,pd.proccalloption) then
  238. begin
  239. hvs:=tparavarsym.create('$high'+name,paranr+1,vs_const,sinttype,[vo_is_high_para,vo_is_hidden_para]);
  240. hvs.symoptions:=[];
  241. owner.insert(hvs);
  242. end
  243. else
  244. begin
  245. { Give a warning that cdecl routines does not include high()
  246. support }
  247. if (pd.proccalloption in [pocall_cdecl,pocall_cppdecl]) and
  248. paramanager.push_high_param(varspez,vardef,pocall_default) then
  249. begin
  250. if is_open_string(vardef) then
  251. Message(parser_w_cdecl_no_openstring);
  252. if not (po_external in pd.procoptions) then
  253. Message(parser_w_cdecl_has_no_high);
  254. end;
  255. if (vardef.typ=formaldef) and (Tformaldef(vardef).typed) then
  256. begin
  257. hvs:=tparavarsym.create('$typinfo'+name,paranr+1,vs_const,voidpointertype,
  258. [vo_is_typinfo_para,vo_is_hidden_para]);
  259. owner.insert(hvs);
  260. end;
  261. end;
  262. end;
  263. end;
  264. procedure check_c_para(pd:Tabstractprocdef);
  265. var
  266. i,
  267. lastparaidx : longint;
  268. sym : TSym;
  269. begin
  270. lastparaidx:=pd.parast.SymList.Count-1;
  271. for i:=0 to pd.parast.SymList.Count-1 do
  272. begin
  273. sym:=tsym(pd.parast.SymList[i]);
  274. if (sym.typ=paravarsym) and
  275. (tparavarsym(sym).vardef.typ=arraydef) then
  276. begin
  277. if not is_variant_array(tparavarsym(sym).vardef) and
  278. not is_array_of_const(tparavarsym(sym).vardef) and
  279. (tparavarsym(sym).varspez<>vs_var) then
  280. Message(parser_h_c_arrays_are_references);
  281. if is_array_of_const(tparavarsym(sym).vardef) and
  282. (i<lastparaidx) and
  283. (tsym(pd.parast.SymList[i+1]).typ=paravarsym) and
  284. not(vo_is_high_para in tparavarsym(pd.parast.SymList[i+1]).varoptions) then
  285. Message(parser_e_C_array_of_const_must_be_last);
  286. end;
  287. end;
  288. end;
  289. procedure check_msg_para(p:TObject;arg:pointer);
  290. begin
  291. if (tsym(p).typ<>paravarsym) then
  292. exit;
  293. with tparavarsym(p) do
  294. begin
  295. { Count parameters }
  296. if (paranr>=10) then
  297. inc(plongint(arg)^);
  298. { First parameter must be var }
  299. if (paranr=10) and
  300. (varspez<>vs_var) then
  301. Message(parser_e_ill_msg_param);
  302. end;
  303. end;
  304. procedure set_addr_param_regable(p:TObject;arg:pointer);
  305. begin
  306. if (tsym(p).typ<>paravarsym) then
  307. exit;
  308. with tparavarsym(p) do
  309. begin
  310. if not vardef.needs_inittable and
  311. paramanager.push_addr_param(varspez,vardef,tprocdef(arg).proccalloption) then
  312. varregable:=vr_intreg;
  313. end;
  314. end;
  315. procedure parse_parameter_dec(pd:tabstractprocdef);
  316. {
  317. handle_procvar needs the same changes
  318. }
  319. type
  320. tppv = (pv_none,pv_proc,pv_func);
  321. var
  322. sc : TFPObjectList;
  323. hdef : tdef;
  324. arrayelementdef : tdef;
  325. vs : tparavarsym;
  326. i : longint;
  327. srsym : tsym;
  328. pv : tprocvardef;
  329. varspez : Tvarspez;
  330. defaultvalue : tconstsym;
  331. defaultrequired : boolean;
  332. old_block_type : tblock_type;
  333. currparast : tparasymtable;
  334. parseprocvar : tppv;
  335. explicit_paraloc : boolean;
  336. locationstr : string;
  337. paranr : integer;
  338. dummytype : ttypesym;
  339. begin
  340. old_block_type:=block_type;
  341. explicit_paraloc:=false;
  342. consume(_LKLAMMER);
  343. { Delphi/Kylix supports nonsense like }
  344. { procedure p(); }
  345. if try_to_consume(_RKLAMMER) and
  346. not(m_tp7 in current_settings.modeswitches) then
  347. exit;
  348. { parsing a proc or procvar ? }
  349. currparast:=tparasymtable(pd.parast);
  350. { reset }
  351. sc:=TFPObjectList.create(false);
  352. defaultrequired:=false;
  353. paranr:=0;
  354. inc(testcurobject);
  355. block_type:=bt_var;
  356. repeat
  357. parseprocvar:=pv_none;
  358. if try_to_consume(_VAR) then
  359. varspez:=vs_var
  360. else
  361. if try_to_consume(_CONST) then
  362. varspez:=vs_const
  363. else
  364. if (m_out in current_settings.modeswitches) and
  365. try_to_consume(_OUT) then
  366. varspez:=vs_out
  367. else
  368. if (m_mac in current_settings.modeswitches) and
  369. try_to_consume(_POINTPOINTPOINT) then
  370. begin
  371. include(pd.procoptions,po_varargs);
  372. break;
  373. end
  374. else
  375. if (m_mac in current_settings.modeswitches) and
  376. try_to_consume(_PROCEDURE) then
  377. begin
  378. parseprocvar:=pv_proc;
  379. varspez:=vs_const;
  380. end
  381. else
  382. if (m_mac in current_settings.modeswitches) and
  383. try_to_consume(_FUNCTION) then
  384. begin
  385. parseprocvar:=pv_func;
  386. varspez:=vs_const;
  387. end
  388. else
  389. varspez:=vs_value;
  390. defaultvalue:=nil;
  391. hdef:=nil;
  392. { read identifiers and insert with error type }
  393. sc.clear;
  394. repeat
  395. inc(paranr);
  396. vs:=tparavarsym.create(orgpattern,paranr*10,varspez,generrordef,[]);
  397. currparast.insert(vs);
  398. if assigned(vs.owner) then
  399. sc.add(vs)
  400. else
  401. vs.free;
  402. consume(_ID);
  403. until not try_to_consume(_COMMA);
  404. locationstr:='';
  405. { macpas anonymous procvar }
  406. if parseprocvar<>pv_none then
  407. begin
  408. pv:=tprocvardef.create(normal_function_level);
  409. if token=_LKLAMMER then
  410. parse_parameter_dec(pv);
  411. if parseprocvar=pv_func then
  412. begin
  413. block_type:=bt_var_type;
  414. consume(_COLON);
  415. single_type(pv.returndef,false);
  416. block_type:=bt_var;
  417. end;
  418. hdef:=pv;
  419. { possible proc directives }
  420. if check_proc_directive(true) then
  421. begin
  422. dummytype:=ttypesym.create('unnamed',hdef);
  423. parse_var_proc_directives(tsym(dummytype));
  424. dummytype.typedef:=nil;
  425. hdef.typesym:=nil;
  426. dummytype.free;
  427. end;
  428. { Add implicit hidden parameters and function result }
  429. handle_calling_convention(pv);
  430. end
  431. else
  432. { read type declaration, force reading for value and const paras }
  433. if (token=_COLON) or (varspez=vs_value) then
  434. begin
  435. consume(_COLON);
  436. { check for an open array }
  437. if token=_ARRAY then
  438. begin
  439. consume(_ARRAY);
  440. consume(_OF);
  441. { define range and type of range }
  442. hdef:=tarraydef.create(0,-1,s32inttype);
  443. { array of const ? }
  444. if (token=_CONST) and (m_objpas in current_settings.modeswitches) then
  445. begin
  446. consume(_CONST);
  447. srsym:=search_system_type('TVARREC');
  448. tarraydef(hdef).elementdef:=ttypesym(srsym).typedef;
  449. include(tarraydef(hdef).arrayoptions,ado_IsArrayOfConst);
  450. end
  451. else
  452. begin
  453. { define field type }
  454. single_type(arrayelementdef,false);
  455. tarraydef(hdef).elementdef:=arrayelementdef;
  456. end;
  457. end
  458. else
  459. begin
  460. if (m_mac in current_settings.modeswitches) then
  461. try_to_consume(_UNIV); {currently does nothing}
  462. if try_to_consume(_TYPE) then
  463. hdef:=ctypedformaltype
  464. else
  465. begin
  466. block_type:=bt_var_type;
  467. single_type(hdef,false);
  468. block_type:=bt_var;
  469. end;
  470. { open string ? }
  471. if (varspez in [vs_out,vs_var]) and
  472. (cs_openstring in current_settings.moduleswitches) and
  473. is_shortstring(hdef) then
  474. hdef:=openshortstringtype;
  475. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  476. begin
  477. if (idtoken=_LOCATION) then
  478. begin
  479. consume(_LOCATION);
  480. locationstr:=pattern;
  481. consume(_CSTRING);
  482. end
  483. else
  484. begin
  485. if explicit_paraloc then
  486. Message(parser_e_paraloc_all_paras);
  487. locationstr:='';
  488. end;
  489. end
  490. else
  491. locationstr:='';
  492. { default parameter }
  493. if (m_default_para in current_settings.modeswitches) then
  494. begin
  495. if try_to_consume(_EQUAL) then
  496. begin
  497. vs:=tparavarsym(sc[0]);
  498. if sc.count>1 then
  499. Message(parser_e_default_value_only_one_para);
  500. { prefix 'def' to the parameter name }
  501. defaultvalue:=ReadConstant('$def'+vs.name,vs.fileinfo);
  502. if assigned(defaultvalue) then
  503. begin
  504. include(defaultvalue.symoptions,sp_internal);
  505. pd.parast.insert(defaultvalue);
  506. end;
  507. defaultrequired:=true;
  508. end
  509. else
  510. begin
  511. if defaultrequired then
  512. Message1(parser_e_default_value_expected_for_para,vs.name);
  513. end;
  514. end;
  515. end;
  516. end
  517. else
  518. hdef:=cformaltype;
  519. { File types are only allowed for var and out parameters }
  520. if (hdef.typ=filedef) and
  521. not(varspez in [vs_out,vs_var]) then
  522. CGMessage(cg_e_file_must_call_by_reference);
  523. for i:=0 to sc.count-1 do
  524. begin
  525. vs:=tparavarsym(sc[i]);
  526. { update varsym }
  527. vs.vardef:=hdef;
  528. vs.defaultconstsym:=defaultvalue;
  529. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  530. begin
  531. if locationstr<>'' then
  532. begin
  533. if sc.count>1 then
  534. Message(parser_e_paraloc_only_one_para);
  535. if (paranr>1) and not(explicit_paraloc) then
  536. Message(parser_e_paraloc_all_paras);
  537. explicit_paraloc:=true;
  538. include(vs.varoptions,vo_has_explicit_paraloc);
  539. if not(paramanager.parseparaloc(vs,upper(locationstr))) then
  540. message(parser_e_illegal_explicit_paraloc);
  541. end
  542. else
  543. if explicit_paraloc then
  544. Message(parser_e_paraloc_all_paras);
  545. end;
  546. end;
  547. until not try_to_consume(_SEMICOLON);
  548. if explicit_paraloc then
  549. begin
  550. pd.has_paraloc_info:=true;
  551. include(pd.procoptions,po_explicitparaloc);
  552. end;
  553. { remove parasymtable from stack }
  554. sc.free;
  555. { reset object options }
  556. dec(testcurobject);
  557. block_type:=old_block_type;
  558. consume(_RKLAMMER);
  559. end;
  560. function parse_proc_head(aclass:tobjectdef;potype:tproctypeoption;var pd:tprocdef):boolean;
  561. var
  562. hs : string;
  563. orgsp,sp : TIDString;
  564. srsym : tsym;
  565. srsymtable : TSymtable;
  566. checkstack : psymtablestackitem;
  567. storepos,
  568. procstartfilepos : tfileposinfo;
  569. searchagain : boolean;
  570. st,
  571. genericst : TSymtable;
  572. aprocsym : tprocsym;
  573. popclass : boolean;
  574. ImplIntf : TImplementedInterface;
  575. old_parse_generic : boolean;
  576. begin
  577. { Save the position where this procedure really starts }
  578. procstartfilepos:=current_tokenpos;
  579. old_parse_generic:=parse_generic;
  580. result:=false;
  581. pd:=nil;
  582. aprocsym:=nil;
  583. if (potype=potype_operator) then
  584. begin
  585. sp:=overloaded_names[optoken];
  586. orgsp:=sp;
  587. end
  588. else
  589. begin
  590. sp:=pattern;
  591. orgsp:=orgpattern;
  592. consume(_ID);
  593. end;
  594. { examine interface map: function/procedure iname.functionname=locfuncname }
  595. if assigned(aclass) and
  596. assigned(aclass.ImplementedInterfaces) and
  597. (aclass.ImplementedInterfaces.count>0) and
  598. try_to_consume(_POINT) then
  599. begin
  600. storepos:=current_tokenpos;
  601. current_tokenpos:=procstartfilepos;
  602. { get interface syms}
  603. searchsym(sp,srsym,srsymtable);
  604. if not assigned(srsym) then
  605. begin
  606. identifier_not_found(orgsp);
  607. srsym:=generrorsym;
  608. end;
  609. current_tokenpos:=storepos;
  610. { qualifier is interface? }
  611. ImplIntf:=nil;
  612. if (srsym.typ=typesym) and
  613. (ttypesym(srsym).typedef.typ=objectdef) then
  614. ImplIntf:=aclass.find_implemented_interface(tobjectdef(ttypesym(srsym).typedef));
  615. if ImplIntf=nil then
  616. Message(parser_e_interface_id_expected);
  617. consume(_ID);
  618. { Create unique name <interface>.<method> }
  619. hs:=sp+'.'+pattern;
  620. consume(_EQUAL);
  621. if assigned(ImplIntf) and
  622. (token=_ID) then
  623. ImplIntf.AddMapping(hs,pattern);
  624. consume(_ID);
  625. result:=true;
  626. exit;
  627. end;
  628. { method ? }
  629. if not assigned(aclass) and
  630. (potype<>potype_operator) and
  631. (symtablestack.top.symtablelevel=main_program_level) and
  632. try_to_consume(_POINT) then
  633. begin
  634. { search for object name }
  635. storepos:=current_tokenpos;
  636. current_tokenpos:=procstartfilepos;
  637. searchsym(sp,srsym,srsymtable);
  638. if not assigned(srsym) then
  639. begin
  640. identifier_not_found(orgsp);
  641. srsym:=generrorsym;
  642. end;
  643. current_tokenpos:=storepos;
  644. { consume proc name }
  645. sp:=pattern;
  646. orgsp:=orgpattern;
  647. procstartfilepos:=current_tokenpos;
  648. consume(_ID);
  649. { qualifier is class name ? }
  650. if (srsym.typ=typesym) and
  651. (ttypesym(srsym).typedef.typ=objectdef) then
  652. begin
  653. aclass:=tobjectdef(ttypesym(srsym).typedef);
  654. srsym:=tsym(aclass.symtable.Find(sp));
  655. if assigned(srsym) then
  656. begin
  657. if srsym.typ=procsym then
  658. aprocsym:=tprocsym(srsym)
  659. else
  660. begin
  661. { we use a different error message for tp7 so it looks more compatible }
  662. if (m_fpc in current_settings.modeswitches) then
  663. Message1(parser_e_overloaded_no_procedure,srsym.realname)
  664. else
  665. Message(parser_e_methode_id_expected);
  666. { rename the name to an unique name to avoid an
  667. error when inserting the symbol in the symtable }
  668. orgsp:=orgsp+'$'+tostr(current_filepos.line);
  669. end;
  670. end
  671. else
  672. begin
  673. Message(parser_e_methode_id_expected);
  674. { recover by making it a normal procedure instead of method }
  675. aclass:=nil;
  676. end;
  677. end
  678. else
  679. Message(parser_e_class_id_expected);
  680. end
  681. else
  682. begin
  683. { check for constructor/destructor which is not allowed here }
  684. if (not parse_only) and
  685. (potype in [potype_constructor,potype_destructor]) then
  686. Message(parser_e_constructors_always_objects);
  687. repeat
  688. searchagain:=false;
  689. current_tokenpos:=procstartfilepos;
  690. srsymtable:=symtablestack.top;
  691. srsym:=tsym(srsymtable.Find(sp));
  692. { Also look in the globalsymtable if we didn't found
  693. the symbol in the localsymtable }
  694. if not assigned(srsym) and
  695. not(parse_only) and
  696. (srsymtable=current_module.localsymtable) and
  697. assigned(current_module.globalsymtable) then
  698. srsym:=tsym(current_module.globalsymtable.Find(sp));
  699. { Check if overloaded is a procsym }
  700. if assigned(srsym) then
  701. begin
  702. if srsym.typ=procsym then
  703. aprocsym:=tprocsym(srsym)
  704. else
  705. begin
  706. { when the other symbol is a unit symbol then hide the unit
  707. symbol, this is not supported in tp7 }
  708. if not(m_tp7 in current_settings.modeswitches) and
  709. (srsym.typ=unitsym) then
  710. begin
  711. HideSym(srsym);
  712. searchagain:=true;
  713. end
  714. else
  715. begin
  716. { we use a different error message for tp7 so it looks more compatible }
  717. if (m_fpc in current_settings.modeswitches) then
  718. Message1(parser_e_overloaded_no_procedure,srsym.realname)
  719. else
  720. Message1(sym_e_duplicate_id,srsym.realname);
  721. { rename the name to an unique name to avoid an
  722. error when inserting the symbol in the symtable }
  723. orgsp:=orgsp+'$'+tostr(current_filepos.line);
  724. end;
  725. end;
  726. end;
  727. until not searchagain;
  728. end;
  729. { test again if assigned, it can be reset to recover }
  730. if not assigned(aprocsym) then
  731. begin
  732. { create a new procsym and set the real filepos }
  733. current_tokenpos:=procstartfilepos;
  734. { for operator we have only one procsym for each overloaded
  735. operation }
  736. if (potype=potype_operator) then
  737. begin
  738. Aprocsym:=Tprocsym(symtablestack.top.Find(sp));
  739. if Aprocsym=nil then
  740. Aprocsym:=tprocsym.create('$'+sp);
  741. end
  742. else
  743. aprocsym:=tprocsym.create(orgsp);
  744. symtablestack.top.insert(aprocsym);
  745. end;
  746. { to get the correct symtablelevel we must ignore ObjectSymtables }
  747. st:=nil;
  748. checkstack:=symtablestack.stack;
  749. while assigned(checkstack) do
  750. begin
  751. st:=checkstack^.symtable;
  752. if st.symtabletype in [staticsymtable,globalsymtable,localsymtable] then
  753. break;
  754. checkstack:=checkstack^.next;
  755. end;
  756. pd:=tprocdef.create(st.symtablelevel+1);
  757. pd._class:=aclass;
  758. pd.procsym:=aprocsym;
  759. pd.proctypeoption:=potype;
  760. { methods inherit df_generic or df_specialization from the objectdef }
  761. if assigned(pd._class) and
  762. (pd.parast.symtablelevel=normal_function_level) then
  763. begin
  764. if (df_generic in pd._class.defoptions) then
  765. begin
  766. include(pd.defoptions,df_generic);
  767. parse_generic:=true;
  768. end;
  769. if (df_specialization in pd._class.defoptions) then
  770. begin
  771. include(pd.defoptions,df_specialization);
  772. { Find corresponding genericdef, we need it later to
  773. replay the tokens to generate the body }
  774. if not assigned(pd._class.genericdef) then
  775. internalerror(200512113);
  776. genericst:=pd._class.genericdef.GetSymtable(gs_record);
  777. if not assigned(genericst) then
  778. internalerror(200512114);
  779. { We are parsing the same objectdef, the def index numbers
  780. are the same }
  781. pd.genericdef:=tstoreddef(genericst.DefList[pd.owner.DefList.IndexOf(pd)]);
  782. if not assigned(pd.genericdef) or
  783. (pd.genericdef.typ<>procdef) then
  784. internalerror(200512115);
  785. end;
  786. end;
  787. { methods need to be exported }
  788. if assigned(aclass) and
  789. (
  790. (symtablestack.top.symtabletype=ObjectSymtable) or
  791. (symtablestack.top.symtablelevel=main_program_level)
  792. ) then
  793. include(pd.procoptions,po_global);
  794. { symbol options that need to be kept per procdef }
  795. pd.fileinfo:=procstartfilepos;
  796. pd.visibility:=symtablestack.top.currentvisibility;
  797. { parse parameters }
  798. if token=_LKLAMMER then
  799. begin
  800. { Add ObjectSymtable to be able to find generic type definitions }
  801. popclass:=false;
  802. if assigned(pd._class) and
  803. (pd.parast.symtablelevel=normal_function_level) and
  804. (symtablestack.top.symtabletype<>ObjectSymtable) then
  805. begin
  806. symtablestack.push(pd._class.symtable);
  807. popclass:=true;
  808. end;
  809. { Add parameter symtable }
  810. if pd.parast.symtabletype<>staticsymtable then
  811. symtablestack.push(pd.parast);
  812. parse_parameter_dec(pd);
  813. if pd.parast.symtabletype<>staticsymtable then
  814. symtablestack.pop(pd.parast);
  815. if popclass then
  816. symtablestack.pop(pd._class.symtable);
  817. end;
  818. parse_generic:=old_parse_generic;
  819. result:=true;
  820. end;
  821. function parse_proc_dec(aclass:tobjectdef):tprocdef;
  822. var
  823. pd : tprocdef;
  824. isclassmethod : boolean;
  825. locationstr: string;
  826. old_parse_generic,
  827. popclass : boolean;
  828. begin
  829. locationstr:='';
  830. pd:=nil;
  831. isclassmethod:=false;
  832. { read class method }
  833. if try_to_consume(_CLASS) then
  834. begin
  835. { class method only allowed for procedures and functions }
  836. if not(token in [_FUNCTION,_PROCEDURE]) then
  837. Message(parser_e_procedure_or_function_expected);
  838. if is_interface(aclass) then
  839. Message(parser_e_no_static_method_in_interfaces)
  840. else
  841. isclassmethod:=true;
  842. end;
  843. case token of
  844. _FUNCTION :
  845. begin
  846. consume(_FUNCTION);
  847. if parse_proc_head(aclass,potype_function,pd) then
  848. begin
  849. { pd=nil when it is a interface mapping }
  850. if assigned(pd) then
  851. begin
  852. if try_to_consume(_COLON) then
  853. begin
  854. old_parse_generic:=parse_generic;
  855. inc(testcurobject);
  856. { Add ObjectSymtable to be able to find generic type definitions }
  857. popclass:=false;
  858. if assigned(pd._class) and
  859. (pd.parast.symtablelevel=normal_function_level) and
  860. (symtablestack.top.symtabletype<>ObjectSymtable) then
  861. begin
  862. symtablestack.push(pd._class.symtable);
  863. popclass:=true;
  864. parse_generic:=(df_generic in pd._class.defoptions);
  865. end;
  866. single_type(pd.returndef,false);
  867. if popclass then
  868. symtablestack.pop(pd._class.symtable);
  869. dec(testcurobject);
  870. parse_generic:=old_parse_generic;
  871. if (target_info.system in [system_m68k_amiga]) then
  872. begin
  873. if (idtoken=_LOCATION) then
  874. begin
  875. if po_explicitparaloc in pd.procoptions then
  876. begin
  877. consume(_LOCATION);
  878. locationstr:=pattern;
  879. consume(_CSTRING);
  880. end
  881. else
  882. { I guess this needs a new message... (KB) }
  883. Message(parser_e_paraloc_all_paras);
  884. end
  885. else
  886. begin
  887. if po_explicitparaloc in pd.procoptions then
  888. { assign default locationstr, if none specified }
  889. { and we've arguments with explicit paraloc }
  890. locationstr:='D0';
  891. end;
  892. end;
  893. end
  894. else
  895. begin
  896. if (
  897. parse_only and
  898. not(is_interface(pd._class))
  899. ) or
  900. (m_repeat_forward in current_settings.modeswitches) then
  901. begin
  902. consume(_COLON);
  903. consume_all_until(_SEMICOLON);
  904. end;
  905. end;
  906. if isclassmethod then
  907. include(pd.procoptions,po_classmethod);
  908. end;
  909. end
  910. else
  911. begin
  912. { recover }
  913. consume(_COLON);
  914. consume_all_until(_SEMICOLON);
  915. end;
  916. end;
  917. _PROCEDURE :
  918. begin
  919. consume(_PROCEDURE);
  920. if parse_proc_head(aclass,potype_procedure,pd) then
  921. begin
  922. { pd=nil when it is a interface mapping }
  923. if assigned(pd) then
  924. begin
  925. pd.returndef:=voidtype;
  926. if isclassmethod then
  927. include(pd.procoptions,po_classmethod);
  928. end;
  929. end;
  930. end;
  931. _CONSTRUCTOR :
  932. begin
  933. consume(_CONSTRUCTOR);
  934. parse_proc_head(aclass,potype_constructor,pd);
  935. if assigned(pd) and
  936. assigned(pd._class) then
  937. begin
  938. { Set return type, class constructors return the
  939. created instance, object constructors return boolean }
  940. if is_class(pd._class) then
  941. pd.returndef:=pd._class
  942. else
  943. {$ifdef CPU64bitaddr}
  944. pd.returndef:=bool64type;
  945. {$else CPU64bitaddr}
  946. pd.returndef:=bool32type;
  947. {$endif CPU64bitaddr}
  948. end;
  949. end;
  950. _DESTRUCTOR :
  951. begin
  952. consume(_DESTRUCTOR);
  953. parse_proc_head(aclass,potype_destructor,pd);
  954. if assigned(pd) then
  955. pd.returndef:=voidtype;
  956. end;
  957. _OPERATOR :
  958. begin
  959. consume(_OPERATOR);
  960. if (token in [first_overloaded..last_overloaded]) then
  961. begin
  962. optoken:=token;
  963. end
  964. else
  965. begin
  966. case token of
  967. _CARET:
  968. Message1(parser_e_overload_operator_failed,'**');
  969. _UNEQUAL:
  970. Message1(parser_e_overload_operator_failed,'=');
  971. else
  972. Message1(parser_e_overload_operator_failed,'');
  973. end;
  974. { Use the dummy NOTOKEN that is also declared
  975. for the overloaded_operator[] }
  976. optoken:=NOTOKEN;
  977. end;
  978. consume(token);
  979. parse_proc_head(aclass,potype_operator,pd);
  980. if assigned(pd) then
  981. begin
  982. if pd.parast.symtablelevel>normal_function_level then
  983. Message(parser_e_no_local_operator);
  984. if token<>_ID then
  985. begin
  986. if not(m_result in current_settings.modeswitches) then
  987. consume(_ID);
  988. end
  989. else
  990. begin
  991. pd.resultname:=stringdup(orgpattern);
  992. consume(_ID);
  993. end;
  994. if not try_to_consume(_COLON) then
  995. begin
  996. consume(_COLON);
  997. pd.returndef:=generrordef;
  998. consume_all_until(_SEMICOLON);
  999. end
  1000. else
  1001. begin
  1002. single_type(pd.returndef,false);
  1003. if (optoken in [_EQUAL,_GT,_LT,_GTE,_LTE]) and
  1004. ((pd.returndef.typ<>orddef) or
  1005. (torddef(pd.returndef).ordtype<>pasbool)) then
  1006. Message(parser_e_comparative_operator_return_boolean);
  1007. if (optoken=_ASSIGNMENT) and
  1008. equal_defs(pd.returndef,tparavarsym(pd.parast.SymList[0]).vardef) then
  1009. message(parser_e_no_such_assignment)
  1010. else if not isoperatoracceptable(pd,optoken) then
  1011. Message(parser_e_overload_impossible);
  1012. end;
  1013. end
  1014. else
  1015. begin
  1016. { recover }
  1017. try_to_consume(_ID);
  1018. consume(_COLON);
  1019. consume_all_until(_SEMICOLON);
  1020. end;
  1021. end;
  1022. end;
  1023. { file types can't be function results }
  1024. if assigned(pd) and
  1025. (pd.returndef.typ=filedef) then
  1026. message(parser_e_illegal_function_result);
  1027. { support procedure proc stdcall export; }
  1028. if not(check_proc_directive(false)) then
  1029. consume(_SEMICOLON);
  1030. result:=pd;
  1031. if locationstr<>'' then
  1032. begin
  1033. if not(paramanager.parsefuncretloc(pd,upper(locationstr))) then
  1034. { I guess this needs a new message... (KB) }
  1035. message(parser_e_illegal_explicit_paraloc);
  1036. end;
  1037. end;
  1038. {****************************************************************************
  1039. Procedure directive handlers
  1040. ****************************************************************************}
  1041. procedure pd_far(pd:tabstractprocdef);
  1042. begin
  1043. Message1(parser_w_proc_directive_ignored,'FAR');
  1044. end;
  1045. procedure pd_near(pd:tabstractprocdef);
  1046. begin
  1047. Message1(parser_w_proc_directive_ignored,'NEAR');
  1048. end;
  1049. procedure pd_export(pd:tabstractprocdef);
  1050. begin
  1051. if pd.typ<>procdef then
  1052. internalerror(200304264);
  1053. if assigned(tprocdef(pd)._class) then
  1054. Message(parser_e_methods_dont_be_export);
  1055. if pd.parast.symtablelevel>normal_function_level then
  1056. Message(parser_e_dont_nest_export);
  1057. end;
  1058. procedure pd_forward(pd:tabstractprocdef);
  1059. begin
  1060. if pd.typ<>procdef then
  1061. internalerror(200304265);
  1062. tprocdef(pd).forwarddef:=true;
  1063. end;
  1064. procedure pd_alias(pd:tabstractprocdef);
  1065. begin
  1066. if pd.typ<>procdef then
  1067. internalerror(200304266);
  1068. consume(_COLON);
  1069. tprocdef(pd).aliasnames.insert(get_stringconst);
  1070. include(pd.procoptions,po_has_public_name);
  1071. end;
  1072. procedure pd_public(pd:tabstractprocdef);
  1073. begin
  1074. if pd.typ<>procdef then
  1075. internalerror(200304266);
  1076. if try_to_consume(_NAME) then
  1077. begin
  1078. tprocdef(pd).aliasnames.insert(get_stringconst);
  1079. include(pd.procoptions,po_has_public_name);
  1080. end;
  1081. end;
  1082. procedure pd_asmname(pd:tabstractprocdef);
  1083. begin
  1084. if pd.typ<>procdef then
  1085. internalerror(200304267);
  1086. tprocdef(pd).aliasnames.insert(target_info.Cprefix+pattern);
  1087. if token=_CCHAR then
  1088. consume(_CCHAR)
  1089. else
  1090. consume(_CSTRING);
  1091. { we don't need anything else }
  1092. tprocdef(pd).forwarddef:=false;
  1093. end;
  1094. procedure pd_internconst(pd:tabstractprocdef);
  1095. var v:Tconstexprint;
  1096. begin
  1097. if pd.typ<>procdef then
  1098. internalerror(200304268);
  1099. consume(_COLON);
  1100. v:=get_intconst;
  1101. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1102. message(parser_e_range_check_error)
  1103. else
  1104. Tprocdef(pd).extnumber:=longint(v.svalue);
  1105. end;
  1106. procedure pd_internproc(pd:tabstractprocdef);
  1107. var v:Tconstexprint;
  1108. begin
  1109. if pd.typ<>procdef then
  1110. internalerror(200304268);
  1111. consume(_COLON);
  1112. v:=get_intconst;
  1113. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1114. message(parser_e_range_check_error)
  1115. else
  1116. Tprocdef(pd).extnumber:=longint(v.svalue);
  1117. { the proc is defined }
  1118. tprocdef(pd).forwarddef:=false;
  1119. end;
  1120. procedure pd_interrupt(pd:tabstractprocdef);
  1121. begin
  1122. if pd.parast.symtablelevel>normal_function_level then
  1123. Message(parser_e_dont_nest_interrupt);
  1124. end;
  1125. procedure pd_abstract(pd:tabstractprocdef);
  1126. begin
  1127. if pd.typ<>procdef then
  1128. internalerror(200304269);
  1129. if (po_virtualmethod in pd.procoptions) then
  1130. include(pd.procoptions,po_abstractmethod)
  1131. else
  1132. Message(parser_e_only_virtual_methods_abstract);
  1133. { the method is defined }
  1134. tprocdef(pd).forwarddef:=false;
  1135. end;
  1136. procedure pd_virtual(pd:tabstractprocdef);
  1137. {$ifdef WITHDMT}
  1138. var
  1139. pt : tnode;
  1140. {$endif WITHDMT}
  1141. begin
  1142. if pd.typ<>procdef then
  1143. internalerror(2003042610);
  1144. if (pd.proctypeoption=potype_constructor) and
  1145. is_object(tprocdef(pd)._class) then
  1146. Message(parser_e_constructor_cannot_be_not_virtual);
  1147. {$ifdef WITHDMT}
  1148. if is_object(tprocdef(pd)._class) and
  1149. (token<>_SEMICOLON) then
  1150. begin
  1151. { any type of parameter is allowed here! }
  1152. pt:=comp_expr(true);
  1153. if is_constintnode(pt) then
  1154. begin
  1155. include(pd.procoptions,po_msgint);
  1156. pd.messageinf.i:=pt.value;
  1157. end
  1158. else
  1159. Message(parser_e_ill_msg_expr);
  1160. disposetree(pt);
  1161. end;
  1162. {$endif WITHDMT}
  1163. end;
  1164. procedure pd_dispid(pd:tabstractprocdef);
  1165. var pt:Tnode;
  1166. begin
  1167. if pd.typ<>procdef then
  1168. internalerror(200604301);
  1169. pt:=comp_expr(true);
  1170. if is_constintnode(pt) then
  1171. if (Tordconstnode(pt).value<int64(low(longint))) or (Tordconstnode(pt).value>int64(high(longint))) then
  1172. message(parser_e_range_check_error)
  1173. else
  1174. Tprocdef(pd).dispid:=Tordconstnode(pt).value.svalue
  1175. else
  1176. message(parser_e_dispid_must_be_ord_const);
  1177. pt.free;
  1178. end;
  1179. procedure pd_static(pd:tabstractprocdef);
  1180. begin
  1181. if (cs_static_keyword in current_settings.moduleswitches) then
  1182. begin
  1183. if pd.typ=procdef then
  1184. include(tprocdef(pd).procsym.symoptions,sp_static);
  1185. include(pd.procoptions,po_staticmethod);
  1186. end;
  1187. end;
  1188. procedure pd_override(pd:tabstractprocdef);
  1189. begin
  1190. if pd.typ<>procdef then
  1191. internalerror(2003042611);
  1192. if not(is_class_or_interface(tprocdef(pd)._class)) then
  1193. Message(parser_e_no_object_override);
  1194. end;
  1195. procedure pd_overload(pd:tabstractprocdef);
  1196. begin
  1197. if pd.typ<>procdef then
  1198. internalerror(2003042612);
  1199. include(tprocdef(pd).procsym.symoptions,sp_has_overloaded);
  1200. end;
  1201. procedure pd_message(pd:tabstractprocdef);
  1202. var
  1203. pt : tnode;
  1204. paracnt : longint;
  1205. begin
  1206. if pd.typ<>procdef then
  1207. internalerror(2003042613);
  1208. if not is_class(tprocdef(pd)._class) then
  1209. Message(parser_e_msg_only_for_classes);
  1210. { check parameter type }
  1211. paracnt:=0;
  1212. pd.parast.SymList.ForEachCall(@check_msg_para,@paracnt);
  1213. if paracnt<>1 then
  1214. Message(parser_e_ill_msg_param);
  1215. pt:=comp_expr(true);
  1216. if pt.nodetype=stringconstn then
  1217. begin
  1218. include(pd.procoptions,po_msgstr);
  1219. tprocdef(pd).messageinf.str:=stringdup(tstringconstnode(pt).value_str);
  1220. end
  1221. else
  1222. if is_constintnode(pt) then
  1223. begin
  1224. include(pd.procoptions,po_msgint);
  1225. if (Tordconstnode(pt).value<int64(low(Tprocdef(pd).messageinf.i))) or
  1226. (Tordconstnode(pt).value>int64(high(Tprocdef(pd).messageinf.i))) then
  1227. message(parser_e_range_check_error)
  1228. else
  1229. Tprocdef(pd).messageinf.i:=tordconstnode(pt).value.svalue;
  1230. end
  1231. else
  1232. Message(parser_e_ill_msg_expr);
  1233. pt.free;
  1234. end;
  1235. procedure pd_reintroduce(pd:tabstractprocdef);
  1236. begin
  1237. if pd.typ<>procdef then
  1238. internalerror(200401211);
  1239. if not(is_class_or_interface(tprocdef(pd)._class)) then
  1240. Message(parser_e_no_object_reintroduce);
  1241. end;
  1242. procedure pd_syscall(pd:tabstractprocdef);
  1243. {$if defined(powerpc) or defined(m68k)}
  1244. var
  1245. vs : tparavarsym;
  1246. sym : tsym;
  1247. symtable : TSymtable;
  1248. v: Tconstexprint;
  1249. {$endif defined(powerpc) or defined(m68k)}
  1250. begin
  1251. if (pd.typ<>procdef) and (target_info.system <> system_powerpc_amiga) then
  1252. internalerror(2003042614);
  1253. tprocdef(pd).forwarddef:=false;
  1254. {$ifdef m68k}
  1255. if target_info.system in [system_m68k_amiga] then
  1256. begin
  1257. include(pd.procoptions,po_syscall_legacy);
  1258. if consume_sym(sym,symtable) then
  1259. begin
  1260. if (sym.typ=staticvarsym) and
  1261. (
  1262. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1263. is_32bitint(tabstractvarsym(sym).vardef)
  1264. ) then
  1265. begin
  1266. tprocdef(pd).libsym:=sym;
  1267. if po_syscall_legacy in tprocdef(pd).procoptions then
  1268. begin
  1269. vs:=tparavarsym.create('$syscalllib',paranr_syscall_legacy,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1270. paramanager.parseparaloc(vs,'A6');
  1271. pd.parast.insert(vs);
  1272. end
  1273. end
  1274. else
  1275. Message(parser_e_32bitint_or_pointer_variable_expected);
  1276. end;
  1277. (paramanager as tm68kparamanager).create_funcretloc_info(pd,calleeside);
  1278. (paramanager as tm68kparamanager).create_funcretloc_info(pd,callerside);
  1279. v:=get_intconst;
  1280. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1281. message(parser_e_range_check_error)
  1282. else
  1283. Tprocdef(pd).extnumber:=v.uvalue;
  1284. end;
  1285. {$endif m68k}
  1286. {$ifdef powerpc}
  1287. if target_info.system = system_powerpc_amiga then
  1288. begin
  1289. include(pd.procoptions,po_syscall_basesysv);
  1290. if consume_sym(sym,symtable) then
  1291. begin
  1292. if (sym.typ=staticvarsym) and
  1293. (
  1294. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1295. is_32bitint(tabstractvarsym(sym).vardef)
  1296. ) then
  1297. begin
  1298. tprocdef(pd).libsym:=sym;
  1299. vs:=tparavarsym.create('$syscalllib',paranr_syscall_basesysv,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1300. pd.parast.insert(vs);
  1301. end
  1302. else
  1303. Message(parser_e_32bitint_or_pointer_variable_expected);
  1304. end;
  1305. (paramanager as tppcparamanager).create_funcretloc_info(pd,calleeside);
  1306. (paramanager as tppcparamanager).create_funcretloc_info(pd,callerside);
  1307. v:=get_intconst;
  1308. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1309. message(parser_e_range_check_error)
  1310. else
  1311. Tprocdef(pd).extnumber:=v.uvalue;
  1312. end else
  1313. if target_info.system = system_powerpc_morphos then
  1314. begin
  1315. if idtoken=_LEGACY then
  1316. begin
  1317. consume(_LEGACY);
  1318. include(pd.procoptions,po_syscall_legacy);
  1319. end
  1320. else if idtoken=_SYSV then
  1321. begin
  1322. consume(_SYSV);
  1323. include(pd.procoptions,po_syscall_sysv);
  1324. end
  1325. else if idtoken=_BASESYSV then
  1326. begin
  1327. consume(_BASESYSV);
  1328. include(pd.procoptions,po_syscall_basesysv);
  1329. end
  1330. else if idtoken=_SYSVBASE then
  1331. begin
  1332. consume(_SYSVBASE);
  1333. include(pd.procoptions,po_syscall_sysvbase);
  1334. end
  1335. else if idtoken=_R12BASE then
  1336. begin
  1337. consume(_R12BASE);
  1338. include(pd.procoptions,po_syscall_r12base);
  1339. end
  1340. else
  1341. if syscall_convention='LEGACY' then
  1342. include(pd.procoptions,po_syscall_legacy)
  1343. else if syscall_convention='SYSV' then
  1344. include(pd.procoptions,po_syscall_sysv)
  1345. else if syscall_convention='BASESYSV' then
  1346. include(pd.procoptions,po_syscall_basesysv)
  1347. else if syscall_convention='SYSVBASE' then
  1348. include(pd.procoptions,po_syscall_sysvbase)
  1349. else if syscall_convention='R12BASE' then
  1350. include(pd.procoptions,po_syscall_r12base)
  1351. else
  1352. internalerror(2005010404);
  1353. if consume_sym(sym,symtable) then
  1354. begin
  1355. if (sym.typ=staticvarsym) and
  1356. (
  1357. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1358. is_32bitint(tabstractvarsym(sym).vardef)
  1359. ) then
  1360. begin
  1361. tprocdef(pd).libsym:=sym;
  1362. if po_syscall_legacy in tprocdef(pd).procoptions then
  1363. begin
  1364. vs:=tparavarsym.create('$syscalllib',paranr_syscall_legacy,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1365. paramanager.parseparaloc(vs,'A6');
  1366. pd.parast.insert(vs);
  1367. end
  1368. else if po_syscall_sysv in tprocdef(pd).procoptions then
  1369. begin
  1370. { Nothing to be done for sysv here for now, but this might change }
  1371. end
  1372. else if po_syscall_basesysv in tprocdef(pd).procoptions then
  1373. begin
  1374. vs:=tparavarsym.create('$syscalllib',paranr_syscall_basesysv,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1375. pd.parast.insert(vs);
  1376. end
  1377. else if po_syscall_sysvbase in tprocdef(pd).procoptions then
  1378. begin
  1379. vs:=tparavarsym.create('$syscalllib',paranr_syscall_sysvbase,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1380. pd.parast.insert(vs);
  1381. end
  1382. else if po_syscall_r12base in tprocdef(pd).procoptions then
  1383. begin
  1384. vs:=tparavarsym.create('$syscalllib',paranr_syscall_r12base,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1385. paramanager.parseparaloc(vs,'R12');
  1386. pd.parast.insert(vs);
  1387. end
  1388. else
  1389. internalerror(2005010501);
  1390. end
  1391. else
  1392. Message(parser_e_32bitint_or_pointer_variable_expected);
  1393. end;
  1394. (paramanager as tppcparamanager).create_funcretloc_info(pd,calleeside);
  1395. (paramanager as tppcparamanager).create_funcretloc_info(pd,callerside);
  1396. v:=get_intconst;
  1397. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1398. message(parser_e_range_check_error)
  1399. else
  1400. Tprocdef(pd).extnumber:=v.uvalue;
  1401. end;
  1402. {$endif powerpc}
  1403. end;
  1404. procedure pd_external(pd:tabstractprocdef);
  1405. {
  1406. If import_dll=nil the procedure is assumed to be in another
  1407. object file. In that object file it should have the name to
  1408. which import_name is pointing to. Otherwise, the procedure is
  1409. assumed to be in the DLL to which import_dll is pointing to. In
  1410. that case either import_nr<>0 or import_name<>nil is true, so
  1411. the procedure is either imported by number or by name. (DM)
  1412. }
  1413. var
  1414. hs : string;
  1415. v:Tconstexprint;
  1416. begin
  1417. if pd.typ<>procdef then
  1418. internalerror(2003042615);
  1419. with tprocdef(pd) do
  1420. begin
  1421. forwarddef:=false;
  1422. { forbid local external procedures }
  1423. if parast.symtablelevel>normal_function_level then
  1424. Message(parser_e_no_local_proc_external);
  1425. { If the procedure should be imported from a DLL, a constant string follows.
  1426. This isn't really correct, an contant string expression follows
  1427. so we check if an semicolon follows, else a string constant have to
  1428. follow (FK) }
  1429. if not(token=_SEMICOLON) and not(idtoken=_NAME) then
  1430. begin
  1431. { Always add library prefix and suffix to create an uniform name }
  1432. hs:=get_stringconst;
  1433. if ExtractFileExt(hs)='' then
  1434. hs:=ChangeFileExt(hs,target_info.sharedlibext);
  1435. if Copy(hs,1,length(target_info.sharedlibprefix))<>target_info.sharedlibprefix then
  1436. hs:=target_info.sharedlibprefix+hs;
  1437. import_dll:=stringdup(hs);
  1438. include(procoptions,po_has_importdll);
  1439. if (idtoken=_NAME) then
  1440. begin
  1441. consume(_NAME);
  1442. import_name:=stringdup(get_stringconst);
  1443. include(procoptions,po_has_importname);
  1444. if import_name^='' then
  1445. message(parser_e_empty_import_name);
  1446. end;
  1447. if (idtoken=_INDEX) then
  1448. begin
  1449. {After the word index follows the index number in the DLL.}
  1450. consume(_INDEX);
  1451. v:=get_intconst;
  1452. if (v<int64(low(import_nr))) or (v>int64(high(import_nr))) then
  1453. message(parser_e_range_check_error)
  1454. else
  1455. import_nr:=longint(v.svalue);
  1456. end;
  1457. { default is to used the realname of the procedure }
  1458. if (import_nr=0) and not assigned(import_name) then
  1459. begin
  1460. import_name:=stringdup(procsym.realname);
  1461. include(procoptions,po_has_importname);
  1462. end;
  1463. end
  1464. else
  1465. begin
  1466. if (idtoken=_NAME) then
  1467. begin
  1468. consume(_NAME);
  1469. import_name:=stringdup(get_stringconst);
  1470. include(procoptions,po_has_importname);
  1471. if import_name^='' then
  1472. message(parser_e_empty_import_name);
  1473. end;
  1474. end;
  1475. end;
  1476. end;
  1477. procedure pd_weakexternal(pd:tabstractprocdef);
  1478. begin
  1479. if not(target_info.system in system_weak_linking) then
  1480. message(parser_e_weak_external_not_supported)
  1481. else
  1482. pd_external(pd);
  1483. end;
  1484. type
  1485. pd_handler=procedure(pd:tabstractprocdef);
  1486. proc_dir_rec=record
  1487. idtok : ttoken;
  1488. pd_flags : tpdflags;
  1489. handler : pd_handler;
  1490. pocall : tproccalloption;
  1491. pooption : tprocoptions;
  1492. mutexclpocall : tproccalloptions;
  1493. mutexclpotype : tproctypeoptions;
  1494. mutexclpo : tprocoptions;
  1495. end;
  1496. const
  1497. {Should contain the number of procedure directives we support.}
  1498. num_proc_directives=40;
  1499. proc_direcdata:array[1..num_proc_directives] of proc_dir_rec=
  1500. (
  1501. (
  1502. idtok:_ABSTRACT;
  1503. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1504. handler : @pd_abstract;
  1505. pocall : pocall_none;
  1506. pooption : [po_abstractmethod];
  1507. mutexclpocall : [pocall_internproc];
  1508. mutexclpotype : [];
  1509. mutexclpo : [po_exports,po_interrupt,po_external,po_inline]
  1510. ),(
  1511. idtok:_ALIAS;
  1512. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  1513. handler : @pd_alias;
  1514. pocall : pocall_none;
  1515. pooption : [];
  1516. mutexclpocall : [];
  1517. mutexclpotype : [];
  1518. mutexclpo : [po_external,po_inline]
  1519. ),(
  1520. idtok:_ASMNAME;
  1521. pd_flags : [pd_interface,pd_implemen,pd_notobjintf];
  1522. handler : @pd_asmname;
  1523. pocall : pocall_cdecl;
  1524. pooption : [po_external];
  1525. mutexclpocall : [pocall_internproc];
  1526. mutexclpotype : [];
  1527. mutexclpo : [po_external,po_inline]
  1528. ),(
  1529. idtok:_ASSEMBLER;
  1530. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1531. handler : nil;
  1532. pocall : pocall_none;
  1533. pooption : [po_assembler];
  1534. mutexclpocall : [];
  1535. mutexclpotype : [];
  1536. mutexclpo : [po_external]
  1537. ),(
  1538. idtok:_C; {same as cdecl for mode mac}
  1539. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1540. handler : nil;
  1541. pocall : pocall_cdecl;
  1542. pooption : [];
  1543. mutexclpocall : [];
  1544. mutexclpotype : [potype_constructor,potype_destructor];
  1545. mutexclpo : [po_assembler,po_external]
  1546. ),(
  1547. idtok:_CDECL;
  1548. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1549. handler : nil;
  1550. pocall : pocall_cdecl;
  1551. pooption : [];
  1552. mutexclpocall : [];
  1553. mutexclpotype : [potype_constructor,potype_destructor];
  1554. mutexclpo : [po_assembler,po_external]
  1555. ),(
  1556. idtok:_DISPID;
  1557. pd_flags : [pd_dispinterface];
  1558. handler : @pd_dispid;
  1559. pocall : pocall_none;
  1560. pooption : [po_dispid];
  1561. mutexclpocall : [pocall_internproc];
  1562. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1563. mutexclpo : [po_interrupt,po_external,po_inline]
  1564. ),(
  1565. idtok:_DYNAMIC;
  1566. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1567. handler : @pd_virtual;
  1568. pocall : pocall_none;
  1569. pooption : [po_virtualmethod];
  1570. mutexclpocall : [pocall_internproc];
  1571. mutexclpotype : [];
  1572. mutexclpo : [po_exports,po_interrupt,po_external,po_overridingmethod,po_inline]
  1573. ),(
  1574. idtok:_EXPORT;
  1575. pd_flags : [pd_body,pd_interface,pd_implemen,pd_notobjintf];
  1576. handler : @pd_export;
  1577. pocall : pocall_none;
  1578. pooption : [po_exports,po_global];
  1579. mutexclpocall : [pocall_internproc];
  1580. mutexclpotype : [potype_constructor,potype_destructor];
  1581. mutexclpo : [po_external,po_interrupt,po_inline]
  1582. ),(
  1583. idtok:_EXTERNAL;
  1584. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject];
  1585. handler : @pd_external;
  1586. pocall : pocall_none;
  1587. pooption : [po_external];
  1588. mutexclpocall : [pocall_internproc,pocall_syscall];
  1589. { allowed for external cpp classes }
  1590. mutexclpotype : [{potype_constructor,potype_destructor}];
  1591. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  1592. ),(
  1593. idtok:_FAR;
  1594. pd_flags : [pd_implemen,pd_body,pd_interface,pd_procvar,pd_notobject,pd_notobjintf];
  1595. handler : @pd_far;
  1596. pocall : pocall_none;
  1597. pooption : [];
  1598. mutexclpocall : [pocall_internproc];
  1599. mutexclpotype : [];
  1600. mutexclpo : [po_inline]
  1601. ),(
  1602. idtok:_FAR16;
  1603. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar,pd_notobject];
  1604. handler : nil;
  1605. pocall : pocall_far16;
  1606. pooption : [];
  1607. mutexclpocall : [];
  1608. mutexclpotype : [];
  1609. mutexclpo : [po_external]
  1610. ),(
  1611. idtok:_FORWARD;
  1612. pd_flags : [pd_implemen,pd_notobject,pd_notobjintf];
  1613. handler : @pd_forward;
  1614. pocall : pocall_none;
  1615. pooption : [];
  1616. mutexclpocall : [pocall_internproc];
  1617. mutexclpotype : [];
  1618. mutexclpo : [po_external,po_inline]
  1619. ),(
  1620. idtok:_OLDFPCCALL;
  1621. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1622. handler : nil;
  1623. pocall : pocall_oldfpccall;
  1624. pooption : [];
  1625. mutexclpocall : [];
  1626. mutexclpotype : [];
  1627. mutexclpo : []
  1628. ),(
  1629. idtok:_INLINE;
  1630. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1631. handler : nil;
  1632. pocall : pocall_none;
  1633. pooption : [po_inline];
  1634. mutexclpocall : [];
  1635. mutexclpotype : [potype_constructor,potype_destructor];
  1636. mutexclpo : [po_exports,po_external,po_interrupt,po_virtualmethod]
  1637. ),(
  1638. idtok:_INTERNCONST;
  1639. pd_flags : [pd_interface,pd_body,pd_notobject,pd_notobjintf];
  1640. handler : @pd_internconst;
  1641. pocall : pocall_none;
  1642. pooption : [po_internconst];
  1643. mutexclpocall : [];
  1644. mutexclpotype : [potype_operator];
  1645. mutexclpo : []
  1646. ),(
  1647. idtok:_INTERNPROC;
  1648. pd_flags : [pd_interface,pd_notobject,pd_notobjintf];
  1649. handler : @pd_internproc;
  1650. pocall : pocall_internproc;
  1651. pooption : [];
  1652. mutexclpocall : [];
  1653. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1654. mutexclpo : [po_exports,po_external,po_interrupt,po_assembler,po_iocheck,po_virtualmethod]
  1655. ),(
  1656. idtok:_INTERRUPT;
  1657. pd_flags : [pd_implemen,pd_body,pd_notobject,pd_notobjintf];
  1658. handler : @pd_interrupt;
  1659. pocall : pocall_oldfpccall;
  1660. pooption : [po_interrupt];
  1661. mutexclpocall : [pocall_internproc,pocall_cdecl,pocall_cppdecl,pocall_stdcall,
  1662. pocall_pascal,pocall_far16,pocall_oldfpccall];
  1663. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1664. mutexclpo : [po_external,po_inline]
  1665. ),(
  1666. idtok:_IOCHECK;
  1667. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  1668. handler : nil;
  1669. pocall : pocall_none;
  1670. pooption : [po_iocheck];
  1671. mutexclpocall : [pocall_internproc];
  1672. mutexclpotype : [];
  1673. mutexclpo : [po_external]
  1674. ),(
  1675. idtok:_LOCAL;
  1676. pd_flags : [pd_implemen,pd_body];
  1677. handler : nil;
  1678. pocall : pocall_none;
  1679. pooption : [po_kylixlocal];
  1680. mutexclpocall : [pocall_internproc,pocall_far16];
  1681. mutexclpotype : [];
  1682. mutexclpo : [po_external,po_exports]
  1683. ),(
  1684. idtok:_MESSAGE;
  1685. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1686. handler : @pd_message;
  1687. pocall : pocall_none;
  1688. pooption : []; { can be po_msgstr or po_msgint }
  1689. mutexclpocall : [pocall_internproc];
  1690. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1691. mutexclpo : [po_interrupt,po_external,po_inline]
  1692. ),(
  1693. idtok:_MWPASCAL;
  1694. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1695. handler : nil;
  1696. pocall : pocall_mwpascal;
  1697. pooption : [];
  1698. mutexclpocall : [];
  1699. mutexclpotype : [];
  1700. mutexclpo : []
  1701. ),(
  1702. idtok:_NEAR;
  1703. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  1704. handler : @pd_near;
  1705. pocall : pocall_none;
  1706. pooption : [];
  1707. mutexclpocall : [pocall_internproc];
  1708. mutexclpotype : [];
  1709. mutexclpo : []
  1710. ),(
  1711. idtok:_NOSTACKFRAME;
  1712. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  1713. handler : nil;
  1714. pocall : pocall_none;
  1715. pooption : [po_nostackframe];
  1716. mutexclpocall : [pocall_internproc];
  1717. mutexclpotype : [];
  1718. mutexclpo : []
  1719. ),(
  1720. idtok:_OVERLOAD;
  1721. pd_flags : [pd_implemen,pd_interface,pd_body];
  1722. handler : @pd_overload;
  1723. pocall : pocall_none;
  1724. pooption : [po_overload];
  1725. mutexclpocall : [pocall_internproc];
  1726. mutexclpotype : [];
  1727. mutexclpo : []
  1728. ),(
  1729. idtok:_OVERRIDE;
  1730. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1731. handler : @pd_override;
  1732. pocall : pocall_none;
  1733. pooption : [po_overridingmethod,po_virtualmethod];
  1734. mutexclpocall : [pocall_internproc];
  1735. mutexclpotype : [];
  1736. mutexclpo : [po_exports,po_external,po_interrupt,po_virtualmethod,po_inline]
  1737. ),(
  1738. idtok:_PASCAL;
  1739. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1740. handler : nil;
  1741. pocall : pocall_pascal;
  1742. pooption : [];
  1743. mutexclpocall : [];
  1744. mutexclpotype : [potype_constructor,potype_destructor];
  1745. mutexclpo : [po_external]
  1746. ),(
  1747. idtok:_PUBLIC;
  1748. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobject,pd_notobjintf];
  1749. handler : @pd_public;
  1750. pocall : pocall_none;
  1751. pooption : [po_public,po_global];
  1752. mutexclpocall : [pocall_internproc];
  1753. mutexclpotype : [];
  1754. mutexclpo : [po_external,po_inline]
  1755. ),(
  1756. idtok:_REGISTER;
  1757. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1758. handler : nil;
  1759. pocall : pocall_register;
  1760. pooption : [];
  1761. mutexclpocall : [];
  1762. mutexclpotype : [potype_constructor,potype_destructor];
  1763. mutexclpo : [po_external]
  1764. ),(
  1765. idtok:_REINTRODUCE;
  1766. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1767. handler : @pd_reintroduce;
  1768. pocall : pocall_none;
  1769. pooption : [po_reintroduce];
  1770. mutexclpocall : [pocall_internproc];
  1771. mutexclpotype : [];
  1772. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod,po_inline]
  1773. ),(
  1774. idtok:_SAFECALL;
  1775. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1776. handler : nil;
  1777. pocall : pocall_safecall;
  1778. pooption : [];
  1779. mutexclpocall : [];
  1780. mutexclpotype : [potype_constructor,potype_destructor];
  1781. mutexclpo : [po_external]
  1782. ),(
  1783. idtok:_SOFTFLOAT;
  1784. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1785. handler : nil;
  1786. pocall : pocall_softfloat;
  1787. pooption : [];
  1788. mutexclpocall : [];
  1789. mutexclpotype : [potype_constructor,potype_destructor];
  1790. { it's available with po_external because the libgcc floating point routines on the arm
  1791. uses this calling convention }
  1792. mutexclpo : []
  1793. ),(
  1794. idtok:_STATIC;
  1795. pd_flags : [pd_interface,pd_implemen,pd_body,pd_object,pd_notobjintf];
  1796. handler : @pd_static;
  1797. pocall : pocall_none;
  1798. pooption : [po_staticmethod];
  1799. mutexclpocall : [pocall_internproc];
  1800. mutexclpotype : [potype_constructor,potype_destructor];
  1801. mutexclpo : [po_external,po_interrupt,po_exports,po_inline]
  1802. ),(
  1803. idtok:_STDCALL;
  1804. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1805. handler : nil;
  1806. pocall : pocall_stdcall;
  1807. pooption : [];
  1808. mutexclpocall : [];
  1809. mutexclpotype : [potype_constructor,potype_destructor];
  1810. mutexclpo : [po_external]
  1811. ),(
  1812. idtok:_SYSCALL;
  1813. { Different kind of syscalls are valid for AOS68k, AOSPPC and MOS. }
  1814. { FIX ME!!! MorphOS/AOS68k pd_flags should be:
  1815. pd_interface, pd_implemen, pd_notobject, pd_notobjintf (KB) }
  1816. pd_flags : [pd_interface,pd_implemen,pd_procvar];
  1817. handler : @pd_syscall;
  1818. pocall : pocall_syscall;
  1819. pooption : [];
  1820. mutexclpocall : [];
  1821. mutexclpotype : [potype_constructor,potype_destructor];
  1822. mutexclpo : [po_external,po_assembler,po_interrupt,po_exports]
  1823. ),(
  1824. idtok:_VIRTUAL;
  1825. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1826. handler : @pd_virtual;
  1827. pocall : pocall_none;
  1828. pooption : [po_virtualmethod];
  1829. mutexclpocall : [pocall_internproc];
  1830. mutexclpotype : [];
  1831. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod,po_inline]
  1832. ),(
  1833. idtok:_CPPDECL;
  1834. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1835. handler : nil;
  1836. pocall : pocall_cppdecl;
  1837. pooption : [];
  1838. mutexclpocall : [];
  1839. mutexclpotype : [potype_constructor,potype_destructor];
  1840. mutexclpo : [po_assembler,po_external,po_virtualmethod]
  1841. ),(
  1842. idtok:_VARARGS;
  1843. pd_flags : [pd_interface,pd_implemen,pd_procvar];
  1844. handler : nil;
  1845. pocall : pocall_none;
  1846. pooption : [po_varargs];
  1847. mutexclpocall : [pocall_internproc,pocall_stdcall,pocall_register,
  1848. pocall_far16,pocall_oldfpccall,pocall_mwpascal];
  1849. mutexclpotype : [];
  1850. mutexclpo : [po_assembler,po_interrupt,po_inline]
  1851. ),(
  1852. idtok:_COMPILERPROC;
  1853. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1854. handler : nil;
  1855. pocall : pocall_none;
  1856. pooption : [po_compilerproc];
  1857. mutexclpocall : [];
  1858. mutexclpotype : [potype_constructor,potype_destructor];
  1859. mutexclpo : [po_interrupt]
  1860. ),(
  1861. idtok:_WEAKEXTERNAL;
  1862. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject];
  1863. handler : @pd_weakexternal;
  1864. pocall : pocall_none;
  1865. { mark it both external and weak external, so we don't have to
  1866. adapt all code for external symbols to also check for weak external
  1867. }
  1868. pooption : [po_external,po_weakexternal];
  1869. mutexclpocall : [pocall_internproc,pocall_syscall];
  1870. { allowed for external cpp classes }
  1871. mutexclpotype : [{potype_constructor,potype_destructor}];
  1872. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  1873. )
  1874. );
  1875. function check_proc_directive(isprocvar:boolean):boolean;
  1876. var
  1877. i : longint;
  1878. begin
  1879. result:=false;
  1880. for i:=1 to num_proc_directives do
  1881. if proc_direcdata[i].idtok=idtoken then
  1882. begin
  1883. if ((not isprocvar) or
  1884. (pd_procvar in proc_direcdata[i].pd_flags)) and
  1885. { don't eat a public directive in classes }
  1886. not((idtoken=_PUBLIC) and (symtablestack.top.symtabletype=ObjectSymtable)) then
  1887. result:=true;
  1888. exit;
  1889. end;
  1890. end;
  1891. function parse_proc_direc(pd:tabstractprocdef;var pdflags:tpdflags):boolean;
  1892. {
  1893. Parse the procedure directive, returns true if a correct directive is found
  1894. }
  1895. var
  1896. p : longint;
  1897. found : boolean;
  1898. name : TIDString;
  1899. begin
  1900. parse_proc_direc:=false;
  1901. name:=tokeninfo^[idtoken].str;
  1902. found:=false;
  1903. { Hint directive? Then exit immediatly }
  1904. if (m_hintdirective in current_settings.modeswitches) then
  1905. begin
  1906. case idtoken of
  1907. _LIBRARY,
  1908. _PLATFORM,
  1909. _UNIMPLEMENTED,
  1910. _EXPERIMENTAL,
  1911. _DEPRECATED :
  1912. exit;
  1913. end;
  1914. end;
  1915. { C directive is MAC only, because it breaks too much existing code
  1916. on other platforms (PFV) }
  1917. if (idtoken=_C) and
  1918. not(m_mac in current_settings.modeswitches) then
  1919. exit;
  1920. { retrieve data for directive if found }
  1921. for p:=1 to num_proc_directives do
  1922. if proc_direcdata[p].idtok=idtoken then
  1923. begin
  1924. found:=true;
  1925. break;
  1926. end;
  1927. { Check if the procedure directive is known }
  1928. if not found then
  1929. begin
  1930. { parsing a procvar type the name can be any
  1931. next variable !! }
  1932. if ((pdflags * [pd_procvar,pd_object])=[]) and
  1933. not(idtoken=_PROPERTY) then
  1934. Message1(parser_w_unknown_proc_directive_ignored,name);
  1935. exit;
  1936. end;
  1937. { static needs a special treatment }
  1938. if (idtoken=_STATIC) and not (cs_static_keyword in current_settings.moduleswitches) then
  1939. exit;
  1940. { check if method and directive not for object, like public.
  1941. This needs to be checked also for procvars }
  1942. if (pd_notobject in proc_direcdata[p].pd_flags) and
  1943. (symtablestack.top.symtabletype=ObjectSymtable) and
  1944. { directive allowed for cpp classes? }
  1945. not(is_cppclass(tdef(symtablestack.top.defowner)) and (pd_cppobject in proc_direcdata[p].pd_flags)) then
  1946. exit;
  1947. { Conflicts between directives ? }
  1948. if (pd.proctypeoption in proc_direcdata[p].mutexclpotype) or
  1949. (pd.proccalloption in proc_direcdata[p].mutexclpocall) or
  1950. ((pd.procoptions*proc_direcdata[p].mutexclpo)<>[]) then
  1951. begin
  1952. Message1(parser_e_proc_dir_conflict,name);
  1953. exit;
  1954. end;
  1955. { set calling convention }
  1956. if proc_direcdata[p].pocall<>pocall_none then
  1957. begin
  1958. if (po_hascallingconvention in pd.procoptions) then
  1959. begin
  1960. Message2(parser_w_proc_overriding_calling,
  1961. proccalloptionStr[pd.proccalloption],
  1962. proccalloptionStr[proc_direcdata[p].pocall]);
  1963. end;
  1964. { check if the target processor supports this calling convention }
  1965. if not(proc_direcdata[p].pocall in supported_calling_conventions) then
  1966. begin
  1967. Message1(parser_e_illegal_calling_convention,proccalloptionStr[proc_direcdata[p].pocall]);
  1968. { recover }
  1969. proc_direcdata[p].pocall:=pocall_stdcall;
  1970. end;
  1971. pd.proccalloption:=proc_direcdata[p].pocall;
  1972. include(pd.procoptions,po_hascallingconvention);
  1973. end;
  1974. if pd.typ=procdef then
  1975. begin
  1976. { Check if the directive is only for objects }
  1977. if (pd_object in proc_direcdata[p].pd_flags) and
  1978. not assigned(tprocdef(pd)._class) then
  1979. exit;
  1980. { check if method and directive not for interface }
  1981. if (pd_notobjintf in proc_direcdata[p].pd_flags) and
  1982. is_interface(tprocdef(pd)._class) then
  1983. exit;
  1984. { check if method and directive not for interface }
  1985. if is_dispinterface(tprocdef(pd)._class) and
  1986. not(pd_dispinterface in proc_direcdata[p].pd_flags) then
  1987. exit;
  1988. end;
  1989. { consume directive, and turn flag on }
  1990. consume(token);
  1991. parse_proc_direc:=true;
  1992. { Check the pd_flags if the directive should be allowed }
  1993. if (pd_interface in pdflags) and
  1994. not(pd_interface in proc_direcdata[p].pd_flags) then
  1995. begin
  1996. Message1(parser_e_proc_dir_not_allowed_in_interface,name);
  1997. exit;
  1998. end;
  1999. if (pd_implemen in pdflags) and
  2000. not(pd_implemen in proc_direcdata[p].pd_flags) then
  2001. begin
  2002. Message1(parser_e_proc_dir_not_allowed_in_implementation,name);
  2003. exit;
  2004. end;
  2005. if (pd_procvar in pdflags) and
  2006. not(pd_procvar in proc_direcdata[p].pd_flags) then
  2007. begin
  2008. Message1(parser_e_proc_dir_not_allowed_in_procvar,name);
  2009. exit;
  2010. end;
  2011. { Return the new pd_flags }
  2012. if not(pd_body in proc_direcdata[p].pd_flags) then
  2013. exclude(pdflags,pd_body);
  2014. { Add the correct flag }
  2015. pd.procoptions:=pd.procoptions+proc_direcdata[p].pooption;
  2016. { Call the handler }
  2017. if pointer(proc_direcdata[p].handler)<>nil then
  2018. proc_direcdata[p].handler(pd);
  2019. end;
  2020. function proc_get_importname(pd:tprocdef):string;
  2021. function maybe_cprefix(const s:string):string;
  2022. begin
  2023. if not(pd.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  2024. result:=s
  2025. else
  2026. result:=target_info.Cprefix+s;
  2027. end;
  2028. begin
  2029. result:='';
  2030. if not(po_external in pd.procoptions) then
  2031. internalerror(200412151);
  2032. { external name or number is specified }
  2033. if assigned(pd.import_name) or (pd.import_nr<>0) then
  2034. begin
  2035. if assigned(pd.import_dll) then
  2036. begin
  2037. { If we are not using direct dll linking under win32 then imports
  2038. need to use the normal name since two functions can refer to the
  2039. same DLL function. This is also needed for compatability
  2040. with Delphi and TP7 }
  2041. (*
  2042. case target_info.system of
  2043. system_i386_emx,
  2044. system_i386_os2 :
  2045. begin
  2046. { keep normal mangledname }
  2047. if not (Assigned (PD.Import_Name)) then
  2048. Result := PD.MangledName;
  2049. end;
  2050. else
  2051. *)
  2052. if assigned(pd.import_name) then
  2053. begin
  2054. if target_info.system in (system_all_windows +
  2055. [system_i386_emx, system_i386_os2]) then
  2056. { cprefix is not used in DLL imports under Windows or OS/2 }
  2057. result:=pd.import_name^
  2058. else
  2059. result:=maybe_cprefix(pd.import_name^);
  2060. end
  2061. else
  2062. result:=ExtractFileName(pd.import_dll^)+'_index_'+tostr(pd.import_nr);
  2063. end
  2064. else
  2065. result:=maybe_cprefix(pd.import_name^);
  2066. end
  2067. else
  2068. begin
  2069. { Default names when importing variables }
  2070. case pd.proccalloption of
  2071. pocall_cdecl :
  2072. begin
  2073. if assigned(pd._class) then
  2074. result:=target_info.Cprefix+pd._class.objrealname^+'_'+pd.procsym.realname
  2075. else
  2076. result:=target_info.Cprefix+pd.procsym.realname;
  2077. end;
  2078. pocall_cppdecl :
  2079. begin
  2080. result:=target_info.Cprefix+pd.cplusplusmangledname;
  2081. end;
  2082. else
  2083. begin
  2084. {In MacPas a single "external" has the same effect as "external name 'xxx'" }
  2085. if (m_mac in current_settings.modeswitches) then
  2086. result:=tprocdef(pd).procsym.realname;
  2087. end;
  2088. end;
  2089. end;
  2090. end;
  2091. procedure proc_set_mangledname(pd:tprocdef);
  2092. var
  2093. s : string;
  2094. begin
  2095. { When the mangledname is already set we aren't allowed to change
  2096. it because it can already be used somewhere (PFV) }
  2097. if not(po_has_mangledname in pd.procoptions) then
  2098. begin
  2099. if (po_external in pd.procoptions) then
  2100. begin
  2101. { External Procedures are only allowed to change the mangledname
  2102. in their first declaration }
  2103. if (pd.forwarddef or (not pd.hasforward)) then
  2104. begin
  2105. s:=proc_get_importname(pd);
  2106. if s<>'' then
  2107. begin
  2108. { Replace ? and @ in import name }
  2109. { these replaces broke existing code on i386-win32 at least, while fixed
  2110. bug 8391 on arm-wince so limit this to arm-wince (KB) }
  2111. if target_info.system in [system_arm_wince] then
  2112. begin
  2113. Replace(s,'?','__q$$');
  2114. Replace(s,'@','__a$$');
  2115. end;
  2116. pd.setmangledname(s);
  2117. end;
  2118. end;
  2119. end
  2120. else
  2121. { Normal procedures }
  2122. begin
  2123. if (po_compilerproc in pd.procoptions) then
  2124. begin
  2125. pd.setmangledname(lower(pd.procsym.name));
  2126. end;
  2127. end;
  2128. end;
  2129. { Public/exported alias names }
  2130. if (([po_public,po_exports]*pd.procoptions)<>[]) and
  2131. not(po_has_public_name in pd.procoptions) then
  2132. begin
  2133. case pd.proccalloption of
  2134. pocall_cdecl :
  2135. begin
  2136. if assigned(pd._class) then
  2137. pd.aliasnames.insert(target_info.Cprefix+pd._class.objrealname^+'_'+pd.procsym.realname)
  2138. else
  2139. pd.aliasnames.insert(target_info.Cprefix+pd.procsym.realname);
  2140. end;
  2141. pocall_cppdecl :
  2142. begin
  2143. pd.aliasnames.insert(target_info.Cprefix+pd.cplusplusmangledname);
  2144. end;
  2145. end;
  2146. { prevent adding the alias a second time }
  2147. include(pd.procoptions,po_has_public_name);
  2148. end;
  2149. end;
  2150. procedure handle_calling_convention(pd:tabstractprocdef);
  2151. begin
  2152. { set the default calling convention if none provided }
  2153. if not(po_hascallingconvention in pd.procoptions) then
  2154. pd.proccalloption:=current_settings.defproccall
  2155. else
  2156. begin
  2157. if pd.proccalloption=pocall_none then
  2158. internalerror(200309081);
  2159. end;
  2160. { handle proccall specific settings }
  2161. case pd.proccalloption of
  2162. pocall_cdecl,
  2163. pocall_cppdecl :
  2164. begin
  2165. { check C cdecl para types }
  2166. check_c_para(pd);
  2167. end;
  2168. pocall_far16 :
  2169. begin
  2170. { Temporary stub, must be rewritten to support OS/2 far16 }
  2171. Message1(parser_w_proc_directive_ignored,'FAR16');
  2172. end;
  2173. end;
  2174. { Inlining is enabled and supported? }
  2175. if (po_inline in pd.procoptions) and
  2176. not(cs_do_inline in current_settings.localswitches) then
  2177. begin
  2178. { Give an error if inline is not supported by the compiler mode,
  2179. otherwise only give a warning that this procedure will not be inlined }
  2180. if not(m_default_inline in current_settings.modeswitches) then
  2181. Message(parser_e_proc_inline_not_supported)
  2182. else
  2183. Message(parser_w_inlining_disabled);
  2184. exclude(pd.procoptions,po_inline);
  2185. end;
  2186. { For varargs directive also cdecl and external must be defined }
  2187. if (po_varargs in pd.procoptions) then
  2188. begin
  2189. { check first for external in the interface, if available there
  2190. then the cdecl must also be there since there is no implementation
  2191. available to contain it }
  2192. if parse_only then
  2193. begin
  2194. { if external is available, then cdecl must also be available,
  2195. procvars don't need external }
  2196. if not((po_external in pd.procoptions) or
  2197. (pd.typ=procvardef)) and
  2198. not(pd.proccalloption in [pocall_cdecl,pocall_cppdecl,pocall_mwpascal]) then
  2199. Message(parser_e_varargs_need_cdecl_and_external);
  2200. end
  2201. else
  2202. begin
  2203. { both must be defined now }
  2204. if not((po_external in pd.procoptions) or
  2205. (pd.typ=procvardef)) or
  2206. not(pd.proccalloption in [pocall_cdecl,pocall_cppdecl,pocall_mwpascal]) then
  2207. Message(parser_e_varargs_need_cdecl_and_external);
  2208. end;
  2209. end;
  2210. { insert hidden high parameters }
  2211. pd.parast.SymList.ForEachCall(@insert_hidden_para,pd);
  2212. { insert hidden self parameter }
  2213. insert_self_and_vmt_para(pd);
  2214. { insert funcret parameter if required }
  2215. insert_funcret_para(pd);
  2216. { Make var parameters regable, this must be done after the calling
  2217. convention is set. }
  2218. { this must be done before parentfp is insert, because getting all cases
  2219. where parentfp must be in a memory location isn't catched properly so
  2220. we put parentfp never in a register }
  2221. pd.parast.SymList.ForEachCall(@set_addr_param_regable,pd);
  2222. { insert parentfp parameter if required }
  2223. insert_parentfp_para(pd);
  2224. { Calculate parameter tlist }
  2225. pd.calcparas;
  2226. end;
  2227. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  2228. {
  2229. Parse the procedure directives. It does not matter if procedure directives
  2230. are written using ;procdir; or ['procdir'] syntax.
  2231. }
  2232. var
  2233. res : boolean;
  2234. begin
  2235. if (m_mac in current_settings.modeswitches) and (cs_externally_visible in current_settings.localswitches) then
  2236. begin
  2237. tprocdef(pd).aliasnames.insert(tprocdef(pd).procsym.realname);
  2238. include(pd.procoptions,po_public);
  2239. include(pd.procoptions,po_has_public_name);
  2240. include(pd.procoptions,po_global);
  2241. end;
  2242. while token in [_ID,_LECKKLAMMER] do
  2243. begin
  2244. if try_to_consume(_LECKKLAMMER) then
  2245. begin
  2246. repeat
  2247. parse_proc_direc(pd,pdflags);
  2248. until not try_to_consume(_COMMA);
  2249. consume(_RECKKLAMMER);
  2250. { we always expect at least '[];' }
  2251. res:=true;
  2252. end
  2253. else
  2254. begin
  2255. res:=parse_proc_direc(pd,pdflags);
  2256. end;
  2257. { A procedure directive normally followed by a semicolon, but in
  2258. a const section or reading a type we should stop when _EQUAL is found,
  2259. because a constant/default value follows }
  2260. if res then
  2261. begin
  2262. if (block_type=bt_const_type) and
  2263. (token=_EQUAL) then
  2264. break;
  2265. { support procedure proc;stdcall export; }
  2266. if not(check_proc_directive((pd.typ=procvardef))) then
  2267. begin
  2268. { support "record p : procedure stdcall end;" and
  2269. "var p : procedure stdcall = nil;" }
  2270. if (pd_procvar in pdflags) and
  2271. (token in [_END,_RKLAMMER,_EQUAL]) then
  2272. break
  2273. else
  2274. consume(_SEMICOLON);
  2275. end;
  2276. end
  2277. else
  2278. break;
  2279. end;
  2280. end;
  2281. procedure parse_var_proc_directives(sym:tsym);
  2282. var
  2283. pdflags : tpdflags;
  2284. pd : tabstractprocdef;
  2285. begin
  2286. pdflags:=[pd_procvar];
  2287. pd:=nil;
  2288. case sym.typ of
  2289. fieldvarsym,
  2290. staticvarsym,
  2291. localvarsym,
  2292. paravarsym :
  2293. pd:=tabstractprocdef(tabstractvarsym(sym).vardef);
  2294. typesym :
  2295. pd:=tabstractprocdef(ttypesym(sym).typedef);
  2296. else
  2297. internalerror(2003042617);
  2298. end;
  2299. if pd.typ<>procvardef then
  2300. internalerror(2003042618);
  2301. { names should never be used anyway }
  2302. parse_proc_directives(pd,pdflags);
  2303. end;
  2304. procedure parse_object_proc_directives(pd:tabstractprocdef);
  2305. var
  2306. pdflags : tpdflags;
  2307. begin
  2308. pdflags:=[pd_object];
  2309. parse_proc_directives(pd,pdflags);
  2310. end;
  2311. function proc_add_definition(var currpd:tprocdef):boolean;
  2312. {
  2313. Add definition aprocdef to the overloaded definitions of aprocsym. If a
  2314. forwarddef is found and reused it returns true
  2315. }
  2316. var
  2317. fwpd : tprocdef;
  2318. currparasym,
  2319. fwparasym : tsym;
  2320. currparacnt,
  2321. fwparacnt,
  2322. curridx,
  2323. fwidx,
  2324. i : longint;
  2325. po_comp : tprocoptions;
  2326. paracompopt: tcompare_paras_options;
  2327. forwardfound : boolean;
  2328. begin
  2329. forwardfound:=false;
  2330. { check overloaded functions if the same function already exists }
  2331. for i:=0 to tprocsym(currpd.procsym).ProcdefList.Count-1 do
  2332. begin
  2333. fwpd:=tprocdef(tprocsym(currpd.procsym).ProcdefList[i]);
  2334. { Skip overloaded definitions that are declared in other units }
  2335. if fwpd.procsym<>currpd.procsym then
  2336. continue;
  2337. { check the parameters, for delphi/tp it is possible to
  2338. leave the parameters away in the implementation (forwarddef=false).
  2339. But for an overload declared function this is not allowed }
  2340. if { check if empty implementation arguments match is allowed }
  2341. (
  2342. not(m_repeat_forward in current_settings.modeswitches) and
  2343. not(currpd.forwarddef) and
  2344. is_bareprocdef(currpd) and
  2345. not(po_overload in fwpd.procoptions)
  2346. ) or
  2347. { check arguments, we need to check only the user visible parameters. The hidden parameters
  2348. can be in a different location because of the calling convention, eg. L-R vs. R-L order (PFV) }
  2349. (
  2350. (compare_paras(currpd.paras,fwpd.paras,cp_none,[cpo_comparedefaultvalue,cpo_ignorehidden,cpo_openequalisexact])=te_exact) and
  2351. (fwpd.returndef=currpd.returndef)
  2352. ) then
  2353. begin
  2354. { Check if we've found the forwarddef, if found then
  2355. we need to update the forward def with the current
  2356. implementation settings }
  2357. if fwpd.forwarddef then
  2358. begin
  2359. forwardfound:=true;
  2360. if not(m_repeat_forward in current_settings.modeswitches) and
  2361. (fwpd.proccalloption<>currpd.proccalloption) then
  2362. paracompopt:=[cpo_ignorehidden,cpo_comparedefaultvalue,cpo_openequalisexact]
  2363. else
  2364. paracompopt:=[cpo_comparedefaultvalue,cpo_openequalisexact];
  2365. { Check calling convention }
  2366. if (fwpd.proccalloption<>currpd.proccalloption) then
  2367. begin
  2368. { In delphi it is possible to specify the calling
  2369. convention in the interface or implementation if
  2370. there was no convention specified in the other
  2371. part }
  2372. if (m_delphi in current_settings.modeswitches) then
  2373. begin
  2374. if not(po_hascallingconvention in currpd.procoptions) then
  2375. currpd.proccalloption:=fwpd.proccalloption
  2376. else
  2377. if not(po_hascallingconvention in fwpd.procoptions) then
  2378. fwpd.proccalloption:=currpd.proccalloption
  2379. else
  2380. begin
  2381. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  2382. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2383. { restore interface settings }
  2384. currpd.proccalloption:=fwpd.proccalloption;
  2385. end;
  2386. end
  2387. else
  2388. begin
  2389. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  2390. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2391. { restore interface settings }
  2392. currpd.proccalloption:=fwpd.proccalloption;
  2393. end;
  2394. end;
  2395. { Check if the procedure type and return type are correct,
  2396. also the parameters must match also with the type }
  2397. if ((m_repeat_forward in current_settings.modeswitches) or
  2398. not is_bareprocdef(currpd)) and
  2399. ((compare_paras(currpd.paras,fwpd.paras,cp_all,paracompopt)<>te_exact) or
  2400. (fwpd.returndef<>currpd.returndef)) then
  2401. begin
  2402. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  2403. fwpd.fullprocname(false));
  2404. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2405. break;
  2406. end;
  2407. { Check if both are declared forward }
  2408. if fwpd.forwarddef and currpd.forwarddef then
  2409. begin
  2410. MessagePos1(currpd.fileinfo,parser_e_function_already_declared_public_forward,
  2411. currpd.fullprocname(false));
  2412. end;
  2413. { internconst or internproc only need to be defined once }
  2414. if (fwpd.proccalloption=pocall_internproc) then
  2415. currpd.proccalloption:=fwpd.proccalloption
  2416. else
  2417. if (currpd.proccalloption=pocall_internproc) then
  2418. fwpd.proccalloption:=currpd.proccalloption;
  2419. { Check procedure options, Delphi requires that class is
  2420. repeated in the implementation for class methods }
  2421. if (m_fpc in current_settings.modeswitches) then
  2422. po_comp:=[po_classmethod,po_varargs,po_methodpointer,po_interrupt]
  2423. else
  2424. po_comp:=[po_classmethod,po_methodpointer];
  2425. if ((po_comp * fwpd.procoptions)<>(po_comp * currpd.procoptions)) or
  2426. (fwpd.proctypeoption <> currpd.proctypeoption) then
  2427. begin
  2428. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  2429. fwpd.fullprocname(false));
  2430. tprocsym(fwpd.procsym).write_parameter_lists(fwpd);
  2431. { This error is non-fatal, we can recover }
  2432. end;
  2433. { Forward declaration is external? }
  2434. if (po_external in fwpd.procoptions) then
  2435. MessagePos(currpd.fileinfo,parser_e_proc_already_external);
  2436. { Check parameters }
  2437. if (m_repeat_forward in current_settings.modeswitches) or
  2438. (currpd.minparacount>0) then
  2439. begin
  2440. { If mangled names are equal then they have the same amount of arguments }
  2441. { We can check the names of the arguments }
  2442. { both symtables are in the same order from left to right }
  2443. curridx:=0;
  2444. fwidx:=0;
  2445. currparacnt:=currpd.parast.SymList.Count;
  2446. fwparacnt:=fwpd.parast.SymList.Count;
  2447. repeat
  2448. { skip default parameter constsyms }
  2449. while (curridx<currparacnt) and
  2450. (tsym(currpd.parast.SymList[curridx]).typ<>paravarsym) do
  2451. inc(curridx);
  2452. while (fwidx<fwparacnt) and
  2453. (tsym(fwpd.parast.SymList[fwidx]).typ<>paravarsym) do
  2454. inc(fwidx);
  2455. { stop when one of the two lists is at the end }
  2456. if (fwidx>=fwparacnt) or (curridx>=currparacnt) then
  2457. break;
  2458. { compare names of parameters, ignore implictly
  2459. renamed parameters }
  2460. currparasym:=tsym(currpd.parast.SymList[curridx]);
  2461. fwparasym:=tsym(fwpd.parast.SymList[fwidx]);
  2462. if not(sp_implicitrename in currparasym.symoptions) and
  2463. not(sp_implicitrename in fwparasym.symoptions) then
  2464. begin
  2465. if (currparasym.name<>fwparasym.name) then
  2466. begin
  2467. MessagePos3(currpd.fileinfo,parser_e_header_different_var_names,
  2468. tprocsym(currpd.procsym).realname,fwparasym.realname,currparasym.realname);
  2469. break;
  2470. end;
  2471. end;
  2472. { next parameter }
  2473. inc(curridx);
  2474. inc(fwidx);
  2475. until false;
  2476. end;
  2477. { Everything is checked, now we can update the forward declaration
  2478. with the new data from the implementation }
  2479. fwpd.forwarddef:=currpd.forwarddef;
  2480. fwpd.hasforward:=true;
  2481. fwpd.procoptions:=fwpd.procoptions+currpd.procoptions;
  2482. { marked as local but exported from unit? }
  2483. if (po_kylixlocal in fwpd.procoptions) and (fwpd.owner.symtabletype=globalsymtable) then
  2484. MessagePos(fwpd.fileinfo,type_e_cant_export_local);
  2485. if fwpd.extnumber=$ffff then
  2486. fwpd.extnumber:=currpd.extnumber;
  2487. while not currpd.aliasnames.empty do
  2488. fwpd.aliasnames.insert(currpd.aliasnames.getfirst);
  2489. { update fileinfo so position references the implementation,
  2490. also update funcretsym if it is already generated }
  2491. fwpd.fileinfo:=currpd.fileinfo;
  2492. if assigned(fwpd.funcretsym) then
  2493. fwpd.funcretsym.fileinfo:=currpd.fileinfo;
  2494. { import names }
  2495. if assigned(currpd.import_dll) then
  2496. begin
  2497. stringdispose(fwpd.import_dll);
  2498. fwpd.import_dll:=stringdup(currpd.import_dll^);
  2499. end;
  2500. if assigned(currpd.import_name) then
  2501. begin
  2502. stringdispose(fwpd.import_name);
  2503. fwpd.import_name:=stringdup(currpd.import_name^);
  2504. end;
  2505. fwpd.import_nr:=currpd.import_nr;
  2506. { for compilerproc defines we need to rename and update the
  2507. symbolname to lowercase }
  2508. if (po_compilerproc in fwpd.procoptions) then
  2509. begin
  2510. { rename to lowercase so users can't access it }
  2511. fwpd.procsym.realname:='$'+lower(fwpd.procsym.name);
  2512. { the mangeled name is already changed by the pd_compilerproc }
  2513. { handler. It must be done immediately because if we have a }
  2514. { call to a compilerproc before it's implementation is }
  2515. { encountered, it must already use the new mangled name (JM) }
  2516. end;
  2517. { Release current procdef }
  2518. currpd.owner.deletedef(currpd);
  2519. currpd:=fwpd;
  2520. end
  2521. else
  2522. begin
  2523. { abstract methods aren't forward defined, but this }
  2524. { needs another error message }
  2525. if (po_abstractmethod in fwpd.procoptions) then
  2526. MessagePos(currpd.fileinfo,parser_e_abstract_no_definition)
  2527. else
  2528. begin
  2529. MessagePos(currpd.fileinfo,parser_e_overloaded_have_same_parameters);
  2530. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2531. end;
  2532. end;
  2533. { we found one proc with the same arguments, there are no others
  2534. so we can stop }
  2535. break;
  2536. end;
  2537. { check for allowing overload directive }
  2538. if not(m_fpc in current_settings.modeswitches) then
  2539. begin
  2540. { overload directive turns on overloading }
  2541. if ((po_overload in currpd.procoptions) or
  2542. (po_overload in fwpd.procoptions)) then
  2543. begin
  2544. { check if all procs have overloading, but not if the proc is a method or
  2545. already declared forward, then the check is already done }
  2546. if not(fwpd.hasforward or
  2547. assigned(currpd._class) or
  2548. (currpd.forwarddef<>fwpd.forwarddef) or
  2549. ((po_overload in currpd.procoptions) and
  2550. (po_overload in fwpd.procoptions))) then
  2551. begin
  2552. MessagePos1(currpd.fileinfo,parser_e_no_overload_for_all_procs,currpd.procsym.realname);
  2553. break;
  2554. end;
  2555. end
  2556. else
  2557. begin
  2558. if not(fwpd.forwarddef) then
  2559. begin
  2560. MessagePos(currpd.fileinfo,parser_e_procedure_overloading_is_off);
  2561. break;
  2562. end;
  2563. end;
  2564. end; { equal arguments }
  2565. end;
  2566. { if we didn't reuse a forwarddef then we add the procdef to the overloaded
  2567. list }
  2568. if not forwardfound then
  2569. begin
  2570. { can happen in Delphi mode }
  2571. if (currpd.proctypeoption = potype_function) and
  2572. is_void(currpd.returndef) then
  2573. MessagePos1(currpd.fileinfo,parser_e_no_funcret_specified,currpd.procsym.realname);
  2574. tprocsym(currpd.procsym).ProcdefList.Add(currpd);
  2575. end;
  2576. proc_add_definition:=forwardfound;
  2577. end;
  2578. end.