aboutsummaryrefslogtreecommitdiff
path: root/paludis/elike_use_requirement.cc
blob: 1d2c30509e0d2b9f43564a54c93e18a2c2e25b1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2008, 2009, 2010, 2011 Ciaran McCreesh
 *
 * This file is part of the Paludis package manager. Paludis is free software;
 * you can redistribute it and/or modify it under the terms of the GNU General
 * Public License version 2, as published by the Free Software Foundation.
 *
 * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <paludis/elike_use_requirement.hh>
#include <paludis/util/options.hh>
#include <paludis/util/stringify.hh>
#include <paludis/util/join.hh>
#include <paludis/util/tokeniser.hh>
#include <paludis/util/log.hh>
#include <paludis/util/wrapped_forward_iterator.hh>
#include <paludis/util/tribool.hh>
#include <paludis/dep_spec.hh>
#include <paludis/name.hh>
#include <paludis/environment.hh>
#include <paludis/package_id.hh>
#include <paludis/metadata_key.hh>
#include <paludis/choice.hh>
#include <paludis/changed_choices.hh>
#include <paludis/additional_package_dep_spec_requirement.hh>
#include <vector>
#include <functional>
#include <algorithm>

using namespace paludis;

namespace
{
    bool icky_use_query(
            const ELikeUseRequirementOptions & options,
            const ChoiceNameWithPrefix & f,
            const std::shared_ptr<const PackageID> & id,
            const ChangedChoices * const changed_choices,
            Tribool default_value = Tribool(indeterminate))
    {
        if (! id->choices_key())
        {
            Log::get_instance()->message("elike_use_requirement.query", ll_warning, lc_context) <<
                "ID '" << *id << "' has no choices, so couldn't get the state of flag '" << f << "'";
            return false;
        }

        if (changed_choices)
        {
            auto c(changed_choices->overridden_value(f));
            if (! c.is_indeterminate())
                return c.is_true();
        }

        auto choices(id->choices_key()->parse_value());
        auto v(choices->find_by_name_with_prefix(f));
        if (v)
        {
            if (co_special == v->origin())
                Log::get_instance()->message("elike_use_requirement.query", ll_warning, lc_context) <<
                    "ID '" << *id << "' flag '" << f << "' should not be used as a conditional";
            return v->enabled();
        }

        if (default_value.is_indeterminate() && ! choices->has_matching_contains_every_value_prefix(f) && options[euro_missing_is_qa])
            Log::get_instance()->message("elike_use_requirement.query", ll_qa, lc_context) <<
                "ID '" << *id << "' has no flag named '" << f << "'";
        return default_value.is_true();
    }

    class UseRequirement
    {
        private:
            const std::string _flags;
            const Tribool _default_value;
            const bool _ignore_if_no_such_group;

        protected:
            const ELikeUseRequirementOptions _options;

        public:
            UseRequirement(const std::string & n,
                    const ELikeUseRequirementOptions & o, Tribool d, const bool g) :
                _flags(n),
                _default_value(d),
                _ignore_if_no_such_group(g),
                _options(o)
            {
            }

            virtual ~UseRequirement()
            {
            }

            virtual bool one_requirement_met_base(
                    const Environment * const,
                    const ChoiceNameWithPrefix &,
                    const ChangedChoices * const,
                    const std::shared_ptr<const PackageID> &,
                    const std::shared_ptr<const PackageID> &,
                    const ChangedChoices * const) const PALUDIS_ATTRIBUTE((warn_unused_result)) = 0;

