pdecsub.pas 128 KB

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