pdecsub.pas 98 KB

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