            bool one_requirement_met(
                    const Environment * const env,
                    const ChoiceNameWithPrefix & c,
                    const ChangedChoices * const maybe_changes_to_owner,
                    const std::shared_ptr<const PackageID> & id,
                    const std::shared_ptr<const PackageID> & from_id,
                    const ChangedChoices * const maybe_changes_to_target) const PALUDIS_ATTRIBUTE((warn_unused_result))
            {
                if (_ignore_if_no_such_group)
                {
                    std::string::size_type p(_flags.find(':'));
                    ChoicePrefixName prefix((std::string::npos == p) ? "" : _flags.substr(0, p));
                    if (! id->choices_key())
                        return true;
                    auto choices(id->choices_key()->parse_value());
                    Choices::ConstIterator k(choices->find(prefix));
                    if (choices->end() == k)
                        return true;
                    if ((*k)->begin() == (*k)->end())
                        return true;
                }

                return one_requirement_met_base(env, c, maybe_changes_to_owner, id, from_id, maybe_changes_to_target);
            }

            virtual const std::string as_human_string(
                    const std::shared_ptr<const PackageID> & from_id) const PALUDIS_ATTRIBUTE((warn_unused_result)) = 0;

            const std::string flags() const PALUDIS_ATTRIBUTE((warn_unused_result))
            {
                return _flags;
            }

            const std::pair<bool, std::string> requirement_met(
                    const Environment * const env,
                    const ChangedChoices * const maybe_changes_to_owner,
                    const std::shared_ptr<const PackageID> & id,
                    const std::shared_ptr<const PackageID> & from_id,
                    const ChangedChoices * const maybe_changes_to_target) const
            {
                if (_flags.length() >= 2 && ":*" == _flags.substr(_flags.length() - 2))
                {
                    if ((! _options[euro_allow_self_deps]) || (! from_id) || (! from_id->choices_key()))
                        throw ELikeUseRequirementError(_flags, "[prefix:*] not allowed here");
                    if (_options[euro_portage_syntax] && ! _options[euro_both_syntaxes])
                    {
                        if (_options[euro_strict_parsing])
                            throw ELikeUseRequirementError(_flags, "[prefix:*] not allowed here");
                        else
                            Log::get_instance()->message("elike_use_requirement.prefix_star_not_allowed", ll_warning, lc_context)
                                << "[prefix:*] not safe for use here";
                    }

                    ChoicePrefixName prefix(_flags.substr(0, _flags.length() - 2));
                    auto choices(from_id->choices_key()->parse_value());
                    Choices::ConstIterator cc(choices->find(prefix));
                    if (cc == choices->end())
                        Log::get_instance()->message("elike_use_requirement.prefix_star_unmatching", ll_warning, lc_context) <<
                            "ID '" << *from_id << "' uses requirement '" << _flags << "' but has no choice prefix '" << prefix << "'";
                    else
                    {
                        std::pair<bool, std::string> result(true, "");
                        for (Choice::ConstIterator v((*cc)->begin()), v_end((*cc)->end()) ;
                                v != v_end ; ++v)
                            if (! one_requirement_met(env, (*v)->name_with_prefix(), maybe_changes_to_owner, id, from_id, maybe_changes_to_target))
                            {
                                if (! result.first)
                                    result.second.append(", ");
                                result.second.append(stringify((*v)->name_with_prefix()));
                                result.first = false;
                            }

                        if (! result.first)
                        {
                            result.second = as_human_string(from_id) + " (unmet: " + result.second + ")";
                            return result;
                        }
                    }
                }
                else
                    if (! one_requirement_met(env, ChoiceNameWithPrefix(_flags), maybe_changes_to_owner, id, from_id, maybe_changes_to_target))
                        return std::make_pair(false, as_human_string(from_id));

                return std::make_pair(true, as_human_string(from_id));
            }

            const Tribool default_value() const PALUDIS_ATTRIBUTE((warn_unused_result))
            {
                return _default_value;
            }

            const std::string default_value_human_string_fragment() const PALUDIS_ATTRIBUTE((warn_unused_result))
            {
                std::string result;
                if (_default_value.is_true())
                    result = ", assuming enabled if missing";
                else if (_default_value.is_false())
                    result = ", assuming disabled if missing";

                if (_ignore_if_no_such_group)
                    result.append(", only if the target has any flags with this prefix");

                return result;
            }

