pdecsub.pas 130 KB

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