pdecsub.pas 126 KB

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