            Tribool accumulate_changes_to_make_met(
                    const Environment * const env,
                    const ChangedChoices * const maybe_changes_to_owner,
                    const std::shared_ptr<const PackageID> & id,
                    const std::shared_ptr<const PackageID> & from_id,
                    ChangedChoices & changed_choices) const
            {
                if (requirement_met(env, maybe_changes_to_owner, id, from_id, &changed_choices).first)
                    return indeterminate;

                if (_flags.length() >= 2 && ":*" == _flags.substr(_flags.length() - 2))
                {
                    if ((! _options[euro_allow_self_deps]) || (! from_id) || (! from_id->choices_key()))
                        throw ELikeUseRequirementError(_flags, "[prefix:*] not allowed here");
                    if (_options[euro_portage_syntax] && ! _options[euro_both_syntaxes])
                    {
                        if (_options[euro_strict_parsing])
                            throw ELikeUseRequirementError(_flags, "[prefix:*] not allowed here");
                        else
                            Log::get_instance()->message("elike_use_requirement.prefix_star_not_allowed", ll_warning, lc_context)
                                << "[prefix:*] not safe for use here";
                    }

                    ChoicePrefixName prefix(_flags.substr(0, _flags.length() - 2));
                    auto choices(from_id->choices_key()->parse_value());
                    Choices::ConstIterator cc(choices->find(prefix));
                    if (cc == choices->end())
                        Log::get_instance()->message("elike_use_requirement.prefix_star_unmatching", ll_warning, lc_context) <<
                            "ID '" << *from_id << "' uses requirement '" << _flags << "' but has no choice prefix '" << prefix << "'";
                    else
                    {
                        for (Choice::ConstIterator v((*cc)->begin()), v_end((*cc)->end()) ;
                                v != v_end ; ++v)
                            if (! one_accumulate_changes_to_make_met(env, maybe_changes_to_owner, id, changed_choices, (*v)->name_with_prefix()))
                                return false;
                    }
                }
                else
                    if (! one_accumulate_changes_to_make_met(env, maybe_changes_to_owner, id, changed_choices, ChoiceNameWithPrefix(_flags)))
                        return false;

                return true;
            }

            bool one_accumulate_changes_to_make_met(
                    const Environment * const,
                    const ChangedChoices * const,
                    const std::shared_ptr<const PackageID> & id,
                    ChangedChoices & changed_choices,
                    const ChoiceNameWithPrefix & flag) const
            {
                const std::shared_ptr<const ChoiceValue> v(id->choices_key()->parse_value()->find_by_name_with_prefix(flag));

                if (! v)
                {
                    /* warning messages get done elsewhere, no need here */
                    return false;
                }

                if (v->locked() || co_explicit != v->origin())
                    return false;

                return changed_choices.add_override_if_possible(flag, ! v->enabled());;
            }
    };

    class PALUDIS_VISIBLE EnabledUseRequirement :
        public UseRequirement
    {
        public:
            EnabledUseRequirement(const std::string & n,
                    const ELikeUseRequirementOptions & o,
                    Tribool d, const bool b) :
                UseRequirement(n, o, d, b)
            {
            }

            virtual bool one_requirement_met_base(const Environment * const, const ChoiceNameWithPrefix & flag, const ChangedChoices * const,
                    const std::shared_ptr<const PackageID> & pkg, const std::shared_ptr<const PackageID> &, const ChangedChoices * const changed_choices) const
            {
                return icky_use_query(_options, flag, pkg, changed_choices, default_value());
            }

            virtual const std::string as_human_string(
                    const std::shared_ptr<const PackageID> &) const
            {
                return "Flag '" + stringify(flags()) + "' enabled" + default_value_human_string_fragment();
            }
    };

