pdecsub.pas 127 KB

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