pdecsub.pas 92 KB

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