    class PALUDIS_VISIBLE DisabledUseRequirement :
        public UseRequirement
    {
        public:
            DisabledUseRequirement(const std::string & n,
                    const ELikeUseRequirementOptions & o,
                    Tribool d, const bool b) :
                UseRequirement(n, o, d, b)
            {
            }

            virtual bool one_requirement_met_base(const Environment * const, const ChoiceNameWithPrefix & flag, const ChangedChoices * const,
                    const std::shared_ptr<const PackageID> & pkg, const std::shared_ptr<const PackageID> &, const ChangedChoices * const changed_choices) const
            {
                return ! icky_use_query(_options, flag, pkg, changed_choices, default_value());
            }

            virtual const std::string as_human_string(
                    const std::shared_ptr<const PackageID> &) const
            {
                return "Flag '" + stringify(flags()) + "' disabled" + default_value_human_string_fragment();
            }
    };

    class PALUDIS_VISIBLE ConditionalUseRequirement :
        public UseRequirement
    {
        public:
            ConditionalUseRequirement(const std::string & n,
                    const ELikeUseRequirementOptions & o,
                    Tribool d, const bool b) :
                UseRequirement(n, o, d, b)
            {
            }
    };

    class PALUDIS_VISIBLE IfMineThenUseRequirement :
        public ConditionalUseRequirement
    {
        public:
            IfMineThenUseRequirement(
                    const std::string & n,
                    const ELikeUseRequirementOptions & o,
                    Tribool d, const bool b) :
                ConditionalUseRequirement(n, o, d, b)
            {
            }

            virtual bool one_requirement_met_base(const Environment * const, const ChoiceNameWithPrefix & flag,
                    const ChangedChoices * const maybe_changes_to_owner, const std::shared_ptr<const PackageID> & pkg,
                    const std::shared_ptr<const PackageID> & from_id, const ChangedChoices * const changed_choices) const
            {
                return ! icky_use_query(_options, flag, from_id, maybe_changes_to_owner) ||
                    icky_use_query(_options, flag, pkg, changed_choices, default_value());
            }

            virtual const std::string as_human_string(
                    const std::shared_ptr<const PackageID> & from_id) const
            {
                return "Flag '" + stringify(flags()) + "' enabled if it is enabled for '"
                    + stringify(*from_id) + "'" + default_value_human_string_fragment();
            }
    };

    class PALUDIS_VISIBLE IfNotMineThenUseRequirement :
        public ConditionalUseRequirement
    {
        public:
            IfNotMineThenUseRequirement(const std::string & n,
                    const ELikeUseRequirementOptions & o,
                    Tribool d, const bool b) :
                ConditionalUseRequirement(n, o, d, b)
            {
            }

            virtual bool one_requirement_met_base(const Environment * const, const ChoiceNameWithPrefix & flag,
                    const ChangedChoices * const maybe_changes_to_owner,
                    const std::shared_ptr<const PackageID> & pkg, const std::shared_ptr<const PackageID> & from_id,
                    const ChangedChoices * const changed_choices) const
            {
                return icky_use_query(_options, flag, from_id, maybe_changes_to_owner) ||
                    icky_use_query(_options, flag, pkg, changed_choices, default_value());
            }

            virtual const std::string as_human_string(
                    const std::shared_ptr<const PackageID> & from_id) const
            {
                return "Flag '" + stringify(flags()) + "' enabled if it is disabled for '" +
                    stringify(*from_id) + "'" + default_value_human_string_fragment();
            }
    };

    class PALUDIS_VISIBLE IfMineThenNotUseRequirement :
        public ConditionalUseRequirement
    {
        public:
            IfMineThenNotUseRequirement(const std::string & n,
                    const ELikeUseRequirementOptions & o,
                    Tribool d, const bool b) :
                ConditionalUseRequirement(n, o, d, b)
            {
            }

            virtual bool one_requirement_met_base(const Environment * const, const ChoiceNameWithPrefix & flag,
                    const ChangedChoices * const maybe_changes_to_owner, const std::shared_ptr<const PackageID> & pkg,
                    const std::shared_ptr<const PackageID> & from_id, const ChangedChoices * const changed_choices) const
            {
                return ! icky_use_query(_options, flag, from_id, maybe_changes_to_owner) ||
                    ! icky_use_query(_options, flag, pkg, changed_choices, default_value());
            }

            virtual const std::string as_human_string(
                    const std::shared_ptr<const PackageID> & from_id) const
            {
                return "Flag '" + stringify(flags()) + "' disabled if it is enabled for '" +
                    stringify(*from_id) + "'" + default_value_human_string_fragment();
            }
    };

