pdecsub.pas 118 KB

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