pdecsub.pas 128 KB

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