    class PALUDIS_VISIBLE IfNotMineThenNotUseRequirement :
        public ConditionalUseRequirement
    {
        public:
            IfNotMineThenNotUseRequirement(const std::string & n,
                    const ELikeUseRequirementOptions & o,
                    Tribool d, const bool b) :
                ConditionalUseRequirement(n, o, d, b)
            {
            }

            virtual bool one_requirement_met_base(const Environment * const, const ChoiceNameWithPrefix & flag,
                    const ChangedChoices * const maybe_changes_to_owner, const std::shared_ptr<const PackageID> & pkg,
                    const std::shared_ptr<const PackageID> & from_id, const ChangedChoices * const changed_choices) const
            {
                return icky_use_query(_options, flag, from_id, maybe_changes_to_owner) ||
                    ! icky_use_query(_options, flag, pkg, changed_choices, default_value());
            }

            virtual const std::string as_human_string(
                    const std::shared_ptr<const PackageID> & from_id) const
            {
                return "Flag '" + stringify(flags()) + "' disabled if it is disabled for '" +
                    stringify(*from_id) + "'" + default_value_human_string_fragment();
            }
    };

    class PALUDIS_VISIBLE EqualUseRequirement :
        public ConditionalUseRequirement
    {
        public:
            EqualUseRequirement(const std::string & n,
                    const ELikeUseRequirementOptions & o,
                    Tribool d, const bool b) :
                ConditionalUseRequirement(n, o, d, b)
            {
            }

            virtual bool one_requirement_met_base(const Environment * const, const ChoiceNameWithPrefix & flag,
                    const ChangedChoices * const maybe_changes_to_owner, const std::shared_ptr<const PackageID> & pkg,
                    const std::shared_ptr<const PackageID> & from_id, const ChangedChoices * const changed_choices) const
            {
                return icky_use_query(_options, flag, pkg, changed_choices, default_value()) ==
                    icky_use_query(_options, flag, from_id, maybe_changes_to_owner);
            }

            virtual const std::string as_human_string(
                    const std::shared_ptr<const PackageID> & from_id) const
            {
                return "Flag '" + stringify(flags()) + "' enabled or disabled like it is for '"
                    + stringify(*from_id) + "'" + default_value_human_string_fragment();
            }
    };

    class PALUDIS_VISIBLE NotEqualUseRequirement :
        public ConditionalUseRequirement
    {
        public:
            NotEqualUseRequirement(const std::string & n,
                    const ELikeUseRequirementOptions & o,
                    Tribool d, const bool b) :
                ConditionalUseRequirement(n, o, d, b)
            {
            }

            virtual bool one_requirement_met_base(const Environment * const, const ChoiceNameWithPrefix & flag,
                    const ChangedChoices * const maybe_changes_to_owner, const std::shared_ptr<const PackageID> & pkg,
                    const std::shared_ptr<const PackageID> & from_id, const ChangedChoices * const changed_choices) const
            {
                return icky_use_query(_options, flag, pkg, changed_choices, default_value()) !=
                    icky_use_query(_options, flag, from_id, maybe_changes_to_owner);
            }

            virtual const std::string as_human_string(
                    const std::shared_ptr<const PackageID> & from_id) const
            {
                return "Flag '" + stringify(flags()) + "' enabled or disabled opposite to how it is for '"
                    + stringify(*from_id) + "'" + default_value_human_string_fragment();
            }
    };

