pdecsub.pas 128 KB

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