pdecsub.pas 100 KB

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