pdecsub.pas 142 KB

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