pdecsub.pas 116 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094
  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. var
  663. hs : string;
  664. orgsp,sp : TIDString;
  665. srsym : tsym;
  666. srsymtable : TSymtable;
  667. checkstack : psymtablestackitem;
  668. storepos,
  669. procstartfilepos : tfileposinfo;
  670. searchagain : boolean;
  671. st,
  672. genericst : TSymtable;
  673. aprocsym : tprocsym;
  674. popclass : boolean;
  675. ImplIntf : TImplementedInterface;
  676. old_parse_generic : boolean;
  677. old_current_objectdef: tobjectdef;
  678. begin
  679. { Save the position where this procedure really starts }
  680. procstartfilepos:=current_tokenpos;
  681. old_parse_generic:=parse_generic;
  682. result:=false;
  683. pd:=nil;
  684. aprocsym:=nil;
  685. if potype=potype_operator then
  686. begin
  687. sp:=overloaded_names[optoken];
  688. orgsp:=sp;
  689. end
  690. else
  691. begin
  692. sp:=pattern;
  693. orgsp:=orgpattern;
  694. consume(_ID);
  695. end;
  696. { examine interface map: function/procedure iname.functionname=locfuncname }
  697. if assigned(aclass) and
  698. assigned(aclass.ImplementedInterfaces) and
  699. (aclass.ImplementedInterfaces.count>0) and
  700. try_to_consume(_POINT) then
  701. begin
  702. storepos:=current_tokenpos;
  703. current_tokenpos:=procstartfilepos;
  704. { get interface syms}
  705. searchsym(sp,srsym,srsymtable);
  706. if not assigned(srsym) then
  707. begin
  708. identifier_not_found(orgsp);
  709. srsym:=generrorsym;
  710. end;
  711. current_tokenpos:=storepos;
  712. { qualifier is interface? }
  713. ImplIntf:=nil;
  714. if (srsym.typ=typesym) and
  715. (ttypesym(srsym).typedef.typ=objectdef) then
  716. ImplIntf:=aclass.find_implemented_interface(tobjectdef(ttypesym(srsym).typedef));
  717. if ImplIntf=nil then
  718. Message(parser_e_interface_id_expected);
  719. consume(_ID);
  720. { Create unique name <interface>.<method> }
  721. hs:=sp+'.'+pattern;
  722. consume(_EQUAL);
  723. if assigned(ImplIntf) and
  724. (token=_ID) then
  725. ImplIntf.AddMapping(hs,pattern);
  726. consume(_ID);
  727. result:=true;
  728. exit;
  729. end;
  730. { method ? }
  731. if not assigned(aclass) and
  732. (potype<>potype_operator) and
  733. (symtablestack.top.symtablelevel=main_program_level) and
  734. try_to_consume(_POINT) then
  735. begin
  736. repeat
  737. searchagain:=false;
  738. if not assigned(aclass) then
  739. begin
  740. { search for object name }
  741. storepos:=current_tokenpos;
  742. current_tokenpos:=procstartfilepos;
  743. searchsym(sp,srsym,srsymtable);
  744. if not assigned(srsym) then
  745. begin
  746. identifier_not_found(orgsp);
  747. srsym:=generrorsym;
  748. end;
  749. current_tokenpos:=storepos;
  750. end;
  751. { consume proc name }
  752. sp:=pattern;
  753. orgsp:=orgpattern;
  754. procstartfilepos:=current_tokenpos;
  755. consume(_ID);
  756. { qualifier is class name ? }
  757. if (srsym.typ=typesym) and
  758. (ttypesym(srsym).typedef.typ=objectdef) then
  759. begin
  760. aclass:=tobjectdef(ttypesym(srsym).typedef);
  761. if (token<>_POINT) and (potype in [potype_class_constructor,potype_class_destructor]) then
  762. sp := lower(sp);
  763. srsym:=tsym(aclass.symtable.Find(sp));
  764. if assigned(srsym) then
  765. begin
  766. if srsym.typ=procsym then
  767. aprocsym:=tprocsym(srsym)
  768. else
  769. if (srsym.typ=typesym) and
  770. (ttypesym(srsym).typedef.typ=objectdef) then
  771. begin
  772. searchagain:=true;
  773. consume(_POINT);
  774. end
  775. else
  776. begin
  777. { we use a different error message for tp7 so it looks more compatible }
  778. if (m_fpc in current_settings.modeswitches) then
  779. Message1(parser_e_overloaded_no_procedure,srsym.realname)
  780. else
  781. Message(parser_e_methode_id_expected);
  782. { rename the name to an unique name to avoid an
  783. error when inserting the symbol in the symtable }
  784. orgsp:=orgsp+'$'+tostr(current_filepos.line);
  785. end;
  786. end
  787. else
  788. begin
  789. Message(parser_e_methode_id_expected);
  790. { recover by making it a normal procedure instead of method }
  791. aclass:=nil;
  792. end;
  793. end
  794. else
  795. Message(parser_e_class_id_expected);
  796. until not searchagain;
  797. end
  798. else
  799. begin
  800. { check for constructor/destructor which is not allowed here }
  801. if (not parse_only) and
  802. (potype in [potype_constructor,potype_destructor,
  803. potype_class_constructor,potype_class_destructor]) then
  804. Message(parser_e_constructors_always_objects);
  805. repeat
  806. searchagain:=false;
  807. current_tokenpos:=procstartfilepos;
  808. srsymtable:=symtablestack.top;
  809. srsym:=tsym(srsymtable.Find(sp));
  810. { Also look in the globalsymtable if we didn't found
  811. the symbol in the localsymtable }
  812. if not assigned(srsym) and
  813. not(parse_only) and
  814. (srsymtable=current_module.localsymtable) and
  815. assigned(current_module.globalsymtable) then
  816. srsym:=tsym(current_module.globalsymtable.Find(sp));
  817. { Check if overloaded is a procsym }
  818. if assigned(srsym) then
  819. begin
  820. if srsym.typ=procsym then
  821. aprocsym:=tprocsym(srsym)
  822. else
  823. begin
  824. { when the other symbol is a unit symbol then hide the unit
  825. symbol, this is not supported in tp7 }
  826. if not(m_tp7 in current_settings.modeswitches) and
  827. (srsym.typ=unitsym) then
  828. begin
  829. HideSym(srsym);
  830. searchagain:=true;
  831. end
  832. else
  833. begin
  834. { we use a different error message for tp7 so it looks more compatible }
  835. if (m_fpc in current_settings.modeswitches) then
  836. Message1(parser_e_overloaded_no_procedure,srsym.realname)
  837. else
  838. Message1(sym_e_duplicate_id,srsym.realname);
  839. { rename the name to an unique name to avoid an
  840. error when inserting the symbol in the symtable }
  841. orgsp:=orgsp+'$'+tostr(current_filepos.line);
  842. end;
  843. end;
  844. end;
  845. until not searchagain;
  846. end;
  847. { test again if assigned, it can be reset to recover }
  848. if not assigned(aprocsym) then
  849. begin
  850. { create a new procsym and set the real filepos }
  851. current_tokenpos:=procstartfilepos;
  852. { for operator we have only one procsym for each overloaded
  853. operation }
  854. if (potype=potype_operator) then
  855. begin
  856. aprocsym:=Tprocsym(symtablestack.top.Find(sp));
  857. if aprocsym=nil then
  858. aprocsym:=tprocsym.create('$'+sp);
  859. end
  860. else
  861. if (potype in [potype_class_constructor,potype_class_destructor]) then
  862. aprocsym:=tprocsym.create('$'+lower(sp))
  863. else
  864. aprocsym:=tprocsym.create(orgsp);
  865. symtablestack.top.insert(aprocsym);
  866. end;
  867. { to get the correct symtablelevel we must ignore ObjectSymtables }
  868. st:=nil;
  869. checkstack:=symtablestack.stack;
  870. while assigned(checkstack) do
  871. begin
  872. st:=checkstack^.symtable;
  873. if st.symtabletype in [staticsymtable,globalsymtable,localsymtable] then
  874. break;
  875. checkstack:=checkstack^.next;
  876. end;
  877. pd:=tprocdef.create(st.symtablelevel+1);
  878. pd._class:=aclass;
  879. pd.procsym:=aprocsym;
  880. pd.proctypeoption:=potype;
  881. { methods inherit df_generic or df_specialization from the objectdef }
  882. if assigned(pd._class) and
  883. (pd.parast.symtablelevel=normal_function_level) then
  884. begin
  885. if (df_generic in pd._class.defoptions) then
  886. begin
  887. include(pd.defoptions,df_generic);
  888. parse_generic:=true;
  889. end;
  890. if (df_specialization in pd._class.defoptions) then
  891. begin
  892. include(pd.defoptions,df_specialization);
  893. { Find corresponding genericdef, we need it later to
  894. replay the tokens to generate the body }
  895. if not assigned(pd._class.genericdef) then
  896. internalerror(200512113);
  897. genericst:=pd._class.genericdef.GetSymtable(gs_record);
  898. if not assigned(genericst) then
  899. internalerror(200512114);
  900. { We are parsing the same objectdef, the def index numbers
  901. are the same }
  902. pd.genericdef:=tstoreddef(genericst.DefList[pd.owner.DefList.IndexOf(pd)]);
  903. if not assigned(pd.genericdef) or
  904. (pd.genericdef.typ<>procdef) then
  905. internalerror(200512115);
  906. end;
  907. end;
  908. { methods need to be exported }
  909. if assigned(aclass) and
  910. (
  911. (symtablestack.top.symtabletype=ObjectSymtable) or
  912. (symtablestack.top.symtablelevel=main_program_level)
  913. ) then
  914. include(pd.procoptions,po_global);
  915. { symbol options that need to be kept per procdef }
  916. pd.fileinfo:=procstartfilepos;
  917. pd.visibility:=symtablestack.top.currentvisibility;
  918. if symtablestack.top.currentlyoptional then
  919. include(pd.procoptions,po_optional);
  920. { parse parameters }
  921. if token=_LKLAMMER then
  922. begin
  923. { Add ObjectSymtable to be able to find generic type definitions }
  924. popclass:=false;
  925. if assigned(pd._class) and
  926. (pd.parast.symtablelevel=normal_function_level) and
  927. (symtablestack.top.symtabletype<>ObjectSymtable) then
  928. begin
  929. symtablestack.push(pd._class.symtable);
  930. old_current_objectdef:=current_objectdef;
  931. current_objectdef:=pd._class;
  932. popclass:=true;
  933. end;
  934. { Add parameter symtable }
  935. if pd.parast.symtabletype<>staticsymtable then
  936. symtablestack.push(pd.parast);
  937. parse_parameter_dec(pd);
  938. if pd.parast.symtabletype<>staticsymtable then
  939. symtablestack.pop(pd.parast);
  940. if popclass then
  941. begin
  942. current_objectdef:=old_current_objectdef;
  943. symtablestack.pop(pd._class.symtable);
  944. end;
  945. end;
  946. parse_generic:=old_parse_generic;
  947. result:=true;
  948. end;
  949. function parse_proc_dec(isclassmethod:boolean; aclass:tobjectdef):tprocdef;
  950. var
  951. pd : tprocdef;
  952. locationstr: string;
  953. old_parse_generic,
  954. popclass: boolean;
  955. old_current_objectdef: tobjectdef;
  956. begin
  957. locationstr:='';
  958. pd:=nil;
  959. case token of
  960. _FUNCTION :
  961. begin
  962. consume(_FUNCTION);
  963. if parse_proc_head(aclass,potype_function,pd) then
  964. begin
  965. { pd=nil when it is a interface mapping }
  966. if assigned(pd) then
  967. begin
  968. if try_to_consume(_COLON) then
  969. begin
  970. old_parse_generic:=parse_generic;
  971. inc(testcurobject);
  972. { Add ObjectSymtable to be able to find generic type definitions }
  973. popclass:=false;
  974. if assigned(pd._class) and
  975. (pd.parast.symtablelevel=normal_function_level) and
  976. (symtablestack.top.symtabletype<>ObjectSymtable) then
  977. begin
  978. symtablestack.push(pd._class.symtable);
  979. popclass:=true;
  980. parse_generic:=(df_generic in pd._class.defoptions);
  981. old_current_objectdef:=current_objectdef;
  982. current_objectdef:=pd._class;
  983. end;
  984. single_type(pd.returndef,false,false);
  985. if is_dispinterface(pd._class) and not is_automatable(pd.returndef) then
  986. Message1(type_e_not_automatable,pd.returndef.typename);
  987. if popclass then
  988. begin
  989. current_objectdef:=old_current_objectdef;
  990. symtablestack.pop(pd._class.symtable);
  991. end;
  992. dec(testcurobject);
  993. parse_generic:=old_parse_generic;
  994. if (target_info.system in [system_m68k_amiga]) then
  995. begin
  996. if (idtoken=_LOCATION) then
  997. begin
  998. if po_explicitparaloc in pd.procoptions then
  999. begin
  1000. consume(_LOCATION);
  1001. locationstr:=cstringpattern;
  1002. consume(_CSTRING);
  1003. end
  1004. else
  1005. { I guess this needs a new message... (KB) }
  1006. Message(parser_e_paraloc_all_paras);
  1007. end
  1008. else
  1009. begin
  1010. if po_explicitparaloc in pd.procoptions then
  1011. { assign default locationstr, if none specified }
  1012. { and we've arguments with explicit paraloc }
  1013. locationstr:='D0';
  1014. end;
  1015. end;
  1016. end
  1017. else
  1018. begin
  1019. if (
  1020. parse_only and
  1021. not(is_interface(pd._class))
  1022. ) or
  1023. (m_repeat_forward in current_settings.modeswitches) then
  1024. begin
  1025. consume(_COLON);
  1026. consume_all_until(_SEMICOLON);
  1027. end;
  1028. end;
  1029. if isclassmethod then
  1030. include(pd.procoptions,po_classmethod);
  1031. end;
  1032. end
  1033. else
  1034. begin
  1035. { recover }
  1036. consume(_COLON);
  1037. consume_all_until(_SEMICOLON);
  1038. end;
  1039. end;
  1040. _PROCEDURE :
  1041. begin
  1042. consume(_PROCEDURE);
  1043. if parse_proc_head(aclass,potype_procedure,pd) then
  1044. begin
  1045. { pd=nil when it is an interface mapping }
  1046. if assigned(pd) then
  1047. begin
  1048. pd.returndef:=voidtype;
  1049. if isclassmethod then
  1050. include(pd.procoptions,po_classmethod);
  1051. end;
  1052. end;
  1053. end;
  1054. _CONSTRUCTOR :
  1055. begin
  1056. consume(_CONSTRUCTOR);
  1057. if isclassmethod then
  1058. parse_proc_head(aclass,potype_class_constructor,pd)
  1059. else
  1060. parse_proc_head(aclass,potype_constructor,pd);
  1061. if not isclassmethod and
  1062. assigned(pd) and
  1063. assigned(pd._class) then
  1064. begin
  1065. { Set return type, class constructors return the
  1066. created instance, object constructors return boolean }
  1067. if is_class(pd._class) then
  1068. pd.returndef:=pd._class
  1069. else
  1070. {$ifdef CPU64bitaddr}
  1071. pd.returndef:=bool64type;
  1072. {$else CPU64bitaddr}
  1073. pd.returndef:=bool32type;
  1074. {$endif CPU64bitaddr}
  1075. end
  1076. else
  1077. pd.returndef:=voidtype;
  1078. end;
  1079. _DESTRUCTOR :
  1080. begin
  1081. consume(_DESTRUCTOR);
  1082. if isclassmethod then
  1083. parse_proc_head(aclass,potype_class_destructor,pd)
  1084. else
  1085. parse_proc_head(aclass,potype_destructor,pd);
  1086. if assigned(pd) then
  1087. pd.returndef:=voidtype;
  1088. end;
  1089. _OPERATOR :
  1090. begin
  1091. consume(_OPERATOR);
  1092. if (token in [first_overloaded..last_overloaded]) then
  1093. begin
  1094. optoken:=token;
  1095. end
  1096. else
  1097. begin
  1098. { Use the dummy NOTOKEN that is also declared
  1099. for the overloaded_operator[] }
  1100. optoken:=NOTOKEN;
  1101. case token of
  1102. _CARET:
  1103. Message1(parser_e_overload_operator_failed,'**');
  1104. _ID:
  1105. if idtoken = _ENUMERATOR then
  1106. optoken := _OP_ENUMERATOR
  1107. else
  1108. Message1(parser_e_overload_operator_failed,'');
  1109. _UNEQUAL:
  1110. Message1(parser_e_overload_operator_failed,'=');
  1111. else
  1112. Message1(parser_e_overload_operator_failed,'');
  1113. end;
  1114. end;
  1115. consume(token);
  1116. parse_proc_head(aclass,potype_operator,pd);
  1117. if assigned(pd) then
  1118. begin
  1119. { operators always need to be searched in all units }
  1120. include(pd.procoptions,po_overload);
  1121. if pd.parast.symtablelevel>normal_function_level then
  1122. Message(parser_e_no_local_operator);
  1123. if token<>_ID then
  1124. begin
  1125. if not(m_result in current_settings.modeswitches) then
  1126. consume(_ID);
  1127. end
  1128. else
  1129. begin
  1130. pd.resultname:=stringdup(orgpattern);
  1131. consume(_ID);
  1132. end;
  1133. if not try_to_consume(_COLON) then
  1134. begin
  1135. consume(_COLON);
  1136. pd.returndef:=generrordef;
  1137. consume_all_until(_SEMICOLON);
  1138. end
  1139. else
  1140. begin
  1141. single_type(pd.returndef,false,false);
  1142. if (optoken in [_EQUAL,_GT,_LT,_GTE,_LTE]) and
  1143. ((pd.returndef.typ<>orddef) or
  1144. (torddef(pd.returndef).ordtype<>pasbool)) then
  1145. Message(parser_e_comparative_operator_return_boolean);
  1146. if (optoken=_ASSIGNMENT) and
  1147. equal_defs(pd.returndef,tparavarsym(pd.parast.SymList[0]).vardef) then
  1148. message(parser_e_no_such_assignment)
  1149. else if not isoperatoracceptable(pd,optoken) then
  1150. Message(parser_e_overload_impossible);
  1151. end;
  1152. end
  1153. else
  1154. begin
  1155. { recover }
  1156. try_to_consume(_ID);
  1157. consume(_COLON);
  1158. consume_all_until(_SEMICOLON);
  1159. end;
  1160. end;
  1161. end;
  1162. { file types can't be function results }
  1163. if assigned(pd) and
  1164. (pd.returndef.typ=filedef) then
  1165. message(parser_e_illegal_function_result);
  1166. { support procedure proc stdcall export; }
  1167. if not(check_proc_directive(false)) then
  1168. begin
  1169. if (token=_COLON) then
  1170. begin
  1171. message(parser_e_field_not_allowed_here);
  1172. consume_all_until(_SEMICOLON);
  1173. end;
  1174. consume(_SEMICOLON);
  1175. end;
  1176. result:=pd;
  1177. if locationstr<>'' then
  1178. begin
  1179. if not(paramanager.parsefuncretloc(pd,upper(locationstr))) then
  1180. { I guess this needs a new message... (KB) }
  1181. message(parser_e_illegal_explicit_paraloc);
  1182. end;
  1183. end;
  1184. {****************************************************************************
  1185. Procedure directive handlers
  1186. ****************************************************************************}
  1187. procedure pd_far(pd:tabstractprocdef);
  1188. begin
  1189. Message1(parser_w_proc_directive_ignored,'FAR');
  1190. end;
  1191. procedure pd_near(pd:tabstractprocdef);
  1192. begin
  1193. Message1(parser_w_proc_directive_ignored,'NEAR');
  1194. end;
  1195. procedure pd_export(pd:tabstractprocdef);
  1196. begin
  1197. if pd.typ<>procdef then
  1198. internalerror(200304264);
  1199. if assigned(tprocdef(pd)._class) then
  1200. Message(parser_e_methods_dont_be_export);
  1201. if pd.parast.symtablelevel>normal_function_level then
  1202. Message(parser_e_dont_nest_export);
  1203. end;
  1204. procedure pd_forward(pd:tabstractprocdef);
  1205. begin
  1206. if pd.typ<>procdef then
  1207. internalerror(200304265);
  1208. tprocdef(pd).forwarddef:=true;
  1209. end;
  1210. procedure pd_alias(pd:tabstractprocdef);
  1211. begin
  1212. if pd.typ<>procdef then
  1213. internalerror(200304266);
  1214. consume(_COLON);
  1215. tprocdef(pd).aliasnames.insert(get_stringconst);
  1216. include(pd.procoptions,po_has_public_name);
  1217. end;
  1218. procedure pd_public(pd:tabstractprocdef);
  1219. begin
  1220. if pd.typ<>procdef then
  1221. internalerror(200304266);
  1222. if try_to_consume(_NAME) then
  1223. begin
  1224. tprocdef(pd).aliasnames.insert(get_stringconst);
  1225. include(pd.procoptions,po_has_public_name);
  1226. end;
  1227. end;
  1228. procedure pd_asmname(pd:tabstractprocdef);
  1229. begin
  1230. if pd.typ<>procdef then
  1231. internalerror(200304267);
  1232. if token=_CCHAR then
  1233. begin
  1234. tprocdef(pd).aliasnames.insert(target_info.Cprefix+pattern);
  1235. consume(_CCHAR)
  1236. end
  1237. else
  1238. begin
  1239. tprocdef(pd).aliasnames.insert(target_info.Cprefix+cstringpattern);
  1240. consume(_CSTRING);
  1241. end;
  1242. { we don't need anything else }
  1243. tprocdef(pd).forwarddef:=false;
  1244. end;
  1245. procedure pd_internconst(pd:tabstractprocdef);
  1246. var v:Tconstexprint;
  1247. begin
  1248. if pd.typ<>procdef then
  1249. internalerror(200304268);
  1250. consume(_COLON);
  1251. v:=get_intconst;
  1252. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1253. message(parser_e_range_check_error)
  1254. else
  1255. Tprocdef(pd).extnumber:=longint(v.svalue);
  1256. end;
  1257. procedure pd_internproc(pd:tabstractprocdef);
  1258. var v:Tconstexprint;
  1259. begin
  1260. if pd.typ<>procdef then
  1261. internalerror(200304268);
  1262. consume(_COLON);
  1263. v:=get_intconst;
  1264. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1265. message(parser_e_range_check_error)
  1266. else
  1267. Tprocdef(pd).extnumber:=longint(v.svalue);
  1268. { the proc is defined }
  1269. tprocdef(pd).forwarddef:=false;
  1270. end;
  1271. procedure pd_interrupt(pd:tabstractprocdef);
  1272. begin
  1273. if pd.parast.symtablelevel>normal_function_level then
  1274. Message(parser_e_dont_nest_interrupt);
  1275. end;
  1276. procedure pd_abstract(pd:tabstractprocdef);
  1277. begin
  1278. if pd.typ<>procdef then
  1279. internalerror(200304269);
  1280. if oo_is_sealed in tprocdef(pd)._class.objectoptions then
  1281. Message(parser_e_sealed_class_cannot_have_abstract_methods)
  1282. else
  1283. if (po_virtualmethod in pd.procoptions) then
  1284. include(pd.procoptions,po_abstractmethod)
  1285. else
  1286. Message(parser_e_only_virtual_methods_abstract);
  1287. { the method is defined }
  1288. tprocdef(pd).forwarddef:=false;
  1289. end;
  1290. procedure pd_final(pd:tabstractprocdef);
  1291. begin
  1292. if pd.typ<>procdef then
  1293. internalerror(200910170);
  1294. if (po_virtualmethod in pd.procoptions) then
  1295. include(pd.procoptions,po_finalmethod)
  1296. else
  1297. Message(parser_e_only_virtual_methods_final);
  1298. end;
  1299. procedure pd_enumerator(pd:tabstractprocdef);
  1300. begin
  1301. if pd.typ<>procdef then
  1302. internalerror(200910250);
  1303. if (token = _ID) then
  1304. begin
  1305. if pattern='MOVENEXT' then
  1306. begin
  1307. if oo_has_enumerator_movenext in tprocdef(pd)._class.objectoptions then
  1308. message(parser_e_only_one_enumerator_movenext);
  1309. pd.calcparas;
  1310. if (pd.proctypeoption = potype_function) and is_boolean(pd.returndef) and
  1311. (pd.minparacount = 0) then
  1312. begin
  1313. include(tprocdef(pd)._class.objectoptions, oo_has_enumerator_movenext);
  1314. include(pd.procoptions,po_enumerator_movenext);
  1315. end
  1316. else
  1317. Message(parser_e_enumerator_movenext_is_not_valid)
  1318. end
  1319. else
  1320. Message1(parser_e_invalid_enumerator_identifier, pattern);
  1321. consume(token);
  1322. end
  1323. else
  1324. Message(parser_e_enumerator_identifier_required);
  1325. end;
  1326. procedure pd_virtual(pd:tabstractprocdef);
  1327. {$ifdef WITHDMT}
  1328. var
  1329. pt : tnode;
  1330. {$endif WITHDMT}
  1331. begin
  1332. if pd.typ<>procdef then
  1333. internalerror(2003042610);
  1334. if (pd.proctypeoption=potype_constructor) and
  1335. is_object(tprocdef(pd)._class) then
  1336. Message(parser_e_constructor_cannot_be_not_virtual);
  1337. {$ifdef WITHDMT}
  1338. if is_object(tprocdef(pd)._class) and
  1339. (token<>_SEMICOLON) then
  1340. begin
  1341. { any type of parameter is allowed here! }
  1342. pt:=comp_expr(true);
  1343. if is_constintnode(pt) then
  1344. begin
  1345. include(pd.procoptions,po_msgint);
  1346. pd.messageinf.i:=pt.value;
  1347. end
  1348. else
  1349. Message(parser_e_ill_msg_expr);
  1350. disposetree(pt);
  1351. end;
  1352. {$endif WITHDMT}
  1353. end;
  1354. procedure pd_dispid(pd:tabstractprocdef);
  1355. var pt:Tnode;
  1356. begin
  1357. if pd.typ<>procdef then
  1358. internalerror(200604301);
  1359. pt:=comp_expr(true);
  1360. if is_constintnode(pt) then
  1361. if (Tordconstnode(pt).value<int64(low(longint))) or (Tordconstnode(pt).value>int64(high(longint))) then
  1362. message(parser_e_range_check_error)
  1363. else
  1364. Tprocdef(pd).dispid:=Tordconstnode(pt).value.svalue
  1365. else
  1366. message(parser_e_dispid_must_be_ord_const);
  1367. pt.free;
  1368. end;
  1369. procedure pd_static(pd:tabstractprocdef);
  1370. begin
  1371. if pd.typ=procdef then
  1372. include(tprocdef(pd).procsym.symoptions,sp_static);
  1373. include(pd.procoptions,po_staticmethod);
  1374. end;
  1375. procedure pd_override(pd:tabstractprocdef);
  1376. begin
  1377. if pd.typ<>procdef then
  1378. internalerror(2003042611);
  1379. if not(is_class_or_interface_or_objc(tprocdef(pd)._class)) then
  1380. Message(parser_e_no_object_override)
  1381. else if is_objccategory(tprocdef(pd)._class) then
  1382. Message(parser_e_no_category_override);
  1383. end;
  1384. procedure pd_overload(pd:tabstractprocdef);
  1385. begin
  1386. if pd.typ<>procdef then
  1387. internalerror(2003042612);
  1388. include(tprocdef(pd).procsym.symoptions,sp_has_overloaded);
  1389. end;
  1390. procedure pd_message(pd:tabstractprocdef);
  1391. var
  1392. pt : tnode;
  1393. paracnt : longint;
  1394. begin
  1395. if pd.typ<>procdef then
  1396. internalerror(2003042613);
  1397. if not is_class(tprocdef(pd)._class) and
  1398. not is_objc_class_or_protocol(tprocdef(pd)._class) then
  1399. Message(parser_e_msg_only_for_classes);
  1400. if ([po_msgstr,po_msgint]*pd.procoptions)<>[] then
  1401. Message(parser_e_multiple_messages);
  1402. { check parameter type }
  1403. if not is_objc_class_or_protocol(tprocdef(pd)._class) then
  1404. begin
  1405. paracnt:=0;
  1406. pd.parast.SymList.ForEachCall(@check_msg_para,@paracnt);
  1407. if paracnt<>1 then
  1408. Message(parser_e_ill_msg_param);
  1409. end;
  1410. pt:=comp_expr(true);
  1411. { message is 1-character long }
  1412. if is_constcharnode(pt) then
  1413. begin
  1414. include(pd.procoptions,po_msgstr);
  1415. tprocdef(pd).messageinf.str:=stringdup(chr(byte(tordconstnode(pt).value.uvalue and $FF)));
  1416. end
  1417. else if pt.nodetype=stringconstn then
  1418. begin
  1419. include(pd.procoptions,po_msgstr);
  1420. if (tstringconstnode(pt).len>255) then
  1421. Message(parser_e_message_string_too_long);
  1422. tprocdef(pd).messageinf.str:=stringdup(tstringconstnode(pt).value_str);
  1423. end
  1424. else
  1425. if is_constintnode(pt) and
  1426. is_class(tprocdef(pd)._class) then
  1427. begin
  1428. include(pd.procoptions,po_msgint);
  1429. if (Tordconstnode(pt).value<int64(low(Tprocdef(pd).messageinf.i))) or
  1430. (Tordconstnode(pt).value>int64(high(Tprocdef(pd).messageinf.i))) then
  1431. message(parser_e_range_check_error)
  1432. else
  1433. Tprocdef(pd).messageinf.i:=tordconstnode(pt).value.svalue;
  1434. end
  1435. else
  1436. Message(parser_e_ill_msg_expr);
  1437. { check whether the selector name is valid in case of Objective-C }
  1438. if (po_msgstr in pd.procoptions) and
  1439. is_objc_class_or_protocol(tprocdef(pd)._class) and
  1440. not objcvalidselectorname(@tprocdef(pd).messageinf.str^[1],length(tprocdef(pd).messageinf.str^)) then
  1441. Message1(type_e_invalid_objc_selector_name,tprocdef(pd).messageinf.str^);
  1442. pt.free;
  1443. end;
  1444. procedure pd_reintroduce(pd:tabstractprocdef);
  1445. begin
  1446. if pd.typ<>procdef then
  1447. internalerror(200401211);
  1448. if not(is_class_or_interface_or_object(tprocdef(pd)._class)) and
  1449. not(is_objccategory(tprocdef(pd)._class)) then
  1450. Message(parser_e_no_object_reintroduce);
  1451. end;
  1452. procedure pd_syscall(pd:tabstractprocdef);
  1453. {$if defined(powerpc) or defined(m68k)}
  1454. var
  1455. vs : tparavarsym;
  1456. sym : tsym;
  1457. symtable : TSymtable;
  1458. v: Tconstexprint;
  1459. {$endif defined(powerpc) or defined(m68k)}
  1460. begin
  1461. if (pd.typ<>procdef) and (target_info.system <> system_powerpc_amiga) then
  1462. internalerror(2003042614);
  1463. tprocdef(pd).forwarddef:=false;
  1464. {$ifdef m68k}
  1465. if target_info.system in [system_m68k_amiga] then
  1466. begin
  1467. include(pd.procoptions,po_syscall_legacy);
  1468. if consume_sym(sym,symtable) then
  1469. begin
  1470. if (sym.typ=staticvarsym) and
  1471. (
  1472. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1473. is_32bitint(tabstractvarsym(sym).vardef)
  1474. ) then
  1475. begin
  1476. tprocdef(pd).libsym:=sym;
  1477. if po_syscall_legacy in tprocdef(pd).procoptions then
  1478. begin
  1479. vs:=tparavarsym.create('$syscalllib',paranr_syscall_legacy,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1480. paramanager.parseparaloc(vs,'A6');
  1481. pd.parast.insert(vs);
  1482. end
  1483. end
  1484. else
  1485. Message(parser_e_32bitint_or_pointer_variable_expected);
  1486. end;
  1487. (paramanager as tm68kparamanager).create_funcretloc_info(pd,calleeside);
  1488. (paramanager as tm68kparamanager).create_funcretloc_info(pd,callerside);
  1489. v:=get_intconst;
  1490. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1491. message(parser_e_range_check_error)
  1492. else
  1493. Tprocdef(pd).extnumber:=v.uvalue;
  1494. end;
  1495. {$endif m68k}
  1496. {$ifdef powerpc}
  1497. if target_info.system = system_powerpc_amiga then
  1498. begin
  1499. include(pd.procoptions,po_syscall_basesysv);
  1500. if consume_sym(sym,symtable) then
  1501. begin
  1502. if (sym.typ=staticvarsym) and
  1503. (
  1504. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1505. is_32bitint(tabstractvarsym(sym).vardef)
  1506. ) then
  1507. begin
  1508. tprocdef(pd).libsym:=sym;
  1509. vs:=tparavarsym.create('$syscalllib',paranr_syscall_basesysv,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1510. pd.parast.insert(vs);
  1511. end
  1512. else
  1513. Message(parser_e_32bitint_or_pointer_variable_expected);
  1514. end;
  1515. (paramanager as tppcparamanager).create_funcretloc_info(pd,calleeside);
  1516. (paramanager as tppcparamanager).create_funcretloc_info(pd,callerside);
  1517. v:=get_intconst;
  1518. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1519. message(parser_e_range_check_error)
  1520. else
  1521. Tprocdef(pd).extnumber:=v.uvalue;
  1522. end else
  1523. if target_info.system = system_powerpc_morphos then
  1524. begin
  1525. if idtoken=_LEGACY then
  1526. begin
  1527. consume(_LEGACY);
  1528. include(pd.procoptions,po_syscall_legacy);
  1529. end
  1530. else if idtoken=_SYSV then
  1531. begin
  1532. consume(_SYSV);
  1533. include(pd.procoptions,po_syscall_sysv);
  1534. end
  1535. else if idtoken=_BASESYSV then
  1536. begin
  1537. consume(_BASESYSV);
  1538. include(pd.procoptions,po_syscall_basesysv);
  1539. end
  1540. else if idtoken=_SYSVBASE then
  1541. begin
  1542. consume(_SYSVBASE);
  1543. include(pd.procoptions,po_syscall_sysvbase);
  1544. end
  1545. else if idtoken=_R12BASE then
  1546. begin
  1547. consume(_R12BASE);
  1548. include(pd.procoptions,po_syscall_r12base);
  1549. end
  1550. else
  1551. if syscall_convention='LEGACY' then
  1552. include(pd.procoptions,po_syscall_legacy)
  1553. else if syscall_convention='SYSV' then
  1554. include(pd.procoptions,po_syscall_sysv)
  1555. else if syscall_convention='BASESYSV' then
  1556. include(pd.procoptions,po_syscall_basesysv)
  1557. else if syscall_convention='SYSVBASE' then
  1558. include(pd.procoptions,po_syscall_sysvbase)
  1559. else if syscall_convention='R12BASE' then
  1560. include(pd.procoptions,po_syscall_r12base)
  1561. else
  1562. internalerror(2005010404);
  1563. if consume_sym(sym,symtable) then
  1564. begin
  1565. if (sym.typ=staticvarsym) and
  1566. (
  1567. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1568. is_32bitint(tabstractvarsym(sym).vardef)
  1569. ) then
  1570. begin
  1571. tprocdef(pd).libsym:=sym;
  1572. if po_syscall_legacy in tprocdef(pd).procoptions then
  1573. begin
  1574. vs:=tparavarsym.create('$syscalllib',paranr_syscall_legacy,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1575. paramanager.parseparaloc(vs,'A6');
  1576. pd.parast.insert(vs);
  1577. end
  1578. else if po_syscall_sysv in tprocdef(pd).procoptions then
  1579. begin
  1580. { Nothing to be done for sysv here for now, but this might change }
  1581. end
  1582. else if po_syscall_basesysv in tprocdef(pd).procoptions then
  1583. begin
  1584. vs:=tparavarsym.create('$syscalllib',paranr_syscall_basesysv,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1585. pd.parast.insert(vs);
  1586. end
  1587. else if po_syscall_sysvbase in tprocdef(pd).procoptions then
  1588. begin
  1589. vs:=tparavarsym.create('$syscalllib',paranr_syscall_sysvbase,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1590. pd.parast.insert(vs);
  1591. end
  1592. else if po_syscall_r12base in tprocdef(pd).procoptions then
  1593. begin
  1594. vs:=tparavarsym.create('$syscalllib',paranr_syscall_r12base,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1595. paramanager.parseparaloc(vs,'R12');
  1596. pd.parast.insert(vs);
  1597. end
  1598. else
  1599. internalerror(2005010501);
  1600. end
  1601. else
  1602. Message(parser_e_32bitint_or_pointer_variable_expected);
  1603. end;
  1604. (paramanager as tppcparamanager).create_funcretloc_info(pd,calleeside);
  1605. (paramanager as tppcparamanager).create_funcretloc_info(pd,callerside);
  1606. v:=get_intconst;
  1607. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1608. message(parser_e_range_check_error)
  1609. else
  1610. Tprocdef(pd).extnumber:=v.uvalue;
  1611. end;
  1612. {$endif powerpc}
  1613. end;
  1614. procedure pd_external(pd:tabstractprocdef);
  1615. {
  1616. If import_dll=nil the procedure is assumed to be in another
  1617. object file. In that object file it should have the name to
  1618. which import_name is pointing to. Otherwise, the procedure is
  1619. assumed to be in the DLL to which import_dll is pointing to. In
  1620. that case either import_nr<>0 or import_name<>nil is true, so
  1621. the procedure is either imported by number or by name. (DM)
  1622. }
  1623. var
  1624. hs : string;
  1625. v:Tconstexprint;
  1626. begin
  1627. if pd.typ<>procdef then
  1628. internalerror(2003042615);
  1629. with tprocdef(pd) do
  1630. begin
  1631. forwarddef:=false;
  1632. { forbid local external procedures }
  1633. if parast.symtablelevel>normal_function_level then
  1634. Message(parser_e_no_local_proc_external);
  1635. { If the procedure should be imported from a DLL, a constant string follows.
  1636. This isn't really correct, an contant string expression follows
  1637. so we check if an semicolon follows, else a string constant have to
  1638. follow (FK) }
  1639. if not(token=_SEMICOLON) and not(idtoken=_NAME) then
  1640. begin
  1641. { Always add library prefix and suffix to create an uniform name }
  1642. hs:=get_stringconst;
  1643. if ExtractFileExt(hs)='' then
  1644. hs:=ChangeFileExt(hs,target_info.sharedlibext);
  1645. if Copy(hs,1,length(target_info.sharedlibprefix))<>target_info.sharedlibprefix then
  1646. hs:=target_info.sharedlibprefix+hs;
  1647. import_dll:=stringdup(hs);
  1648. include(procoptions,po_has_importdll);
  1649. if (idtoken=_NAME) then
  1650. begin
  1651. consume(_NAME);
  1652. import_name:=stringdup(get_stringconst);
  1653. include(procoptions,po_has_importname);
  1654. if import_name^='' then
  1655. message(parser_e_empty_import_name);
  1656. end;
  1657. if (idtoken=_INDEX) then
  1658. begin
  1659. {After the word index follows the index number in the DLL.}
  1660. consume(_INDEX);
  1661. v:=get_intconst;
  1662. if (v<int64(low(import_nr))) or (v>int64(high(import_nr))) then
  1663. message(parser_e_range_check_error)
  1664. else
  1665. import_nr:=longint(v.svalue);
  1666. end;
  1667. { default is to used the realname of the procedure }
  1668. if (import_nr=0) and not assigned(import_name) then
  1669. begin
  1670. import_name:=stringdup(procsym.realname);
  1671. include(procoptions,po_has_importname);
  1672. end;
  1673. end
  1674. else
  1675. begin
  1676. if (idtoken=_NAME) then
  1677. begin
  1678. consume(_NAME);
  1679. import_name:=stringdup(get_stringconst);
  1680. include(procoptions,po_has_importname);
  1681. if import_name^='' then
  1682. message(parser_e_empty_import_name);
  1683. end;
  1684. end;
  1685. end;
  1686. end;
  1687. procedure pd_weakexternal(pd:tabstractprocdef);
  1688. begin
  1689. if not(target_info.system in systems_weak_linking) then
  1690. message(parser_e_weak_external_not_supported)
  1691. else
  1692. pd_external(pd);
  1693. end;
  1694. type
  1695. pd_handler=procedure(pd:tabstractprocdef);
  1696. proc_dir_rec=record
  1697. idtok : ttoken;
  1698. pd_flags : tpdflags;
  1699. handler : pd_handler;
  1700. pocall : tproccalloption;
  1701. pooption : tprocoptions;
  1702. mutexclpocall : tproccalloptions;
  1703. mutexclpotype : tproctypeoptions;
  1704. mutexclpo : tprocoptions;
  1705. end;
  1706. const
  1707. {Should contain the number of procedure directives we support.}
  1708. num_proc_directives=42;
  1709. proc_direcdata:array[1..num_proc_directives] of proc_dir_rec=
  1710. (
  1711. (
  1712. idtok:_ABSTRACT;
  1713. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1714. handler : @pd_abstract;
  1715. pocall : pocall_none;
  1716. pooption : [po_abstractmethod];
  1717. mutexclpocall : [pocall_internproc];
  1718. mutexclpotype : [];
  1719. mutexclpo : [po_exports,po_interrupt,po_external,po_inline]
  1720. ),(
  1721. idtok:_ALIAS;
  1722. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  1723. handler : @pd_alias;
  1724. pocall : pocall_none;
  1725. pooption : [];
  1726. mutexclpocall : [];
  1727. mutexclpotype : [];
  1728. mutexclpo : [po_external,po_inline]
  1729. ),(
  1730. idtok:_ASMNAME;
  1731. pd_flags : [pd_interface,pd_implemen,pd_notobjintf];
  1732. handler : @pd_asmname;
  1733. pocall : pocall_cdecl;
  1734. pooption : [po_external];
  1735. mutexclpocall : [pocall_internproc];
  1736. mutexclpotype : [];
  1737. mutexclpo : [po_external,po_inline]
  1738. ),(
  1739. idtok:_ASSEMBLER;
  1740. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1741. handler : nil;
  1742. pocall : pocall_none;
  1743. pooption : [po_assembler];
  1744. mutexclpocall : [];
  1745. mutexclpotype : [];
  1746. mutexclpo : [po_external]
  1747. ),(
  1748. idtok:_C; {same as cdecl for mode mac}
  1749. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1750. handler : nil;
  1751. pocall : pocall_cdecl;
  1752. pooption : [];
  1753. mutexclpocall : [];
  1754. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  1755. mutexclpo : [po_assembler,po_external]
  1756. ),(
  1757. idtok:_CDECL;
  1758. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1759. handler : nil;
  1760. pocall : pocall_cdecl;
  1761. pooption : [];
  1762. mutexclpocall : [];
  1763. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  1764. mutexclpo : [po_assembler,po_external]
  1765. ),(
  1766. idtok:_DISPID;
  1767. pd_flags : [pd_dispinterface];
  1768. handler : @pd_dispid;
  1769. pocall : pocall_none;
  1770. pooption : [po_dispid];
  1771. mutexclpocall : [pocall_internproc];
  1772. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  1773. mutexclpo : [po_interrupt,po_external,po_inline]
  1774. ),(
  1775. idtok:_DYNAMIC;
  1776. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1777. handler : @pd_virtual;
  1778. pocall : pocall_none;
  1779. pooption : [po_virtualmethod];
  1780. mutexclpocall : [pocall_internproc];
  1781. mutexclpotype : [potype_class_constructor,potype_class_destructor];
  1782. mutexclpo : [po_exports,po_interrupt,po_external,po_overridingmethod,po_inline]
  1783. ),(
  1784. idtok:_EXPORT;
  1785. pd_flags : [pd_body,pd_interface,pd_implemen,pd_notobjintf];
  1786. handler : @pd_export;
  1787. pocall : pocall_none;
  1788. pooption : [po_exports,po_global];
  1789. mutexclpocall : [pocall_internproc];
  1790. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  1791. mutexclpo : [po_external,po_interrupt,po_inline]
  1792. ),(
  1793. idtok:_EXTERNAL;
  1794. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject];
  1795. handler : @pd_external;
  1796. pocall : pocall_none;
  1797. pooption : [po_external];
  1798. mutexclpocall : [pocall_internproc,pocall_syscall];
  1799. { allowed for external cpp classes }
  1800. mutexclpotype : [{potype_constructor,potype_destructor}potype_class_constructor,potype_class_destructor];
  1801. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  1802. ),(
  1803. idtok:_FAR;
  1804. pd_flags : [pd_implemen,pd_body,pd_interface,pd_procvar,pd_notobject,pd_notobjintf];
  1805. handler : @pd_far;
  1806. pocall : pocall_none;
  1807. pooption : [];
  1808. mutexclpocall : [pocall_internproc];
  1809. mutexclpotype : [];
  1810. mutexclpo : [po_inline]
  1811. ),(
  1812. idtok:_FAR16;
  1813. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar,pd_notobject];
  1814. handler : nil;
  1815. pocall : pocall_far16;
  1816. pooption : [];
  1817. mutexclpocall : [];
  1818. mutexclpotype : [];
  1819. mutexclpo : [po_external]
  1820. ),(
  1821. idtok:_FINAL;
  1822. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1823. handler : @pd_final;
  1824. pocall : pocall_none;
  1825. pooption : [po_finalmethod];
  1826. mutexclpocall : [pocall_internproc];
  1827. mutexclpotype : [];
  1828. mutexclpo : [po_exports,po_interrupt,po_external,po_inline]
  1829. ),(
  1830. idtok:_FORWARD;
  1831. pd_flags : [pd_implemen,pd_notobject,pd_notobjintf];
  1832. handler : @pd_forward;
  1833. pocall : pocall_none;
  1834. pooption : [];
  1835. mutexclpocall : [pocall_internproc];
  1836. mutexclpotype : [];
  1837. mutexclpo : [po_external,po_inline]
  1838. ),(
  1839. idtok:_OLDFPCCALL;
  1840. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1841. handler : nil;
  1842. pocall : pocall_oldfpccall;
  1843. pooption : [];
  1844. mutexclpocall : [];
  1845. mutexclpotype : [];
  1846. mutexclpo : []
  1847. ),(
  1848. idtok:_INLINE;
  1849. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1850. handler : nil;
  1851. pocall : pocall_none;
  1852. pooption : [po_inline];
  1853. mutexclpocall : [];
  1854. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  1855. mutexclpo : [po_exports,po_external,po_interrupt,po_virtualmethod]
  1856. ),(
  1857. idtok:_INTERNCONST;
  1858. pd_flags : [pd_interface,pd_body,pd_notobject,pd_notobjintf];
  1859. handler : @pd_internconst;
  1860. pocall : pocall_none;
  1861. pooption : [po_internconst];
  1862. mutexclpocall : [];
  1863. mutexclpotype : [potype_operator];
  1864. mutexclpo : []
  1865. ),(
  1866. idtok:_INTERNPROC;
  1867. pd_flags : [pd_interface,pd_notobject,pd_notobjintf];
  1868. handler : @pd_internproc;
  1869. pocall : pocall_internproc;
  1870. pooption : [];
  1871. mutexclpocall : [];
  1872. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  1873. mutexclpo : [po_exports,po_external,po_interrupt,po_assembler,po_iocheck,po_virtualmethod]
  1874. ),(
  1875. idtok:_INTERRUPT;
  1876. pd_flags : [pd_implemen,pd_body,pd_notobject,pd_notobjintf];
  1877. handler : @pd_interrupt;
  1878. pocall : pocall_oldfpccall;
  1879. pooption : [po_interrupt];
  1880. mutexclpocall : [pocall_internproc,pocall_cdecl,pocall_cppdecl,pocall_stdcall,
  1881. pocall_pascal,pocall_far16,pocall_oldfpccall];
  1882. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  1883. mutexclpo : [po_external,po_inline]
  1884. ),(
  1885. idtok:_IOCHECK;
  1886. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  1887. handler : nil;
  1888. pocall : pocall_none;
  1889. pooption : [po_iocheck];
  1890. mutexclpocall : [pocall_internproc];
  1891. mutexclpotype : [];
  1892. mutexclpo : [po_external]
  1893. ),(
  1894. idtok:_LOCAL;
  1895. pd_flags : [pd_implemen,pd_body];
  1896. handler : nil;
  1897. pocall : pocall_none;
  1898. pooption : [po_kylixlocal];
  1899. mutexclpocall : [pocall_internproc,pocall_far16];
  1900. mutexclpotype : [];
  1901. mutexclpo : [po_external,po_exports]
  1902. ),(
  1903. idtok:_MESSAGE;
  1904. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass, pd_objcprot];
  1905. handler : @pd_message;
  1906. pocall : pocall_none;
  1907. pooption : []; { can be po_msgstr or po_msgint }
  1908. mutexclpocall : [pocall_internproc];
  1909. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  1910. mutexclpo : [po_interrupt,po_external,po_inline]
  1911. ),(
  1912. idtok:_MWPASCAL;
  1913. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1914. handler : nil;
  1915. pocall : pocall_mwpascal;
  1916. pooption : [];
  1917. mutexclpocall : [];
  1918. mutexclpotype : [];
  1919. mutexclpo : []
  1920. ),(
  1921. idtok:_NEAR;
  1922. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  1923. handler : @pd_near;
  1924. pocall : pocall_none;
  1925. pooption : [];
  1926. mutexclpocall : [pocall_internproc];
  1927. mutexclpotype : [];
  1928. mutexclpo : []
  1929. ),(
  1930. idtok:_NOSTACKFRAME;
  1931. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  1932. handler : nil;
  1933. pocall : pocall_none;
  1934. pooption : [po_nostackframe];
  1935. mutexclpocall : [pocall_internproc];
  1936. mutexclpotype : [];
  1937. mutexclpo : []
  1938. ),(
  1939. idtok:_OVERLOAD;
  1940. pd_flags : [pd_implemen,pd_interface,pd_body];
  1941. handler : @pd_overload;
  1942. pocall : pocall_none;
  1943. pooption : [po_overload];
  1944. mutexclpocall : [pocall_internproc];
  1945. mutexclpotype : [];
  1946. mutexclpo : []
  1947. ),(
  1948. idtok:_OVERRIDE;
  1949. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass];
  1950. handler : @pd_override;
  1951. pocall : pocall_none;
  1952. pooption : [po_overridingmethod,po_virtualmethod];
  1953. mutexclpocall : [pocall_internproc];
  1954. mutexclpotype : [];
  1955. mutexclpo : [po_exports,po_external,po_interrupt,po_virtualmethod,po_inline]
  1956. ),(
  1957. idtok:_PASCAL;
  1958. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1959. handler : nil;
  1960. pocall : pocall_pascal;
  1961. pooption : [];
  1962. mutexclpocall : [];
  1963. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  1964. mutexclpo : [po_external]
  1965. ),(
  1966. idtok:_PUBLIC;
  1967. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobject,pd_notobjintf];
  1968. handler : @pd_public;
  1969. pocall : pocall_none;
  1970. pooption : [po_public,po_global];
  1971. mutexclpocall : [pocall_internproc];
  1972. mutexclpotype : [];
  1973. mutexclpo : [po_external,po_inline]
  1974. ),(
  1975. idtok:_REGISTER;
  1976. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1977. handler : nil;
  1978. pocall : pocall_register;
  1979. pooption : [];
  1980. mutexclpocall : [];
  1981. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  1982. mutexclpo : [po_external]
  1983. ),(
  1984. idtok:_REINTRODUCE;
  1985. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass];
  1986. handler : @pd_reintroduce;
  1987. pocall : pocall_none;
  1988. pooption : [po_reintroduce];
  1989. mutexclpocall : [pocall_internproc];
  1990. mutexclpotype : [];
  1991. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod,po_inline]
  1992. ),(
  1993. idtok:_SAFECALL;
  1994. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1995. handler : nil;
  1996. pocall : pocall_safecall;
  1997. pooption : [];
  1998. mutexclpocall : [];
  1999. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2000. mutexclpo : [po_external]
  2001. ),(
  2002. idtok:_SOFTFLOAT;
  2003. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2004. handler : nil;
  2005. pocall : pocall_softfloat;
  2006. pooption : [];
  2007. mutexclpocall : [];
  2008. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2009. { it's available with po_external because the libgcc floating point routines on the arm
  2010. uses this calling convention }
  2011. mutexclpo : []
  2012. ),(
  2013. idtok:_STATIC;
  2014. pd_flags : [pd_interface,pd_implemen,pd_body,pd_object,pd_notobjintf];
  2015. handler : @pd_static;
  2016. pocall : pocall_none;
  2017. pooption : [po_staticmethod];
  2018. mutexclpocall : [pocall_internproc];
  2019. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2020. mutexclpo : [po_external,po_interrupt,po_exports]
  2021. ),(
  2022. idtok:_STDCALL;
  2023. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2024. handler : nil;
  2025. pocall : pocall_stdcall;
  2026. pooption : [];
  2027. mutexclpocall : [];
  2028. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2029. mutexclpo : [po_external]
  2030. ),(
  2031. idtok:_SYSCALL;
  2032. { Different kind of syscalls are valid for AOS68k, AOSPPC and MOS. }
  2033. { FIX ME!!! MorphOS/AOS68k pd_flags should be:
  2034. pd_interface, pd_implemen, pd_notobject, pd_notobjintf (KB) }
  2035. pd_flags : [pd_interface,pd_implemen,pd_procvar];
  2036. handler : @pd_syscall;
  2037. pocall : pocall_syscall;
  2038. pooption : [];
  2039. mutexclpocall : [];
  2040. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2041. mutexclpo : [po_external,po_assembler,po_interrupt,po_exports]
  2042. ),(
  2043. idtok:_VIRTUAL;
  2044. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  2045. handler : @pd_virtual;
  2046. pocall : pocall_none;
  2047. pooption : [po_virtualmethod];
  2048. mutexclpocall : [pocall_internproc];
  2049. mutexclpotype : [potype_class_constructor,potype_class_destructor];
  2050. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod,po_inline]
  2051. ),(
  2052. idtok:_CPPDECL;
  2053. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2054. handler : nil;
  2055. pocall : pocall_cppdecl;
  2056. pooption : [];
  2057. mutexclpocall : [];
  2058. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2059. mutexclpo : [po_assembler,po_external,po_virtualmethod]
  2060. ),(
  2061. idtok:_VARARGS;
  2062. pd_flags : [pd_interface,pd_implemen,pd_procvar,pd_objcclass, pd_objcprot];
  2063. handler : nil;
  2064. pocall : pocall_none;
  2065. pooption : [po_varargs];
  2066. mutexclpocall : [pocall_internproc,pocall_stdcall,pocall_register,
  2067. pocall_far16,pocall_oldfpccall,pocall_mwpascal];
  2068. mutexclpotype : [];
  2069. mutexclpo : [po_assembler,po_interrupt,po_inline]
  2070. ),(
  2071. idtok:_COMPILERPROC;
  2072. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2073. handler : nil;
  2074. pocall : pocall_none;
  2075. pooption : [po_compilerproc];
  2076. mutexclpocall : [];
  2077. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2078. mutexclpo : [po_interrupt]
  2079. ),(
  2080. idtok:_WEAKEXTERNAL;
  2081. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject];
  2082. handler : @pd_weakexternal;
  2083. pocall : pocall_none;
  2084. { mark it both external and weak external, so we don't have to
  2085. adapt all code for external symbols to also check for weak external
  2086. }
  2087. pooption : [po_external,po_weakexternal];
  2088. mutexclpocall : [pocall_internproc,pocall_syscall];
  2089. { allowed for external cpp classes }
  2090. mutexclpotype : [{potype_constructor,potype_destructor}potype_class_constructor,potype_class_destructor];
  2091. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  2092. ),(
  2093. idtok:_ENUMERATOR;
  2094. pd_flags : [pd_interface,pd_object];
  2095. handler : @pd_enumerator;
  2096. pocall : pocall_none;
  2097. pooption : [];
  2098. mutexclpocall : [pocall_internproc];
  2099. mutexclpotype : [];
  2100. mutexclpo : [po_exports,po_interrupt,po_external,po_inline]
  2101. )
  2102. );
  2103. function check_proc_directive(isprocvar:boolean):boolean;
  2104. var
  2105. i : longint;
  2106. begin
  2107. result:=false;
  2108. for i:=1 to num_proc_directives do
  2109. if proc_direcdata[i].idtok=idtoken then
  2110. begin
  2111. if ((not isprocvar) or
  2112. (pd_procvar in proc_direcdata[i].pd_flags)) and
  2113. { don't eat a public directive in classes }
  2114. not((idtoken=_PUBLIC) and (symtablestack.top.symtabletype=ObjectSymtable)) then
  2115. result:=true;
  2116. exit;
  2117. end;
  2118. end;
  2119. function parse_proc_direc(pd:tabstractprocdef;var pdflags:tpdflags):boolean;
  2120. {
  2121. Parse the procedure directive, returns true if a correct directive is found
  2122. }
  2123. var
  2124. p : longint;
  2125. found : boolean;
  2126. name : TIDString;
  2127. begin
  2128. parse_proc_direc:=false;
  2129. name:=tokeninfo^[idtoken].str;
  2130. found:=false;
  2131. { Hint directive? Then exit immediatly }
  2132. if (m_hintdirective in current_settings.modeswitches) then
  2133. begin
  2134. case idtoken of
  2135. _LIBRARY,
  2136. _PLATFORM,
  2137. _UNIMPLEMENTED,
  2138. _EXPERIMENTAL,
  2139. _DEPRECATED :
  2140. exit;
  2141. end;
  2142. end;
  2143. { C directive is MacPas only, because it breaks too much existing code
  2144. on other platforms (PFV) }
  2145. if (idtoken=_C) and
  2146. not(m_mac in current_settings.modeswitches) then
  2147. exit;
  2148. { retrieve data for directive if found }
  2149. for p:=1 to num_proc_directives do
  2150. if proc_direcdata[p].idtok=idtoken then
  2151. begin
  2152. found:=true;
  2153. break;
  2154. end;
  2155. { Check if the procedure directive is known }
  2156. if not found then
  2157. begin
  2158. { parsing a procvar type the name can be any
  2159. next variable !! }
  2160. if ((pdflags * [pd_procvar,pd_object,pd_objcclass,pd_objcprot])=[]) and
  2161. not(idtoken=_PROPERTY) then
  2162. Message1(parser_w_unknown_proc_directive_ignored,name);
  2163. exit;
  2164. end;
  2165. { check if method and directive not for object, like public.
  2166. This needs to be checked also for procvars }
  2167. if (pd_notobject in proc_direcdata[p].pd_flags) and
  2168. (symtablestack.top.symtabletype=ObjectSymtable) and
  2169. { directive allowed for cpp classes? }
  2170. not(is_cppclass(tdef(symtablestack.top.defowner)) and (pd_cppobject in proc_direcdata[p].pd_flags)) then
  2171. exit;
  2172. { Conflicts between directives ? }
  2173. if (pd.proctypeoption in proc_direcdata[p].mutexclpotype) or
  2174. (pd.proccalloption in proc_direcdata[p].mutexclpocall) or
  2175. ((pd.procoptions*proc_direcdata[p].mutexclpo)<>[]) then
  2176. begin
  2177. Message1(parser_e_proc_dir_conflict,name);
  2178. exit;
  2179. end;
  2180. { set calling convention }
  2181. if proc_direcdata[p].pocall<>pocall_none then
  2182. begin
  2183. if (po_hascallingconvention in pd.procoptions) then
  2184. begin
  2185. Message2(parser_w_proc_overriding_calling,
  2186. proccalloptionStr[pd.proccalloption],
  2187. proccalloptionStr[proc_direcdata[p].pocall]);
  2188. end;
  2189. { check if the target processor supports this calling convention }
  2190. if not(proc_direcdata[p].pocall in supported_calling_conventions) then
  2191. begin
  2192. Message1(parser_e_illegal_calling_convention,proccalloptionStr[proc_direcdata[p].pocall]);
  2193. { recover }
  2194. proc_direcdata[p].pocall:=pocall_stdcall;
  2195. end;
  2196. pd.proccalloption:=proc_direcdata[p].pocall;
  2197. include(pd.procoptions,po_hascallingconvention);
  2198. end;
  2199. if pd.typ=procdef then
  2200. begin
  2201. { Check if the directive is only for objects }
  2202. if (pd_object in proc_direcdata[p].pd_flags) and
  2203. not assigned(tprocdef(pd)._class) then
  2204. exit;
  2205. { check if method and directive not for interface }
  2206. if (pd_notobjintf in proc_direcdata[p].pd_flags) and
  2207. is_interface(tprocdef(pd)._class) then
  2208. exit;
  2209. { check if method and directive not for interface }
  2210. if is_dispinterface(tprocdef(pd)._class) and
  2211. not(pd_dispinterface in proc_direcdata[p].pd_flags) then
  2212. exit;
  2213. { check if method and directive not for objcclass }
  2214. if is_objcclass(tprocdef(pd)._class) and
  2215. not(pd_objcclass in proc_direcdata[p].pd_flags) then
  2216. exit;
  2217. { check if method and directive not for objcprotocol }
  2218. if is_objcprotocol(tprocdef(pd)._class) and
  2219. not(pd_objcprot in proc_direcdata[p].pd_flags) then
  2220. exit;
  2221. end;
  2222. { consume directive, and turn flag on }
  2223. consume(token);
  2224. parse_proc_direc:=true;
  2225. { Check the pd_flags if the directive should be allowed }
  2226. if (pd_interface in pdflags) and
  2227. not(pd_interface in proc_direcdata[p].pd_flags) then
  2228. begin
  2229. Message1(parser_e_proc_dir_not_allowed_in_interface,name);
  2230. exit;
  2231. end;
  2232. if (pd_implemen in pdflags) and
  2233. not(pd_implemen in proc_direcdata[p].pd_flags) then
  2234. begin
  2235. Message1(parser_e_proc_dir_not_allowed_in_implementation,name);
  2236. exit;
  2237. end;
  2238. if (pd_procvar in pdflags) and
  2239. not(pd_procvar in proc_direcdata[p].pd_flags) then
  2240. begin
  2241. Message1(parser_e_proc_dir_not_allowed_in_procvar,name);
  2242. exit;
  2243. end;
  2244. { Return the new pd_flags }
  2245. if not(pd_body in proc_direcdata[p].pd_flags) then
  2246. exclude(pdflags,pd_body);
  2247. { Add the correct flag }
  2248. pd.procoptions:=pd.procoptions+proc_direcdata[p].pooption;
  2249. { Call the handler }
  2250. if pointer(proc_direcdata[p].handler)<>nil then
  2251. proc_direcdata[p].handler(pd);
  2252. end;
  2253. function proc_get_importname(pd:tprocdef):string;
  2254. function maybe_cprefix(const s:string):string;
  2255. begin
  2256. if not(pd.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  2257. result:=s
  2258. else
  2259. result:=target_info.Cprefix+s;
  2260. end;
  2261. begin
  2262. result:='';
  2263. if not(po_external in pd.procoptions) then
  2264. internalerror(200412151);
  2265. { external name or number is specified }
  2266. if assigned(pd.import_name) or (pd.import_nr<>0) then
  2267. begin
  2268. if assigned(pd.import_dll) then
  2269. begin
  2270. { If we are not using direct dll linking under win32 then imports
  2271. need to use the normal name since two functions can refer to the
  2272. same DLL function. This is also needed for compatability
  2273. with Delphi and TP7 }
  2274. (*
  2275. case target_info.system of
  2276. system_i386_emx,
  2277. system_i386_os2 :
  2278. begin
  2279. { keep normal mangledname }
  2280. if not (Assigned (PD.Import_Name)) then
  2281. Result := PD.MangledName;
  2282. end;
  2283. else
  2284. *)
  2285. if assigned(pd.import_name) then
  2286. begin
  2287. if target_info.system in (systems_all_windows + systems_nativent +
  2288. [system_i386_emx, system_i386_os2]) then
  2289. { cprefix is not used in DLL imports under Windows or OS/2 }
  2290. result:=pd.import_name^
  2291. else
  2292. result:=maybe_cprefix(pd.import_name^);
  2293. end
  2294. else
  2295. result:=ExtractFileName(pd.import_dll^)+'_index_'+tostr(pd.import_nr);
  2296. end
  2297. else
  2298. result:=maybe_cprefix(pd.import_name^);
  2299. end
  2300. else
  2301. begin
  2302. { Default names when importing variables }
  2303. case pd.proccalloption of
  2304. pocall_cdecl :
  2305. begin
  2306. if assigned(pd._class) then
  2307. result:=target_info.Cprefix+pd._class.objrealname^+'_'+pd.procsym.realname
  2308. else
  2309. result:=target_info.Cprefix+pd.procsym.realname;
  2310. end;
  2311. pocall_cppdecl :
  2312. begin
  2313. result:=target_info.Cprefix+pd.cplusplusmangledname;
  2314. end;
  2315. else
  2316. begin
  2317. {In MacPas a single "external" has the same effect as "external name 'xxx'" }
  2318. { but according to MacPas mode description
  2319. Cprefix should still be used PM }
  2320. if (m_mac in current_settings.modeswitches) then
  2321. result:=target_info.Cprefix+tprocdef(pd).procsym.realname;
  2322. end;
  2323. end;
  2324. end;
  2325. end;
  2326. procedure proc_set_mangledname(pd:tprocdef);
  2327. var
  2328. s : string;
  2329. begin
  2330. { When the mangledname is already set we aren't allowed to change
  2331. it because it can already be used somewhere (PFV) }
  2332. if not(po_has_mangledname in pd.procoptions) then
  2333. begin
  2334. if (po_external in pd.procoptions) then
  2335. begin
  2336. { External Procedures are only allowed to change the mangledname
  2337. in their first declaration }
  2338. if (pd.forwarddef or (not pd.hasforward)) then
  2339. begin
  2340. s:=proc_get_importname(pd);
  2341. if s<>'' then
  2342. begin
  2343. { Replace ? and @ in import name, since GNU AS does not allow these characters in symbol names. }
  2344. { This allows to import VC++ mangled names from DLLs. }
  2345. { Do not perform replacement, if external symbol is not imported from DLL. }
  2346. if (target_info.system in systems_all_windows) and (pd.import_dll<>nil) then
  2347. begin
  2348. Replace(s,'?','__q$$');
  2349. {$ifdef arm}
  2350. { @ symbol is not allowed in ARM assembler only }
  2351. Replace(s,'@','__a$$');
  2352. {$endif arm}
  2353. end;
  2354. pd.setmangledname(s);
  2355. end;
  2356. end;
  2357. end
  2358. else
  2359. { Normal procedures }
  2360. begin
  2361. if (po_compilerproc in pd.procoptions) then
  2362. begin
  2363. pd.setmangledname(lower(pd.procsym.name));
  2364. end;
  2365. end;
  2366. end;
  2367. { Public/exported alias names }
  2368. if (([po_public,po_exports]*pd.procoptions)<>[]) and
  2369. not(po_has_public_name in pd.procoptions) then
  2370. begin
  2371. case pd.proccalloption of
  2372. pocall_cdecl :
  2373. begin
  2374. if assigned(pd._class) then
  2375. pd.aliasnames.insert(target_info.Cprefix+pd._class.objrealname^+'_'+pd.procsym.realname)
  2376. else
  2377. begin
  2378. { Export names are not mangled on Windows and OS/2, see also pexports.pas }
  2379. if (target_info.system in (systems_all_windows+[system_i386_emx, system_i386_os2])) and
  2380. (po_exports in pd.procoptions) then
  2381. pd.aliasnames.insert(pd.procsym.realname)
  2382. else
  2383. pd.aliasnames.insert(target_info.Cprefix+pd.procsym.realname);
  2384. end;
  2385. end;
  2386. pocall_cppdecl :
  2387. begin
  2388. pd.aliasnames.insert(target_info.Cprefix+pd.cplusplusmangledname);
  2389. end;
  2390. end;
  2391. { prevent adding the alias a second time }
  2392. include(pd.procoptions,po_has_public_name);
  2393. end;
  2394. end;
  2395. procedure handle_calling_convention(pd:tabstractprocdef);
  2396. begin
  2397. { set the default calling convention if none provided }
  2398. if (pd.typ=procdef) and
  2399. (is_objc_class_or_protocol(tprocdef(pd)._class) or
  2400. is_cppclass(tprocdef(pd)._class)) then
  2401. begin
  2402. { none of the explicit calling conventions should be allowed }
  2403. if (po_hascallingconvention in pd.procoptions) then
  2404. internalerror(2009032501);
  2405. if is_cppclass(tprocdef(pd)._class) then
  2406. pd.proccalloption:=pocall_cppdecl
  2407. else
  2408. pd.proccalloption:=pocall_cdecl;
  2409. end
  2410. else if not(po_hascallingconvention in pd.procoptions) then
  2411. pd.proccalloption:=current_settings.defproccall
  2412. else
  2413. begin
  2414. if pd.proccalloption=pocall_none then
  2415. internalerror(200309081);
  2416. end;
  2417. { handle proccall specific settings }
  2418. case pd.proccalloption of
  2419. pocall_cdecl,
  2420. pocall_cppdecl :
  2421. begin
  2422. { check C cdecl para types }
  2423. check_c_para(pd);
  2424. end;
  2425. pocall_far16 :
  2426. begin
  2427. { Temporary stub, must be rewritten to support OS/2 far16 }
  2428. Message1(parser_w_proc_directive_ignored,'FAR16');
  2429. end;
  2430. end;
  2431. { Inlining is enabled and supported? }
  2432. if (po_inline in pd.procoptions) and
  2433. not(cs_do_inline in current_settings.localswitches) then
  2434. begin
  2435. { Give an error if inline is not supported by the compiler mode,
  2436. otherwise only give a warning that this procedure will not be inlined }
  2437. if not(m_default_inline in current_settings.modeswitches) then
  2438. Message(parser_e_proc_inline_not_supported)
  2439. else
  2440. Message(parser_w_inlining_disabled);
  2441. exclude(pd.procoptions,po_inline);
  2442. end;
  2443. { For varargs directive also cdecl and external must be defined }
  2444. if (po_varargs in pd.procoptions) then
  2445. begin
  2446. { check first for external in the interface, if available there
  2447. then the cdecl must also be there since there is no implementation
  2448. available to contain it }
  2449. if parse_only then
  2450. begin
  2451. { if external is available, then cdecl must also be available,
  2452. procvars don't need external }
  2453. if not((po_external in pd.procoptions) or
  2454. (pd.typ=procvardef) or
  2455. { for objcclasses this is checked later, because the entire
  2456. class may be external. }
  2457. is_objc_class_or_protocol(tprocdef(pd)._class)) and
  2458. not(pd.proccalloption in (cdecl_pocalls + [pocall_mwpascal])) then
  2459. Message(parser_e_varargs_need_cdecl_and_external);
  2460. end
  2461. else
  2462. begin
  2463. { both must be defined now }
  2464. if not((po_external in pd.procoptions) or
  2465. (pd.typ=procvardef)) or
  2466. not(pd.proccalloption in (cdecl_pocalls + [pocall_mwpascal])) then
  2467. Message(parser_e_varargs_need_cdecl_and_external);
  2468. end;
  2469. end;
  2470. { insert hidden high parameters }
  2471. pd.parast.SymList.ForEachCall(@insert_hidden_para,pd);
  2472. { insert hidden self parameter }
  2473. insert_self_and_vmt_para(pd);
  2474. { insert funcret parameter if required }
  2475. insert_funcret_para(pd);
  2476. { Make var parameters regable, this must be done after the calling
  2477. convention is set. }
  2478. { this must be done before parentfp is insert, because getting all cases
  2479. where parentfp must be in a memory location isn't catched properly so
  2480. we put parentfp never in a register }
  2481. pd.parast.SymList.ForEachCall(@set_addr_param_regable,pd);
  2482. { insert parentfp parameter if required }
  2483. insert_parentfp_para(pd);
  2484. { Calculate parameter tlist }
  2485. pd.calcparas;
  2486. end;
  2487. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  2488. {
  2489. Parse the procedure directives. It does not matter if procedure directives
  2490. are written using ;procdir; or ['procdir'] syntax.
  2491. }
  2492. var
  2493. res : boolean;
  2494. begin
  2495. if (m_mac in current_settings.modeswitches) and (cs_externally_visible in current_settings.localswitches) then
  2496. begin
  2497. tprocdef(pd).aliasnames.insert(tprocdef(pd).procsym.realname);
  2498. include(pd.procoptions,po_public);
  2499. include(pd.procoptions,po_has_public_name);
  2500. include(pd.procoptions,po_global);
  2501. end;
  2502. { Class constructors and destructor are static class methods in real. }
  2503. { There are many places in the compiler where either class or static }
  2504. { method flag changes the behavior. It is simplier to add them to }
  2505. { the class constructors/destructors options than to fix all the }
  2506. { occurencies. (Paul) }
  2507. if pd.proctypeoption in [potype_class_constructor,potype_class_destructor] then
  2508. begin
  2509. include(pd.procoptions,po_classmethod);
  2510. include(pd.procoptions,po_staticmethod);
  2511. end;
  2512. while token in [_ID,_LECKKLAMMER] do
  2513. begin
  2514. if try_to_consume(_LECKKLAMMER) then
  2515. begin
  2516. repeat
  2517. parse_proc_direc(pd,pdflags);
  2518. until not try_to_consume(_COMMA);
  2519. consume(_RECKKLAMMER);
  2520. { we always expect at least '[];' }
  2521. res:=true;
  2522. end
  2523. else
  2524. begin
  2525. res:=parse_proc_direc(pd,pdflags);
  2526. end;
  2527. { A procedure directive normally followed by a semicolon, but in
  2528. a const section or reading a type we should stop when _EQUAL is found,
  2529. because a constant/default value follows }
  2530. if res then
  2531. begin
  2532. if (block_type=bt_const_type) and
  2533. (token=_EQUAL) then
  2534. break;
  2535. { support procedure proc;stdcall export; }
  2536. if not(check_proc_directive((pd.typ=procvardef))) then
  2537. begin
  2538. { support "record p : procedure stdcall end;" and
  2539. "var p : procedure stdcall = nil;" }
  2540. if (pd_procvar in pdflags) and
  2541. (token in [_END,_RKLAMMER,_EQUAL]) then
  2542. break
  2543. else
  2544. begin
  2545. if (token=_COLON) then
  2546. begin
  2547. Message(parser_e_field_not_allowed_here);
  2548. consume_all_until(_SEMICOLON);
  2549. end;
  2550. consume(_SEMICOLON)
  2551. end;
  2552. end;
  2553. end
  2554. else
  2555. break;
  2556. end;
  2557. end;
  2558. procedure parse_var_proc_directives(sym:tsym);
  2559. var
  2560. pdflags : tpdflags;
  2561. pd : tabstractprocdef;
  2562. begin
  2563. pdflags:=[pd_procvar];
  2564. pd:=nil;
  2565. case sym.typ of
  2566. fieldvarsym,
  2567. staticvarsym,
  2568. localvarsym,
  2569. paravarsym :
  2570. pd:=tabstractprocdef(tabstractvarsym(sym).vardef);
  2571. typesym :
  2572. pd:=tabstractprocdef(ttypesym(sym).typedef);
  2573. else
  2574. internalerror(2003042617);
  2575. end;
  2576. if pd.typ<>procvardef then
  2577. internalerror(2003042618);
  2578. { names should never be used anyway }
  2579. parse_proc_directives(pd,pdflags);
  2580. end;
  2581. procedure parse_object_proc_directives(pd:tabstractprocdef);
  2582. var
  2583. pdflags : tpdflags;
  2584. begin
  2585. pdflags:=[pd_object];
  2586. parse_proc_directives(pd,pdflags);
  2587. end;
  2588. function proc_add_definition(var currpd:tprocdef):boolean;
  2589. {
  2590. Add definition aprocdef to the overloaded definitions of aprocsym. If a
  2591. forwarddef is found and reused it returns true
  2592. }
  2593. var
  2594. fwpd : tprocdef;
  2595. currparasym,
  2596. fwparasym : tsym;
  2597. currparacnt,
  2598. fwparacnt,
  2599. curridx,
  2600. fwidx,
  2601. i : longint;
  2602. po_comp : tprocoptions;
  2603. paracompopt: tcompare_paras_options;
  2604. forwardfound : boolean;
  2605. symentry: TSymEntry;
  2606. begin
  2607. forwardfound:=false;
  2608. { check overloaded functions if the same function already exists }
  2609. for i:=0 to tprocsym(currpd.procsym).ProcdefList.Count-1 do
  2610. begin
  2611. fwpd:=tprocdef(tprocsym(currpd.procsym).ProcdefList[i]);
  2612. { Skip overloaded definitions that are declared in other units }
  2613. if fwpd.procsym<>currpd.procsym then
  2614. continue;
  2615. { check the parameters, for delphi/tp it is possible to
  2616. leave the parameters away in the implementation (forwarddef=false).
  2617. But for an overload declared function this is not allowed }
  2618. if { check if empty implementation arguments match is allowed }
  2619. (
  2620. not(m_repeat_forward in current_settings.modeswitches) and
  2621. not(currpd.forwarddef) and
  2622. is_bareprocdef(currpd) and
  2623. not(po_overload in fwpd.procoptions)
  2624. ) or
  2625. { check arguments, we need to check only the user visible parameters. The hidden parameters
  2626. can be in a different location because of the calling convention, eg. L-R vs. R-L order (PFV) }
  2627. (
  2628. (compare_paras(currpd.paras,fwpd.paras,cp_none,[cpo_comparedefaultvalue,cpo_ignorehidden,cpo_openequalisexact,cpo_ignoreuniv])=te_exact) and
  2629. (fwpd.returndef=currpd.returndef)
  2630. ) then
  2631. begin
  2632. { Check if we've found the forwarddef, if found then
  2633. we need to update the forward def with the current
  2634. implementation settings }
  2635. if fwpd.forwarddef then
  2636. begin
  2637. forwardfound:=true;
  2638. if not(m_repeat_forward in current_settings.modeswitches) and
  2639. (fwpd.proccalloption<>currpd.proccalloption) then
  2640. paracompopt:=[cpo_ignorehidden,cpo_comparedefaultvalue,cpo_openequalisexact,cpo_ignoreuniv]
  2641. else
  2642. paracompopt:=[cpo_comparedefaultvalue,cpo_openequalisexact,cpo_ignoreuniv];
  2643. { Check calling convention }
  2644. if (fwpd.proccalloption<>currpd.proccalloption) then
  2645. begin
  2646. { In delphi it is possible to specify the calling
  2647. convention in the interface or implementation if
  2648. there was no convention specified in the other
  2649. part }
  2650. if (m_delphi in current_settings.modeswitches) then
  2651. begin
  2652. if not(po_hascallingconvention in currpd.procoptions) then
  2653. currpd.proccalloption:=fwpd.proccalloption
  2654. else
  2655. if not(po_hascallingconvention in fwpd.procoptions) then
  2656. fwpd.proccalloption:=currpd.proccalloption
  2657. else
  2658. begin
  2659. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  2660. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2661. { restore interface settings }
  2662. currpd.proccalloption:=fwpd.proccalloption;
  2663. end;
  2664. end
  2665. else
  2666. begin
  2667. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  2668. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2669. { restore interface settings }
  2670. currpd.proccalloption:=fwpd.proccalloption;
  2671. end;
  2672. end;
  2673. { Check static }
  2674. if (po_staticmethod in fwpd.procoptions) then
  2675. begin
  2676. if not (po_staticmethod in currpd.procoptions) then
  2677. begin
  2678. include(currpd.procoptions, po_staticmethod);
  2679. if (po_classmethod in currpd.procoptions) then
  2680. begin
  2681. { remove self from the hidden paras }
  2682. symentry:=currpd.parast.Find('self');
  2683. if symentry<>nil then
  2684. begin
  2685. currpd.parast.Delete(symentry);
  2686. currpd.calcparas;
  2687. end;
  2688. end;
  2689. end;
  2690. end;
  2691. { Check if the procedure type and return type are correct,
  2692. also the parameters must match also with the type }
  2693. if ((m_repeat_forward in current_settings.modeswitches) or
  2694. not is_bareprocdef(currpd)) and
  2695. ((compare_paras(currpd.paras,fwpd.paras,cp_all,paracompopt)<>te_exact) or
  2696. (fwpd.returndef<>currpd.returndef)) then
  2697. begin
  2698. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  2699. fwpd.fullprocname(false));
  2700. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2701. break;
  2702. end;
  2703. { Check if both are declared forward }
  2704. if fwpd.forwarddef and currpd.forwarddef then
  2705. begin
  2706. MessagePos1(currpd.fileinfo,parser_e_function_already_declared_public_forward,
  2707. currpd.fullprocname(false));
  2708. end;
  2709. { internconst or internproc only need to be defined once }
  2710. if (fwpd.proccalloption=pocall_internproc) then
  2711. currpd.proccalloption:=fwpd.proccalloption
  2712. else
  2713. if (currpd.proccalloption=pocall_internproc) then
  2714. fwpd.proccalloption:=currpd.proccalloption;
  2715. { Check procedure options, Delphi requires that class is
  2716. repeated in the implementation for class methods }
  2717. if (m_fpc in current_settings.modeswitches) then
  2718. po_comp:=[po_classmethod,po_varargs,po_methodpointer,po_interrupt]
  2719. else
  2720. po_comp:=[po_classmethod,po_methodpointer];
  2721. if ((po_comp * fwpd.procoptions)<>(po_comp * currpd.procoptions)) or
  2722. (fwpd.proctypeoption <> currpd.proctypeoption) then
  2723. begin
  2724. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  2725. fwpd.fullprocname(false));
  2726. tprocsym(fwpd.procsym).write_parameter_lists(fwpd);
  2727. { This error is non-fatal, we can recover }
  2728. end;
  2729. { Forward declaration is external? }
  2730. if (po_external in fwpd.procoptions) then
  2731. MessagePos(currpd.fileinfo,parser_e_proc_already_external);
  2732. { Check parameters }
  2733. if (m_repeat_forward in current_settings.modeswitches) or
  2734. (currpd.minparacount>0) then
  2735. begin
  2736. { If mangled names are equal then they have the same amount of arguments }
  2737. { We can check the names of the arguments }
  2738. { both symtables are in the same order from left to right }
  2739. curridx:=0;
  2740. fwidx:=0;
  2741. currparacnt:=currpd.parast.SymList.Count;
  2742. fwparacnt:=fwpd.parast.SymList.Count;
  2743. repeat
  2744. { skip default parameter constsyms }
  2745. while (curridx<currparacnt) and
  2746. (tsym(currpd.parast.SymList[curridx]).typ<>paravarsym) do
  2747. inc(curridx);
  2748. while (fwidx<fwparacnt) and
  2749. (tsym(fwpd.parast.SymList[fwidx]).typ<>paravarsym) do
  2750. inc(fwidx);
  2751. { stop when one of the two lists is at the end }
  2752. if (fwidx>=fwparacnt) or (curridx>=currparacnt) then
  2753. break;
  2754. { compare names of parameters, ignore implictly
  2755. renamed parameters }
  2756. currparasym:=tsym(currpd.parast.SymList[curridx]);
  2757. fwparasym:=tsym(fwpd.parast.SymList[fwidx]);
  2758. if not(sp_implicitrename in currparasym.symoptions) and
  2759. not(sp_implicitrename in fwparasym.symoptions) then
  2760. begin
  2761. if (currparasym.name<>fwparasym.name) then
  2762. begin
  2763. MessagePos3(currpd.fileinfo,parser_e_header_different_var_names,
  2764. tprocsym(currpd.procsym).realname,fwparasym.realname,currparasym.realname);
  2765. break;
  2766. end;
  2767. end;
  2768. { next parameter }
  2769. inc(curridx);
  2770. inc(fwidx);
  2771. until false;
  2772. end;
  2773. { Everything is checked, now we can update the forward declaration
  2774. with the new data from the implementation }
  2775. fwpd.forwarddef:=currpd.forwarddef;
  2776. fwpd.hasforward:=true;
  2777. fwpd.procoptions:=fwpd.procoptions+currpd.procoptions;
  2778. { marked as local but exported from unit? }
  2779. if (po_kylixlocal in fwpd.procoptions) and (fwpd.owner.symtabletype=globalsymtable) then
  2780. MessagePos(fwpd.fileinfo,type_e_cant_export_local);
  2781. if fwpd.extnumber=$ffff then
  2782. fwpd.extnumber:=currpd.extnumber;
  2783. while not currpd.aliasnames.empty do
  2784. fwpd.aliasnames.insert(currpd.aliasnames.getfirst);
  2785. { update fileinfo so position references the implementation,
  2786. also update funcretsym if it is already generated }
  2787. fwpd.fileinfo:=currpd.fileinfo;
  2788. if assigned(fwpd.funcretsym) then
  2789. fwpd.funcretsym.fileinfo:=currpd.fileinfo;
  2790. if assigned(currpd.deprecatedmsg) then
  2791. begin
  2792. stringdispose(fwpd.deprecatedmsg);
  2793. fwpd.deprecatedmsg:=stringdup(currpd.deprecatedmsg^);
  2794. end;
  2795. { import names }
  2796. if assigned(currpd.import_dll) then
  2797. begin
  2798. stringdispose(fwpd.import_dll);
  2799. fwpd.import_dll:=stringdup(currpd.import_dll^);
  2800. end;
  2801. if assigned(currpd.import_name) then
  2802. begin
  2803. stringdispose(fwpd.import_name);
  2804. fwpd.import_name:=stringdup(currpd.import_name^);
  2805. end;
  2806. fwpd.import_nr:=currpd.import_nr;
  2807. { for compilerproc defines we need to rename and update the
  2808. symbolname to lowercase }
  2809. if (po_compilerproc in fwpd.procoptions) then
  2810. begin
  2811. { rename to lowercase so users can't access it }
  2812. fwpd.procsym.realname:='$'+lower(fwpd.procsym.name);
  2813. { the mangeled name is already changed by the pd_compilerproc }
  2814. { handler. It must be done immediately because if we have a }
  2815. { call to a compilerproc before it's implementation is }
  2816. { encountered, it must already use the new mangled name (JM) }
  2817. end;
  2818. { Release current procdef }
  2819. currpd.owner.deletedef(currpd);
  2820. currpd:=fwpd;
  2821. end
  2822. else
  2823. begin
  2824. { abstract methods aren't forward defined, but this }
  2825. { needs another error message }
  2826. if (po_abstractmethod in fwpd.procoptions) then
  2827. MessagePos(currpd.fileinfo,parser_e_abstract_no_definition)
  2828. else
  2829. begin
  2830. MessagePos(currpd.fileinfo,parser_e_overloaded_have_same_parameters);
  2831. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2832. end;
  2833. end;
  2834. { we found one proc with the same arguments, there are no others
  2835. so we can stop }
  2836. break;
  2837. end;
  2838. { check for allowing overload directive }
  2839. if not(m_fpc in current_settings.modeswitches) then
  2840. begin
  2841. { overload directive turns on overloading }
  2842. if ((po_overload in currpd.procoptions) or
  2843. (po_overload in fwpd.procoptions)) then
  2844. begin
  2845. { check if all procs have overloading, but not if the proc is a method or
  2846. already declared forward, then the check is already done }
  2847. if not(fwpd.hasforward or
  2848. assigned(currpd._class) or
  2849. (currpd.forwarddef<>fwpd.forwarddef) or
  2850. ((po_overload in currpd.procoptions) and
  2851. (po_overload in fwpd.procoptions))) then
  2852. begin
  2853. MessagePos1(currpd.fileinfo,parser_e_no_overload_for_all_procs,currpd.procsym.realname);
  2854. break;
  2855. end;
  2856. end
  2857. else
  2858. begin
  2859. if not(fwpd.forwarddef) then
  2860. begin
  2861. if (m_tp7 in current_settings.modeswitches) then
  2862. MessagePos(currpd.fileinfo,parser_e_procedure_overloading_is_off)
  2863. else
  2864. MessagePos1(currpd.fileinfo,parser_e_no_overload_for_all_procs,currpd.procsym.realname);
  2865. break;
  2866. end;
  2867. end;
  2868. end; { equal arguments }
  2869. end;
  2870. { if we didn't reuse a forwarddef then we add the procdef to the overloaded
  2871. list }
  2872. if not forwardfound then
  2873. begin
  2874. { can happen in Delphi mode }
  2875. if (currpd.proctypeoption = potype_function) and
  2876. is_void(currpd.returndef) then
  2877. MessagePos1(currpd.fileinfo,parser_e_no_funcret_specified,currpd.procsym.realname);
  2878. tprocsym(currpd.procsym).ProcdefList.Add(currpd);
  2879. end;
  2880. proc_add_definition:=forwardfound;
  2881. end;
  2882. end.