    class UseRequirements :
        public AdditionalPackageDepSpecRequirement
    {
        private:
            typedef std::vector<std::shared_ptr<const UseRequirement> > Reqs;

            std::string _raw;
            Reqs _reqs;

        public:
            UseRequirements(const std::string & r) :
                _raw(r)
            {
            }

            virtual const std::pair<bool, std::string> requirement_met(
                    const Environment * const env,
                    const ChangedChoices * const maybe_changes_to_owner,
                    const std::shared_ptr<const PackageID> & id,
                    const std::shared_ptr<const PackageID> & from_id,
                    const ChangedChoices * const maybe_changes_to_target) const
            {
                using namespace std::placeholders;

                std::pair<bool, std::string> result(true, "");
                for (Reqs::const_iterator r(_reqs.begin()), r_end(_reqs.end()) ;
                        r != r_end ; ++r)
                {
                    std::pair<bool, std::string> r_result((*r)->requirement_met(env, maybe_changes_to_owner, id, from_id, maybe_changes_to_target));
                    if (! r_result.first)
                    {
                        if (! result.first)
                            result.second.append("; ");
                        result.second.append(r_result.second);
                        result.first = false;
                    }
                }

                return result;
            }

            virtual const std::string as_human_string(
                    const std::shared_ptr<const PackageID> & from_id) const
            {
                return join(_reqs.begin(), _reqs.end(), "; ", std::bind(std::mem_fn(&UseRequirement::as_human_string),
                            std::placeholders::_1, from_id));
            }

            virtual const std::string as_raw_string() const
            {
                return _raw;
            }

            void add_requirement(const std::shared_ptr<const UseRequirement> & req)
            {
                _reqs.push_back(req);
            }

            virtual Tribool accumulate_changes_to_make_met(
                    const Environment * const env,
                    const ChangedChoices * const maybe_changes_to_owner,
                    const std::shared_ptr<const PackageID> & id,
                    const std::shared_ptr<const PackageID> & from_id,
                    ChangedChoices & changed_choices) const
            {
                Tribool result(indeterminate);
                for (auto r(_reqs.begin()), r_end(_reqs.end()) ;
                        r != r_end ; ++r)
                {
                    auto b((*r)->accumulate_changes_to_make_met(env, maybe_changes_to_owner, id, from_id, changed_choices));
                    if (b.is_false())
                        return false;
                    else if (b.is_true())
                        result = true;
                }

                return result;
            }
    };

    template <typename T_>
    std::shared_ptr<const UseRequirement>
    make_requirement(const std::string & n, const ELikeUseRequirementOptions & o, Tribool d, const bool b)
    {
        return std::make_shared<T_>(n, o, d, b);
    }

    typedef std::shared_ptr<const UseRequirement> (* Factory)(
            const std::string &, const ELikeUseRequirementOptions &, Tribool, bool);

    void
    parse_flag(
            const std::shared_ptr<UseRequirements> & result,
            const Factory & factory,
            const std::string & c,
            Tribool d,
            const bool i,
            const ELikeUseRequirementOptions & options)
    {
        result->add_requirement(factory(c, options, d, i));
    }

    void
    parse_one_use_requirement(
            const std::shared_ptr<UseRequirements> & result,
            const std::string & s, std::string & flag,
            const ELikeUseRequirementOptions & options)
    {
        Factory factory;

        if (flag.empty())
            throw ELikeUseRequirementError(s, "Invalid [] contents");

        if ('=' == flag.at(flag.length() - 1))
        {
            if ((! options[euro_allow_self_deps]))
                throw ELikeUseRequirementError(s, "Cannot use [use=] here");

            flag.erase(flag.length() - 1);
            if (flag.empty())
                throw ELikeUseRequirementError(s, "Invalid [] contents");

            if ('!' == flag.at(flag.length() - 1))
            {
                if (options[euro_portage_syntax] && ! options[euro_both_syntaxes])
                {
                    if (options[euro_strict_parsing])
                        throw ELikeUseRequirementError(s, "[use!=] not safe for use here");
                    else
                        Log::get_instance()->message("e.use_requirement.flag_not_equal_not_allowed", ll_warning, lc_context)
                            << "[use!=] not safe for use here";
                }

                flag.erase(flag.length() - 1, 1);
                if (flag.empty())
                    throw ELikeUseRequirementError(s, "Invalid [] contents");
                factory = make_requirement<NotEqualUseRequirement>;
            }
            else if ('!' == flag.at(0))
            {
                if (! options[euro_portage_syntax] && ! options[euro_both_syntaxes])
                {
                    if (options[euro_strict_parsing])
                        throw ELikeUseRequirementError(s, "[!use=] not safe for use here");
                    else
                        Log::get_instance()->message("e.use_requirement.not_flag_equal_not_allowed", ll_warning, lc_context)
                            << "[!use=] not safe for use here";
                }

                flag.erase(0, 1);
                if (flag.empty())
                    throw ELikeUseRequirementError(s, "Invalid [] contents");
                factory = make_requirement<NotEqualUseRequirement>;
            }
            else
                factory = make_requirement<EqualUseRequirement>;
        }
        else if ('?' == flag.at(flag.length() - 1))
        {
            if ((! options[euro_allow_self_deps]))
                throw ELikeUseRequirementError(s, "Cannot use [use?] here");

            flag.erase(flag.length() - 1);
            if (flag.empty())
                throw ELikeUseRequirementError(s, "Invalid [] contents");

            if ('!' == flag.at(flag.length() - 1))
            {
                flag.erase(flag.length() - 1, 1);
                if (flag.empty())
                    throw ELikeUseRequirementError(s, "Invalid [] contents");

                if ('-' == flag.at(0))
                {
                    if (options[euro_portage_syntax] && ! options[euro_both_syntaxes])
                    {
                        if (options[euro_strict_parsing])
                            throw ELikeUseRequirementError(s, "[-use!?] not safe for use here");
                        else
                            Log::get_instance()->message("e.use_requirement.minus_flag_not_question_not_allowed", ll_warning, lc_context)
                                << "[-use!?] not safe for use here";
                    }

                    flag.erase(0, 1);
                    if (flag.empty())
                        throw ELikeUseRequirementError(s, "Invalid [] contents");

                    factory = make_requirement<IfNotMineThenNotUseRequirement>;
                }
                else
                {
                    if (options[euro_portage_syntax] && ! options[euro_both_syntaxes])
                    {
                        if (options[euro_strict_parsing])
                            throw ELikeUseRequirementError(s, "[use!?] not safe for use here");
                        else
                            Log::get_instance()->message("e.use_requirement.flag_not_question_not_allowed", ll_warning, lc_context)
                                << "[use!?] not safe for use here";
                    }

                    factory = make_requirement<IfNotMineThenUseRequirement>;
                }
            }
            else if ('!' == flag.at(0))
            {
                if (! options[euro_portage_syntax] && ! options[euro_both_syntaxes])
                {
                    if (options[euro_strict_parsing])
                        throw ELikeUseRequirementError(s, "[!use?] not safe for use here");
                    else
                        Log::get_instance()->message("e.use_requirement.not_flag_question_not_allowed", ll_warning, lc_context)
                            << "[!use?] not safe for use here";
                }

                flag.erase(0, 1);
                if (flag.empty())
                    throw ELikeUseRequirementError(s, "Invalid [] contents");
                factory = make_requirement<IfNotMineThenNotUseRequirement>;
            }
            else
            {
                if ('-' == flag.at(0))
                {
                    if (options[euro_portage_syntax] && ! options[euro_both_syntaxes])
                    {
                        if (options[euro_strict_parsing])
                            throw ELikeUseRequirementError(s, "[-use?] not safe for use here");
                        else
                            Log::get_instance()->message("e.use_requirement.minus_flag_question_not_allowed", ll_warning, lc_context)
                                << "[-use?] not safe for use here";
                    }

                    flag.erase(0, 1);
                    if (flag.empty())
                        throw ELikeUseRequirementError(s, "Invalid [] contents");

                    factory = make_requirement<IfMineThenNotUseRequirement>;
                }
                else
                    factory = make_requirement<IfMineThenUseRequirement>;
            }
        }
        else if ('-' == flag.at(0))
        {
            flag.erase(0, 1);
            if (flag.empty())
                throw ELikeUseRequirementError(s, "Invalid [] contents");
            factory = make_requirement<DisabledUseRequirement>;
        }
        else
            factory = make_requirement<EnabledUseRequirement>;

        if (')' == flag.at(flag.length() - 1))
        {
            if (flag.length() < 4 || flag.at(flag.length() - 3) != '(')
                throw ELikeUseRequirementError(s, "Invalid [] contents");

            if ('+' == flag.at(flag.length() - 2))
            {
                if (! options[euro_allow_default_values])
                {
                    if (options[euro_strict_parsing])
                        throw ELikeUseRequirementError(s, "[use(+)] not safe for use here");
                    else
                        Log::get_instance()->message("e.use_requirement.flag_bracket_plus_not_allowed", ll_warning, lc_context)
                            << "[use(+)] not safe for use here";
                }

                flag.erase(flag.length() - 3, 3);
                parse_flag(result, factory, flag, Tribool(true), false, options);
            }
            else if ('-' == flag.at(flag.length() - 2))
            {
                if (! options[euro_allow_default_values])
                {
                    if (options[euro_strict_parsing])
                        throw ELikeUseRequirementError(s, "[use(-)] not safe for use here");
                    else
                        Log::get_instance()->message("e.use_requirement.flag_bracket_minus_not_allowed", ll_warning, lc_context)
                            << "[use(-)] not safe for use here";
                }

                flag.erase(flag.length() - 3, 3);
                parse_flag(result, factory, flag, Tribool(false), false, options);
            }
            else if ('?' == flag.at(flag.length() - 2))
            {
                if (! options[euro_allow_default_question_values])
                {
                    if (options[euro_strict_parsing])
                        throw ELikeUseRequirementError(s, "[use(?)] not safe for use here");
                    else
                        Log::get_instance()->message("e.use_requirement.flag_bracket_question_not_allowed", ll_warning, lc_context)
                            << "[use(?)] not safe for use here";
                }

                flag.erase(flag.length() - 3, 3);
                parse_flag(result, factory, flag, Tribool(false), true, options);
            }
            else
                throw ELikeUseRequirementError(s, "Invalid [] contents");
        }
        else
            parse_flag(result, factory, flag, Tribool(indeterminate), false, options);
    }
}

ELikeUseRequirementError::ELikeUseRequirementError(const std::string & s, const std::string & m) throw () :
    Exception("Error parsing use requirement '" + s + "': " + m)
{
}

std::shared_ptr<const AdditionalPackageDepSpecRequirement>
paludis::parse_elike_use_requirement(const std::string & s,
        const ELikeUseRequirementOptions & options)
{
    Context context("When parsing use requirement '" + s + "':");

    std::shared_ptr<UseRequirements> result(std::make_shared<UseRequirements>("[" + s + "]"));
    std::string::size_type pos(0);
    for (;;)
    {
        std::string::size_type comma(s.find(',', pos));
        std::string flag(s.substr(pos, std::string::npos == comma ? comma : comma - pos));
        parse_one_use_requirement(result, s, flag, options);
        if (std::string::npos == comma)
            break;

        if (! options[euro_portage_syntax] && ! options[euro_both_syntaxes])
        {
            if (options[euro_strict_parsing])
                throw ELikeUseRequirementError(s, "[use,use] not safe for use here");
            else
                Log::get_instance()->message("e.use_requirement.comma_separated_not_allowed", ll_warning, lc_context)
                    << "[use,use] not safe for use here";
        }

        pos = comma + 1;
    }

    return result;
}